| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
| 6 #include "base/prefs/pref_service.h" | 6 #include "base/prefs/pref_service.h" |
| 7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 params.auth_code = kTestAuthCode; | 131 params.auth_code = kTestAuthCode; |
| 132 params.refresh_token = kTestRefreshToken; | 132 params.refresh_token = kTestRefreshToken; |
| 133 params.access_token = kTestAuthLoginAccessToken; | 133 params.access_token = kTestAuthLoginAccessToken; |
| 134 params.gaia_uber_token = kTestGaiaUberToken; | 134 params.gaia_uber_token = kTestGaiaUberToken; |
| 135 params.session_sid_cookie = kTestSessionSIDCookie; | 135 params.session_sid_cookie = kTestSessionSIDCookie; |
| 136 params.session_lsid_cookie = kTestSessionLSIDCookie; | 136 params.session_lsid_cookie = kTestSessionLSIDCookie; |
| 137 fake_gaia_->SetMergeSessionParams(params); | 137 fake_gaia_->SetMergeSessionParams(params); |
| 138 SetupGaiaServerWithAccessTokens(); | 138 SetupGaiaServerWithAccessTokens(); |
| 139 } | 139 } |
| 140 | 140 |
| 141 void SetupGaiaServerForExistingAccount() { | 141 void SetupGaiaServerForUnexpiredAccount() { |
| 142 FakeGaia::MergeSessionParams params; |
| 143 params.email = kTestAccountId; |
| 144 fake_gaia_->SetMergeSessionParams(params); |
| 145 SetupGaiaServerWithAccessTokens(); |
| 146 } |
| 147 |
| 148 void SetupGaiaServerForExpiredAccount() { |
| 142 FakeGaia::MergeSessionParams params; | 149 FakeGaia::MergeSessionParams params; |
| 143 params.gaia_uber_token = kTestGaiaUberToken; | 150 params.gaia_uber_token = kTestGaiaUberToken; |
| 144 params.session_sid_cookie = kTestSession2SIDCookie; | 151 params.session_sid_cookie = kTestSession2SIDCookie; |
| 145 params.session_lsid_cookie = kTestSession2LSIDCookie; | 152 params.session_lsid_cookie = kTestSession2LSIDCookie; |
| 146 fake_gaia_->SetMergeSessionParams(params); | 153 fake_gaia_->SetMergeSessionParams(params); |
| 147 SetupGaiaServerWithAccessTokens(); | 154 SetupGaiaServerWithAccessTokens(); |
| 148 } | 155 } |
| 149 | 156 |
| 157 void LoginAsExistingUser() { |
| 158 content::WindowedNotificationObserver( |
| 159 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, |
| 160 content::NotificationService::AllSources()).Wait(); |
| 161 |
| 162 JsExpect("!!document.querySelector('#account-picker')"); |
| 163 JsExpect("!!document.querySelector('#pod-row')"); |
| 164 |
| 165 EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId), |
| 166 User::OAUTH2_TOKEN_STATUS_VALID); |
| 167 |
| 168 EXPECT_TRUE(TryToLogin(kTestAccountId, kTestAccountPassword)); |
| 169 Profile* profile = ProfileManager::GetPrimaryUserProfile(); |
| 170 |
| 171 // Wait for the session merge to finish. |
| 172 std::set<OAuth2LoginManager::SessionRestoreState> states; |
| 173 states.insert(OAuth2LoginManager::SESSION_RESTORE_DONE); |
| 174 states.insert(OAuth2LoginManager::SESSION_RESTORE_FAILED); |
| 175 states.insert(OAuth2LoginManager::SESSION_RESTORE_CONNECTION_FAILED); |
| 176 OAuth2LoginManagerStateWaiter merge_session_waiter(profile); |
| 177 merge_session_waiter.WaitForStates(states); |
| 178 EXPECT_EQ(merge_session_waiter.final_state(), |
| 179 OAuth2LoginManager::SESSION_RESTORE_DONE); |
| 180 |
| 181 // Check for existance of refresh token. |
| 182 ProfileOAuth2TokenService* token_service = |
| 183 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
| 184 EXPECT_TRUE(token_service->RefreshTokenIsAvailable(kTestAccountId)); |
| 185 |
| 186 EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId), |
| 187 User::OAUTH2_TOKEN_STATUS_VALID); |
| 188 } |
| 189 |
| 150 bool TryToLogin(const std::string& username, | 190 bool TryToLogin(const std::string& username, |
| 151 const std::string& password) { | 191 const std::string& password) { |
| 152 if (!AddUserTosession(username, password)) | 192 if (!AddUserToSession(username, password)) |
| 153 return false; | 193 return false; |
| 154 | 194 |
| 155 if (const User* active_user = UserManager::Get()->GetActiveUser()) | 195 if (const User* active_user = UserManager::Get()->GetActiveUser()) |
| 156 return active_user->email() == username; | 196 return active_user->email() == username; |
| 157 | 197 |
| 158 return false; | 198 return false; |
| 159 } | 199 } |
| 160 | 200 |
| 161 User::OAuthTokenStatus GetOAuthStatusFromLocalState( | 201 User::OAuthTokenStatus GetOAuthStatusFromLocalState( |
| 162 const std::string& user_id) const { | 202 const std::string& user_id) const { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 176 | 216 |
| 177 protected: | 217 protected: |
| 178 // OobeBaseTest overrides. | 218 // OobeBaseTest overrides. |
| 179 virtual Profile* profile() OVERRIDE { | 219 virtual Profile* profile() OVERRIDE { |
| 180 if (UserManager::Get()->GetActiveUser()) | 220 if (UserManager::Get()->GetActiveUser()) |
| 181 return ProfileManager::GetPrimaryUserProfile(); | 221 return ProfileManager::GetPrimaryUserProfile(); |
| 182 | 222 |
| 183 return OobeBaseTest::profile(); | 223 return OobeBaseTest::profile(); |
| 184 } | 224 } |
| 185 | 225 |
| 186 bool AddUserTosession(const std::string& username, | 226 bool AddUserToSession(const std::string& username, |
| 187 const std::string& password) { | 227 const std::string& password) { |
| 188 ExistingUserController* controller = | 228 ExistingUserController* controller = |
| 189 ExistingUserController::current_controller(); | 229 ExistingUserController::current_controller(); |
| 190 if (!controller) { | 230 if (!controller) { |
| 191 ADD_FAILURE(); | 231 ADD_FAILURE(); |
| 192 return false; | 232 return false; |
| 193 } | 233 } |
| 194 | 234 |
| 195 controller->Login(UserContext(username, password, std::string())); | 235 controller->Login(UserContext(username, password, std::string())); |
| 196 content::WindowedNotificationObserver( | 236 content::WindowedNotificationObserver( |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 } | 385 } |
| 346 | 386 |
| 347 scoped_refptr<net::URLRequestContextGetter> context_; | 387 scoped_refptr<net::URLRequestContextGetter> context_; |
| 348 net::CookieList cookie_list_; | 388 net::CookieList cookie_list_; |
| 349 scoped_refptr<content::MessageLoopRunner> runner_; | 389 scoped_refptr<content::MessageLoopRunner> runner_; |
| 350 | 390 |
| 351 DISALLOW_COPY_AND_ASSIGN(CookieReader); | 391 DISALLOW_COPY_AND_ASSIGN(CookieReader); |
| 352 }; | 392 }; |
| 353 | 393 |
| 354 // PRE_MergeSession is testing merge session for a new profile. | 394 // PRE_MergeSession is testing merge session for a new profile. |
| 355 IN_PROC_BROWSER_TEST_F(OAuth2Test, PRE_PRE_MergeSession) { | 395 IN_PROC_BROWSER_TEST_F(OAuth2Test, PRE_PRE_PRE_MergeSession) { |
| 356 StartNewUserSession(); | 396 StartNewUserSession(); |
| 357 | |
| 358 // Wait for the session merge to finish. | |
| 359 WaitForMergeSessionCompletion(OAuth2LoginManager::SESSION_RESTORE_DONE); | |
| 360 | |
| 361 // Check for existance of refresh token. | 397 // Check for existance of refresh token. |
| 362 ProfileOAuth2TokenService* token_service = | 398 ProfileOAuth2TokenService* token_service = |
| 363 ProfileOAuth2TokenServiceFactory::GetForProfile( | 399 ProfileOAuth2TokenServiceFactory::GetForProfile( |
| 364 profile()); | 400 profile()); |
| 365 EXPECT_TRUE(token_service->RefreshTokenIsAvailable(kTestAccountId)); | 401 EXPECT_TRUE(token_service->RefreshTokenIsAvailable(kTestAccountId)); |
| 366 | 402 |
| 367 EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId), | 403 EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId), |
| 368 User::OAUTH2_TOKEN_STATUS_VALID); | 404 User::OAUTH2_TOKEN_STATUS_VALID); |
| 369 | 405 |
| 370 scoped_refptr<CookieReader> cookie_reader(new CookieReader()); | 406 scoped_refptr<CookieReader> cookie_reader(new CookieReader()); |
| 371 cookie_reader->ReadCookies(profile()); | 407 cookie_reader->ReadCookies(profile()); |
| 372 EXPECT_EQ(cookie_reader->GetCookieValue("SID"), kTestSessionSIDCookie); | 408 EXPECT_EQ(cookie_reader->GetCookieValue("SID"), kTestSessionSIDCookie); |
| 373 EXPECT_EQ(cookie_reader->GetCookieValue("LSID"), kTestSessionLSIDCookie); | 409 EXPECT_EQ(cookie_reader->GetCookieValue("LSID"), kTestSessionLSIDCookie); |
| 374 } | 410 } |
| 375 | 411 |
| 376 // MergeSession test is running merge session process for an existing profile | 412 // MergeSession test is running merge session process for an existing profile |
| 413 // that was generated in PRE_PRE_PRE_MergeSession test. In this test, we |
| 414 // are not running /MergeSession process since the /ListAccounts call confirms |
| 415 // that the session is not stale. |
| 416 IN_PROC_BROWSER_TEST_F(OAuth2Test, PRE_PRE_MergeSession) { |
| 417 SetupGaiaServerForUnexpiredAccount(); |
| 418 SimulateNetworkOnline(); |
| 419 LoginAsExistingUser(); |
| 420 scoped_refptr<CookieReader> cookie_reader(new CookieReader()); |
| 421 cookie_reader->ReadCookies(profile()); |
| 422 // These are still cookie values form the initial session since |
| 423 // /ListAccounts |
| 424 EXPECT_EQ(cookie_reader->GetCookieValue("SID"), kTestSessionSIDCookie); |
| 425 EXPECT_EQ(cookie_reader->GetCookieValue("LSID"), kTestSessionLSIDCookie); |
| 426 } |
| 427 |
| 428 // MergeSession test is running merge session process for an existing profile |
| 377 // that was generated in PRE_PRE_MergeSession test. | 429 // that was generated in PRE_PRE_MergeSession test. |
| 378 IN_PROC_BROWSER_TEST_F(OAuth2Test, PRE_MergeSession) { | 430 IN_PROC_BROWSER_TEST_F(OAuth2Test, PRE_MergeSession) { |
| 379 SetupGaiaServerForExistingAccount(); | 431 SetupGaiaServerForExpiredAccount(); |
| 380 SimulateNetworkOnline(); | 432 SimulateNetworkOnline(); |
| 381 | 433 LoginAsExistingUser(); |
| 382 content::WindowedNotificationObserver( | |
| 383 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, | |
| 384 content::NotificationService::AllSources()).Wait(); | |
| 385 | |
| 386 JsExpect("!!document.querySelector('#account-picker')"); | |
| 387 JsExpect("!!document.querySelector('#pod-row')"); | |
| 388 | |
| 389 EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId), | |
| 390 User::OAUTH2_TOKEN_STATUS_VALID); | |
| 391 | |
| 392 EXPECT_TRUE(TryToLogin(kTestAccountId, kTestAccountPassword)); | |
| 393 | |
| 394 // Wait for the session merge to finish. | |
| 395 WaitForMergeSessionCompletion(OAuth2LoginManager::SESSION_RESTORE_DONE); | |
| 396 | |
| 397 // Check for existance of refresh token. | |
| 398 ProfileOAuth2TokenService* token_service = | |
| 399 ProfileOAuth2TokenServiceFactory::GetForProfile(profile()); | |
| 400 EXPECT_TRUE(token_service->RefreshTokenIsAvailable(kTestAccountId)); | |
| 401 | |
| 402 EXPECT_EQ(GetOAuthStatusFromLocalState(kTestAccountId), | |
| 403 User::OAUTH2_TOKEN_STATUS_VALID); | |
| 404 | |
| 405 scoped_refptr<CookieReader> cookie_reader(new CookieReader()); | 434 scoped_refptr<CookieReader> cookie_reader(new CookieReader()); |
| 406 cookie_reader->ReadCookies(profile()); | 435 cookie_reader->ReadCookies(profile()); |
| 436 // These should be cookie values that we generated by calling /MergeSession, |
| 437 // since /ListAccounts should have tell us that the initial session cookies |
| 438 // are stale. |
| 407 EXPECT_EQ(cookie_reader->GetCookieValue("SID"), kTestSession2SIDCookie); | 439 EXPECT_EQ(cookie_reader->GetCookieValue("SID"), kTestSession2SIDCookie); |
| 408 EXPECT_EQ(cookie_reader->GetCookieValue("LSID"), kTestSession2LSIDCookie); | 440 EXPECT_EQ(cookie_reader->GetCookieValue("LSID"), kTestSession2LSIDCookie); |
| 409 } | 441 } |
| 410 | 442 |
| 411 // MergeSession test is attempting to merge session for an existing profile | 443 // MergeSession test is attempting to merge session for an existing profile |
| 412 // that was generated in PRE_PRE_MergeSession test. This attempt should fail | 444 // that was generated in PRE_PRE_MergeSession test. This attempt should fail |
| 413 // since FakeGaia instance isn't configured to return relevant tokens/cookies. | 445 // since FakeGaia instance isn't configured to return relevant tokens/cookies. |
| 414 IN_PROC_BROWSER_TEST_F(OAuth2Test, MergeSession) { | 446 IN_PROC_BROWSER_TEST_F(OAuth2Test, MergeSession) { |
| 415 SimulateNetworkOnline(); | 447 SimulateNetworkOnline(); |
| 416 | 448 |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 738 | 770 |
| 739 if (!catcher.GetNextResult()) { | 771 if (!catcher.GetNextResult()) { |
| 740 std::string message = catcher.message(); | 772 std::string message = catcher.message(); |
| 741 ADD_FAILURE() << "Tests failed: " << message; | 773 ADD_FAILURE() << "Tests failed: " << message; |
| 742 } | 774 } |
| 743 | 775 |
| 744 EXPECT_TRUE(fake_google_.IsPageRequested()); | 776 EXPECT_TRUE(fake_google_.IsPageRequested()); |
| 745 } | 777 } |
| 746 | 778 |
| 747 } // namespace chromeos | 779 } // namespace chromeos |
| OLD | NEW |