Skip to content

Commit 8721506

Browse files
authored
Refactoring the user management integration tests (#152)
* Adding new user mgt integ tests * Reimplemented user mgt integ tests * Fixed update test * Minor updates for clarity
1 parent 32e45a6 commit 8721506

File tree

2 files changed

+282
-385
lines changed

2 files changed

+282
-385
lines changed

integration/auth/auth_test.go

Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -47,128 +47,118 @@ func TestMain(m *testing.M) {
4747
os.Exit(0)
4848
}
4949

50-
ctx := context.Background()
51-
app, err := internal.NewTestApp(ctx, nil)
50+
app, err := internal.NewTestApp(context.Background(), nil)
5251
if err != nil {
5352
log.Fatalln(err)
5453
}
55-
56-
client, err = app.Auth(ctx)
54+
client, err = app.Auth(context.Background())
5755
if err != nil {
5856
log.Fatalln(err)
5957
}
6058
os.Exit(m.Run())
6159
}
6260

6361
func TestCustomToken(t *testing.T) {
64-
ct, err := client.CustomToken(context.Background(), "user1")
62+
uid := randomUID()
63+
ct, err := client.CustomToken(context.Background(), uid)
64+
if err != nil {
65+
t.Fatal(err)
66+
}
67+
idt, err := signInWithCustomToken(ct)
68+
if err != nil {
69+
t.Fatal(err)
70+
}
71+
defer deleteUser(uid)
72+
73+
vt, err := client.VerifyIDToken(context.Background(), idt)
74+
if err != nil {
75+
t.Fatal(err)
76+
}
77+
if vt.UID != uid {
78+
t.Errorf("UID = %q; want UID = %q", vt.UID, uid)
79+
}
80+
}
6581

