OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/memory/ref_counted.h" | 5 #include "base/memory/ref_counted.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
12 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
13 #include "chrome/browser/policy/browser_policy_connector.h" | 13 #include "chrome/browser/policy/browser_policy_connector.h" |
14 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" | 14 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" |
15 #include "chrome/browser/policy/cloud/mock_device_management_service.h" | 15 #include "chrome/browser/policy/cloud/mock_device_management_service.h" |
16 #include "chrome/browser/policy/cloud/mock_user_cloud_policy_store.h" | 16 #include "chrome/browser/policy/cloud/mock_user_cloud_policy_store.h" |
17 #include "chrome/browser/policy/cloud/user_cloud_policy_manager.h" | 17 #include "chrome/browser/policy/cloud/user_cloud_policy_manager.h" |
18 #include "chrome/browser/policy/cloud/user_policy_signin_service_factory.h" | 18 #include "chrome/browser/policy/cloud/user_policy_signin_service_factory.h" |
19 #include "chrome/browser/prefs/browser_prefs.h" | 19 #include "chrome/browser/prefs/browser_prefs.h" |
20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/signin/fake_signin_manager.h" | 21 #include "chrome/browser/signin/fake_signin_manager.h" |
| 22 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
22 #include "chrome/browser/signin/signin_manager.h" | 23 #include "chrome/browser/signin/signin_manager.h" |
23 #include "chrome/browser/signin/signin_manager_factory.h" | 24 #include "chrome/browser/signin/signin_manager_factory.h" |
24 #include "chrome/test/base/testing_browser_process.h" | 25 #include "chrome/test/base/testing_browser_process.h" |
25 #include "chrome/test/base/testing_pref_service_syncable.h" | 26 #include "chrome/test/base/testing_pref_service_syncable.h" |
26 #include "chrome/test/base/testing_profile.h" | 27 #include "chrome/test/base/testing_profile.h" |
27 #include "content/public/browser/browser_context.h" | 28 #include "content/public/browser/browser_context.h" |
28 #include "content/public/browser/notification_details.h" | 29 #include "content/public/browser/notification_details.h" |
29 #include "content/public/browser/notification_service.h" | 30 #include "content/public/browser/notification_service.h" |
30 #include "content/public/browser/notification_source.h" | 31 #include "content/public/browser/notification_source.h" |
31 #include "content/public/test/test_browser_thread_bundle.h" | 32 #include "content/public/test/test_browser_thread_bundle.h" |
32 #include "google_apis/gaia/gaia_constants.h" | 33 #include "google_apis/gaia/gaia_constants.h" |
33 #include "google_apis/gaia/google_service_auth_error.h" | 34 #include "google_apis/gaia/google_service_auth_error.h" |
34 #include "net/http/http_status_code.h" | 35 #include "net/http/http_status_code.h" |
35 #include "net/url_request/test_url_fetcher_factory.h" | 36 #include "net/url_request/test_url_fetcher_factory.h" |
36 #include "net/url_request/url_request_context_getter.h" | 37 #include "net/url_request/url_request_context_getter.h" |
37 #include "net/url_request/url_request_status.h" | 38 #include "net/url_request/url_request_status.h" |
38 #include "testing/gmock/include/gmock/gmock.h" | 39 #include "testing/gmock/include/gmock/gmock.h" |
39 #include "testing/gtest/include/gtest/gtest.h" | 40 #include "testing/gtest/include/gtest/gtest.h" |
40 | 41 |
41 #if defined(OS_ANDROID) | 42 #if defined(OS_ANDROID) |
42 #include "chrome/browser/policy/cloud/user_policy_signin_service_android.h" | 43 #include "chrome/browser/policy/cloud/user_policy_signin_service_android.h" |
43 #include "chrome/browser/signin/android_profile_oauth2_token_service.h" | 44 #include "chrome/browser/signin/android_profile_oauth2_token_service.h" |
44 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
45 #else | 45 #else |
46 #include "chrome/browser/policy/cloud/user_policy_signin_service.h" | 46 #include "chrome/browser/policy/cloud/user_policy_signin_service.h" |
47 #include "chrome/browser/signin/token_service.h" | 47 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" |
48 #include "chrome/browser/signin/token_service_factory.h" | |
49 #endif | 48 #endif |
50 | 49 |
51 namespace em = enterprise_management; | 50 namespace em = enterprise_management; |
52 | 51 |
53 using testing::AnyNumber; | 52 using testing::AnyNumber; |
54 using testing::Mock; | 53 using testing::Mock; |
55 using testing::_; | 54 using testing::_; |
56 | 55 |
57 namespace policy { | 56 namespace policy { |
58 | 57 |
59 namespace { | 58 namespace { |
60 | 59 |
61 const char kTestUser[] = "testuser@test.com"; | 60 const char kTestUser[] = "testuser@test.com"; |
62 | 61 |
63 const char kValidTokenResponse[] = | 62 const char kValidTokenResponse[] = |
64 "{" | 63 "{" |
65 " \"access_token\": \"at1\"," | 64 " \"access_token\": \"at1\"," |
66 " \"expires_in\": 3600," | 65 " \"expires_in\": 3600," |
67 " \"token_type\": \"Bearer\"" | 66 " \"token_type\": \"Bearer\"" |
68 "}"; | 67 "}"; |
69 | 68 |
70 const char kHostedDomainResponse[] = | 69 const char kHostedDomainResponse[] = |
71 "{" | 70 "{" |
72 " \"hd\": \"test.com\"" | 71 " \"hd\": \"test.com\"" |
73 "}"; | 72 "}"; |
74 | 73 |
75 const char kCombinedScopes[] = | |
76 "https://www.googleapis.com/auth/chromeosdevicemanagement " | |
77 "https://www.googleapis.com/auth/userinfo.email"; | |
78 | |
79 class SigninManagerFake : public FakeSigninManager { | 74 class SigninManagerFake : public FakeSigninManager { |
80 public: | 75 public: |
81 explicit SigninManagerFake(Profile* profile) | 76 explicit SigninManagerFake(Profile* profile) |
82 : FakeSigninManager(profile) { | 77 : FakeSigninManager(profile) { |
83 Initialize(profile, NULL); | 78 Initialize(profile, NULL); |
84 } | 79 } |
85 | 80 |
86 void ForceSignOut() { | 81 void ForceSignOut() { |
87 // Allow signing out now. | 82 // Allow signing out now. |
88 prohibit_signout_ = false; | 83 prohibit_signout_ = false; |
89 SignOut(); | 84 SignOut(); |
90 } | 85 } |
91 | 86 |
92 static BrowserContextKeyedService* Build(content::BrowserContext* profile) { | 87 static BrowserContextKeyedService* Build(content::BrowserContext* profile) { |
93 return new SigninManagerFake(static_cast<Profile*>(profile)); | 88 return new SigninManagerFake(static_cast<Profile*>(profile)); |
94 } | 89 } |
95 }; | 90 }; |
96 | 91 |
97 #if defined(OS_ANDROID) | 92 #if defined(OS_ANDROID) |
98 | 93 // TODO(atwilson): Remove this when ProfileOAuth2TokenService supports |
99 class FakeProfileOAuth2TokenService : public AndroidProfileOAuth2TokenService { | 94 // usernames. |
| 95 class FakeAndroidProfileOAuth2TokenService |
| 96 : public AndroidProfileOAuth2TokenService { |
100 public: | 97 public: |
101 explicit FakeProfileOAuth2TokenService(Profile* profile) { | 98 explicit FakeAndroidProfileOAuth2TokenService(Profile* profile) { |
102 Initialize(profile); | 99 Initialize(profile); |
103 } | 100 } |
104 | 101 |
105 static BrowserContextKeyedService* Build(content::BrowserContext* profile) { | 102 static BrowserContextKeyedService* Build(content::BrowserContext* profile) { |
106 return new FakeProfileOAuth2TokenService(static_cast<Profile*>(profile)); | 103 return new FakeAndroidProfileOAuth2TokenService( |
| 104 static_cast<Profile*>(profile)); |
107 } | 105 } |
108 | 106 |
109 // AndroidProfileOAuth2TokenService overrides: | 107 // AndroidProfileOAuth2TokenService overrides: |
110 virtual void FetchOAuth2Token( | 108 virtual void FetchOAuth2TokenWithUsername( |
| 109 RequestImpl* request, |
111 const std::string& username, | 110 const std::string& username, |
112 const std::string& scope, | 111 const OAuth2TokenService::ScopeSet& scope) OVERRIDE { |
113 const FetchOAuth2TokenCallback& callback) OVERRIDE { | |
114 ASSERT_TRUE(!HasPendingRequest()); | 112 ASSERT_TRUE(!HasPendingRequest()); |
115 ASSERT_EQ(kTestUser, username); | 113 ASSERT_EQ(kTestUser, username); |
116 ASSERT_EQ(kCombinedScopes, scope); | 114 ASSERT_EQ(2U, scope.size()); |
117 pending_callback_ = callback; | 115 EXPECT_EQ(1U, scope.count(GaiaConstants::kDeviceManagementServiceOAuth)); |
| 116 EXPECT_EQ(1U, scope.count( |
| 117 "https://www.googleapis.com/auth/userinfo.email")); |
| 118 pending_request_ = request->AsWeakPtr(); |
118 } | 119 } |
119 | 120 |
120 void IssueToken(const std::string& token) { | 121 void IssueToken(const std::string& token) { |
121 ASSERT_TRUE(HasPendingRequest()); | 122 ASSERT_TRUE(HasPendingRequest()); |
122 GoogleServiceAuthError error = GoogleServiceAuthError::AuthErrorNone(); | 123 GoogleServiceAuthError error = GoogleServiceAuthError::AuthErrorNone(); |
123 if (token.empty()) | 124 if (token.empty()) |
124 error = GoogleServiceAuthError::FromServiceError("fail"); | 125 error = GoogleServiceAuthError::FromServiceError("fail"); |
125 pending_callback_.Run( | 126 if (pending_request_) { |
126 error, token, base::Time::Now() + base::TimeDelta::FromDays(1)); | 127 pending_request_->InformConsumer( |
127 pending_callback_.Reset(); | 128 error, |
| 129 token, |
| 130 base::Time::Now() + base::TimeDelta::FromDays(1)); |
| 131 } |
| 132 pending_request_.reset(); |
128 } | 133 } |
129 | 134 |
130 bool HasPendingRequest() const { | 135 bool HasPendingRequest() const { |
131 return !pending_callback_.is_null(); | 136 return pending_request_; |
132 } | 137 } |
133 | 138 |
134 private: | 139 private: |
135 FetchOAuth2TokenCallback pending_callback_; | 140 base::WeakPtr<RequestImpl> pending_request_; |
136 }; | 141 }; |
137 | 142 |
138 #endif | 143 #endif |
139 | 144 |
140 class UserPolicySigninServiceTest : public testing::Test { | 145 class UserPolicySigninServiceTest : public testing::Test { |
141 public: | 146 public: |
142 UserPolicySigninServiceTest() | 147 UserPolicySigninServiceTest() |
143 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 148 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
144 register_completed_(false) {} | 149 register_completed_(false) {} |
145 | 150 |
(...skipping 27 matching lines...) Expand all Loading... |
173 g_browser_process->browser_policy_connector()->Init( | 178 g_browser_process->browser_policy_connector()->Init( |
174 local_state_.get(), system_request_context); | 179 local_state_.get(), system_request_context); |
175 | 180 |
176 // Create a testing profile with cloud-policy-on-signin enabled, and bring | 181 // Create a testing profile with cloud-policy-on-signin enabled, and bring |
177 // up a UserCloudPolicyManager with a MockUserCloudPolicyStore. | 182 // up a UserCloudPolicyManager with a MockUserCloudPolicyStore. |
178 scoped_ptr<TestingPrefServiceSyncable> prefs( | 183 scoped_ptr<TestingPrefServiceSyncable> prefs( |
179 new TestingPrefServiceSyncable()); | 184 new TestingPrefServiceSyncable()); |
180 chrome::RegisterUserProfilePrefs(prefs->registry()); | 185 chrome::RegisterUserProfilePrefs(prefs->registry()); |
181 TestingProfile::Builder builder; | 186 TestingProfile::Builder builder; |
182 builder.SetPrefService(scoped_ptr<PrefServiceSyncable>(prefs.Pass())); | 187 builder.SetPrefService(scoped_ptr<PrefServiceSyncable>(prefs.Pass())); |
| 188 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), |
| 189 SigninManagerFake::Build); |
| 190 #if defined(OS_ANDROID) |
| 191 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), |
| 192 FakeAndroidProfileOAuth2TokenService::Build); |
| 193 #else |
| 194 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), |
| 195 FakeProfileOAuth2TokenService::Build); |
| 196 #endif |
| 197 |
183 profile_ = builder.Build().Pass(); | 198 profile_ = builder.Build().Pass(); |
| 199 signin_manager_ = static_cast<SigninManagerFake*>( |
| 200 SigninManagerFactory::GetForProfile(profile_.get())); |
184 | 201 |
185 mock_store_ = new MockUserCloudPolicyStore(); | 202 mock_store_ = new MockUserCloudPolicyStore(); |
186 EXPECT_CALL(*mock_store_, Load()).Times(AnyNumber()); | 203 EXPECT_CALL(*mock_store_, Load()).Times(AnyNumber()); |
187 manager_.reset(new UserCloudPolicyManager( | 204 manager_.reset(new UserCloudPolicyManager( |
188 profile_.get(), scoped_ptr<UserCloudPolicyStore>(mock_store_))); | 205 profile_.get(), scoped_ptr<UserCloudPolicyStore>(mock_store_))); |
189 signin_manager_ = static_cast<SigninManagerFake*>( | |
190 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( | |
191 profile_.get(), SigninManagerFake::Build)); | |
192 | 206 |
193 #if defined(OS_ANDROID) | |
194 ProfileOAuth2TokenServiceFactory* factory = | |
195 ProfileOAuth2TokenServiceFactory::GetInstance(); | |
196 token_service_ = static_cast<FakeProfileOAuth2TokenService*>( | |
197 factory->SetTestingFactoryAndUse(profile_.get(), | |
198 FakeProfileOAuth2TokenService::Build)); | |
199 #endif | |
200 | |
201 // Make sure the UserPolicySigninService is created. | |
202 UserPolicySigninServiceFactory::GetForProfile(profile_.get()); | |
203 Mock::VerifyAndClearExpectations(mock_store_); | 207 Mock::VerifyAndClearExpectations(mock_store_); |
204 url_factory_.set_remove_fetcher_on_delete(true); | 208 url_factory_.set_remove_fetcher_on_delete(true); |
205 } | 209 } |
206 | 210 |
207 virtual void TearDown() OVERRIDE { | 211 virtual void TearDown() OVERRIDE { |
208 UserPolicySigninServiceFactory::SetDeviceManagementServiceForTesting(NULL); | 212 UserPolicySigninServiceFactory::SetDeviceManagementServiceForTesting(NULL); |
209 // Free the profile before we clear out the browser prefs. | 213 // Free the profile before we clear out the browser prefs. |
210 profile_.reset(); | 214 profile_.reset(); |
211 TestingBrowserProcess* testing_browser_process = | 215 TestingBrowserProcess* testing_browser_process = |
212 TestingBrowserProcess::GetGlobal(); | 216 TestingBrowserProcess::GetGlobal(); |
213 testing_browser_process->SetLocalState(NULL); | 217 testing_browser_process->SetLocalState(NULL); |
214 local_state_.reset(); | 218 local_state_.reset(); |
215 testing_browser_process->SetBrowserPolicyConnector(NULL); | 219 testing_browser_process->SetBrowserPolicyConnector(NULL); |
216 base::RunLoop run_loop; | 220 base::RunLoop run_loop; |
217 run_loop.RunUntilIdle(); | 221 run_loop.RunUntilIdle(); |
218 } | 222 } |
219 | 223 |
| 224 #if defined(OS_ANDROID) |
| 225 FakeAndroidProfileOAuth2TokenService* GetTokenService() { |
| 226 ProfileOAuth2TokenService* service = |
| 227 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()); |
| 228 return static_cast<FakeAndroidProfileOAuth2TokenService*>(service); |
| 229 } |
| 230 #else |
| 231 FakeProfileOAuth2TokenService* GetTokenService() { |
| 232 ProfileOAuth2TokenService* service = |
| 233 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()); |
| 234 return static_cast<FakeProfileOAuth2TokenService*>(service); |
| 235 } |
| 236 #endif |
| 237 |
220 bool IsRequestActive() { | 238 bool IsRequestActive() { |
221 #if defined(OS_ANDROID) | 239 #if defined(OS_ANDROID) |
222 if (token_service_->HasPendingRequest()) | 240 if (GetTokenService()->HasPendingRequest()) |
| 241 return true; |
| 242 #else |
| 243 if (!GetTokenService()->GetPendingRequests().empty()) |
223 return true; | 244 return true; |
224 #endif | 245 #endif |
225 return url_factory_.GetFetcherByID(0); | 246 return url_factory_.GetFetcherByID(0); |
226 } | 247 } |
227 | 248 |
228 void MakeOAuthTokenFetchSucceed() { | 249 void MakeOAuthTokenFetchSucceed() { |
229 #if defined(OS_ANDROID) | 250 #if defined(OS_ANDROID) |
230 ASSERT_TRUE(token_service_->HasPendingRequest()); | 251 ASSERT_TRUE(GetTokenService()->HasPendingRequest()); |
231 token_service_->IssueToken("fake_token"); | 252 GetTokenService()->IssueToken("fake_token"); |
232 #else | 253 #else |
233 ASSERT_TRUE(IsRequestActive()); | 254 ASSERT_TRUE(IsRequestActive()); |
234 net::TestURLFetcher* fetcher = url_factory_.GetFetcherByID(0); | 255 net::TestURLFetcher* fetcher = url_factory_.GetFetcherByID(0); |
235 fetcher->set_response_code(net::HTTP_OK); | 256 fetcher->set_response_code(net::HTTP_OK); |
236 fetcher->SetResponseString(kValidTokenResponse); | 257 fetcher->SetResponseString(kValidTokenResponse); |
237 fetcher->delegate()->OnURLFetchComplete(fetcher); | 258 fetcher->delegate()->OnURLFetchComplete(fetcher); |
238 #endif | 259 #endif |
239 } | 260 } |
240 | 261 |
241 void ReportHostedDomainStatus(bool is_hosted_domain) { | 262 void ReportHostedDomainStatus(bool is_hosted_domain) { |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 MockUserCloudPolicyStore* mock_store_; | 354 MockUserCloudPolicyStore* mock_store_; |
334 scoped_ptr<UserCloudPolicyManager> manager_; | 355 scoped_ptr<UserCloudPolicyManager> manager_; |
335 | 356 |
336 // BrowserPolicyConnector and UrlFetcherFactory want to initialize and free | 357 // BrowserPolicyConnector and UrlFetcherFactory want to initialize and free |
337 // various components asynchronously via tasks, so create fake threads here. | 358 // various components asynchronously via tasks, so create fake threads here. |
338 content::TestBrowserThreadBundle thread_bundle_; | 359 content::TestBrowserThreadBundle thread_bundle_; |
339 | 360 |
340 net::TestURLFetcherFactory url_factory_; | 361 net::TestURLFetcherFactory url_factory_; |
341 | 362 |
342 SigninManagerFake* signin_manager_; | 363 SigninManagerFake* signin_manager_; |
343 #if defined(OS_ANDROID) | |
344 FakeProfileOAuth2TokenService* token_service_; // Not owned. | |
345 #endif | |
346 | 364 |
347 // Used in conjunction with OnRegisterCompleted() to test client registration | 365 // Used in conjunction with OnRegisterCompleted() to test client registration |
348 // callbacks. | 366 // callbacks. |
349 scoped_ptr<CloudPolicyClient> created_client_; | 367 scoped_ptr<CloudPolicyClient> created_client_; |
350 | 368 |
351 // True if OnRegisterCompleted() was called. | 369 // True if OnRegisterCompleted() was called. |
352 bool register_completed_; | 370 bool register_completed_; |
353 | 371 |
354 // Weak ptr to the MockDeviceManagementService (object is owned by the | 372 // Weak ptr to the MockDeviceManagementService (object is owned by the |
355 // BrowserPolicyConnector). | 373 // BrowserPolicyConnector). |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 // UserCloudPolicyManager should be initialized. | 411 // UserCloudPolicyManager should be initialized. |
394 ASSERT_TRUE(manager_->core()->service()); | 412 ASSERT_TRUE(manager_->core()->service()); |
395 | 413 |
396 // Complete initialization of the store. | 414 // Complete initialization of the store. |
397 mock_store_->NotifyStoreLoaded(); | 415 mock_store_->NotifyStoreLoaded(); |
398 | 416 |
399 // No oauth access token yet, so client registration should be deferred. | 417 // No oauth access token yet, so client registration should be deferred. |
400 ASSERT_FALSE(IsRequestActive()); | 418 ASSERT_FALSE(IsRequestActive()); |
401 | 419 |
402 // Make oauth token available. | 420 // Make oauth token available. |
403 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( | 421 GetTokenService()->IssueRefreshToken("oauth_login_refresh_token"); |
404 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth_login_refresh_token"); | |
405 | 422 |
406 // Client registration should be in progress since we now have an oauth token. | 423 // Client registration should be in progress since we now have an oauth token. |
407 ASSERT_TRUE(IsRequestActive()); | 424 ASSERT_TRUE(IsRequestActive()); |
408 } | 425 } |
409 | 426 |
| 427 TEST_F(UserPolicySigninServiceTest, InitWhileSignedInOAuthError) { |
| 428 // Set the user as signed in. |
| 429 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( |
| 430 kTestUser); |
| 431 |
| 432 // Let the SigninService know that the profile has been created. |
| 433 content::NotificationService::current()->Notify( |
| 434 chrome::NOTIFICATION_PROFILE_ADDED, |
| 435 content::Source<Profile>(profile_.get()), |
| 436 content::NotificationService::NoDetails()); |
| 437 |
| 438 // UserCloudPolicyManager should be initialized. |
| 439 ASSERT_TRUE(manager_->core()->service()); |
| 440 |
| 441 // Complete initialization of the store. |
| 442 mock_store_->NotifyStoreLoaded(); |
| 443 |
| 444 // No oauth access token yet, so client registration should be deferred. |
| 445 ASSERT_FALSE(IsRequestActive()); |
| 446 |
| 447 // Make oauth token available. |
| 448 GetTokenService()->IssueRefreshToken("oauth_login_refresh_token"); |
| 449 |
| 450 // Client registration should be in progress since we now have an oauth token. |
| 451 ASSERT_TRUE(IsRequestActive()); |
| 452 |
| 453 // Now fail the access token fetch. |
| 454 GoogleServiceAuthError error( |
| 455 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| 456 GetTokenService()->IssueErrorForAllPendingRequests(error); |
| 457 ASSERT_FALSE(IsRequestActive()); |
| 458 } |
| 459 |
410 TEST_F(UserPolicySigninServiceTest, SignInAfterInit) { | 460 TEST_F(UserPolicySigninServiceTest, SignInAfterInit) { |
411 EXPECT_CALL(*mock_store_, Clear()); | 461 EXPECT_CALL(*mock_store_, Clear()); |
412 // Let the SigninService know that the profile has been created. | 462 // Let the SigninService know that the profile has been created. |
413 content::NotificationService::current()->Notify( | 463 content::NotificationService::current()->Notify( |
414 chrome::NOTIFICATION_PROFILE_ADDED, | 464 chrome::NOTIFICATION_PROFILE_ADDED, |
415 content::Source<Profile>(profile_.get()), | 465 content::Source<Profile>(profile_.get()), |
416 content::NotificationService::NoDetails()); | 466 content::NotificationService::NoDetails()); |
417 | 467 |
418 // UserCloudPolicyManager should not be initialized since there is no | 468 // UserCloudPolicyManager should not be initialized since there is no |
419 // signed-in user. | 469 // signed-in user. |
420 ASSERT_FALSE(manager_->core()->service()); | 470 ASSERT_FALSE(manager_->core()->service()); |
421 | 471 |
422 // Now sign in the user. | 472 // Now sign in the user. |
423 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( | 473 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( |
424 kTestUser); | 474 kTestUser); |
425 | 475 |
426 // Complete initialization of the store. | 476 // Complete initialization of the store. |
427 mock_store_->NotifyStoreLoaded(); | 477 mock_store_->NotifyStoreLoaded(); |
428 | 478 |
429 // Make oauth token available. | 479 // Make oauth token available. |
430 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( | 480 GetTokenService()->IssueRefreshToken("oauth_login_refresh_token"); |
431 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth_login_refresh_token"); | |
432 | 481 |
433 // UserCloudPolicyManager should be initialized. | 482 // UserCloudPolicyManager should be initialized. |
434 ASSERT_TRUE(manager_->core()->service()); | 483 ASSERT_TRUE(manager_->core()->service()); |
435 | 484 |
436 // Client registration should be in progress since we have an oauth token. | 485 // Client registration should be in progress since we have an oauth token. |
437 ASSERT_TRUE(IsRequestActive()); | 486 ASSERT_TRUE(IsRequestActive()); |
438 } | 487 } |
439 | 488 |
440 TEST_F(UserPolicySigninServiceTest, SignInWithNonEnterpriseUser) { | 489 TEST_F(UserPolicySigninServiceTest, SignInWithNonEnterpriseUser) { |
441 EXPECT_CALL(*mock_store_, Clear()); | 490 EXPECT_CALL(*mock_store_, Clear()); |
442 // Let the SigninService know that the profile has been created. | 491 // Let the SigninService know that the profile has been created. |
443 content::NotificationService::current()->Notify( | 492 content::NotificationService::current()->Notify( |
444 chrome::NOTIFICATION_PROFILE_ADDED, | 493 chrome::NOTIFICATION_PROFILE_ADDED, |
445 content::Source<Profile>(profile_.get()), | 494 content::Source<Profile>(profile_.get()), |
446 content::NotificationService::NoDetails()); | 495 content::NotificationService::NoDetails()); |
447 | 496 |
448 // UserCloudPolicyManager should not be initialized since there is no | 497 // UserCloudPolicyManager should not be initialized since there is no |
449 // signed-in user. | 498 // signed-in user. |
450 ASSERT_FALSE(manager_->core()->service()); | 499 ASSERT_FALSE(manager_->core()->service()); |
451 | 500 |
452 // Now sign in a non-enterprise user (blacklisted gmail.com domain). | 501 // Now sign in a non-enterprise user (blacklisted gmail.com domain). |
453 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( | 502 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( |
454 "non_enterprise_user@gmail.com"); | 503 "non_enterprise_user@gmail.com"); |
455 | 504 |
456 // Complete initialization of the store. | 505 // Complete initialization of the store. |
457 mock_store_->NotifyStoreLoaded(); | 506 mock_store_->NotifyStoreLoaded(); |
458 | 507 |
459 // Make oauth token available. | 508 // Make oauth token available. |
460 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( | 509 GetTokenService()->IssueRefreshToken("oauth_login_refresh_token"); |
461 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth_login_refresh_token"); | |
462 | 510 |
463 // UserCloudPolicyManager should not be initialized and there should be no | 511 // UserCloudPolicyManager should not be initialized and there should be no |
464 // DMToken request active. | 512 // DMToken request active. |
465 ASSERT_TRUE(!manager_->core()->service()); | 513 ASSERT_TRUE(!manager_->core()->service()); |
466 ASSERT_FALSE(IsRequestActive()); | 514 ASSERT_FALSE(IsRequestActive()); |
467 } | 515 } |
468 | 516 |
469 TEST_F(UserPolicySigninServiceTest, UnregisteredClient) { | 517 TEST_F(UserPolicySigninServiceTest, UnregisteredClient) { |
470 EXPECT_CALL(*mock_store_, Clear()); | 518 EXPECT_CALL(*mock_store_, Clear()); |
471 // Let the SigninService know that the profile has been created. | 519 // Let the SigninService know that the profile has been created. |
472 content::NotificationService::current()->Notify( | 520 content::NotificationService::current()->Notify( |
473 chrome::NOTIFICATION_PROFILE_ADDED, | 521 chrome::NOTIFICATION_PROFILE_ADDED, |
474 content::Source<Profile>(profile_.get()), | 522 content::Source<Profile>(profile_.get()), |
475 content::NotificationService::NoDetails()); | 523 content::NotificationService::NoDetails()); |
476 | 524 |
477 // UserCloudPolicyManager should not be initialized since there is no | 525 // UserCloudPolicyManager should not be initialized since there is no |
478 // signed-in user. | 526 // signed-in user. |
479 ASSERT_FALSE(manager_->core()->service()); | 527 ASSERT_FALSE(manager_->core()->service()); |
480 | 528 |
481 // Now sign in the user. | 529 // Now sign in the user. |
482 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( | 530 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( |
483 kTestUser); | 531 kTestUser); |
484 | 532 |
485 // Make oauth token available. | 533 // Make oauth token available. |
486 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( | 534 GetTokenService()->IssueRefreshToken("oauth_login_refresh_token"); |
487 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth_login_refresh_token"); | |
488 | 535 |
489 // UserCloudPolicyManager should be initialized. | 536 // UserCloudPolicyManager should be initialized. |
490 ASSERT_TRUE(manager_->core()->service()); | 537 ASSERT_TRUE(manager_->core()->service()); |
491 | 538 |
492 // Client registration should not be in progress since the store is not | 539 // Client registration should not be in progress since the store is not |
493 // yet initialized. | 540 // yet initialized. |
494 ASSERT_FALSE(IsRequestActive()); | 541 ASSERT_FALSE(IsRequestActive()); |
495 | 542 |
496 // Complete initialization of the store with no policy (unregistered client). | 543 // Complete initialization of the store with no policy (unregistered client). |
497 mock_store_->NotifyStoreLoaded(); | 544 mock_store_->NotifyStoreLoaded(); |
(...skipping 12 matching lines...) Expand all Loading... |
510 | 557 |
511 // UserCloudPolicyManager should not be initialized since there is no | 558 // UserCloudPolicyManager should not be initialized since there is no |
512 // signed-in user. | 559 // signed-in user. |
513 ASSERT_FALSE(manager_->core()->service()); | 560 ASSERT_FALSE(manager_->core()->service()); |
514 | 561 |
515 // Now sign in the user. | 562 // Now sign in the user. |
516 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( | 563 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( |
517 kTestUser); | 564 kTestUser); |
518 | 565 |
519 // Make oauth token available. | 566 // Make oauth token available. |
520 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( | 567 GetTokenService()->IssueRefreshToken("oauth_login_refresh_token"); |
521 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth_login_refresh_token"); | |
522 | 568 |
523 // UserCloudPolicyManager should be initialized. | 569 // UserCloudPolicyManager should be initialized. |
524 ASSERT_TRUE(manager_->core()->service()); | 570 ASSERT_TRUE(manager_->core()->service()); |
525 | 571 |
526 // Client registration should not be in progress since the store is not | 572 // Client registration should not be in progress since the store is not |
527 // yet initialized. | 573 // yet initialized. |
528 ASSERT_FALSE(manager_->IsClientRegistered()); | 574 ASSERT_FALSE(manager_->IsClientRegistered()); |
529 ASSERT_FALSE(IsRequestActive()); | 575 ASSERT_FALSE(IsRequestActive()); |
530 | 576 |
531 mock_store_->policy_.reset(new enterprise_management::PolicyData()); | 577 mock_store_->policy_.reset(new enterprise_management::PolicyData()); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
571 RegisterPolicyClientWithCallback(signin_service); | 617 RegisterPolicyClientWithCallback(signin_service); |
572 Mock::VerifyAndClearExpectations(this); | 618 Mock::VerifyAndClearExpectations(this); |
573 | 619 |
574 // UserCloudPolicyManager should not be initialized. | 620 // UserCloudPolicyManager should not be initialized. |
575 ASSERT_FALSE(manager_->core()->service()); | 621 ASSERT_FALSE(manager_->core()->service()); |
576 ASSERT_TRUE(IsRequestActive()); | 622 ASSERT_TRUE(IsRequestActive()); |
577 EXPECT_FALSE(register_completed_); | 623 EXPECT_FALSE(register_completed_); |
578 | 624 |
579 // Cause the access token fetch to fail - callback should be invoked. | 625 // Cause the access token fetch to fail - callback should be invoked. |
580 #if defined(OS_ANDROID) | 626 #if defined(OS_ANDROID) |
581 ASSERT_TRUE(token_service_->HasPendingRequest()); | 627 ASSERT_TRUE(GetTokenService()->HasPendingRequest()); |
582 token_service_->IssueToken(""); | 628 GetTokenService()->IssueToken(""); |
583 #else | 629 #else |
584 net::TestURLFetcher* fetcher = url_factory_.GetFetcherByID(0); | 630 net::TestURLFetcher* fetcher = url_factory_.GetFetcherByID(0); |
585 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, -1)); | 631 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, -1)); |
586 fetcher->delegate()->OnURLFetchComplete(fetcher); | 632 fetcher->delegate()->OnURLFetchComplete(fetcher); |
587 #endif | 633 #endif |
588 | 634 |
589 EXPECT_TRUE(register_completed_); | 635 EXPECT_TRUE(register_completed_); |
590 EXPECT_FALSE(created_client_.get()); | 636 EXPECT_FALSE(created_client_.get()); |
591 EXPECT_FALSE(IsRequestActive()); | 637 EXPECT_FALSE(IsRequestActive()); |
592 } | 638 } |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
793 base::RunLoop().RunUntilIdle(); | 839 base::RunLoop().RunUntilIdle(); |
794 EXPECT_FALSE(manager_->IsClientRegistered()); | 840 EXPECT_FALSE(manager_->IsClientRegistered()); |
795 #if !defined(OS_ANDROID) | 841 #if !defined(OS_ANDROID) |
796 EXPECT_FALSE(signin_manager_->IsSignoutProhibited()); | 842 EXPECT_FALSE(signin_manager_->IsSignoutProhibited()); |
797 #endif | 843 #endif |
798 } | 844 } |
799 | 845 |
800 } // namespace | 846 } // namespace |
801 | 847 |
802 } // namespace policy | 848 } // namespace policy |
OLD | NEW |