| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/signin/oauth2_token_service.h" | 6 #include "chrome/browser/signin/oauth2_token_service.h" |
| 6 #include "chrome/browser/signin/oauth2_token_service_test_util.h" | 7 #include "chrome/browser/signin/oauth2_token_service_test_util.h" |
| 7 #include "chrome/browser/signin/profile_oauth2_token_service.h" | 8 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
| 8 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 9 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 9 #include "chrome/browser/signin/token_service_unittest.h" | 10 #include "chrome/browser/signin/token_service_unittest.h" |
| 10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 11 #include "google_apis/gaia/gaia_constants.h" | 12 #include "google_apis/gaia/gaia_constants.h" |
| 12 #include "net/http/http_status_code.h" | 13 #include "net/http/http_status_code.h" |
| 13 #include "net/url_request/test_url_fetcher_factory.h" | 14 #include "net/url_request/test_url_fetcher_factory.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 using content::BrowserThread; | 17 using content::BrowserThread; |
| 17 | 18 |
| 18 class ProfileOAuth2TokenServiceTest : public TokenServiceTestHarness, | 19 class ProfileOAuth2TokenServiceTest : public TokenServiceTestHarness, |
| 19 public OAuth2TokenService::Observer { | 20 public OAuth2TokenService::Observer { |
| 20 public: | 21 public: |
| 21 ProfileOAuth2TokenServiceTest() | 22 ProfileOAuth2TokenServiceTest() |
| 22 : token_available_count_(0), | 23 : token_available_count_(0), |
| 23 token_revoked_count_(0), | 24 token_revoked_count_(0), |
| 24 tokens_loaded_count_(0), | 25 tokens_loaded_count_(0), |
| 25 tokens_cleared_count_(0) { | 26 tokens_cleared_count_(0) { |
| 26 } | 27 } |
| 27 | 28 |
| 28 virtual void SetUp() OVERRIDE { | 29 virtual void SetUp() OVERRIDE { |
| 29 TokenServiceTestHarness::SetUp(); | 30 TokenServiceTestHarness::SetUp(); |
| 30 io_thread_.reset(new content::TestBrowserThread(content::BrowserThread::IO, | 31 UpdateCredentialsOnService(); |
| 31 &message_loop_)); | |
| 32 service_->UpdateCredentials(credentials_); | |
| 33 profile_->CreateRequestContext(); | |
| 34 oauth2_service_ = ProfileOAuth2TokenServiceFactory::GetForProfile( | 32 oauth2_service_ = ProfileOAuth2TokenServiceFactory::GetForProfile( |
| 35 profile_.get()); | 33 profile()); |
| 36 | 34 |
| 37 oauth2_service_->AddObserver(this); | 35 oauth2_service_->AddObserver(this); |
| 38 } | 36 } |
| 39 | 37 |
| 40 virtual void TearDown() OVERRIDE { | 38 virtual void TearDown() OVERRIDE { |
| 41 oauth2_service_->RemoveObserver(this); | 39 oauth2_service_->RemoveObserver(this); |
| 42 TokenServiceTestHarness::TearDown(); | 40 TokenServiceTestHarness::TearDown(); |
| 43 } | 41 } |
| 44 | 42 |
| 45 // OAuth2TokenService::Observer implementation. | 43 // OAuth2TokenService::Observer implementation. |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 | 97 |
| 100 void ExpectOneTokensClearedNotification() { | 98 void ExpectOneTokensClearedNotification() { |
| 101 EXPECT_EQ(0, token_available_count_); | 99 EXPECT_EQ(0, token_available_count_); |
| 102 EXPECT_EQ(0, token_revoked_count_); | 100 EXPECT_EQ(0, token_revoked_count_); |
| 103 EXPECT_EQ(0, tokens_loaded_count_); | 101 EXPECT_EQ(0, tokens_loaded_count_); |
| 104 EXPECT_EQ(1, tokens_cleared_count_); | 102 EXPECT_EQ(1, tokens_cleared_count_); |
| 105 ResetObserverCounts(); | 103 ResetObserverCounts(); |
| 106 } | 104 } |
| 107 | 105 |
| 108 protected: | 106 protected: |
| 109 scoped_ptr<content::TestBrowserThread> io_thread_; | |
| 110 net::TestURLFetcherFactory factory_; | 107 net::TestURLFetcherFactory factory_; |
| 111 ProfileOAuth2TokenService* oauth2_service_; | 108 ProfileOAuth2TokenService* oauth2_service_; |
| 112 TestingOAuth2TokenServiceConsumer consumer_; | 109 TestingOAuth2TokenServiceConsumer consumer_; |
| 113 int token_available_count_; | 110 int token_available_count_; |
| 114 int token_revoked_count_; | 111 int token_revoked_count_; |
| 115 int tokens_loaded_count_; | 112 int tokens_loaded_count_; |
| 116 int tokens_cleared_count_; | 113 int tokens_cleared_count_; |
| 117 }; | 114 }; |
| 118 | 115 |
| 119 TEST_F(ProfileOAuth2TokenServiceTest, Notifications) { | 116 TEST_F(ProfileOAuth2TokenServiceTest, Notifications) { |
| 120 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing()); | 117 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing()); |
| 121 service_->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, | 118 service()->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, |
| 122 "refreshToken"); | 119 "refreshToken"); |
| 123 ExpectOneTokenAvailableNotification(); | 120 ExpectOneTokenAvailableNotification(); |
| 124 | 121 |
| 125 service_->EraseTokensFromDB(); | 122 service()->EraseTokensFromDB(); |
| 126 service_->ResetCredentialsInMemory(); | 123 service()->ResetCredentialsInMemory(); |
| 127 ExpectOneTokensClearedNotification(); | 124 ExpectOneTokensClearedNotification(); |
| 128 } | 125 } |
| 129 | 126 |
| 130 // Until the TokenService class is removed, problems fetching the LSO token | 127 // Until the TokenService class is removed, problems fetching the LSO token |
| 131 // should translate to problems fetching the oauth2 refresh token. | 128 // should translate to problems fetching the oauth2 refresh token. |
| 132 TEST_F(ProfileOAuth2TokenServiceTest, LsoNotification) { | 129 TEST_F(ProfileOAuth2TokenServiceTest, LsoNotification) { |
| 133 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing()); | 130 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing()); |
| 134 | 131 |
| 135 // Get a valid token. | 132 // Get a valid token. |
| 136 service_->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, | 133 service()->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, |
| 137 "refreshToken"); | 134 "refreshToken"); |
| 138 ExpectOneTokenAvailableNotification(); | 135 ExpectOneTokenAvailableNotification(); |
| 139 | 136 |
| 140 service_->OnIssueAuthTokenFailure( | 137 service()->OnIssueAuthTokenFailure( |
| 141 GaiaConstants::kLSOService, | 138 GaiaConstants::kLSOService, |
| 142 GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); | 139 GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); |
| 143 ExpectOneTokenRevokedNotification(); | 140 ExpectOneTokenRevokedNotification(); |
| 144 } | 141 } |
| 145 | 142 |
| 146 // Until the TokenService class is removed, finish token loading in TokenService | 143 // Until the TokenService class is removed, finish token loading in TokenService |
| 147 // should translate to finish token loading in ProfileOAuth2TokenService. | 144 // should translate to finish token loading in ProfileOAuth2TokenService. |
| 148 TEST_F(ProfileOAuth2TokenServiceTest, TokensLoaded) { | 145 TEST_F(ProfileOAuth2TokenServiceTest, TokensLoaded) { |
| 149 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing()); | 146 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing()); |
| 150 service_->LoadTokensFromDB(); | 147 service()->LoadTokensFromDB(); |
| 151 WaitForDBLoadCompletion(); | 148 base::RunLoop().RunUntilIdle(); |
| 152 ExpectOneTokensLoadedNotification(); | 149 ExpectOneTokensLoadedNotification(); |
| 153 } | 150 } |
| 154 | 151 |
| 155 TEST_F(ProfileOAuth2TokenServiceTest, UnknownNotificationsAreNoops) { | 152 TEST_F(ProfileOAuth2TokenServiceTest, UnknownNotificationsAreNoops) { |
| 156 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing()); | 153 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing()); |
| 157 service_->IssueAuthTokenForTest("foo", "toto"); | 154 service()->IssueAuthTokenForTest("foo", "toto"); |
| 158 ExpectNoNotifications(); | 155 ExpectNoNotifications(); |
| 159 | 156 |
| 160 // Get a valid token. | 157 // Get a valid token. |
| 161 service_->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, | 158 service()->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, |
| 162 "refreshToken"); | 159 "refreshToken"); |
| 163 ExpectOneTokenAvailableNotification(); | 160 ExpectOneTokenAvailableNotification(); |
| 164 | 161 |
| 165 service_->IssueAuthTokenForTest("bar", "baz"); | 162 service()->IssueAuthTokenForTest("bar", "baz"); |
| 166 ExpectNoNotifications(); | 163 ExpectNoNotifications(); |
| 167 } | 164 } |
| 168 | 165 |
| 169 TEST_F(ProfileOAuth2TokenServiceTest, TokenServiceUpdateClearsCache) { | 166 TEST_F(ProfileOAuth2TokenServiceTest, TokenServiceUpdateClearsCache) { |
| 170 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing()); | 167 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing()); |
| 171 std::set<std::string> scope_list; | 168 std::set<std::string> scope_list; |
| 172 scope_list.insert("scope"); | 169 scope_list.insert("scope"); |
| 173 service_->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, | 170 service()->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, |
| 174 "refreshToken"); | 171 "refreshToken"); |
| 175 ExpectOneTokenAvailableNotification(); | 172 ExpectOneTokenAvailableNotification(); |
| 176 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest( | 173 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest( |
| 177 scope_list, &consumer_)); | 174 scope_list, &consumer_)); |
| 178 message_loop_.RunUntilIdle(); | 175 base::RunLoop().RunUntilIdle(); |
| 179 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); | 176 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); |
| 180 fetcher->set_response_code(net::HTTP_OK); | 177 fetcher->set_response_code(net::HTTP_OK); |
| 181 fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); | 178 fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); |
| 182 fetcher->delegate()->OnURLFetchComplete(fetcher); | 179 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 183 EXPECT_EQ(1, consumer_.number_of_successful_tokens_); | 180 EXPECT_EQ(1, consumer_.number_of_successful_tokens_); |
| 184 EXPECT_EQ(0, consumer_.number_of_errors_); | 181 EXPECT_EQ(0, consumer_.number_of_errors_); |
| 185 EXPECT_EQ("token", consumer_.last_token_); | 182 EXPECT_EQ("token", consumer_.last_token_); |
| 186 EXPECT_EQ(1, oauth2_service_->cache_size_for_testing()); | 183 EXPECT_EQ(1, oauth2_service_->cache_size_for_testing()); |
| 187 | 184 |
| 188 // Signs out and signs in | 185 // Signs out and signs in |
| 189 service_->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, | 186 service()->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, |
| 190 ""); | 187 ""); |
| 191 ExpectOneTokenAvailableNotification(); | 188 ExpectOneTokenAvailableNotification(); |
| 192 service_->EraseTokensFromDB(); | 189 service()->EraseTokensFromDB(); |
| 193 ExpectOneTokensClearedNotification(); | 190 ExpectOneTokensClearedNotification(); |
| 194 | 191 |
| 195 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing()); | 192 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing()); |
| 196 service_->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, | 193 service()->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, |
| 197 "refreshToken"); | 194 "refreshToken"); |
| 198 ExpectOneTokenAvailableNotification(); | 195 ExpectOneTokenAvailableNotification(); |
| 199 | 196 |
| 200 request = oauth2_service_->StartRequest(scope_list, &consumer_); | 197 request = oauth2_service_->StartRequest(scope_list, &consumer_); |
| 201 message_loop_.RunUntilIdle(); | 198 base::RunLoop().RunUntilIdle(); |
| 202 fetcher = factory_.GetFetcherByID(0); | 199 fetcher = factory_.GetFetcherByID(0); |
| 203 fetcher->set_response_code(net::HTTP_OK); | 200 fetcher->set_response_code(net::HTTP_OK); |
| 204 fetcher->SetResponseString(GetValidTokenResponse("another token", 3600)); | 201 fetcher->SetResponseString(GetValidTokenResponse("another token", 3600)); |
| 205 fetcher->delegate()->OnURLFetchComplete(fetcher); | 202 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 206 EXPECT_EQ(2, consumer_.number_of_successful_tokens_); | 203 EXPECT_EQ(2, consumer_.number_of_successful_tokens_); |
| 207 EXPECT_EQ(0, consumer_.number_of_errors_); | 204 EXPECT_EQ(0, consumer_.number_of_errors_); |
| 208 EXPECT_EQ("another token", consumer_.last_token_); | 205 EXPECT_EQ("another token", consumer_.last_token_); |
| 209 EXPECT_EQ(1, oauth2_service_->cache_size_for_testing()); | 206 EXPECT_EQ(1, oauth2_service_->cache_size_for_testing()); |
| 210 } | 207 } |
| 211 | 208 |
| 212 // Android doesn't use the current profile's TokenService login refresh token. | 209 // Android doesn't use the current profile's TokenService login refresh token. |
| 213 #if !defined(OS_ANDROID) | 210 #if !defined(OS_ANDROID) |
| 214 TEST_F(ProfileOAuth2TokenServiceTest, StaleRefreshTokensNotCached) { | 211 TEST_F(ProfileOAuth2TokenServiceTest, StaleRefreshTokensNotCached) { |
| 215 EXPECT_FALSE(service_->HasOAuthLoginToken()); | 212 EXPECT_FALSE(service()->HasOAuthLoginToken()); |
| 216 EXPECT_FALSE(oauth2_service_->ShouldCacheForRefreshToken(service_, "T1")); | 213 EXPECT_FALSE(oauth2_service_->ShouldCacheForRefreshToken(service(), "T1")); |
| 217 | 214 |
| 218 service_->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, | 215 service()->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, |
| 219 "T1"); | 216 "T1"); |
| 220 ExpectOneTokenAvailableNotification(); | 217 ExpectOneTokenAvailableNotification(); |
| 221 EXPECT_TRUE(oauth2_service_->ShouldCacheForRefreshToken(service_, "T1")); | 218 EXPECT_TRUE(oauth2_service_->ShouldCacheForRefreshToken(service(), "T1")); |
| 222 EXPECT_FALSE(oauth2_service_->ShouldCacheForRefreshToken(service_, "T2")); | 219 EXPECT_FALSE(oauth2_service_->ShouldCacheForRefreshToken(service(), "T2")); |
| 223 | 220 |
| 224 service_->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, | 221 service()->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, |
| 225 "T2"); | 222 "T2"); |
| 226 ExpectOneTokenAvailableNotification(); | 223 ExpectOneTokenAvailableNotification(); |
| 227 EXPECT_TRUE(oauth2_service_->ShouldCacheForRefreshToken(service_, "T2")); | 224 EXPECT_TRUE(oauth2_service_->ShouldCacheForRefreshToken(service(), "T2")); |
| 228 EXPECT_FALSE(oauth2_service_->ShouldCacheForRefreshToken(NULL, "T2")); | 225 EXPECT_FALSE(oauth2_service_->ShouldCacheForRefreshToken(NULL, "T2")); |
| 229 } | 226 } |
| 230 #endif | 227 #endif |
| OLD | NEW |