| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/signin/core/browser/access_token_fetcher.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" |
| 13 #include "base/test/mock_callback.h" |
| 14 #include "components/prefs/testing_pref_service.h" |
| 15 #include "components/signin/core/browser/account_tracker_service.h" |
| 16 #include "components/signin/core/browser/fake_signin_manager.h" |
| 17 #include "components/signin/core/browser/test_signin_client.h" |
| 18 #include "components/sync_preferences/testing_pref_service_syncable.h" |
| 19 #include "google_apis/gaia/fake_oauth2_token_service.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gmock_mutant.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 |
| 24 using base::MockCallback; |
| 25 using sync_preferences::TestingPrefServiceSyncable; |
| 26 using testing::_; |
| 27 using testing::CallbackToFunctor; |
| 28 using testing::InvokeWithoutArgs; |
| 29 using testing::StrictMock; |
| 30 |
| 31 class AccessTokenFetcherTest : public testing::Test { |
| 32 public: |
| 33 using TestTokenCallback = |
| 34 StrictMock<MockCallback<AccessTokenFetcher::TokenCallback>>; |
| 35 |
| 36 AccessTokenFetcherTest() |
| 37 : signin_client_(&pref_service_), |
| 38 signin_manager_(&signin_client_, &account_tracker_) { |
| 39 SigninManagerBase::RegisterProfilePrefs(pref_service_.registry()); |
| 40 SigninManagerBase::RegisterPrefs(pref_service_.registry()); |
| 41 } |
| 42 |
| 43 ~AccessTokenFetcherTest() override {} |
| 44 |
| 45 std::unique_ptr<AccessTokenFetcher> CreateFetcher( |
| 46 AccessTokenFetcher::TokenCallback callback) { |
| 47 std::set<std::string> scopes{"scope"}; |
| 48 return base::MakeUnique<AccessTokenFetcher>( |
| 49 "test_consumer", &signin_manager_, &token_service_, scopes, |
| 50 std::move(callback)); |
| 51 } |
| 52 |
| 53 FakeOAuth2TokenService* token_service() { return &token_service_; } |
| 54 FakeSigninManagerBase* signin_manager() { return &signin_manager_; } |
| 55 |
| 56 private: |
| 57 // AccessTokenFetcher may post a task to the current task runner, which |
| 58 // requires an active message loop. |
| 59 base::MessageLoop message_loop_; |
| 60 |
| 61 FakeOAuth2TokenService token_service_; |
| 62 TestingPrefServiceSyncable pref_service_; |
| 63 AccountTrackerService account_tracker_; |
| 64 TestSigninClient signin_client_; |
| 65 FakeSigninManagerBase signin_manager_; |
| 66 }; |
| 67 |
| 68 TEST_F(AccessTokenFetcherTest, ShouldReturnAccessToken) { |
| 69 TestTokenCallback callback; |
| 70 |
| 71 signin_manager()->SignIn("account"); |
| 72 token_service()->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials( |
| 73 "account", "refresh token"); |
| 74 |
| 75 // Signed in and refresh token already exists, so this should result in a |
| 76 // request for an access token. |
| 77 auto fetcher = CreateFetcher(callback.Get()); |
| 78 |
| 79 // Once the access token request is fulfilled, we should get called back with |
| 80 // the access token. |
| 81 EXPECT_CALL(callback, Run("access token")); |
| 82 token_service()->IssueAllTokensForAccount( |
| 83 "account", "access token", |
| 84 base::Time::Now() + base::TimeDelta::FromHours(1)); |
| 85 } |
| 86 |
| 87 TEST_F(AccessTokenFetcherTest, ShouldNotReplyIfDestroyed) { |
| 88 TestTokenCallback callback; |
| 89 |
| 90 signin_manager()->SignIn("account"); |
| 91 token_service()->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials( |
| 92 "account", "refresh token"); |
| 93 |
| 94 // Signed in and refresh token already exists, so this should result in a |
| 95 // request for an access token. |
| 96 auto fetcher = CreateFetcher(callback.Get()); |
| 97 |
| 98 // Destroy the fetcher before the access token request is fulfilled. |
| 99 fetcher.reset(); |
| 100 |
| 101 // Now fulfilling the access token request should have no effect. |
| 102 token_service()->IssueAllTokensForAccount( |
| 103 "account", "access token", |
| 104 base::Time::Now() + base::TimeDelta::FromHours(1)); |
| 105 } |
| 106 |
| 107 TEST_F(AccessTokenFetcherTest, ShouldReturnEmptyTokenWhenSignedOut) { |
| 108 TestTokenCallback callback; |
| 109 |
| 110 // Signed out -> we should get called back with an empty access token. |
| 111 base::RunLoop run_loop; |
| 112 EXPECT_CALL(callback, Run(std::string())) |
| 113 .WillOnce(InvokeWithoutArgs(CallbackToFunctor(run_loop.QuitClosure()))); |
| 114 auto fetcher = CreateFetcher(callback.Get()); |
| 115 run_loop.Run(); |
| 116 } |
| 117 |
| 118 TEST_F(AccessTokenFetcherTest, ShouldNotReplyIfDestroyedWhenSignedOut) { |
| 119 TestTokenCallback callback; |
| 120 |
| 121 auto fetcher = CreateFetcher(callback.Get()); |
| 122 |
| 123 // Destroy the fetcher before it gets a chance to run the callback. |
| 124 fetcher.reset(); |
| 125 |
| 126 // Make sure the fetcher didn't post a callback to the message loop. |
| 127 base::RunLoop().RunUntilIdle(); |
| 128 } |
| 129 |
| 130 TEST_F(AccessTokenFetcherTest, ShouldWaitForSignIn) { |
| 131 TestTokenCallback callback; |
| 132 |
| 133 signin_manager()->SetAuthInProgress(true); |
| 134 |
| 135 // A sign-in is currently in progress, so this should wait for the sign-in to |
| 136 // complete. |
| 137 auto fetcher = CreateFetcher(callback.Get()); |
| 138 |
| 139 signin_manager()->SignIn("account"); |
| 140 token_service()->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials( |
| 141 "account", "refresh token"); |
| 142 |
| 143 // Once the access token request is fulfilled, we should get called back with |
| 144 // the access token. |
| 145 EXPECT_CALL(callback, Run("access token")); |
| 146 token_service()->IssueAllTokensForAccount( |
| 147 "account", "access token", |
| 148 base::Time::Now() + base::TimeDelta::FromHours(1)); |
| 149 } |
| 150 |
| 151 TEST_F(AccessTokenFetcherTest, ShouldWaitForRefreshToken) { |
| 152 TestTokenCallback callback; |
| 153 |
| 154 signin_manager()->SignIn("account"); |
| 155 |
| 156 // Signed in, but there is no refresh token -> we should not get called back |
| 157 // (yet). |
| 158 auto fetcher = CreateFetcher(callback.Get()); |
| 159 |
| 160 // Getting a refresh token should result in a request for an access token. |
| 161 token_service()->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials( |
| 162 "account", "refresh token"); |
| 163 |
| 164 // Once the access token request is fulfilled, we should get called back with |
| 165 // the access token. |
| 166 EXPECT_CALL(callback, Run("access token")); |
| 167 token_service()->IssueAllTokensForAccount( |
| 168 "account", "access token", |
| 169 base::Time::Now() + base::TimeDelta::FromHours(1)); |
| 170 } |
| 171 |
| 172 TEST_F(AccessTokenFetcherTest, ShouldReturnWhenNoRefreshTokenAvailable) { |
| 173 TestTokenCallback callback; |
| 174 |
| 175 signin_manager()->SignIn("account"); |
| 176 |
| 177 // Signed in, but there is no refresh token -> we should not get called back |
| 178 // (yet). |
| 179 auto fetcher = CreateFetcher(callback.Get()); |
| 180 |
| 181 // Getting a refresh token for some other account should have no effect. |
| 182 token_service()->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials( |
| 183 "different account", "refresh token"); |
| 184 |
| 185 // When all refresh tokens have been loaded by the token service, but the one |
| 186 // for our account wasn't among them, we should get called back with an empty |
| 187 // access token |
| 188 EXPECT_CALL(callback, Run(std::string())); |
| 189 token_service()->GetFakeOAuth2TokenServiceDelegate()->LoadCredentials( |
| 190 "account doesn't matter"); |
| 191 } |
| 192 |
| 193 TEST_F(AccessTokenFetcherTest, ShouldRetryCanceledAccessTokenRequest) { |
| 194 TestTokenCallback callback; |
| 195 |
| 196 signin_manager()->SignIn("account"); |
| 197 token_service()->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials( |
| 198 "account", "refresh token"); |
| 199 |
| 200 // Signed in and refresh token already exists, so this should result in a |
| 201 // request for an access token. |
| 202 auto fetcher = CreateFetcher(callback.Get()); |
| 203 |
| 204 // A canceled access token request should get retried once. |
| 205 token_service()->IssueErrorForAllPendingRequestsForAccount( |
| 206 "account", |
| 207 GoogleServiceAuthError(GoogleServiceAuthError::State::REQUEST_CANCELED)); |
| 208 |
| 209 // Once the access token request is fulfilled, we should get called back with |
| 210 // the access token. |
| 211 EXPECT_CALL(callback, Run("access token")); |
| 212 token_service()->IssueAllTokensForAccount( |
| 213 "account", "access token", |
| 214 base::Time::Now() + base::TimeDelta::FromHours(1)); |
| 215 } |
| 216 |
| 217 TEST_F(AccessTokenFetcherTest, ShouldRetryCanceledAccessTokenRequestOnlyOnce) { |
| 218 TestTokenCallback callback; |
| 219 |
| 220 signin_manager()->SignIn("account"); |
| 221 token_service()->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials( |
| 222 "account", "refresh token"); |
| 223 |
| 224 // Signed in and refresh token already exists, so this should result in a |
| 225 // request for an access token. |
| 226 auto fetcher = CreateFetcher(callback.Get()); |
| 227 |
| 228 // A canceled access token request should get retried once. |
| 229 token_service()->IssueErrorForAllPendingRequestsForAccount( |
| 230 "account", |
| 231 GoogleServiceAuthError(GoogleServiceAuthError::State::REQUEST_CANCELED)); |
| 232 |
| 233 // On the second failure, we should get called back with an empty access |
| 234 // token. |
| 235 EXPECT_CALL(callback, Run(std::string())); |
| 236 token_service()->IssueErrorForAllPendingRequestsForAccount( |
| 237 "account", |
| 238 GoogleServiceAuthError(GoogleServiceAuthError::State::REQUEST_CANCELED)); |
| 239 } |
| 240 |
| 241 TEST_F(AccessTokenFetcherTest, ShouldNotRetryFailedAccessTokenRequest) { |
| 242 TestTokenCallback callback; |
| 243 |
| 244 signin_manager()->SignIn("account"); |
| 245 token_service()->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials( |
| 246 "account", "refresh token"); |
| 247 |
| 248 // Signed in and refresh token already exists, so this should result in a |
| 249 // request for an access token. |
| 250 auto fetcher = CreateFetcher(callback.Get()); |
| 251 |
| 252 // An access token failure other than "canceled" should not be retried; we |
| 253 // should immediately get called back with an empty access token. |
| 254 EXPECT_CALL(callback, Run(std::string())); |
| 255 token_service()->IssueErrorForAllPendingRequestsForAccount( |
| 256 "account", GoogleServiceAuthError( |
| 257 GoogleServiceAuthError::State::INVALID_GAIA_CREDENTIALS)); |
| 258 } |
| OLD | NEW |