Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(371)

Delta Between Two Patch Sets: Src/GoogleApis.Auth.Tests/OAuth2/AuthorizationCodeFlowTests.cs

Issue 13972043: Issue 351: Reimplement OAuth2 (Step 3 - Tests, Flows and Credential) (Closed) Base URL: https://google-api-dotnet-client.googlecode.com/hg/
Left Patch Set: Jon Skeet review Created 10 years, 10 months ago
Right Patch Set: minor Created 10 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 /* 1 /*
2 Copyright 2013 Google Inc 2 Copyright 2013 Google Inc
3 3
4 Licensed under the Apache License, Version 2.0 (the "License"); 4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License. 5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at 6 You may obtain a copy of the License at
7 7
8 http://www.apache.org/licenses/LICENSE-2.0 8 http://www.apache.org/licenses/LICENSE-2.0
9 9
10 Unless required by applicable law or agreed to in writing, software 10 Unless required by applicable law or agreed to in writing, software
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 { 50 {
51 // ClientSecrets are missing. 51 // ClientSecrets are missing.
52 try 52 try
53 { 53 {
54 new AuthorizationCodeFlow(new AuthorizationCodeFlow.Initializer( 54 new AuthorizationCodeFlow(new AuthorizationCodeFlow.Initializer(
55 "https://authorization_code.com", "https://token.com")); 55 "https://authorization_code.com", "https://token.com"));
56 Assert.Fail(); 56 Assert.Fail();
57 } 57 }
58 catch (ArgumentException ex) 58 catch (ArgumentException ex)
59 { 59 {
60 Assert.True(ex.Message.Contains("client secret or client secret stream MUST be set"), 60 Assert.True(ex.Message.Contains("You MUST set ClientSecret or Cl ientSecretStream"));
61 "User MUST specify client secrets!");
62 } 61 }
63 } 62 }
64 63
65 [Test] 64 [Test]
66 public void TestConstructor_DefaultValues() 65 public void TestConstructor_DefaultValues()
67 { 66 {
68 var flow = CreateFlow(); 67 var flow = CreateFlow();
69 Assert.NotNull(flow.AccessMethod); 68 Assert.NotNull(flow.AccessMethod);
70 Assert.That(flow.AccessMethod, Is.InstanceOf<BearerToken.Authorizati onHeaderAccessMethod>()); 69 Assert.That(flow.AccessMethod, Is.InstanceOf<BearerToken.Authorizati onHeaderAccessMethod>());
71 Assert.That(flow.AuthorizationServerUrl, Is.EqualTo("https://authori zation.com")); 70 Assert.That(flow.AuthorizationServerUrl, Is.EqualTo("https://authori zation.com"));
72 Assert.NotNull(flow.ClientSecrets); 71 Assert.NotNull(flow.ClientSecrets);
73 Assert.That(flow.ClientSecrets.ClientId, Is.EqualTo("id")); 72 Assert.That(flow.ClientSecrets.ClientId, Is.EqualTo("id"));
74 Assert.That(flow.ClientSecrets.ClientSecret, Is.EqualTo("secret")); 73 Assert.That(flow.ClientSecrets.ClientSecret, Is.EqualTo("secret"));
75 Assert.That(flow.Clock, Is.InstanceOf<SystemClock>()); 74 Assert.That(flow.Clock, Is.InstanceOf<SystemClock>());
76 Assert.Null(flow.DataStore); 75 Assert.Null(flow.DataStore);
77 Assert.NotNull(flow.HttpClient); 76 Assert.NotNull(flow.HttpClient);
78 Assert.NotNull(flow.Scopes); 77 Assert.NotNull(flow.Scopes);
79 Assert.That(flow.TokenServerEncodedUrl, Is.EqualTo("https://token.co m")); 78 Assert.That(flow.TokenServerUrl, Is.EqualTo("https://token.com"));
80 79
81 Assert.That(flow.HttpClient.MessageHandler.UnsuccessfulResponseHandl ers.Count(), Is.EqualTo(1)); 80 Assert.That(flow.HttpClient.MessageHandler.UnsuccessfulResponseHandl ers.Count(), Is.EqualTo(1));
82 Assert.That(flow.HttpClient.MessageHandler.UnsuccessfulResponseHandl ers.First(), 81 Assert.That(flow.HttpClient.MessageHandler.UnsuccessfulResponseHandl ers.First(),
83 Is.InstanceOf<BackOffHandler>()); 82 Is.InstanceOf<BackOffHandler>());
84 } 83 }
85 84
86 #endregion 85 #endregion
87 86
88 #region LoadToken 87 #region LoadToken
89 88
(...skipping 25 matching lines...) Expand all
115 var result = SubtestLoadTokenAsync(tcs); 114 var result = SubtestLoadTokenAsync(tcs);
116 Assert.That(result, Is.EqualTo(response)); 115 Assert.That(result, Is.EqualTo(response));
117 } 116 }
118 117
119 private TokenResponse SubtestLoadTokenAsync(TaskCompletionSource<TokenRe sponse> tcs) 118 private TokenResponse SubtestLoadTokenAsync(TaskCompletionSource<TokenRe sponse> tcs)
120 { 119 {
121 var mock = new Mock<IDataStore>(); 120 var mock = new Mock<IDataStore>();
122 mock.Setup(ds => ds.GetAsync<TokenResponse>("user")).Returns(tcs.Tas k); 121 mock.Setup(ds => ds.GetAsync<TokenResponse>("user")).Returns(tcs.Tas k);
123 var flow = CreateFlow(dataStore: mock.Object); 122 var flow = CreateFlow(dataStore: mock.Object);
124 var result = flow.LoadTokenAsync("user", CancellationToken.None).Res ult; 123 var result = flow.LoadTokenAsync("user", CancellationToken.None).Res ult;
125 // Verify Get("user") was called.
126 mock.Verify(ds => ds.GetAsync<TokenResponse>("user")); 124 mock.Verify(ds => ds.GetAsync<TokenResponse>("user"));
127 return result; 125 return result;
128 } 126 }
129 127
130 #endregion 128 #endregion
131 129
132 #region CreateAuthorizationCodeRequest 130 #region CreateAuthorizationCodeRequest
133 131
134 [Test] 132 [Test]
135 public void TestCreateAuthorizationCodeRequest() 133 public void TestCreateAuthorizationCodeRequest()
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 handler.RefreshTokenRequest = new RefreshTokenRequest() 355 handler.RefreshTokenRequest = new RefreshTokenRequest()
358 { 356 {
359 RefreshToken = "REFRESH", 357 RefreshToken = "REFRESH",
360 Scope = "a" 358 Scope = "a"
361 }; 359 };
362 handler.Error = true; 360 handler.Error = true;
363 SubtestFetchTokenAsync_Error(handler); 361 SubtestFetchTokenAsync_Error(handler);
364 } 362 }
365 363
366 /// <summary>Subtest for receiving an error token response.</summary> 364 /// <summary>Subtest for receiving an error token response.</summary>
367 /// <param name="handler">The message handler</param> 365 /// <param name="handler">The message handler.</param>
368 private void SubtestFetchTokenAsync_Error(FetchTokenMessageHandler handl er) 366 private void SubtestFetchTokenAsync_Error(FetchTokenMessageHandler handl er)
369 { 367 {
370 MockHttpClientFactory mockFactory = new MockHttpClientFactory(handle r); 368 MockHttpClientFactory mockFactory = new MockHttpClientFactory(handle r);
371 var flow = CreateFlow(httpClientFactory: mockFactory); 369 var flow = CreateFlow(httpClientFactory: mockFactory);
372 try 370 try
373 { 371 {
374 var request = 372 var request =
375 (TokenRequest)handler.AuthorizationCodeTokenRequest ?? (Toke nRequest)handler.RefreshTokenRequest; 373 (TokenRequest)handler.AuthorizationCodeTokenRequest ?? (Toke nRequest)handler.RefreshTokenRequest;
376 var result = flow.FetchTokenAsync("user", request, CancellationT oken.None).Result; 374 var result = flow.FetchTokenAsync("user", request, CancellationT oken.None).Result;
377 Assert.Fail(); 375 Assert.Fail();
378 } 376 }
379 catch (AggregateException aex) 377 catch (AggregateException aex)
380 { 378 {
381 var ex = aex.InnerException as TokenResponseException; 379 var ex = aex.InnerException as TokenResponseException;
382 Assert.IsNotNull(ex); 380 Assert.IsNotNull(ex);
383 var result = ex.Error; 381 var result = ex.Error;
384 Assert.That(result.Error, Is.EqualTo("error")); 382 Assert.That(result.Error, Is.EqualTo("error"));
385 Assert.That(result.ErrorDescription, Is.EqualTo("desc")); 383 Assert.That(result.ErrorDescription, Is.EqualTo("desc"));
386 Assert.That(result.ErrorUri, Is.EqualTo("uri")); 384 Assert.That(result.ErrorUri, Is.EqualTo("uri"));
387 } 385 }
388 } 386 }
389 387
390 #endregion 388 #endregion
391 389
392 /// <summary>Creates an authorization code flow with the given parameter s.</summary> 390 /// <summary>Creates an authorization code flow with the given parameter s.</summary>
393 /// <param name="dataStore">The data store.</param> 391 /// <param name="dataStore">The data store.</param>
394 /// <param name="scopes">The Scopes</param> 392 /// <param name="scopes">The Scopes.</param>
395 /// <param name="httpClientFactory">The HTTP client factory. If not set the default will be used</param> 393 /// <param name="httpClientFactory">The HTTP client factory. If not set the default will be used.</param>
396 /// <returns>Authorization code flow</returns> 394 /// <returns>Authorization code flow</returns>
397 private AuthorizationCodeFlow CreateFlow(IDataStore dataStore = null, IE numerable<string> scopes = null, 395 private AuthorizationCodeFlow CreateFlow(IDataStore dataStore = null, IE numerable<string> scopes = null,
398 IHttpClientFactory httpClientFactory = null) 396 IHttpClientFactory httpClientFactory = null)
399 { 397 {
400 var secrets = new ClientSecrets() { ClientId = "id", ClientSecret = "secret" }; 398 var secrets = new ClientSecrets() { ClientId = "id", ClientSecret = "secret" };
401 var initializer = new AuthorizationCodeFlow.Initializer(Authorizatio nCodeUrl, TokenUrl) 399 var initializer = new AuthorizationCodeFlow.Initializer(Authorizatio nCodeUrl, TokenUrl)
402 { 400 {
403 ClientSecrets = secrets, 401 ClientSecrets = secrets,
404 HttpClientFactory = httpClientFactory 402 HttpClientFactory = httpClientFactory
405 }; 403 };
(...skipping 12 matching lines...) Expand all
418 /// <summary>Verifies that the token response contains the expected data .</summary> 416 /// <summary>Verifies that the token response contains the expected data .</summary>
419 /// <param name="response">The token response</param> 417 /// <param name="response">The token response</param>
420 private void SubtestTokenResponse(TokenResponse response) 418 private void SubtestTokenResponse(TokenResponse response)
421 { 419 {
422 Assert.That(response.RefreshToken, Is.EqualTo("r")); 420 Assert.That(response.RefreshToken, Is.EqualTo("r"));
423 Assert.That(response.ExpiresInSeconds, Is.EqualTo(100)); 421 Assert.That(response.ExpiresInSeconds, Is.EqualTo(100));
424 Assert.That(response.Scope, Is.EqualTo("b")); 422 Assert.That(response.Scope, Is.EqualTo("b"));
425 } 423 }
426 } 424 }
427 } 425 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b