Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(444)

Side by Side Diff: chrome/browser/signin/profile_oauth2_token_service_unittest.cc

Issue 23382008: Making OAuth2TokenService multi-login aware, updating callers, minor fixes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebasing to include the update to ProfileSyncService: r224220 Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/run_loop.h"
6 #include "chrome/browser/signin/profile_oauth2_token_service.h" 6 #include "chrome/browser/signin/profile_oauth2_token_service.h"
7 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 7 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
8 #include "chrome/browser/signin/token_service_unittest.h" 8 #include "chrome/browser/signin/token_service_unittest.h"
9 #include "content/public/browser/browser_thread.h" 9 #include "content/public/browser/browser_thread.h"
10 #include "google_apis/gaia/gaia_constants.h" 10 #include "google_apis/gaia/gaia_constants.h"
11 #include "google_apis/gaia/oauth2_token_service.h" 11 #include "google_apis/gaia/oauth2_token_service.h"
12 #include "google_apis/gaia/oauth2_token_service_test_util.h" 12 #include "google_apis/gaia/oauth2_token_service_test_util.h"
13 #include "net/http/http_status_code.h" 13 #include "net/http/http_status_code.h"
14 #include "net/url_request/test_url_fetcher_factory.h" 14 #include "net/url_request/test_url_fetcher_factory.h"
15 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 16
17 using content::BrowserThread; 17 using content::BrowserThread;
18 18
19 // Defining constant here to handle backward compatiblity tests, but this 19 // Defining constant here to handle backward compatiblity tests, but this
20 // constant is no longer used in current versions of chrome. 20 // constant is no longer used in current versions of chrome.
21 static const char kLSOService[] = "lso"; 21 static const char kLSOService[] = "lso";
22 22
23 class ProfileOAuth2TokenServiceTest : public TokenServiceTestHarness, 23 class ProfileOAuth2TokenServiceTest : public TokenServiceTestHarness,
24 public OAuth2TokenService::Observer { 24 public OAuth2TokenService::Observer {
25 public: 25 public:
26 ProfileOAuth2TokenServiceTest() 26 ProfileOAuth2TokenServiceTest()
27 : token_available_count_(0), 27 : token_available_count_(0),
28 token_revoked_count_(0), 28 token_revoked_count_(0),
29 tokens_loaded_count_(0), 29 tokens_loaded_count_(0) {
30 tokens_cleared_count_(0) {
31 } 30 }
32 31
33 virtual void SetUp() OVERRIDE { 32 virtual void SetUp() OVERRIDE {
34 TokenServiceTestHarness::SetUp(); 33 TokenServiceTestHarness::SetUp();
35 UpdateCredentialsOnService(); 34 UpdateCredentialsOnService();
36 oauth2_service_ = ProfileOAuth2TokenServiceFactory::GetForProfile( 35 oauth2_service_ = ProfileOAuth2TokenServiceFactory::GetForProfile(
37 profile()); 36 profile());
38 37
39 oauth2_service_->AddObserver(this); 38 oauth2_service_->AddObserver(this);
40 } 39 }
41 40
42 virtual void TearDown() OVERRIDE { 41 virtual void TearDown() OVERRIDE {
43 oauth2_service_->RemoveObserver(this); 42 oauth2_service_->RemoveObserver(this);
44 TokenServiceTestHarness::TearDown(); 43 TokenServiceTestHarness::TearDown();
45 } 44 }
46 45
47 // OAuth2TokenService::Observer implementation. 46 // OAuth2TokenService::Observer implementation.
48 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE { 47 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE {
49 ++token_available_count_; 48 ++token_available_count_;
50 } 49 }
51 virtual void OnRefreshTokenRevoked(const std::string& account_id) OVERRIDE { 50 virtual void OnRefreshTokenRevoked(const std::string& account_id) OVERRIDE {
52 ++token_revoked_count_; 51 ++token_revoked_count_;
53 } 52 }
54 virtual void OnRefreshTokensLoaded() OVERRIDE { 53 virtual void OnRefreshTokensLoaded() OVERRIDE {
55 ++tokens_loaded_count_; 54 ++tokens_loaded_count_;
56 } 55 }
57 virtual void OnRefreshTokensCleared() OVERRIDE {
58 ++tokens_cleared_count_;
59 }
60 56
61 void ResetObserverCounts() { 57 void ResetObserverCounts() {
62 token_available_count_ = 0; 58 token_available_count_ = 0;
63 token_revoked_count_ = 0; 59 token_revoked_count_ = 0;
64 tokens_loaded_count_ = 0; 60 tokens_loaded_count_ = 0;
65 tokens_cleared_count_ = 0;
66 } 61 }
67 62
68 void ExpectNoNotifications() { 63 void ExpectNoNotifications() {
69 EXPECT_EQ(0, token_available_count_); 64 EXPECT_EQ(0, token_available_count_);
70 EXPECT_EQ(0, token_revoked_count_); 65 EXPECT_EQ(0, token_revoked_count_);
71 EXPECT_EQ(0, tokens_loaded_count_); 66 EXPECT_EQ(0, tokens_loaded_count_);
72 EXPECT_EQ(0, tokens_cleared_count_);
73 ResetObserverCounts(); 67 ResetObserverCounts();
74 } 68 }
75 69
76 void ExpectOneTokenAvailableNotification() { 70 void ExpectOneTokenAvailableNotification() {
77 EXPECT_EQ(1, token_available_count_); 71 EXPECT_EQ(1, token_available_count_);
78 EXPECT_EQ(0, token_revoked_count_); 72 EXPECT_EQ(0, token_revoked_count_);
79 EXPECT_EQ(0, tokens_loaded_count_); 73 EXPECT_EQ(0, tokens_loaded_count_);
80 EXPECT_EQ(0, tokens_cleared_count_);
81 ResetObserverCounts(); 74 ResetObserverCounts();
82 } 75 }
83 76
84 void ExpectOneTokenRevokedNotification() { 77 void ExpectOneTokenRevokedNotification() {
85 EXPECT_EQ(0, token_available_count_); 78 EXPECT_EQ(0, token_available_count_);
86 EXPECT_EQ(1, token_revoked_count_); 79 EXPECT_EQ(1, token_revoked_count_);
87 EXPECT_EQ(0, tokens_loaded_count_); 80 EXPECT_EQ(0, tokens_loaded_count_);
88 EXPECT_EQ(0, tokens_cleared_count_);
89 ResetObserverCounts(); 81 ResetObserverCounts();
90 } 82 }
91 83
92 void ExpectOneTokensLoadedNotification() { 84 void ExpectOneTokensLoadedNotification() {
93 EXPECT_EQ(0, token_available_count_); 85 EXPECT_EQ(0, token_available_count_);
94 EXPECT_EQ(0, token_revoked_count_); 86 EXPECT_EQ(0, token_revoked_count_);
95 EXPECT_EQ(1, tokens_loaded_count_); 87 EXPECT_EQ(1, tokens_loaded_count_);
96 EXPECT_EQ(0, tokens_cleared_count_);
97 ResetObserverCounts(); 88 ResetObserverCounts();
98 } 89 }
99 90
100 void ExpectOneTokensClearedNotification() {
101 EXPECT_EQ(0, token_available_count_);
102 EXPECT_EQ(0, token_revoked_count_);
103 EXPECT_EQ(0, tokens_loaded_count_);
104 EXPECT_EQ(1, tokens_cleared_count_);
105 ResetObserverCounts();
106 }
107
108 protected: 91 protected:
109 net::TestURLFetcherFactory factory_; 92 net::TestURLFetcherFactory factory_;
110 ProfileOAuth2TokenService* oauth2_service_; 93 ProfileOAuth2TokenService* oauth2_service_;
111 TestingOAuth2TokenServiceConsumer consumer_; 94 TestingOAuth2TokenServiceConsumer consumer_;
112 int token_available_count_; 95 int token_available_count_;
113 int token_revoked_count_; 96 int token_revoked_count_;
114 int tokens_loaded_count_; 97 int tokens_loaded_count_;
115 int tokens_cleared_count_;
116 }; 98 };
117 99
118 TEST_F(ProfileOAuth2TokenServiceTest, Notifications) { 100 TEST_F(ProfileOAuth2TokenServiceTest, Notifications) {
119 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing()); 101 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing());
120 service()->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, 102 service()->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken,
121 "refreshToken"); 103 "refreshToken");
122 ExpectOneTokenAvailableNotification(); 104 ExpectOneTokenAvailableNotification();
123 105
124 service()->EraseTokensFromDB(); 106 service()->EraseTokensFromDB();
125 service()->ResetCredentialsInMemory(); 107 service()->ResetCredentialsInMemory();
126 ExpectOneTokensClearedNotification();
127 } 108 }
128 109
129 TEST_F(ProfileOAuth2TokenServiceTest, PersistenceDBUpgrade) { 110 TEST_F(ProfileOAuth2TokenServiceTest, PersistenceDBUpgrade) {
130 // No username for profile in unit tests, defaulting to empty string. 111 // No username for profile in unit tests, defaulting to empty string.
131 std::string main_account_id; 112 std::string main_account_id;
132 std::string main_refresh_token("old_refresh_token"); 113 std::string main_refresh_token("old_refresh_token");
133 114
134 // Populate DB with legacy tokens. 115 // Populate DB with legacy tokens.
135 service()->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, 116 service()->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService,
136 "syncServiceToken"); 117 "syncServiceToken");
137 service()->OnIssueAuthTokenSuccess(kLSOService, "lsoToken"); 118 service()->OnIssueAuthTokenSuccess(kLSOService, "lsoToken");
138 service()->OnIssueAuthTokenSuccess( 119 service()->OnIssueAuthTokenSuccess(
139 GaiaConstants::kGaiaOAuth2LoginRefreshToken, 120 GaiaConstants::kGaiaOAuth2LoginRefreshToken,
140 main_refresh_token); 121 main_refresh_token);
141 // Add a token using the new API. 122 // Add a token using the new API.
142 ResetObserverCounts(); 123 ResetObserverCounts();
143 124
144 // Force LoadCredentials. 125 // Force LoadCredentials.
145 oauth2_service_->LoadCredentials(); 126 oauth2_service_->LoadCredentials();
146 base::RunLoop().RunUntilIdle(); 127 base::RunLoop().RunUntilIdle();
147 128
148 // Legacy tokens get discarded, but the old refresh token is kept. 129 // Legacy tokens get discarded, but the old refresh token is kept.
149 EXPECT_EQ(1, tokens_loaded_count_); 130 EXPECT_EQ(1, tokens_loaded_count_);
150 EXPECT_EQ(1, token_available_count_); 131 EXPECT_EQ(1, token_available_count_);
151 EXPECT_TRUE(oauth2_service_->RefreshTokenIsAvailable()); 132 EXPECT_TRUE(oauth2_service_->RefreshTokenIsAvailable(main_account_id));
152 EXPECT_EQ(1U, oauth2_service_->refresh_tokens_.size()); 133 EXPECT_EQ(1U, oauth2_service_->refresh_tokens_.size());
153 EXPECT_EQ(main_refresh_token, 134 EXPECT_EQ(main_refresh_token,
154 oauth2_service_->refresh_tokens_[main_account_id]); 135 oauth2_service_->refresh_tokens_[main_account_id]);
155 136
156 // Add an old legacy token to the DB, to ensure it will not overwrite existing 137 // Add an old legacy token to the DB, to ensure it will not overwrite existing
157 // credentials for main account. 138 // credentials for main account.
158 service()->OnIssueAuthTokenSuccess( 139 service()->OnIssueAuthTokenSuccess(
159 GaiaConstants::kGaiaOAuth2LoginRefreshToken, 140 GaiaConstants::kGaiaOAuth2LoginRefreshToken,
160 "secondOldRefreshToken"); 141 "secondOldRefreshToken");
161 // Add some other legacy token. (Expected to get discarded). 142 // Add some other legacy token. (Expected to get discarded).
162 service()->OnIssueAuthTokenSuccess(kLSOService, "lsoToken"); 143 service()->OnIssueAuthTokenSuccess(kLSOService, "lsoToken");
163 // Also add a token using PO2TS.UpdateCredentials and make sure upgrade does 144 // Also add a token using PO2TS.UpdateCredentials and make sure upgrade does
164 // not wipe it. 145 // not wipe it.
165 std::string other_account_id("other_account_id"); 146 std::string other_account_id("other_account_id");
166 std::string other_refresh_token("other_refresh_token"); 147 std::string other_refresh_token("other_refresh_token");
167 oauth2_service_->UpdateCredentials(other_account_id, other_refresh_token); 148 oauth2_service_->UpdateCredentials(other_account_id, other_refresh_token);
168 ResetObserverCounts(); 149 ResetObserverCounts();
169 150
170 // Force LoadCredentials. 151 // Force LoadCredentials.
171 oauth2_service_->LoadCredentials(); 152 oauth2_service_->LoadCredentials();
172 base::RunLoop().RunUntilIdle(); 153 base::RunLoop().RunUntilIdle();
173 154
174 // Again legacy tokens get discarded, but since the main porfile account 155 // Again legacy tokens get discarded, but since the main porfile account
175 // token is present it is not overwritten. 156 // token is present it is not overwritten.
176 EXPECT_EQ(2, token_available_count_); 157 EXPECT_EQ(2, token_available_count_);
177 EXPECT_EQ(1, tokens_loaded_count_); 158 EXPECT_EQ(1, tokens_loaded_count_);
178 EXPECT_TRUE(oauth2_service_->RefreshTokenIsAvailable()); 159 EXPECT_TRUE(oauth2_service_->RefreshTokenIsAvailable(main_account_id));
160 // TODO(fgorski): cover both using RefreshTokenIsAvailable() and then get the
161 // tokens using GetRefreshToken()
179 EXPECT_EQ(2U, oauth2_service_->refresh_tokens_.size()); 162 EXPECT_EQ(2U, oauth2_service_->refresh_tokens_.size());
180 EXPECT_EQ(main_refresh_token, 163 EXPECT_EQ(main_refresh_token,
181 oauth2_service_->refresh_tokens_[main_account_id]); 164 oauth2_service_->refresh_tokens_[main_account_id]);
182 EXPECT_EQ(other_refresh_token, 165 EXPECT_EQ(other_refresh_token,
183 oauth2_service_->refresh_tokens_[other_account_id]); 166 oauth2_service_->refresh_tokens_[other_account_id]);
184 167
185 oauth2_service_->RevokeAllCredentials(); 168 oauth2_service_->RevokeAllCredentials();
186 } 169 }
187 170
188 TEST_F(ProfileOAuth2TokenServiceTest, PersistenceRevokeCredentials) { 171 TEST_F(ProfileOAuth2TokenServiceTest, PersistenceRevokeCredentials) {
(...skipping 18 matching lines...) Expand all
207 ExpectOneTokenRevokedNotification(); 190 ExpectOneTokenRevokedNotification();
208 191
209 // TODO(fgorski): Enable below when implemented: 192 // TODO(fgorski): Enable below when implemented:
210 // EXPECT_FALSE(oauth2_servive_->RefreshTokenIsAvailable(account_id_1)); 193 // EXPECT_FALSE(oauth2_servive_->RefreshTokenIsAvailable(account_id_1));
211 // EXPECT_TRUE(oauth2_service_->RefreshTokenIsAvailable(account_id_2)); 194 // EXPECT_TRUE(oauth2_service_->RefreshTokenIsAvailable(account_id_2));
212 195
213 oauth2_service_->RevokeAllCredentials(); 196 oauth2_service_->RevokeAllCredentials();
214 EXPECT_EQ(0, token_available_count_); 197 EXPECT_EQ(0, token_available_count_);
215 EXPECT_EQ(1, token_revoked_count_); 198 EXPECT_EQ(1, token_revoked_count_);
216 EXPECT_EQ(0, tokens_loaded_count_); 199 EXPECT_EQ(0, tokens_loaded_count_);
217 EXPECT_EQ(1, tokens_cleared_count_);
218 ResetObserverCounts(); 200 ResetObserverCounts();
219 } 201 }
220 202
221 TEST_F(ProfileOAuth2TokenServiceTest, PersistenceLoadCredentials) { 203 TEST_F(ProfileOAuth2TokenServiceTest, PersistenceLoadCredentials) {
222 // Ensure DB is clean. 204 // Ensure DB is clean.
223 oauth2_service_->RevokeAllCredentials(); 205 oauth2_service_->RevokeAllCredentials();
224 ExpectOneTokensClearedNotification(); 206 ResetObserverCounts();
225 // Perform a load from an empty DB. 207 // Perform a load from an empty DB.
226 oauth2_service_->LoadCredentials(); 208 oauth2_service_->LoadCredentials();
227 base::RunLoop().RunUntilIdle(); 209 base::RunLoop().RunUntilIdle();
228 ExpectOneTokensLoadedNotification(); 210 ExpectOneTokensLoadedNotification();
229 EXPECT_EQ(0U, oauth2_service_->refresh_tokens_.size()); 211 EXPECT_EQ(0U, oauth2_service_->refresh_tokens_.size());
230 // Setup a DB with tokens that don't require upgrade and clear memory. 212 // Setup a DB with tokens that don't require upgrade and clear memory.
231 oauth2_service_->UpdateCredentials("account_id", "refresh_token"); 213 oauth2_service_->UpdateCredentials("account_id", "refresh_token");
232 oauth2_service_->UpdateCredentials("account_id2", "refresh_token2"); 214 oauth2_service_->UpdateCredentials("account_id2", "refresh_token2");
233 oauth2_service_->refresh_tokens_.clear(); 215 oauth2_service_->refresh_tokens_.clear();
234 ResetObserverCounts(); 216 ResetObserverCounts();
235 217
236 oauth2_service_->LoadCredentials(); 218 oauth2_service_->LoadCredentials();
237 base::RunLoop().RunUntilIdle(); 219 base::RunLoop().RunUntilIdle();
238 EXPECT_EQ(2, token_available_count_); 220 EXPECT_EQ(2, token_available_count_);
239 EXPECT_EQ(0, token_revoked_count_); 221 EXPECT_EQ(0, token_revoked_count_);
240 EXPECT_EQ(1, tokens_loaded_count_); 222 EXPECT_EQ(1, tokens_loaded_count_);
241 EXPECT_EQ(0, tokens_cleared_count_);
242 ResetObserverCounts(); 223 ResetObserverCounts();
243 224
244 // TODO(fgorski): Enable below when implemented: 225 // TODO(fgorski): Enable below when implemented:
245 // EXPECT_TRUE(oauth2_servive_->RefreshTokenIsAvailable("account_id")); 226 // EXPECT_TRUE(oauth2_servive_->RefreshTokenIsAvailable("account_id"));
246 // EXPECT_TRUE(oauth2_service_->RefreshTokenIsAvailable("account_id2")); 227 // EXPECT_TRUE(oauth2_service_->RefreshTokenIsAvailable("account_id2"));
247 228
248 oauth2_service_->RevokeAllCredentials(); 229 oauth2_service_->RevokeAllCredentials();
249 EXPECT_EQ(0, token_available_count_); 230 EXPECT_EQ(0, token_available_count_);
250 EXPECT_EQ(2, token_revoked_count_); 231 EXPECT_EQ(2, token_revoked_count_);
251 EXPECT_EQ(0, tokens_loaded_count_); 232 EXPECT_EQ(0, tokens_loaded_count_);
252 EXPECT_EQ(1, tokens_cleared_count_);
253 ResetObserverCounts(); 233 ResetObserverCounts();
254 } 234 }
255 235
256 TEST_F(ProfileOAuth2TokenServiceTest, PersistanceNotifications) { 236 TEST_F(ProfileOAuth2TokenServiceTest, PersistanceNotifications) {
257 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing()); 237 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing());
258 oauth2_service_->UpdateCredentials("account_id", "refresh_token"); 238 oauth2_service_->UpdateCredentials("account_id", "refresh_token");
259 ExpectOneTokenAvailableNotification(); 239 ExpectOneTokenAvailableNotification();
260 240
261 oauth2_service_->UpdateCredentials("account_id", "refresh_token"); 241 oauth2_service_->UpdateCredentials("account_id", "refresh_token");
262 ExpectNoNotifications(); 242 ExpectNoNotifications();
263 243
264 oauth2_service_->UpdateCredentials("account_id", "refresh_token2"); 244 oauth2_service_->UpdateCredentials("account_id", "refresh_token2");
265 ExpectOneTokenAvailableNotification(); 245 ExpectOneTokenAvailableNotification();
266 246
267 oauth2_service_->RevokeCredentials("account_id"); 247 oauth2_service_->RevokeCredentials("account_id");
268 ExpectOneTokenRevokedNotification(); 248 ExpectOneTokenRevokedNotification();
269 249
270 oauth2_service_->UpdateCredentials("account_id", "refresh_token2"); 250 oauth2_service_->UpdateCredentials("account_id", "refresh_token2");
271 ExpectOneTokenAvailableNotification(); 251 ExpectOneTokenAvailableNotification();
272 252
273 oauth2_service_->RevokeAllCredentials(); 253 oauth2_service_->RevokeAllCredentials();
274 EXPECT_EQ(1, tokens_cleared_count_);
275 ResetObserverCounts(); 254 ResetObserverCounts();
276 } 255 }
277 256
257 TEST_F(ProfileOAuth2TokenServiceTest, GetAccounts) {
258 EXPECT_TRUE(oauth2_service_->GetAccounts().empty());
259 oauth2_service_->UpdateCredentials("account_id1", "refresh_token1");
260 oauth2_service_->UpdateCredentials("account_id2", "refresh_token2");
261 std::vector<std::string> accounts = oauth2_service_->GetAccounts();
262 EXPECT_EQ(2u, accounts.size());
263 EXPECT_EQ(1, count(accounts.begin(), accounts.end(), "account_id1"));
264 EXPECT_EQ(1, count(accounts.begin(), accounts.end(), "account_id2"));
265 oauth2_service_->RevokeCredentials("account_id2");
266 accounts = oauth2_service_->GetAccounts();
267 EXPECT_EQ(1u, oauth2_service_->GetAccounts().size());
268 EXPECT_EQ(1, count(accounts.begin(), accounts.end(), "account_id1"));
269 }
270
278 // Until the TokenService class is removed, finish token loading in TokenService 271 // Until the TokenService class is removed, finish token loading in TokenService
279 // should translate to finish token loading in ProfileOAuth2TokenService. 272 // should translate to finish token loading in ProfileOAuth2TokenService.
280 TEST_F(ProfileOAuth2TokenServiceTest, TokensLoaded) { 273 TEST_F(ProfileOAuth2TokenServiceTest, TokensLoaded) {
281 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing()); 274 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing());
282 service()->LoadTokensFromDB(); 275 service()->LoadTokensFromDB();
283 base::RunLoop().RunUntilIdle(); 276 base::RunLoop().RunUntilIdle();
284 ExpectOneTokensLoadedNotification(); 277 ExpectOneTokensLoadedNotification();
285 } 278 }
286 279
287 TEST_F(ProfileOAuth2TokenServiceTest, UnknownNotificationsAreNoops) { 280 TEST_F(ProfileOAuth2TokenServiceTest, UnknownNotificationsAreNoops) {
288 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing()); 281 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing());
289 service()->IssueAuthTokenForTest("foo", "toto"); 282 service()->IssueAuthTokenForTest("foo", "toto");
290 ExpectNoNotifications(); 283 ExpectNoNotifications();
291 284
292 // Get a valid token. 285 // Get a valid token.
293 service()->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, 286 service()->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken,
294 "refreshToken"); 287 "refreshToken");
295 ExpectOneTokenAvailableNotification(); 288 ExpectOneTokenAvailableNotification();
296 289
297 service()->IssueAuthTokenForTest("bar", "baz"); 290 service()->IssueAuthTokenForTest("bar", "baz");
298 ExpectNoNotifications(); 291 ExpectNoNotifications();
299 } 292 }
300 293
301 TEST_F(ProfileOAuth2TokenServiceTest, TokenServiceUpdateClearsCache) { 294 TEST_F(ProfileOAuth2TokenServiceTest, TokenServiceUpdateClearsCache) {
302 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing()); 295 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing());
303 std::set<std::string> scope_list; 296 std::set<std::string> scope_list;
304 scope_list.insert("scope"); 297 scope_list.insert("scope");
305 service()->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, 298 oauth2_service_->UpdateCredentials(oauth2_service_->GetPrimaryAccountId(),
306 "refreshToken"); 299 "refreshToken");
307 ExpectOneTokenAvailableNotification(); 300 ExpectOneTokenAvailableNotification();
301
308 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest( 302 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest(
309 scope_list, &consumer_)); 303 oauth2_service_->GetPrimaryAccountId(), scope_list, &consumer_));
310 base::RunLoop().RunUntilIdle(); 304 base::RunLoop().RunUntilIdle();
311 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); 305 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
312 fetcher->set_response_code(net::HTTP_OK); 306 fetcher->set_response_code(net::HTTP_OK);
313 fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); 307 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
314 fetcher->delegate()->OnURLFetchComplete(fetcher); 308 fetcher->delegate()->OnURLFetchComplete(fetcher);
315 EXPECT_EQ(1, consumer_.number_of_successful_tokens_); 309 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
316 EXPECT_EQ(0, consumer_.number_of_errors_); 310 EXPECT_EQ(0, consumer_.number_of_errors_);
317 EXPECT_EQ("token", consumer_.last_token_); 311 EXPECT_EQ("token", consumer_.last_token_);
318 EXPECT_EQ(1, oauth2_service_->cache_size_for_testing()); 312 EXPECT_EQ(1, oauth2_service_->cache_size_for_testing());
319 313
320 // Signs out and signs in 314 // Signs out and signs in
321 service()->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, 315 oauth2_service_->RevokeCredentials(oauth2_service_->GetPrimaryAccountId());
322 ""); 316 ExpectOneTokenRevokedNotification();
323 ExpectOneTokenAvailableNotification();
324 service()->EraseTokensFromDB();
325 ExpectOneTokensClearedNotification();
326 317
327 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing()); 318 EXPECT_EQ(0, oauth2_service_->cache_size_for_testing());
328 service()->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken, 319 oauth2_service_->UpdateCredentials(oauth2_service_->GetPrimaryAccountId(),
329 "refreshToken"); 320 "refreshToken");
330 ExpectOneTokenAvailableNotification(); 321 ExpectOneTokenAvailableNotification();
331 322
332 request = oauth2_service_->StartRequest(scope_list, &consumer_); 323 request = oauth2_service_->StartRequest(
324 oauth2_service_->GetPrimaryAccountId(), scope_list, &consumer_);
333 base::RunLoop().RunUntilIdle(); 325 base::RunLoop().RunUntilIdle();
334 fetcher = factory_.GetFetcherByID(0); 326 fetcher = factory_.GetFetcherByID(0);
335 fetcher->set_response_code(net::HTTP_OK); 327 fetcher->set_response_code(net::HTTP_OK);
336 fetcher->SetResponseString(GetValidTokenResponse("another token", 3600)); 328 fetcher->SetResponseString(GetValidTokenResponse("another token", 3600));
337 fetcher->delegate()->OnURLFetchComplete(fetcher); 329 fetcher->delegate()->OnURLFetchComplete(fetcher);
338 EXPECT_EQ(2, consumer_.number_of_successful_tokens_); 330 EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
339 EXPECT_EQ(0, consumer_.number_of_errors_); 331 EXPECT_EQ(0, consumer_.number_of_errors_);
340 EXPECT_EQ("another token", consumer_.last_token_); 332 EXPECT_EQ("another token", consumer_.last_token_);
341 EXPECT_EQ(1, oauth2_service_->cache_size_for_testing()); 333 EXPECT_EQ(1, oauth2_service_->cache_size_for_testing());
342 } 334 }
343
344 // Android doesn't use the current profile's TokenService login refresh token.
345 #if !defined(OS_ANDROID)
346 TEST_F(ProfileOAuth2TokenServiceTest, StaleRefreshTokensNotCached) {
347 EXPECT_FALSE(service()->HasOAuthLoginToken());
348 EXPECT_FALSE(oauth2_service_->ShouldCacheForRefreshToken(service(), "T1"));
349
350 service()->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken,
351 "T1");
352 ExpectOneTokenAvailableNotification();
353 EXPECT_TRUE(oauth2_service_->ShouldCacheForRefreshToken(service(), "T1"));
354 EXPECT_FALSE(oauth2_service_->ShouldCacheForRefreshToken(service(), "T2"));
355
356 service()->IssueAuthTokenForTest(GaiaConstants::kGaiaOAuth2LoginRefreshToken,
357 "T2");
358 ExpectOneTokenAvailableNotification();
359 EXPECT_TRUE(oauth2_service_->ShouldCacheForRefreshToken(service(), "T2"));
360 EXPECT_FALSE(oauth2_service_->ShouldCacheForRefreshToken(NULL, "T2"));
361 }
362 #endif
OLDNEW
« no previous file with comments | « chrome/browser/signin/profile_oauth2_token_service_request_unittest.cc ('k') | chrome/browser/signin/ubertoken_fetcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698