15
15
package auth
16
16
17
17
import (
18
+ "encoding/json"
18
19
"errors"
20
+ "fmt"
19
21
"io/ioutil"
20
22
"log"
21
23
"os"
@@ -81,6 +83,38 @@ func TestMain(m *testing.M) {
81
83
os .Exit (m .Run ())
82
84
}
83
85
86
+ func TestNewClientInvalidCredentials (t * testing.T ) {
87
+ creds := & google.DefaultCredentials {
88
+ JSON : []byte ("foo" ),
89
+ }
90
+ conf := & internal.AuthConfig {
91
+ Ctx : context .Background (),
92
+ Creds : creds ,
93
+ }
94
+ if c , err := NewClient (conf ); c != nil || err == nil {
95
+ t .Errorf ("NewCient() = (%v,%v); want = (nil, error)" , c , err )
96
+ }
97
+ }
98
+
99
+ func TestNewClientInvalidPrivateKey (t * testing.T ) {
100
+ sa := map [string ]interface {}{
101
+ "private_key" : "foo" ,
102
+ "client_email" :
"[email protected] " ,
103
+ }
104
+ b , err := json .Marshal (sa )
105
+ if err != nil {
106
+ t .Fatal (err )
107
+ }
108
+ creds := & google.DefaultCredentials {JSON : b }
109
+ conf := & internal.AuthConfig {
110
+ Ctx : context .Background (),
111
+ Creds : creds ,
112
+ }
113
+ if c , err := NewClient (conf ); c != nil || err == nil {
114
+ t .Errorf ("NewCient() = (%v,%v); want = (nil, error)" , c , err )
115
+ }
116
+ }
117
+
84
118
func TestCustomToken (t * testing.T ) {
85
119
token , err := client .CustomToken ("user1" )
86
120
if err != nil {
@@ -118,13 +152,14 @@ func TestCustomTokenError(t *testing.T) {
118
152
}{
119
153
{"EmptyName" , "" , nil },
120
154
{"LongUid" , strings .Repeat ("a" , 129 ), nil },
121
- {"ReservedClaims" , "uid" , map [string ]interface {}{"sub" : "1234" }},
155
+ {"ReservedClaim" , "uid" , map [string ]interface {}{"sub" : "1234" }},
156
+ {"ReservedClaims" , "uid" , map [string ]interface {}{"sub" : "1234" , "aud" : "foo" }},
122
157
}
123
158
124
159
for _ , tc := range cases {
125
160
token , err := client .CustomTokenWithClaims (tc .uid , tc .claims )
126
161
if token != "" || err == nil {
127
- t .Errorf ("CustomTokenWithClaims(%q) = (%q, %v); want: (\" \" , error)" , tc .name , token , err )
162
+ t .Errorf ("CustomTokenWithClaims(%q) = (%q, %v); want = (\" \" , error)" , tc .name , token , err )
128
163
}
129
164
}
130
165
}
@@ -137,12 +172,12 @@ func TestCustomTokenInvalidCredential(t *testing.T) {
137
172
138
173
token , err := s .CustomToken ("user1" )
139
174
if token != "" || err == nil {
140
- t .Errorf ("CustomTokenWithClaims() = (%q, %v); want: (\" \" , error)" , token , err )
175
+ t .Errorf ("CustomTokenWithClaims() = (%q, %v); want = (\" \" , error)" , token , err )
141
176
}
142
177
143
178
token , err = s .CustomTokenWithClaims ("user1" , map [string ]interface {}{"foo" : "bar" })
144
179
if token != "" || err == nil {
145
- t .Errorf ("CustomTokenWithClaims() = (%q, %v); want: (\" \" , error)" , token , err )
180
+ t .Errorf ("CustomTokenWithClaims() = (%q, %v); want = (\" \" , error)" , token , err )
146
181
}
147
182
}
148
183
@@ -152,15 +187,23 @@ func TestVerifyIDToken(t *testing.T) {
152
187
t .Fatal (err )
153
188
}
154
189
if ft .Claims ["admin" ] != true {
155
- t .Errorf ("Claims['admin'] = %v; want: true" , ft .Claims ["admin" ])
190
+ t .Errorf ("Claims['admin'] = %v; want = true" , ft .Claims ["admin" ])
156
191
}
157
192
if ft .UID != ft .Subject {
158
193
t .Errorf ("UID = %q; Sub = %q; want UID = Sub" , ft .UID , ft .Subject )
159
194
}
160
195
}
161
196
197
+ func TestVerifyIDTokenInvalidSignature (t * testing.T ) {
198
+ parts := strings .Split (testIDToken , "." )
199
+ token := fmt .Sprintf ("%s:%s:invalidsignature" , parts [0 ], parts [1 ])
200
+ if ft , err := client .VerifyIDToken (token ); ft != nil || err == nil {
201
+ t .Errorf ("VerifyiDToken('invalid-signature') = (%v, %v); want = (nil, error)" , ft , err )
202
+ }
203
+ }
204
+
162
205
func TestVerifyIDTokenError (t * testing.T ) {
163
- var now int64 = 1000
206
+ now := time . Now (). Unix ()
164
207
cases := []struct {
165
208
name string
166
209
token string
@@ -172,22 +215,18 @@ func TestVerifyIDTokenError(t *testing.T) {
172
215
{"EmptySubject" , getIDToken (mockIDTokenPayload {"sub" : "" })},
173
216
{"IntSubject" , getIDToken (mockIDTokenPayload {"sub" : 10 })},
174
217
{"LongSubject" , getIDToken (mockIDTokenPayload {"sub" : strings .Repeat ("a" , 129 )})},
175
- {"FutureToken" , getIDToken (mockIDTokenPayload {"iat" : time . Unix ( now + 1 , 0 ) })},
218
+ {"FutureToken" , getIDToken (mockIDTokenPayload {"iat" : now + 1000 })},
176
219
{"ExpiredToken" , getIDToken (mockIDTokenPayload {
177
- "iat" : time . Unix ( now - 10 , 0 ) ,
178
- "exp" : time . Unix ( now - 1 , 0 ) ,
220
+ "iat" : now - 1000 ,
221
+ "exp" : now - 100 ,
179
222
})},
180
223
{"EmptyToken" , "" },
181
224
{"BadFormatToken" , "foobar" },
182
225
}
183
226
184
- clk = & mockClock {now : time .Unix (now , 0 )}
185
- defer func () {
186
- clk = & systemClock {}
187
- }()
188
227
for _ , tc := range cases {
189
228
if _ , err := client .VerifyIDToken (tc .token ); err == nil {
190
- t .Errorf ("VerifyyIDToken (%q) = nil; want error" , tc .name )
229
+ t .Errorf ("VerifyIDToken (%q) = nil; want error" , tc .name )
191
230
}
192
231
}
193
232
}
0 commit comments