@@ -47,128 +47,118 @@ func TestMain(m *testing.M) {
47
47
os .Exit (0 )
48
48
}
49
49
50
- ctx := context .Background ()
51
- app , err := internal .NewTestApp (ctx , nil )
50
+ app , err := internal .NewTestApp (context .Background (), nil )
52
51
if err != nil {
53
52
log .Fatalln (err )
54
53
}
55
-
56
- client , err = app .Auth (ctx )
54
+ client , err = app .Auth (context .Background ())
57
55
if err != nil {
58
56
log .Fatalln (err )
59
57
}
60
58
os .Exit (m .Run ())
61
59
}
62
60
63
61
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
+ }
65
81
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
+ })
66
88
if err != nil {
67
89
t .Fatal (err )
68
90
}
91
+
69
92
idt , err := signInWithCustomToken (ct )
70
93
if err != nil {
71
94
t .Fatal (err )
72
95
}
96
+ defer deleteUser (uid )
73
97
74
98
vt , err := client .VerifyIDToken (context .Background (), idt )
75
99
if err != nil {
76
100
t .Fatal (err )
77
101
}
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 )
80
104
}
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" ])
83
110
}
84
111
}
85
112
86
- func TestVerifyIDTokenAndCheckRevoked (t * testing.T ) {
113
+ func TestRevokeRefreshTokens (t * testing.T ) {
87
114
uid := "user_revoked"
88
115
ct , err := client .CustomToken (context .Background (), uid )
89
-
90
116
if err != nil {
91
117
t .Fatal (err )
92
118
}
93
119
idt , err := signInWithCustomToken (ct )
94
120
if err != nil {
95
121
t .Fatal (err )
96
122
}
97
- ctx := context .Background ()
98
- vt , err := client .VerifyIDTokenAndCheckRevoked (ctx , idt )
123
+ defer deleteUser (uid )
124
+
125
+ vt , err := client .VerifyIDTokenAndCheckRevoked (context .Background (), idt )
99
126
if err != nil {
100
127
t .Fatal (err )
101
128
}
102
129
if vt .UID != uid {
103
130
t .Errorf ("UID = %q; want UID = %q" , vt .UID , uid )
104
131
}
132
+
105
133
// The backend stores the validSince property in seconds since the epoch.
106
134
// The issuedAt property of the token is also in seconds. If a token was
107
135
// issued, and then in the same second tokens were revoked, the token will
108
136
// have the same timestamp as the tokensValidAfterMillis, and will therefore
109
137
// not be considered revoked. Hence we wait one second before revoking.
110
138
time .Sleep (time .Second )
111
- if err = client .RevokeRefreshTokens (ctx , uid ); err != nil {
139
+ if err = client .RevokeRefreshTokens (context . Background () , uid ); err != nil {
112
140
t .Fatal (err )
113
141
}
114
142
115
- vt , err = client .VerifyIDTokenAndCheckRevoked (ctx , idt )
143
+ vt , err = client .VerifyIDTokenAndCheckRevoked (context . Background () , idt )
116
144
we := "ID token has been revoked"
117
145
if vt != nil || err == nil || err .Error () != we {
118
146
t .Errorf ("tok, err := VerifyIDTokenAndCheckRevoked(); got (%v, %s) ; want (%v, %v)" ,
119
147
vt , err , nil , we )
120
148
}
121
149
122
150
// 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 {
124
152
t .Errorf ("VerifyIDToken(); err = %s; want err = <nil>" , err )
125
153
}
126
154
127
155
// Sign in after revocation.
128
156
if idt , err = signInWithCustomToken (ct ); err != nil {
129
157
t .Fatal (err )
130
158
}
131
-
132
- if _ , err = client .VerifyIDTokenAndCheckRevoked (ctx , idt ); err != nil {
159
+ if _ , err = client .VerifyIDTokenAndCheckRevoked (context .Background (), idt ); err != nil {
133
160
t .Errorf ("VerifyIDTokenAndCheckRevoked(); err = %s; want err = <nil>" , err )
134
161
}
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
- }
172
162
}
173
163
174
164
func signInWithCustomToken (token string ) (string , error ) {
@@ -235,3 +225,10 @@ func postRequest(url string, req []byte) ([]byte, error) {
235
225
}
236
226
return ioutil .ReadAll (resp .Body )
237
227
}
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