OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/message_loop/message_loop.h" |
| 6 #include "chrome/browser/chrome_notification_types.h" |
| 7 #include "chrome/browser/chromeos/login/oauth2_login_manager.h" |
| 8 #include "chrome/browser/chromeos/login/oauth2_login_manager_factory.h" |
| 9 #include "chrome/browser/chromeos/login/oobe_base_test.h" |
| 10 #include "chrome/browser/chromeos/login/wizard_controller.h" |
| 11 #include "chrome/browser/profiles/profile_manager.h" |
| 12 #include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h" |
| 13 #include "content/public/browser/notification_service.h" |
| 14 #include "google_apis/gaia/gaia_constants.h" |
| 15 #include "google_apis/gaia/gaia_urls.h" |
| 16 |
| 17 namespace chromeos { |
| 18 |
| 19 namespace { |
| 20 |
| 21 // Email of owner account for test. |
| 22 const char kTestAccountId[] = "username@gmail.com"; |
| 23 |
| 24 const char kTestAuthCode[] = "fake-auth-code"; |
| 25 const char kTestGaiaUberToken[] = "fake-uber-token"; |
| 26 const char kTestAuthLoginAccessToken[] = "fake-access-token"; |
| 27 const char kTestRefreshToken[] = "fake-refresh-token"; |
| 28 const char kTestSessionSIDCookie[] = "fake-session-SID-cookie"; |
| 29 const char kTestSessionLSIDCookie[] = "fake-session-LSID-cookie"; |
| 30 const char kTestUserinfoToken[] = "fake-userinfo-token"; |
| 31 const char kTestLoginToken[] = "fake-login-token"; |
| 32 const char kTestSyncToken[] = "fake-sync-token"; |
| 33 const char kTestAuthLoginToken[] = "fake-oauthlogin-token"; |
| 34 |
| 35 } // namespace |
| 36 |
| 37 class OAuth2Test : public OobeBaseTest { |
| 38 protected: |
| 39 OAuth2Test() {} |
| 40 |
| 41 virtual void SetUpOnMainThread() OVERRIDE { |
| 42 OobeBaseTest::SetUpOnMainThread(); |
| 43 |
| 44 // Configure OAuth authentication. |
| 45 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); |
| 46 |
| 47 fake_gaia_.SetAuthTokens(kTestAuthCode, |
| 48 kTestRefreshToken, |
| 49 kTestAuthLoginAccessToken, |
| 50 kTestGaiaUberToken, |
| 51 kTestSessionSIDCookie, |
| 52 kTestSessionLSIDCookie); |
| 53 // This token satisfies the userinfo.email request from |
| 54 // DeviceOAuth2TokenService used in token validation. |
| 55 FakeGaia::AccessTokenInfo userinfo_token_info; |
| 56 userinfo_token_info.token = kTestUserinfoToken; |
| 57 userinfo_token_info.scopes.insert( |
| 58 "https://www.googleapis.com/auth/userinfo.email"); |
| 59 userinfo_token_info.audience = gaia_urls->oauth2_chrome_client_id(); |
| 60 userinfo_token_info.email = kTestAccountId; |
| 61 fake_gaia_.IssueOAuthToken(kTestRefreshToken, userinfo_token_info); |
| 62 |
| 63 FakeGaia::AccessTokenInfo userinfo_profile_token_info; |
| 64 userinfo_profile_token_info.token = kTestUserinfoToken; |
| 65 userinfo_profile_token_info.scopes.insert( |
| 66 "https://www.googleapis.com/auth/userinfo.profile"); |
| 67 userinfo_profile_token_info.audience = gaia_urls->oauth2_chrome_client_id(); |
| 68 userinfo_profile_token_info.email = kTestAccountId; |
| 69 fake_gaia_.IssueOAuthToken(kTestRefreshToken, userinfo_profile_token_info); |
| 70 |
| 71 // The any-api access token for accessing the token minting endpoint. |
| 72 FakeGaia::AccessTokenInfo login_token_info; |
| 73 login_token_info.token = kTestLoginToken; |
| 74 login_token_info.scopes.insert(GaiaConstants::kAnyApiOAuth2Scope); |
| 75 login_token_info.audience = gaia_urls->oauth2_chrome_client_id(); |
| 76 fake_gaia_.IssueOAuthToken(kTestRefreshToken, login_token_info); |
| 77 |
| 78 // The /auth/chromesync access token for accessing sync endpoint. |
| 79 FakeGaia::AccessTokenInfo sync_token_info; |
| 80 sync_token_info.token = kTestSyncToken; |
| 81 sync_token_info.scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); |
| 82 sync_token_info.audience = gaia_urls->oauth2_chrome_client_id(); |
| 83 fake_gaia_.IssueOAuthToken(kTestRefreshToken, sync_token_info); |
| 84 |
| 85 FakeGaia::AccessTokenInfo auth_login_token_info; |
| 86 auth_login_token_info.token = kTestAuthLoginToken; |
| 87 auth_login_token_info.scopes.insert(gaia_urls->oauth1_login_scope()); |
| 88 auth_login_token_info.audience = gaia_urls->oauth2_chrome_client_id(); |
| 89 fake_gaia_.IssueOAuthToken(kTestRefreshToken, auth_login_token_info); |
| 90 } |
| 91 |
| 92 private: |
| 93 DISALLOW_COPY_AND_ASSIGN(OAuth2Test); |
| 94 }; |
| 95 |
| 96 class OAuth2LoginManagerStateWaiter : public OAuth2LoginManager::Observer { |
| 97 public: |
| 98 explicit OAuth2LoginManagerStateWaiter(Profile* profile) |
| 99 : profile_(profile), |
| 100 waiting_for_state_(false), |
| 101 final_state_(OAuth2LoginManager::SESSION_RESTORE_NOT_STARTED) { |
| 102 } |
| 103 |
| 104 void WaitForStates( |
| 105 const std::set<OAuth2LoginManager::SessionRestoreState>& states) { |
| 106 DCHECK(!waiting_for_state_); |
| 107 OAuth2LoginManager* login_manager = |
| 108 OAuth2LoginManagerFactory::GetInstance()->GetForProfile(profile_); |
| 109 states_ = states; |
| 110 if (states_.find(login_manager->state()) != states_.end()) { |
| 111 final_state_ = login_manager->state(); |
| 112 return; |
| 113 } |
| 114 |
| 115 waiting_for_state_ = true; |
| 116 login_manager->AddObserver(this); |
| 117 runner_ = new content::MessageLoopRunner; |
| 118 runner_->Run(); |
| 119 login_manager->RemoveObserver(this); |
| 120 } |
| 121 |
| 122 OAuth2LoginManager::SessionRestoreState final_state() { return final_state_; } |
| 123 |
| 124 private: |
| 125 // OAuth2LoginManager::Observer overrides. |
| 126 virtual void OnSessionRestoreStateChanged( |
| 127 Profile* user_profile, |
| 128 OAuth2LoginManager::SessionRestoreState state) OVERRIDE { |
| 129 if (!waiting_for_state_) |
| 130 return; |
| 131 |
| 132 if (states_.find(state) == states_.end()) |
| 133 return; |
| 134 |
| 135 final_state_ = state; |
| 136 waiting_for_state_ = false; |
| 137 runner_->Quit(); |
| 138 } |
| 139 |
| 140 Profile* profile_; |
| 141 std::set<OAuth2LoginManager::SessionRestoreState> states_; |
| 142 bool waiting_for_state_; |
| 143 OAuth2LoginManager::SessionRestoreState final_state_; |
| 144 scoped_refptr<content::MessageLoopRunner> runner_; |
| 145 |
| 146 DISALLOW_COPY_AND_ASSIGN(OAuth2LoginManagerStateWaiter); |
| 147 }; |
| 148 |
| 149 IN_PROC_BROWSER_TEST_F(OAuth2Test, NewUser) { |
| 150 SimulateNetworkOnline(); |
| 151 chromeos::WizardController::SkipPostLoginScreensForTesting(); |
| 152 chromeos::WizardController* wizard_controller = |
| 153 chromeos::WizardController::default_controller(); |
| 154 wizard_controller->SkipToLoginForTesting(LoginScreenContext()); |
| 155 |
| 156 content::WindowedNotificationObserver( |
| 157 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, |
| 158 content::NotificationService::AllSources()).Wait(); |
| 159 |
| 160 // Use capitalized and dotted user name on purpose to make sure |
| 161 // our email normalization kicks in. |
| 162 GetLoginDisplay()->ShowSigninScreenForCreds("User.Name", "password"); |
| 163 |
| 164 content::WindowedNotificationObserver( |
| 165 chrome::NOTIFICATION_SESSION_STARTED, |
| 166 content::NotificationService::AllSources()).Wait(); |
| 167 |
| 168 std::set<OAuth2LoginManager::SessionRestoreState> states; |
| 169 states.insert(OAuth2LoginManager::SESSION_RESTORE_DONE); |
| 170 states.insert(OAuth2LoginManager::SESSION_RESTORE_FAILED); |
| 171 states.insert(OAuth2LoginManager::SESSION_RESTORE_CONNECTION_FAILED); |
| 172 OAuth2LoginManagerStateWaiter merge_session_waiter( |
| 173 ProfileManager::GetPrimaryUserProfile()); |
| 174 merge_session_waiter.WaitForStates(states); |
| 175 EXPECT_EQ(merge_session_waiter.final_state(), |
| 176 OAuth2LoginManager::SESSION_RESTORE_DONE); |
| 177 } |
| 178 |
| 179 } // namespace chromeos |
OLD | NEW |