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
3 // found in the LICENSE file.
8 #include "base/message_loop/message_loop.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/testing_pref_service.h"
11 #include "base/strings/stringprintf.h"
12 #include "components/signin/core/browser/account_tracker_service.h"
13 #include "components/signin/core/browser/test_signin_client.h"
14 #include "components/signin/core/common/signin_pref_names.h"
15 #include "google_apis/gaia/fake_oauth2_token_service.h"
16 #include "google_apis/gaia/gaia_oauth_client.h"
17 #include "net/http/http_status_code.h"
18 #include "net/url_request/test_url_fetcher_factory.h"
19 #include "net/url_request/url_fetcher_delegate.h"
20 #include "net/url_request/url_request_test_util.h"
21 #include "testing/gtest/include/gtest/gtest.h"
25 enum TrackingEventType
{
30 std::string
AccountIdToEmail(const std::string account_id
) {
31 return account_id
+ "@gmail.com";
34 std::string
AccountIdToGaiaId(const std::string account_id
) {
35 return "gaia-" + account_id
;
40 TrackingEvent(TrackingEventType type
,
41 const std::string
& account_id
,
42 const std::string
& gaia_id
)
44 account_id_(account_id
),
47 TrackingEvent(TrackingEventType type
,
48 const std::string
& account_id
)
50 account_id_(account_id
),
51 gaia_id_(AccountIdToGaiaId(account_id
)) {}
53 bool operator==(const TrackingEvent
& event
) const {
54 return type_
== event
.type_
&& account_id_
== event
.account_id_
&&
55 gaia_id_
== event
.gaia_id_
;
58 std::string
ToString() const {
59 const char * typestr
= "INVALID";
68 return base::StringPrintf("{ type: %s, account_id: %s, gaia: %s }",
75 friend bool CompareByUser(TrackingEvent a
, TrackingEvent b
);
77 TrackingEventType type_
;
78 std::string account_id_
;
82 bool CompareByUser(TrackingEvent a
, TrackingEvent b
) {
83 return a
.account_id_
< b
.account_id_
;
86 std::string
Str(const std::vector
<TrackingEvent
>& events
) {
87 std::string str
= "[";
88 bool needs_comma
= false;
89 for (std::vector
<TrackingEvent
>::const_iterator it
=
90 events
.begin(); it
!= events
.end(); ++it
) {
94 str
+= it
->ToString();
100 class AccountTrackerObserver
: public AccountTrackerService::Observer
{
102 AccountTrackerObserver() {}
103 ~AccountTrackerObserver() override
{}
106 void SortEventsByUser();
108 testing::AssertionResult
CheckEvents();
109 testing::AssertionResult
CheckEvents(const TrackingEvent
& e1
);
110 testing::AssertionResult
CheckEvents(const TrackingEvent
& e1
,
111 const TrackingEvent
& e2
);
112 testing::AssertionResult
CheckEvents(const TrackingEvent
& e1
,
113 const TrackingEvent
& e2
,
114 const TrackingEvent
& e3
);
117 // AccountTrackerService::Observer implementation
118 void OnAccountUpdated(const AccountTrackerService::AccountInfo
& ids
) override
;
119 void OnAccountRemoved(const AccountTrackerService::AccountInfo
& ids
) override
;
121 testing::AssertionResult
CheckEvents(
122 const std::vector
<TrackingEvent
>& events
);
124 std::vector
<TrackingEvent
> events_
;
127 void AccountTrackerObserver::OnAccountUpdated(
128 const AccountTrackerService::AccountInfo
& ids
) {
129 events_
.push_back(TrackingEvent(UPDATED
, ids
.account_id
, ids
.gaia
));
132 void AccountTrackerObserver::OnAccountRemoved(
133 const AccountTrackerService::AccountInfo
& ids
) {
134 events_
.push_back(TrackingEvent(REMOVED
, ids
.account_id
, ids
.gaia
));
137 void AccountTrackerObserver::Clear() {
141 void AccountTrackerObserver::SortEventsByUser() {
142 std::stable_sort(events_
.begin(), events_
.end(), CompareByUser
);
145 testing::AssertionResult
AccountTrackerObserver::CheckEvents() {
146 std::vector
<TrackingEvent
> events
;
147 return CheckEvents(events
);
150 testing::AssertionResult
AccountTrackerObserver::CheckEvents(
151 const TrackingEvent
& e1
) {
152 std::vector
<TrackingEvent
> events
;
153 events
.push_back(e1
);
154 return CheckEvents(events
);
157 testing::AssertionResult
AccountTrackerObserver::CheckEvents(
158 const TrackingEvent
& e1
,
159 const TrackingEvent
& e2
) {
160 std::vector
<TrackingEvent
> events
;
161 events
.push_back(e1
);
162 events
.push_back(e2
);
163 return CheckEvents(events
);
166 testing::AssertionResult
AccountTrackerObserver::CheckEvents(
167 const TrackingEvent
& e1
,
168 const TrackingEvent
& e2
,
169 const TrackingEvent
& e3
) {
170 std::vector
<TrackingEvent
> events
;
171 events
.push_back(e1
);
172 events
.push_back(e2
);
173 events
.push_back(e3
);
174 return CheckEvents(events
);
177 testing::AssertionResult
AccountTrackerObserver::CheckEvents(
178 const std::vector
<TrackingEvent
>& events
) {
179 std::string maybe_newline
= (events
.size() + events_
.size()) > 2 ? "\n" : "";
180 testing::AssertionResult
result(
182 ? testing::AssertionSuccess()
183 : (testing::AssertionFailure()
184 << "Expected " << maybe_newline
<< Str(events
) << ", "
185 << maybe_newline
<< "Got " << maybe_newline
<< Str(events_
)));
192 class AccountTrackerServiceTest
: public testing::Test
{
194 AccountTrackerServiceTest() {}
196 ~AccountTrackerServiceTest() override
{}
198 void SetUp() override
{
199 fake_oauth2_token_service_
.reset(new FakeOAuth2TokenService());
201 pref_service_
.registry()->RegisterListPref(
202 AccountTrackerService::kAccountInfoPref
);
203 pref_service_
.registry()->RegisterIntegerPref(
204 prefs::kAccountIdMigrationState
,
205 AccountTrackerService::MIGRATION_NOT_STARTED
);
206 signin_client_
.reset(new TestSigninClient(&pref_service_
));
207 signin_client_
.get()->SetURLRequestContext(
208 new net::TestURLRequestContextGetter(
209 message_loop_
.message_loop_proxy()));
211 account_tracker_
.reset(new AccountTrackerService());
212 account_tracker_
->Initialize(fake_oauth2_token_service_
.get(),
213 signin_client_
.get());
214 account_tracker_
->EnableNetworkFetches();
215 account_tracker_
->AddObserver(&observer_
);
218 void TearDown() override
{
219 account_tracker_
->RemoveObserver(&observer_
);
220 account_tracker_
->Shutdown();
223 void SimulateTokenAvailable(const std::string
& account_id
) {
224 fake_oauth2_token_service_
->AddAccount(account_id
);
227 void SimulateTokenRevoked(const std::string
& account_id
) {
228 fake_oauth2_token_service_
->RemoveAccount(account_id
);
231 // Helpers to fake access token and user info fetching
232 void IssueAccessToken(const std::string
& account_id
) {
233 fake_oauth2_token_service_
->IssueAllTokensForAccount(
234 account_id
, "access_token-" + account_id
, base::Time::Max());
237 std::string
GenerateValidTokenInfoResponse(const std::string
& account_id
) {
238 return base::StringPrintf(
239 "{\"id\": \"%s\", \"email\": \"%s\", \"hd\": \"\"}",
240 AccountIdToGaiaId(account_id
).c_str(),
241 AccountIdToEmail(account_id
).c_str());
243 void ReturnOAuthUrlFetchSuccess(const std::string
& account_id
);
244 void ReturnOAuthUrlFetchFailure(const std::string
& account_id
);
246 net::TestURLFetcherFactory
* test_fetcher_factory() {
247 return &test_fetcher_factory_
;
249 AccountTrackerService
* account_tracker() { return account_tracker_
.get(); }
250 AccountTrackerObserver
* observer() { return &observer_
; }
251 OAuth2TokenService
* token_service() {
252 return fake_oauth2_token_service_
.get();
254 SigninClient
* signin_client() { return signin_client_
.get(); }
257 void ReturnOAuthUrlFetchResults(int fetcher_id
,
258 net::HttpStatusCode response_code
,
259 const std::string
& response_string
);
261 base::MessageLoopForIO message_loop_
;
262 net::TestURLFetcherFactory test_fetcher_factory_
;
263 scoped_ptr
<FakeOAuth2TokenService
> fake_oauth2_token_service_
;
264 TestingPrefServiceSimple pref_service_
;
265 scoped_ptr
<AccountTrackerService
> account_tracker_
;
266 AccountTrackerObserver observer_
;
267 scoped_ptr
<TestSigninClient
> signin_client_
;
270 void AccountTrackerServiceTest::ReturnOAuthUrlFetchResults(
272 net::HttpStatusCode response_code
,
273 const std::string
& response_string
) {
274 net::TestURLFetcher
* fetcher
=
275 test_fetcher_factory_
.GetFetcherByID(fetcher_id
);
276 ASSERT_TRUE(fetcher
);
277 fetcher
->set_response_code(response_code
);
278 fetcher
->SetResponseString(response_string
);
279 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
282 void AccountTrackerServiceTest::ReturnOAuthUrlFetchSuccess(
283 const std::string
& account_id
) {
284 IssueAccessToken(account_id
);
285 ReturnOAuthUrlFetchResults(gaia::GaiaOAuthClient::kUrlFetcherId
,
287 GenerateValidTokenInfoResponse(account_id
));
290 void AccountTrackerServiceTest::ReturnOAuthUrlFetchFailure(
291 const std::string
& account_id
) {
292 IssueAccessToken(account_id
);
293 ReturnOAuthUrlFetchResults(
294 gaia::GaiaOAuthClient::kUrlFetcherId
, net::HTTP_BAD_REQUEST
, "");
297 TEST_F(AccountTrackerServiceTest
, Basic
) {
300 TEST_F(AccountTrackerServiceTest
, TokenAvailable
) {
301 SimulateTokenAvailable("alpha");
302 ASSERT_FALSE(account_tracker()->IsAllUserInfoFetched());
303 ASSERT_TRUE(observer()->CheckEvents());
306 TEST_F(AccountTrackerServiceTest
, TokenAvailable_Revoked
) {
307 SimulateTokenAvailable("alpha");
308 SimulateTokenRevoked("alpha");
309 ASSERT_TRUE(account_tracker()->IsAllUserInfoFetched());
310 ASSERT_TRUE(observer()->CheckEvents());
313 TEST_F(AccountTrackerServiceTest
, TokenAvailable_UserInfo
) {
314 SimulateTokenAvailable("alpha");
315 ReturnOAuthUrlFetchSuccess("alpha");
316 ASSERT_TRUE(account_tracker()->IsAllUserInfoFetched());
317 ASSERT_TRUE(observer()->CheckEvents(TrackingEvent(UPDATED
, "alpha")));
320 TEST_F(AccountTrackerServiceTest
, TokenAvailable_UserInfo_Revoked
) {
321 SimulateTokenAvailable("alpha");
322 ReturnOAuthUrlFetchSuccess("alpha");
323 ASSERT_TRUE(account_tracker()->IsAllUserInfoFetched());
324 ASSERT_TRUE(observer()->CheckEvents(TrackingEvent(UPDATED
, "alpha")));
325 SimulateTokenRevoked("alpha");
326 ASSERT_TRUE(observer()->CheckEvents(TrackingEvent(REMOVED
, "alpha")));
329 TEST_F(AccountTrackerServiceTest
, TokenAvailable_UserInfoFailed
) {
330 SimulateTokenAvailable("alpha");
331 ReturnOAuthUrlFetchFailure("alpha");
332 ASSERT_TRUE(account_tracker()->IsAllUserInfoFetched());
333 ASSERT_TRUE(observer()->CheckEvents());
336 TEST_F(AccountTrackerServiceTest
, TokenAvailableTwice_UserInfoOnce
) {
337 SimulateTokenAvailable("alpha");
338 ReturnOAuthUrlFetchSuccess("alpha");
339 ASSERT_TRUE(account_tracker()->IsAllUserInfoFetched());
340 ASSERT_TRUE(observer()->CheckEvents(TrackingEvent(UPDATED
, "alpha")));
342 SimulateTokenAvailable("alpha");
343 ASSERT_TRUE(account_tracker()->IsAllUserInfoFetched());
344 ASSERT_TRUE(observer()->CheckEvents());
347 TEST_F(AccountTrackerServiceTest
, TokenAlreadyExists
) {
348 SimulateTokenAvailable("alpha");
349 AccountTrackerService tracker
;
350 AccountTrackerObserver observer
;
351 tracker
.AddObserver(&observer
);
352 tracker
.Initialize(token_service(), signin_client());
353 tracker
.EnableNetworkFetches();
354 ASSERT_FALSE(tracker
.IsAllUserInfoFetched());
355 ASSERT_TRUE(observer
.CheckEvents());
356 tracker
.RemoveObserver(&observer
);
360 TEST_F(AccountTrackerServiceTest
, TwoTokenAvailable_TwoUserInfo
) {
361 SimulateTokenAvailable("alpha");
362 SimulateTokenAvailable("beta");
363 ReturnOAuthUrlFetchSuccess("alpha");
364 ReturnOAuthUrlFetchSuccess("beta");
365 ASSERT_TRUE(account_tracker()->IsAllUserInfoFetched());
366 ASSERT_TRUE(observer()->CheckEvents(TrackingEvent(UPDATED
, "alpha"),
367 TrackingEvent(UPDATED
, "beta")));
370 TEST_F(AccountTrackerServiceTest
, TwoTokenAvailable_OneUserInfo
) {
371 SimulateTokenAvailable("alpha");
372 SimulateTokenAvailable("beta");
373 ReturnOAuthUrlFetchSuccess("beta");
374 ASSERT_FALSE(account_tracker()->IsAllUserInfoFetched());
375 ASSERT_TRUE(observer()->CheckEvents(TrackingEvent(UPDATED
, "beta")));
376 ReturnOAuthUrlFetchSuccess("alpha");
377 ASSERT_TRUE(account_tracker()->IsAllUserInfoFetched());
378 ASSERT_TRUE(observer()->CheckEvents(TrackingEvent(UPDATED
, "alpha")));
381 TEST_F(AccountTrackerServiceTest
, GetAccounts
) {
382 SimulateTokenAvailable("alpha");
383 SimulateTokenAvailable("beta");
384 SimulateTokenAvailable("gamma");
385 ReturnOAuthUrlFetchSuccess("alpha");
386 ReturnOAuthUrlFetchSuccess("beta");
387 ReturnOAuthUrlFetchSuccess("gamma");
389 std::vector
<AccountTrackerService::AccountInfo
> infos
=
390 account_tracker()->GetAccounts();
392 EXPECT_EQ(3u, infos
.size());
393 EXPECT_EQ("alpha", infos
[0].account_id
);
394 EXPECT_EQ(AccountIdToGaiaId("alpha"), infos
[0].gaia
);
395 EXPECT_EQ(AccountIdToEmail("alpha"), infos
[0].email
);
396 EXPECT_EQ(AccountTrackerService::kNoHostedDomainFound
,
397 infos
[0].hosted_domain
);
398 EXPECT_EQ("beta", infos
[1].account_id
);
399 EXPECT_EQ(AccountIdToGaiaId("beta"), infos
[1].gaia
);
400 EXPECT_EQ(AccountIdToEmail("beta"), infos
[1].email
);
401 EXPECT_EQ(AccountTrackerService::kNoHostedDomainFound
,
402 infos
[1].hosted_domain
);
403 EXPECT_EQ("gamma", infos
[2].account_id
);
404 EXPECT_EQ(AccountIdToGaiaId("gamma"), infos
[2].gaia
);
405 EXPECT_EQ(AccountIdToEmail("gamma"), infos
[2].email
);
406 EXPECT_EQ(AccountTrackerService::kNoHostedDomainFound
,
407 infos
[2].hosted_domain
);
410 TEST_F(AccountTrackerServiceTest
, GetAccountInfo_Empty
) {
411 AccountTrackerService::AccountInfo info
=
412 account_tracker()->GetAccountInfo("alpha");
413 ASSERT_EQ("", info
.account_id
);
416 TEST_F(AccountTrackerServiceTest
, GetAccountInfo_TokenAvailable
) {
417 SimulateTokenAvailable("alpha");
418 AccountTrackerService::AccountInfo info
=
419 account_tracker()->GetAccountInfo("alpha");
420 ASSERT_EQ("alpha", info
.account_id
);
421 ASSERT_EQ("", info
.gaia
);
422 ASSERT_EQ("", info
.email
);
425 TEST_F(AccountTrackerServiceTest
, GetAccountInfo_TokenAvailable_UserInfo
) {
426 SimulateTokenAvailable("alpha");
427 ReturnOAuthUrlFetchSuccess("alpha");
428 AccountTrackerService::AccountInfo info
=
429 account_tracker()->GetAccountInfo("alpha");
430 ASSERT_EQ("alpha", info
.account_id
);
431 ASSERT_EQ(AccountIdToGaiaId("alpha"), info
.gaia
);
432 ASSERT_EQ(AccountIdToEmail("alpha"), info
.email
);
433 ASSERT_EQ(AccountTrackerService::kNoHostedDomainFound
, info
.hosted_domain
);
436 TEST_F(AccountTrackerServiceTest
, GetAccountInfo_TokenAvailable_EnableNetwork
) {
437 // Shutdown the network-enabled tracker built into the test case.
440 // Create an account tracker but do not enable network fetches.
441 AccountTrackerService tracker
;
442 tracker
.AddObserver(observer());
443 tracker
.Initialize(token_service(), signin_client());
445 SimulateTokenAvailable("alpha");
446 IssueAccessToken("alpha");
447 // No fetcher has been created yet.
448 net::TestURLFetcher
* fetcher
= test_fetcher_factory()->GetFetcherByID(
449 gaia::GaiaOAuthClient::kUrlFetcherId
);
450 ASSERT_FALSE(fetcher
);
452 // Enable the network to create the fetcher then issue the access token.
453 tracker
.EnableNetworkFetches();
455 // Fetcher was created and executes properly.
456 ReturnOAuthUrlFetchSuccess("alpha");
458 AccountTrackerService::AccountInfo info
=
459 tracker
.GetAccountInfo("alpha");
460 ASSERT_EQ("alpha", info
.account_id
);
461 ASSERT_EQ(AccountIdToGaiaId("alpha"), info
.gaia
);
462 ASSERT_EQ(AccountIdToEmail("alpha"), info
.email
);
463 ASSERT_EQ(AccountTrackerService::kNoHostedDomainFound
, info
.hosted_domain
);
467 TEST_F(AccountTrackerServiceTest
, FindAccountInfoByGaiaId
) {
468 SimulateTokenAvailable("alpha");
469 ReturnOAuthUrlFetchSuccess("alpha");
471 std::string gaia_id
= AccountIdToGaiaId("alpha");
472 AccountTrackerService::AccountInfo info
=
473 account_tracker()->FindAccountInfoByGaiaId(gaia_id
);
474 ASSERT_EQ("alpha", info
.account_id
);
475 ASSERT_EQ(gaia_id
, info
.gaia
);
477 gaia_id
= AccountIdToGaiaId("beta");
478 info
= account_tracker()->FindAccountInfoByGaiaId(gaia_id
);
479 ASSERT_EQ("", info
.account_id
);
482 TEST_F(AccountTrackerServiceTest
, FindAccountInfoByEmail
) {
483 SimulateTokenAvailable("alpha");
484 ReturnOAuthUrlFetchSuccess("alpha");
486 std::string email
= AccountIdToEmail("alpha");
487 AccountTrackerService::AccountInfo info
=
488 account_tracker()->FindAccountInfoByEmail(email
);
489 ASSERT_EQ("alpha", info
.account_id
);
490 ASSERT_EQ(email
, info
.email
);
492 // Should also work with "canonically-equal" email addresses.
493 info
= account_tracker()->FindAccountInfoByEmail("Alpha@Gmail.COM");
494 ASSERT_EQ("alpha", info
.account_id
);
495 ASSERT_EQ(email
, info
.email
);
496 info
= account_tracker()->FindAccountInfoByEmail("al.pha@gmail.com");
497 ASSERT_EQ("alpha", info
.account_id
);
498 ASSERT_EQ(email
, info
.email
);
500 email
= AccountIdToEmail("beta");
501 info
= account_tracker()->FindAccountInfoByEmail(email
);
502 ASSERT_EQ("", info
.account_id
);
505 TEST_F(AccountTrackerServiceTest
, Persistence
) {
506 // Create a tracker and add two accounts. This should cause the accounts
507 // to be saved to persistence.
509 AccountTrackerService tracker
;
510 tracker
.Initialize(token_service(), signin_client());
511 tracker
.EnableNetworkFetches();
512 SimulateTokenAvailable("alpha");
513 ReturnOAuthUrlFetchSuccess("alpha");
514 SimulateTokenAvailable("beta");
515 ReturnOAuthUrlFetchSuccess("beta");
519 // Create a new tracker and make sure it loads the accounts corectly from
522 AccountTrackerService tracker
;
523 tracker
.AddObserver(observer());
524 tracker
.Initialize(token_service(), signin_client());
525 tracker
.EnableNetworkFetches();
526 ASSERT_TRUE(observer()->CheckEvents(TrackingEvent(UPDATED
, "alpha"),
527 TrackingEvent(UPDATED
, "beta")));
529 std::vector
<AccountTrackerService::AccountInfo
> infos
=
530 tracker
.GetAccounts();
531 ASSERT_EQ(2u, infos
.size());
532 EXPECT_EQ(AccountIdToGaiaId("alpha"), infos
[0].gaia
);
533 EXPECT_EQ(AccountIdToEmail("alpha"), infos
[0].email
);
534 EXPECT_EQ("beta", infos
[1].account_id
);
535 EXPECT_EQ(AccountIdToGaiaId("beta"), infos
[1].gaia
);
536 EXPECT_EQ(AccountIdToEmail("beta"), infos
[1].email
);
539 SimulateTokenRevoked("alpha");
540 tracker
.RemoveObserver(observer());
544 // Create a new tracker and make sure it loads the single account from
547 AccountTrackerService tracker
;
548 tracker
.Initialize(token_service(), signin_client());
549 tracker
.EnableNetworkFetches();
551 std::vector
<AccountTrackerService::AccountInfo
> infos
=
552 tracker
.GetAccounts();
553 ASSERT_EQ(1u, infos
.size());
554 EXPECT_EQ("beta", infos
[0].account_id
);
555 EXPECT_EQ(AccountIdToGaiaId("beta"), infos
[0].gaia
);
556 EXPECT_EQ(AccountIdToEmail("beta"), infos
[0].email
);
561 TEST_F(AccountTrackerServiceTest
, SeedAccountInfo
) {
562 std::vector
<AccountTrackerService::AccountInfo
> infos
=
563 account_tracker()->GetAccounts();
564 EXPECT_EQ(0u, infos
.size());
566 const std::string gaia_id
= AccountIdToGaiaId("alpha");
567 const std::string email
= AccountIdToEmail("alpha");
568 const std::string account_id
=
569 account_tracker()->PickAccountIdForAccount(gaia_id
, email
);
570 account_tracker()->SeedAccountInfo(gaia_id
, email
);
572 infos
= account_tracker()->GetAccounts();
573 EXPECT_EQ(1u, infos
.size());
574 EXPECT_EQ(account_id
, infos
[0].account_id
);
575 EXPECT_EQ(gaia_id
, infos
[0].gaia
);
576 EXPECT_EQ(email
, infos
[0].email
);