Skip to content

Commit 0b70d36

Browse files
committed
chore: add unit test to verify OIDCClaims query
1 parent 962c54a commit 0b70d36

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

coderd/database/modelqueries.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,6 @@ func insertAuthorizedFilter(query string, replaceWith string) (string, error) {
531531

532532
// UpdateUserLinkRawJSON is a custom query for unit testing. Do not ever expose this
533533
func (q *sqlQuerier) UpdateUserLinkRawJSON(ctx context.Context, userID uuid.UUID, data json.RawMessage) error {
534-
_, err := q.sdb.Exec("INSERT INTO user_links (user_id, claims) VALUES ($1, $2)", userID, data)
534+
_, err := q.sdb.ExecContext(ctx, "UPDATE user_links SET claims = $2 WHERE user_id = $1", userID, data)
535535
return err
536536
}

coderd/database/oidcclaims_test.go

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import (
1010

1111
"github.com/coder/coder/v2/coderd/database"
1212
"github.com/coder/coder/v2/coderd/database/dbfake"
13+
"github.com/coder/coder/v2/coderd/database/dbgen"
1314
"github.com/coder/coder/v2/coderd/database/dbtestutil"
14-
"github.com/coder/coder/v2/coderd/database/dbtime"
15+
"github.com/coder/coder/v2/coderd/util/slice"
1516
"github.com/coder/coder/v2/testutil"
1617
)
1718

@@ -35,29 +36,41 @@ func TestOIDCClaims(t *testing.T) {
3536
alice := g.withLink(database.LoginTypeOIDC, toJSON(extraKeys{
3637
UserLinkClaims: database.UserLinkClaims{
3738
IDTokenClaims: map[string]interface{}{
38-
"sub": "alice",
39-
},
40-
UserInfoClaims: map[string]interface{}{
41-
"sub": "alice",
39+
"sub": "alice",
40+
"alice-id": "from-bob",
4241
},
42+
UserInfoClaims: nil,
4343
},
4444
// Always should be a no-op
4545
Foo: "bar",
4646
}))
4747
bob := g.withLink(database.LoginTypeOIDC, toJSON(database.UserLinkClaims{
4848
IDTokenClaims: map[string]interface{}{
49-
"sub": "bob",
49+
"sub": "bob",
50+
"bob-id": "from-bob",
51+
"array": []string{
52+
"a", "b", "c",
53+
},
54+
"map": map[string]interface{}{
55+
"key": "value",
56+
"foo": "bar",
57+
},
58+
"nil": nil,
5059
},
5160
UserInfoClaims: map[string]interface{}{
52-
"sub": "bob",
61+
"sub": "bob",
62+
"bob-info": []string{},
63+
"number": 42,
5364
},
5465
}))
5566
charlie := g.withLink(database.LoginTypeOIDC, toJSON(database.UserLinkClaims{
5667
IDTokenClaims: map[string]interface{}{
57-
"sub": "charlie",
68+
"sub": "charlie",
69+
"charlie-id": "charlie",
5870
},
5971
UserInfoClaims: map[string]interface{}{
60-
"sub": "charlie",
72+
"sub": "charlie",
73+
"charlie-info": "charlie",
6174
},
6275
}))
6376

@@ -87,17 +100,23 @@ func TestOIDCClaims(t *testing.T) {
87100
orgA := dbfake.Organization(t, db).Members(
88101
append(problematics,
89102
alice,
90-
bob)...,
103+
bob,
104+
)...,
91105
).Do()
92106
orgB := dbfake.Organization(t, db).Members(
93107
append(problematics,
108+
bob,
94109
charlie,
95110
)...,
96111
).Do()
97112

98113
// Verify the OIDC claim fields
99-
requireClaims(t, db, orgA.Org.ID, []string{"sub"})
100-
requireClaims(t, db, orgB.Org.ID, []string{"sub"})
114+
always := []string{"array", "map", "nil", "number"}
115+
expectA := append([]string{"sub", "alice-id", "bob-id", "bob-info"}, always...)
116+
expectB := append([]string{"sub", "bob-id", "bob-info", "charlie-id", "charlie-info"}, always...)
117+
requireClaims(t, db, orgA.Org.ID, expectA)
118+
requireClaims(t, db, orgB.Org.ID, expectB)
119+
requireClaims(t, db, uuid.Nil, slice.Unique(append(expectA, expectB...)))
101120
}
102121

103122
func requireClaims(t *testing.T, db database.Store, orgID uuid.UUID, want []string) {
@@ -129,34 +148,23 @@ func (g userGenerator) user(lt database.LoginType, createLink bool, rawJSON json
129148

130149
t.Helper()
131150

132-
u, err := db.InsertUser(context.Background(), database.InsertUserParams{
133-
ID: uuid.New(),
134-
Email: testutil.GetRandomName(t),
135-
Username: testutil.GetRandomName(t),
136-
Name: testutil.GetRandomName(t),
137-
CreatedAt: dbtime.Now(),
138-
UpdatedAt: dbtime.Now(),
139-
RBACRoles: []string{},
151+
u := dbgen.User(t, db, database.User{
140152
LoginType: lt,
141-
Status: string(database.UserStatusActive),
142153
})
143-
require.NoError(t, err)
144154

145155
if !createLink {
146156
return u
147157
}
148158

149-
link, err := db.InsertUserLink(context.Background(), database.InsertUserLinkParams{
159+
link := dbgen.UserLink(t, db, database.UserLink{
150160
UserID: u.ID,
151161
LoginType: lt,
152-
Claims: database.UserLinkClaims{},
153162
})
154-
require.NoError(t, err)
155163

156164
if sql, ok := db.(rawUpdater); ok {
157165
// The only way to put arbitrary json into the db for testing edge cases.
158166
// Making this a public API would be a mistake.
159-
err = sql.UpdateUserLinkRawJSON(context.Background(), u.ID, rawJSON)
167+
err := sql.UpdateUserLinkRawJSON(context.Background(), u.ID, rawJSON)
160168
require.NoError(t, err)
161169
} else {
162170
// no need to test the json key logic in dbmem. Everything is type safe.

0 commit comments

Comments
 (0)