-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
325 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/basemind-ai/monorepo/go-services/dashboard-backend/constants" | ||
dbTestutils "github.com/basemind-ai/monorepo/go-shared/db/testutils" | ||
"github.com/basemind-ai/monorepo/go-shared/firebaseutils/testutils" | ||
"github.com/stretchr/testify/assert" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
dbTestutils.TestMainWrapper(m) | ||
} | ||
|
||
func TestHandleDashboardUserPostLogin(t *testing.T) { | ||
|
||
t.Run("Creates a new user", func(t *testing.T) { | ||
rr := httptest.NewRecorder() | ||
|
||
req, err := http.NewRequestWithContext(testutils.MockFirebaseContext(), http.MethodGet, constants.DashboardLoginEndpoint, nil) | ||
assert.Nil(t, err) | ||
|
||
HandleDashboardUserPostLogin(rr, req) | ||
|
||
res := rr.Result() | ||
assert.Equal(t, http.StatusCreated, res.StatusCode) | ||
|
||
defer res.Body.Close() | ||
body, err := io.ReadAll(res.Body) | ||
assert.Nil(t, err) | ||
|
||
var responseUser HandleDashboardUserPostLoginDTO | ||
_ = json.Unmarshal(body, &responseUser) | ||
|
||
assert.Equal(t, "1", responseUser.User.FirebaseID) | ||
assert.Equal(t, "Default Project", responseUser.Projects[0].Name) | ||
assert.Equal(t, "Default Project", responseUser.Projects[0].Description) | ||
}) | ||
|
||
t.Run("Retrieves an existing user", func(t *testing.T) { | ||
// Create a new user first | ||
req, err := http.NewRequestWithContext(testutils.MockFirebaseContext(), http.MethodGet, constants.DashboardLoginEndpoint, nil) | ||
assert.Nil(t, err) | ||
HandleDashboardUserPostLogin(httptest.NewRecorder(), req) | ||
|
||
req, err = http.NewRequestWithContext(testutils.MockFirebaseContext(), http.MethodGet, constants.DashboardLoginEndpoint, nil) | ||
assert.Nil(t, err) | ||
|
||
rr := httptest.NewRecorder() | ||
HandleDashboardUserPostLogin(rr, req) | ||
|
||
res := rr.Result() | ||
assert.Equal(t, http.StatusOK, res.StatusCode) | ||
|
||
defer res.Body.Close() | ||
body, err := io.ReadAll(res.Body) | ||
assert.Nil(t, err) | ||
|
||
var responseUser HandleDashboardUserPostLoginDTO | ||
_ = json.Unmarshal(body, &responseUser) | ||
|
||
assert.Equal(t, "1", responseUser.User.FirebaseID) | ||
assert.Equal(t, "Default Project", responseUser.Projects[0].Name) | ||
assert.Equal(t, "Default Project", responseUser.Projects[0].Description) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package testutils | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"github.com/basemind-ai/monorepo/go-shared/db" | ||
"os/exec" | ||
|
||
"github.com/rs/zerolog/log" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
_ "github.com/lib/pq" | ||
"github.com/ory/dockertest/v3" | ||
"github.com/ory/dockertest/v3/docker" | ||
) | ||
|
||
func TestMainWrapper(m *testing.M) { | ||
// uses a sensible default on windows (tcp/http) and linux/osx (socket) | ||
pool, err := dockertest.NewPool("") | ||
if err != nil { | ||
log.Fatal().Err(err).Msgf("Could not construct pool: %s", err) | ||
} | ||
|
||
err = pool.Client.Ping() | ||
if err != nil { | ||
log.Fatal().Err(err).Msgf("Could not connect to Docker: %s", err) | ||
} | ||
|
||
// pulls an image, creates a container based on it and runs it | ||
resource, err := pool.RunWithOptions(&dockertest.RunOptions{ | ||
Repository: "postgres", | ||
Tag: "latest", | ||
Env: []string{ | ||
"POSTGRES_PASSWORD=secret", | ||
"POSTGRES_USER=user_name", | ||
"POSTGRES_DB=dbname", | ||
"listen_addresses = '*'", | ||
}, | ||
}, func(config *docker.HostConfig) { | ||
// set AutoRemove to true so that stopped container goes away by itself | ||
config.AutoRemove = true | ||
config.RestartPolicy = docker.RestartPolicy{Name: "no"} | ||
}) | ||
if err != nil { | ||
log.Fatal().Err(err).Msgf("Could not start resource: %s", err) | ||
} | ||
|
||
hostAndPort := resource.GetHostPort("5432/tcp") | ||
databaseUrl := fmt.Sprintf("postgresql://user_name:secret@%s/dbname?sslmode=disable", hostAndPort) | ||
|
||
log.Info().Msgf("Connecting to database on url: %s", databaseUrl) | ||
|
||
resource.Expire(120) // Tell docker to hard kill the container in 120 seconds | ||
|
||
// exponential backoff-retry, because the application in the container might not be ready to accept connections yet | ||
pool.MaxWait = 120 * time.Second | ||
if err = pool.Retry(func() error { | ||
|
||
tmpDb, err := sql.Open("postgres", databaseUrl) | ||
if err != nil { | ||
return err | ||
} | ||
return tmpDb.Ping() | ||
}); err != nil { | ||
log.Fatal().Err(err).Msgf("Could not connect to docker: %s", err) | ||
} | ||
|
||
localContext := context.TODO() | ||
conn := db.CreateConnection(localContext, databaseUrl) | ||
err = conn.Ping(localContext) | ||
if err != nil { | ||
log.Fatal().Err(err).Msgf("Could not connect to docker: %s", err) | ||
} | ||
|
||
// Migrating DB | ||
if err := runMigrations("../../../sql/migrations", databaseUrl); err != nil { | ||
log.Fatal().Err(err).Msgf("Could not migrate db: %s", err) | ||
} | ||
|
||
//Run tests | ||
code := m.Run() | ||
|
||
// You can't defer this because os.Exit doesn't care for defer | ||
if err := pool.Purge(resource); err != nil { | ||
log.Fatal().Err(err).Msgf("Could not purge resource: %s", err) | ||
} | ||
|
||
os.Exit(code) | ||
} | ||
|
||
func runMigrations(migrationsPath string, connUrl string) error { | ||
if migrationsPath == "" { | ||
return errors.New("missing migrations path") | ||
} | ||
|
||
out, err := exec.Command("atlas", "migrate", "apply", "--dir", "file://"+migrationsPath, "--url", connUrl).Output() | ||
|
||
log.Info().Msg(string(out)) | ||
if err != nil { | ||
return err | ||
} | ||
if err := m.Up(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.