@@ -198,6 +198,85 @@ func TestAcquireTokenSilentWithoutAccount(t *testing.T) {
198
198
}
199
199
}
200
200
201
+ func TestAcquireTokenByDeviceCode (t * testing.T ) {
202
+ accessToken := "*"
203
+ expected := accesstokens.DeviceCodeResult {
204
+ ClientID : "client-id" ,
205
+ DeviceCode : "device-code" ,
206
+ Message : "msg" ,
207
+ Interval : 1 ,
208
+ Scopes : tokenScope ,
209
+ UserCode : "user-code" ,
210
+ VerificationURL : "https://device.code.local" ,
211
+ }
212
+ mockClient := mock.Client {}
213
+ mockClient .AppendResponse (mock .WithBody (mock .GetTenantDiscoveryBody ("http://localhost" , "tenant" )))
214
+ mockClient .AppendResponse (mock .WithBody ([]byte (
215
+ fmt .Sprintf (
216
+ `{"device_code":%q,"expires_in":60,"interval":%d,"message":%q,"user_code":%q,"verification_uri":%q}` ,
217
+ expected .DeviceCode ,
218
+ expected .Interval ,
219
+ expected .Message ,
220
+ expected .UserCode ,
221
+ expected .VerificationURL ,
222
+ ),
223
+ )))
224
+ mockClient .AppendResponse (
225
+ mock .WithBody (mock .GetAccessTokenBody (accessToken , "" , "rt" , "" , 3600 )),
226
+ mock .WithCallback (func (r * http.Request ) {
227
+ if r .Method != http .MethodPost {
228
+ t .Fatalf ("unexpected method %q" , r .Method )
229
+ }
230
+ if err := r .ParseForm (); err != nil {
231
+ t .Fatal (err )
232
+ }
233
+ if v := r .Form .Get ("client_id" ); v != expected .ClientID {
234
+ t .Fatalf ("unexpected client_id %q" , v )
235
+ }
236
+ if v := r .Form .Get ("device_code" ); v != expected .DeviceCode {
237
+ t .Fatalf ("unexpected device_code %q" , v )
238
+ }
239
+ }),
240
+ )
241
+ client , err := New (expected .ClientID , WithHTTPClient (& mockClient ))
242
+ if err != nil {
243
+ t .Fatal (err )
244
+ }
245
+ dc , err := client .AcquireTokenByDeviceCode (context .Background (), tokenScope )
246
+ if err != nil {
247
+ t .Fatal (err )
248
+ }
249
+ actual := dc .Result
250
+ if actual .ClientID != expected .ClientID {
251
+ t .Fatalf ("unexpected client ID %q" , actual .ClientID )
252
+ }
253
+ if actual .DeviceCode != expected .DeviceCode {
254
+ t .Fatalf ("unexpected device code %q" , actual .DeviceCode )
255
+ }
256
+ if ! actual .ExpiresOn .After (time .Now ()) {
257
+ t .Fatalf ("expected a future expiration time but got %v" , actual .ExpiresOn )
258
+ }
259
+ if actual .Interval != expected .Interval {
260
+ t .Fatalf ("unexpected interval %d" , actual .Interval )
261
+ }
262
+ if actual .Message != expected .Message {
263
+ t .Fatalf ("unexpected message %q" , actual .Message )
264
+ }
265
+ if actual .UserCode != expected .UserCode {
266
+ t .Fatalf ("unexpected user code %q" , actual .UserCode )
267
+ }
268
+ if actual .VerificationURL != expected .VerificationURL {
269
+ t .Fatalf ("unexpected verification URL %q" , actual .VerificationURL )
270
+ }
271
+ ar , err := dc .AuthenticationResult (context .Background ())
272
+ if err != nil {
273
+ t .Fatal (err )
274
+ }
275
+ if ar .AccessToken != accessToken {
276
+ t .Fatalf ("unexpected access token %q" , ar .AccessToken )
277
+ }
278
+ }
279
+
201
280
func TestAcquireTokenWithTenantID (t * testing.T ) {
202
281
accessToken := "*"
203
282
clientInfo := base64 .RawStdEncoding .EncodeToString ([]byte (`{"uid":"uid","utid":"utid"}` ))
0 commit comments