82+
func TestCustomTokenWithClaims(t *testing.T) {
83+
uid := randomUID()
84+
ct, err := client.CustomTokenWithClaims(context.Background(), uid, map[string]interface{}{
85+
"premium": true,
86+
"package": "gold",
87+
})
6688
if err != nil {
6789
t.Fatal(err)
6890
}
91+
6992
idt, err := signInWithCustomToken(ct)
7093
if err != nil {
7194
t.Fatal(err)
7295
}
96+
defer deleteUser(uid)
7397

7498
vt, err := client.VerifyIDToken(context.Background(), idt)
7599
if err != nil {
76100
t.Fatal(err)
77101
}
78-
if vt.UID != "user1" {
79-
t.Errorf("UID = %q; want UID = %q", vt.UID, "user1")
102+
if vt.UID != uid {
103+
t.Errorf("UID = %q; want UID = %q", vt.UID, uid)
80104
}
81-
if err = client.DeleteUser(context.Background(), "user1"); err != nil {
82-
t.Error(err)
105+
if premium, ok := vt.Claims["premium"].(bool); !ok || !premium {
106+
t.Errorf("Claims['premium'] = %v; want Claims['premium'] = true", vt.Claims["premium"])
107+
}
108+
if pkg, ok := vt.Claims["package"].(string); !ok || pkg != "gold" {
109+
t.Errorf("Claims['package'] = %v; want Claims['package'] = \"gold\"", vt.Claims["package"])
83110
}
84111
}
85112

86-
func TestVerifyIDTokenAndCheckRevoked(t *testing.T) {
113+
func TestRevokeRefreshTokens(t *testing.T) {
87114
uid := "user_revoked"
88115
ct, err := client.CustomToken(context.Background(), uid)
89-
90116
if err != nil {
91117
t.Fatal(err)
92118
}
93119
idt, err := signInWithCustomToken(ct)
94120
if err != nil {
95121
t.Fatal(err)
96122
}
97-
ctx := context.Background()
98-
vt, err := client.VerifyIDTokenAndCheckRevoked(ctx, idt)
123+
defer deleteUser(uid)
124+
125+
vt, err := client.VerifyIDTokenAndCheckRevoked(context.Background(), idt)
99126
if err != nil {
100127
t.Fatal(err)
101128
}
102129
if vt.UID != uid {
103130
t.Errorf("UID = %q; want UID = %q", vt.UID, uid)
104131
}
132+
105133
// The backend stores the validSince property in seconds since the epoch.
106134
// The issuedAt property of the token is also in seconds. If a token was
107135
// issued, and then in the same second tokens were revoked, the token will
108136
// have the same timestamp as the tokensValidAfterMillis, and will therefore
109137
// not be considered revoked. Hence we wait one second before revoking.
110138
time.Sleep(time.Second)
111-
if err = client.RevokeRefreshTokens(ctx, uid); err != nil {
139+
if err = client.RevokeRefreshTokens(context.Background(), uid); err != nil {
112140
t.Fatal(err)
113141
}
114142

115-
vt, err = client.VerifyIDTokenAndCheckRevoked(ctx, idt)
143+
vt, err = client.VerifyIDTokenAndCheckRevoked(context.Background(), idt)
116144
we := "ID token has been revoked"
117145
if vt != nil || err == nil || err.Error() != we {
118146
t.Errorf("tok, err := VerifyIDTokenAndCheckRevoked(); got (%v, %s) ; want (%v, %v)",
119147
vt, err, nil, we)
120148
}
121149

122150
// Does not return error for revoked token.
123-
if _, err = client.VerifyIDToken(ctx, idt); err != nil {
151+
if _, err = client.VerifyIDToken(context.Background(), idt); err != nil {
124152
t.Errorf("VerifyIDToken(); err = %s; want err = <nil>", err)
125153
}
126154

127155
// Sign in after revocation.
128156
if idt, err = signInWithCustomToken(ct); err != nil {
129157
t.Fatal(err)
130158
}
131-
132-
if _, err = client.VerifyIDTokenAndCheckRevoked(ctx, idt); err != nil {
159+
if _, err = client.VerifyIDTokenAndCheckRevoked(context.Background(), idt); err != nil {
133160
t.Errorf("VerifyIDTokenAndCheckRevoked(); err = %s; want err = <nil>", err)
134161
}
135-
136-
err = client.DeleteUser(ctx, uid)
137-
if err != nil {
138-
t.Error(err)
139-
}
140-
}
141-
142-
func TestCustomTokenWithClaims(t *testing.T) {
143-
ct, err := client.CustomTokenWithClaims(context.Background(), "user2", map[string]interface{}{
144-
"premium": true,
145-
"package": "gold",
146-
})
147-
if err != nil {
148-
t.Fatal(err)
149-
}
150-
151-
idt, err := signInWithCustomToken(ct)
152-
if err != nil {
153-
t.Fatal(err)
154-
}
155-
156-
vt, err := client.VerifyIDToken(context.Background(), idt)
157-
if err != nil {
158-
t.Fatal(err)
159-
}
160-
if vt.UID != "user2" {
161-
t.Errorf("UID = %q; want UID = %q", vt.UID, "user2")
162-
}
163-
if premium, ok := vt.Claims["premium"].(bool); !ok || !premium {
164-
t.Errorf("Claims['premium'] = %v; want Claims['premium'] = true", vt.Claims["premium"])
165-
}
166-
if pkg, ok := vt.Claims["package"].(string); !ok || pkg != "gold" {
167-
t.Errorf("Claims['package'] = %v; want Claims['package'] = \"gold\"", vt.Claims["package"])
168-
}
169-
if err = client.DeleteUser(context.Background(), "user2"); err != nil {
170-
t.Error(err)
171-
}
172162
}
173163

174164
func signInWithCustomToken(token string) (string, error) {
@@ -235,3 +225,10 @@ func postRequest(url string, req []byte) ([]byte, error) {
235225
}
236226
return ioutil.ReadAll(resp.Body)
237227
}
228+
229+
// deleteUser makes a best effort attempt to delete the given user.
230+
//
231+
// Any errors encountered during the delete are ignored.
232+
func deleteUser(uid string) {
233+
client.DeleteUser(context.Background(), uid)
234+
}

0 commit comments

Comments
 (0)