|
| 1 | +package scim |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/gofrs/uuid" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/supabase/auth/internal/api/scim/core" |
| 12 | + "github.com/supabase/auth/internal/conf/confload" |
| 13 | + "github.com/supabase/auth/internal/storage" |
| 14 | + "github.com/supabase/auth/internal/storage/test" |
| 15 | +) |
| 16 | + |
| 17 | +const scimTestConfig = "../../../hack/test.env" |
| 18 | + |
| 19 | +const testExternalURL = "http://localhost:9999" |
| 20 | + |
| 21 | +func newTestDB(t *testing.T) *storage.Connection { |
| 22 | + t.Helper() |
| 23 | + |
| 24 | + globalConfig, err := confload.LoadGlobal(scimTestConfig) |
| 25 | + require.NoError(t, err) |
| 26 | + |
| 27 | + conn, err := test.SetupDBConnection(globalConfig) |
| 28 | + require.NoError(t, err) |
| 29 | + |
| 30 | + return conn |
| 31 | +} |
| 32 | + |
| 33 | +func newTenant(t *testing.T, db *storage.Connection) string { |
| 34 | + t.Helper() |
| 35 | + |
| 36 | + id := uuid.Must(uuid.NewV4()).String() |
| 37 | + require.NoError(t, db.RawQuery("INSERT INTO sso_providers (id, resource_id, created_at, updated_at) VALUES (?, ?, now(), now())", id, "scim-test-"+id).Exec()) |
| 38 | + |
| 39 | + t.Cleanup(func() { |
| 40 | + _ = db.RawQuery("DELETE FROM sso_providers WHERE id = ?", id).Exec() |
| 41 | + }) |
| 42 | + |
| 43 | + return id |
| 44 | +} |
| 45 | + |
| 46 | +func putUser(t *testing.T, db *storage.Connection, tenant string, user *core.User) { |
| 47 | + t.Helper() |
| 48 | + |
| 49 | + stored := *user |
| 50 | + stored.ID, stored.Meta = "", core.Meta{} |
| 51 | + |
| 52 | + document, err := json.Marshal(&stored) |
| 53 | + require.NoError(t, err) |
| 54 | + |
| 55 | + require.NoError(t, db.RawQuery( |
| 56 | + "INSERT INTO scim_users (id, sso_provider_id, resource, created_at, updated_at)"+ |
| 57 | + " VALUES (?, ?, ?, ?, ?)", |
| 58 | + user.ID, tenant, string(document), user.Meta.Created, user.Meta.LastModified, |
| 59 | + ).Exec()) |
| 60 | +} |
| 61 | + |
| 62 | +func newStoredUser(t *testing.T, db *storage.Connection, tenant string, user *core.User) *core.User { |
| 63 | + t.Helper() |
| 64 | + |
| 65 | + if user.ID == "" { |
| 66 | + user.ID = uuid.Must(uuid.NewV4()).String() |
| 67 | + } |
| 68 | + putUser(t, db, tenant, user) |
| 69 | + return user |
| 70 | +} |
| 71 | + |
| 72 | +func newTestRepo(db *storage.Connection) *userRepository { |
| 73 | + return &userRepository{db: db, baseURL: core.Join(testExternalURL, BasePath)} |
| 74 | +} |
| 75 | + |
| 76 | +func ctxFor(tenant string) context.Context { |
| 77 | + return withTenant(context.Background(), tenant) |
| 78 | +} |
| 79 | + |
| 80 | +func seedPostgres(t *testing.T, users []*core.User) (Repository[*core.User], context.Context) { |
| 81 | + db := newTestDB(t) |
| 82 | + owner := newTenant(t, db) |
| 83 | + for _, user := range users { |
| 84 | + putUser(t, db, owner, user) |
| 85 | + } |
| 86 | + return newTestRepo(db), ctxFor(owner) |
| 87 | +} |
| 88 | + |
| 89 | +func grantToken(t *testing.T, db *storage.Connection, provider string) string { |
| 90 | + t.Helper() |
| 91 | + |
| 92 | + token, digest := NewToken() |
| 93 | + require.NoError(t, db.RawQuery( |
| 94 | + "INSERT INTO scim_tokens (sso_provider_id, token_hash, prefix) VALUES (?, ?, ?)", |
| 95 | + provider, digest, token[:12], |
| 96 | + ).Exec()) |
| 97 | + |
| 98 | + return token |
| 99 | +} |
| 100 | + |
| 101 | +func userNamesOf(users []*core.User) []string { |
| 102 | + names := make([]string, 0, len(users)) |
| 103 | + for _, user := range users { |
| 104 | + names = append(names, user.UserName) |
| 105 | + } |
| 106 | + return names |
| 107 | +} |
0 commit comments