mirror of
https://github.com/hanzo-fi/stack.git
synced 2026-10-30 20:34:43 +00:00
87 lines
2.1 KiB
Go
87 lines
2.1 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/formancehq/stack/libs/go-libs/logging"
|
|
|
|
"github.com/formancehq/stack/libs/go-libs/bun/bunconnect"
|
|
"github.com/uptrace/bun"
|
|
|
|
auth "github.com/formancehq/auth/pkg"
|
|
"github.com/formancehq/auth/pkg/storage/sqlstorage"
|
|
"github.com/formancehq/stack/libs/go-libs/pgtesting"
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
var (
|
|
user1 = &auth.User{
|
|
ID: uuid.NewString(),
|
|
Subject: "alice",
|
|
Email: "[email protected]",
|
|
}
|
|
|
|
user2 = &auth.User{
|
|
ID: uuid.NewString(),
|
|
Subject: "bob",
|
|
Email: "[email protected]",
|
|
}
|
|
)
|
|
|
|
func TestListUsers(t *testing.T) {
|
|
withDbAndUserRouter(t, func(router chi.Router, db *bun.DB) {
|
|
_, err := db.NewInsert().Model(user1).Exec(context.Background())
|
|
require.NoError(t, err)
|
|
|
|
_, err = db.NewInsert().Model(user2).Exec(context.Background())
|
|
require.NoError(t, err)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/users", nil)
|
|
res := httptest.NewRecorder()
|
|
router.ServeHTTP(res, req)
|
|
require.Equal(t, http.StatusOK, res.Code)
|
|
|
|
users := readTestResponse[[]auth.User](t, res)
|
|
require.Len(t, users, 2)
|
|
})
|
|
}
|
|
|
|
func TestReadUser(t *testing.T) {
|
|
withDbAndUserRouter(t, func(router chi.Router, db *bun.DB) {
|
|
_, err := db.NewInsert().Model(user1).Exec(context.Background())
|
|
require.NoError(t, err)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/users/"+user1.ID, nil)
|
|
res := httptest.NewRecorder()
|
|
router.ServeHTTP(res, req)
|
|
require.Equal(t, http.StatusOK, res.Code)
|
|
|
|
user := readTestResponse[auth.User](t, res)
|
|
require.Equal(t, *user1, user)
|
|
})
|
|
}
|
|
|
|
func withDbAndUserRouter(t *testing.T, callback func(router chi.Router, db *bun.DB)) {
|
|
t.Parallel()
|
|
|
|
pgDatabase := pgtesting.NewPostgresDatabase(t)
|
|
db, err := bunconnect.OpenSQLDB(logging.TestingContext(), bunconnect.ConnectionOptions{
|
|
DatabaseSourceName: pgDatabase.ConnString(),
|
|
})
|
|
require.NoError(t, err)
|
|
defer db.Close()
|
|
|
|
require.NoError(t, sqlstorage.Migrate(context.Background(), db))
|
|
|
|
router := chi.NewRouter()
|
|
addUserRoutes(db, router)
|
|
|
|
callback(router, db)
|
|
}
|