OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/run_loop.h" | 5 #include "base/run_loop.h" |
6 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 6 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
7 #include "components/signin/core/browser/test_signin_client.h" | 7 #include "components/signin/core/browser/test_signin_client.h" |
8 #include "components/signin/ios/browser/profile_oauth2_token_service_ios.h" | 8 #include "components/signin/ios/browser/profile_oauth2_token_service_ios.h" |
9 #include "google_apis/gaia/gaia_urls.h" | 9 #include "google_apis/gaia/gaia_urls.h" |
10 #include "google_apis/gaia/oauth2_token_service_test_util.h" | 10 #include "google_apis/gaia/oauth2_token_service_test_util.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 access_token_success_(0), | 25 access_token_success_(0), |
26 access_token_failure_(0), | 26 access_token_failure_(0), |
27 last_access_token_error_(GoogleServiceAuthError::NONE) {} | 27 last_access_token_error_(GoogleServiceAuthError::NONE) {} |
28 | 28 |
29 virtual void SetUp() OVERRIDE { | 29 virtual void SetUp() OVERRIDE { |
30 factory_.SetFakeResponse(GaiaUrls::GetInstance()->oauth2_revoke_url(), | 30 factory_.SetFakeResponse(GaiaUrls::GetInstance()->oauth2_revoke_url(), |
31 "", | 31 "", |
32 net::HTTP_OK, | 32 net::HTTP_OK, |
33 net::URLRequestStatus::SUCCESS); | 33 net::URLRequestStatus::SUCCESS); |
34 fake_provider_ = client_.GetIOSProviderAsFake(); | 34 fake_provider_ = client_.GetIOSProviderAsFake(); |
35 fake_provider_->set_using_shared_authentication(true); | |
36 oauth2_service_.Initialize(&client_); | 35 oauth2_service_.Initialize(&client_); |
37 oauth2_service_.AddObserver(this); | 36 oauth2_service_.AddObserver(this); |
38 } | 37 } |
39 | 38 |
40 virtual void TearDown() OVERRIDE { | 39 virtual void TearDown() OVERRIDE { |
41 oauth2_service_.RemoveObserver(this); | 40 oauth2_service_.RemoveObserver(this); |
42 oauth2_service_.Shutdown(); | 41 oauth2_service_.Shutdown(); |
43 } | 42 } |
44 | 43 |
45 // OAuth2TokenService::Consumer implementation. | 44 // OAuth2TokenService::Consumer implementation. |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 oauth2_service_.StartRequest("account_id_1", scopes, this)); | 189 oauth2_service_.StartRequest("account_id_1", scopes, this)); |
191 EXPECT_EQ(0, access_token_success_); | 190 EXPECT_EQ(0, access_token_success_); |
192 EXPECT_EQ(0, access_token_failure_); | 191 EXPECT_EQ(0, access_token_failure_); |
193 | 192 |
194 ResetObserverCounts(); | 193 ResetObserverCounts(); |
195 fake_provider_->IssueAccessTokenErrorForAllRequests(); | 194 fake_provider_->IssueAccessTokenErrorForAllRequests(); |
196 base::RunLoop().RunUntilIdle(); | 195 base::RunLoop().RunUntilIdle(); |
197 EXPECT_EQ(0, access_token_success_); | 196 EXPECT_EQ(0, access_token_success_); |
198 EXPECT_EQ(1, access_token_failure_); | 197 EXPECT_EQ(1, access_token_failure_); |
199 } | 198 } |
200 | |
201 TEST_F(ProfileOAuth2TokenServiceIOSTest, Migration) { | |
202 fake_provider_->set_using_shared_authentication(false); | |
203 oauth2_service_.LoadCredentials("account_id_1"); | |
204 base::RunLoop().RunUntilIdle(); | |
205 | |
206 ResetObserverCounts(); | |
207 oauth2_service_.UpdateCredentials("account_id_1", "pre_sso_refresh_token_1"); | |
208 oauth2_service_.UpdateCredentials("account_id_2", "pre_sso_refresh_token_2"); | |
209 EXPECT_EQ(2, token_available_count_); | |
210 EXPECT_EQ(0, tokens_loaded_count_); | |
211 EXPECT_EQ(0, token_revoked_count_); | |
212 EXPECT_EQ(2U, oauth2_service_.GetAccounts().size()); | |
213 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_1")); | |
214 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_2")); | |
215 EXPECT_EQ("pre_sso_refresh_token_1", | |
216 oauth2_service_.GetRefreshTokenWhenNotUsingSharedAuthentication( | |
217 "account_id_1")); | |
218 EXPECT_EQ("pre_sso_refresh_token_2", | |
219 oauth2_service_.GetRefreshTokenWhenNotUsingSharedAuthentication( | |
220 "account_id_2")); | |
221 | |
222 ResetObserverCounts(); | |
223 oauth2_service_.StartUsingSharedAuthentication(); | |
224 EXPECT_EQ(0, token_available_count_); | |
225 EXPECT_EQ(0, tokens_loaded_count_); | |
226 EXPECT_EQ(2, token_revoked_count_); | |
227 EXPECT_EQ(0U, oauth2_service_.GetAccounts().size()); | |
228 EXPECT_FALSE(oauth2_service_.RefreshTokenIsAvailable("account_id_1")); | |
229 EXPECT_FALSE(oauth2_service_.RefreshTokenIsAvailable("account_id_2")); | |
230 | |
231 ResetObserverCounts(); | |
232 fake_provider_->AddAccount("account_id_1"); | |
233 oauth2_service_.ReloadCredentials(); | |
234 EXPECT_EQ(1, token_available_count_); | |
235 EXPECT_EQ(0, tokens_loaded_count_); | |
236 EXPECT_EQ(0, token_revoked_count_); | |
237 EXPECT_EQ(1U, oauth2_service_.GetAccounts().size()); | |
238 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_1")); | |
239 EXPECT_FALSE(oauth2_service_.RefreshTokenIsAvailable("account_id_2")); | |
240 } | |
241 | |
242 TEST_F(ProfileOAuth2TokenServiceIOSTest, ForceInvalidGrantResponses) { | |
243 fake_provider_->set_using_shared_authentication(false); | |
244 oauth2_service_.LoadCredentials("account_id_1"); | |
245 base::RunLoop().RunUntilIdle(); | |
246 oauth2_service_.UpdateCredentials("account_id_1", "pre_sso_refresh_token_1"); | |
247 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_1")); | |
248 | |
249 // First call revokes the existing token and then updates the credentials | |
250 // with a fake token. | |
251 ResetObserverCounts(); | |
252 oauth2_service_.ForceInvalidGrantResponses(); | |
253 EXPECT_EQ(1, token_available_count_); | |
254 EXPECT_EQ(0, tokens_loaded_count_); | |
255 EXPECT_EQ(1, token_revoked_count_); | |
256 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_1")); | |
257 | |
258 // Fetching access tokens fails with invalid grant responses. | |
259 OAuth2TokenService::ScopeSet scopes; | |
260 scopes.insert("scope"); | |
261 scoped_ptr<OAuth2TokenService::Request> request( | |
262 oauth2_service_.StartRequest("account_id_1", scopes, this)); | |
263 base::RunLoop().RunUntilIdle(); | |
264 EXPECT_EQ(0, access_token_success_); | |
265 EXPECT_EQ(1, access_token_failure_); | |
266 EXPECT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS, | |
267 last_access_token_error_.state()); | |
268 | |
269 // Second call to force invalid grant responses is ignored. | |
270 ResetObserverCounts(); | |
271 oauth2_service_.ForceInvalidGrantResponses(); | |
272 EXPECT_EQ(0, token_available_count_); | |
273 EXPECT_EQ(0, tokens_loaded_count_); | |
274 EXPECT_EQ(0, token_revoked_count_); | |
275 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_1")); | |
276 } | |
OLD | NEW |