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

Side by Side Diff: chrome/browser/policy/user_policy_signin_service_unittest.cc

Issue 12220060: Load policy before signin completes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge with ToT. Created 7 years, 10 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 | Annotate | Revision Log
OLDNEW
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/command_line.h" 5 #include "base/command_line.h"
6 #include "base/message_loop.h" 6 #include "base/message_loop.h"
7 #include "base/run_loop.h" 7 #include "base/run_loop.h"
8 #include "chrome/browser/policy/browser_policy_connector.h" 8 #include "chrome/browser/policy/browser_policy_connector.h"
9 #include "chrome/browser/policy/cloud_policy_constants.h" 9 #include "chrome/browser/policy/cloud_policy_constants.h"
10 #include "chrome/browser/policy/mock_device_management_service.h" 10 #include "chrome/browser/policy/mock_device_management_service.h"
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 "{" 57 "{"
58 " \"hd\": \"test.com\"" 58 " \"hd\": \"test.com\""
59 "}"; 59 "}";
60 60
61 class UserPolicySigninServiceTest : public testing::Test { 61 class UserPolicySigninServiceTest : public testing::Test {
62 public: 62 public:
63 UserPolicySigninServiceTest() 63 UserPolicySigninServiceTest()
64 : loop_(MessageLoop::TYPE_UI), 64 : loop_(MessageLoop::TYPE_UI),
65 ui_thread_(content::BrowserThread::UI, &loop_), 65 ui_thread_(content::BrowserThread::UI, &loop_),
66 file_thread_(content::BrowserThread::FILE, &loop_), 66 file_thread_(content::BrowserThread::FILE, &loop_),
67 io_thread_(content::BrowserThread::IO, &loop_) {} 67 io_thread_(content::BrowserThread::IO, &loop_),
68 register_completed_(false) {}
68 69
69 MOCK_METHOD1(OnPolicyRefresh, void(bool)); 70 MOCK_METHOD1(OnPolicyRefresh, void(bool));
71 void OnRegisterCompleted(scoped_ptr<CloudPolicyClient> client) {
72 register_completed_ = true;
73 created_client_.swap(client);
74 }
75
76 void RegisterPolicyClientWithCallback(UserPolicySigninService* service) {
77 service->RegisterPolicyClient(
78 "testuser@test.com",
79 "mock_oauth_token",
80 base::Bind(&UserPolicySigninServiceTest::OnRegisterCompleted,
81 base::Unretained(this)));
82 ASSERT_TRUE(IsRequestActive());
83 }
70 84
71 virtual void SetUp() OVERRIDE { 85 virtual void SetUp() OVERRIDE {
72 device_management_service_ = new MockDeviceManagementService(); 86 device_management_service_ = new MockDeviceManagementService();
73 g_browser_process->browser_policy_connector()-> 87 g_browser_process->browser_policy_connector()->
74 SetDeviceManagementServiceForTesting( 88 SetDeviceManagementServiceForTesting(
75 scoped_ptr<DeviceManagementService>(device_management_service_)); 89 scoped_ptr<DeviceManagementService>(device_management_service_));
76 90
77 g_browser_process->browser_policy_connector()->Init(); 91 g_browser_process->browser_policy_connector()->Init();
78 92
79 local_state_.reset(new TestingPrefServiceSimple); 93 local_state_.reset(new TestingPrefServiceSimple);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 145
132 void ReportHostedDomainStatus(bool is_hosted_domain) { 146 void ReportHostedDomainStatus(bool is_hosted_domain) {
133 ASSERT_TRUE(IsRequestActive()); 147 ASSERT_TRUE(IsRequestActive());
134 net::TestURLFetcher* fetcher = url_factory_.GetFetcherByID(0); 148 net::TestURLFetcher* fetcher = url_factory_.GetFetcherByID(0);
135 fetcher->set_response_code(net::HTTP_OK); 149 fetcher->set_response_code(net::HTTP_OK);
136 fetcher->SetResponseString(is_hosted_domain ? kHostedDomainResponse : "{}"); 150 fetcher->SetResponseString(is_hosted_domain ? kHostedDomainResponse : "{}");
137 fetcher->delegate()->OnURLFetchComplete(fetcher); 151 fetcher->delegate()->OnURLFetchComplete(fetcher);
138 } 152 }
139 153
140 void TestSuccessfulSignin() { 154 void TestSuccessfulSignin() {
141 // Set the user as signed in.
142 SigninManagerFactory::GetForProfile(profile_.get())->
143 SetAuthenticatedUsername("testuser@test.com");
144
145 UserPolicySigninService* signin_service = 155 UserPolicySigninService* signin_service =
146 UserPolicySigninServiceFactory::GetForProfile(profile_.get()); 156 UserPolicySigninServiceFactory::GetForProfile(profile_.get());
147 EXPECT_CALL(*this, OnPolicyRefresh(true)).Times(0); 157 EXPECT_CALL(*this, OnPolicyRefresh(true)).Times(0);
148 signin_service->FetchPolicyForSignedInUser( 158 RegisterPolicyClientWithCallback(signin_service);
149 "mock_token",
150 base::Bind(&UserPolicySigninServiceTest::OnPolicyRefresh,
151 base::Unretained(this)));
152
153 // UserCloudPolicyManager should be initialized.
154 ASSERT_TRUE(manager_->core()->service());
155 ASSERT_TRUE(IsRequestActive());
156 159
157 // Mimic successful oauth token fetch. 160 // Mimic successful oauth token fetch.
158 MakeOAuthTokenFetchSucceed(); 161 MakeOAuthTokenFetchSucceed();
159 162
160 // When the user is from a hosted domain, this should kick off client 163 // When the user is from a hosted domain, this should kick off client
161 // registration. 164 // registration.
162 MockDeviceManagementJob* register_request = NULL; 165 MockDeviceManagementJob* register_request = NULL;
163 EXPECT_CALL(*device_management_service_, 166 EXPECT_CALL(*device_management_service_,
164 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION)) 167 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
165 .WillOnce(device_management_service_->CreateAsyncJob( 168 .WillOnce(device_management_service_->CreateAsyncJob(
166 &register_request)); 169 &register_request));
167 EXPECT_CALL(*device_management_service_, StartJob(_, _, _, _, _, _, _)) 170 EXPECT_CALL(*device_management_service_, StartJob(_, _, _, _, _, _, _))
168 .Times(1); 171 .Times(1);
169 172
170 // Now mimic the user being a hosted domain - this should cause a Register() 173 // Now mimic the user being a hosted domain - this should cause a Register()
171 // call. 174 // call.
172 ReportHostedDomainStatus(true); 175 ReportHostedDomainStatus(true);
173 176
174 // Should have no more outstanding requests. 177 // Should have no more outstanding requests.
175 ASSERT_FALSE(IsRequestActive()); 178 ASSERT_FALSE(IsRequestActive());
176 Mock::VerifyAndClearExpectations(this); 179 Mock::VerifyAndClearExpectations(this);
177 ASSERT_TRUE(register_request); 180 ASSERT_TRUE(register_request);
178 181
179 // Mimic successful client registration - this should kick off a policy 182 // Mimic successful client registration - this should register the client
180 // fetch. 183 // and invoke the callback.
184 em::DeviceManagementResponse registration_blob;
185 registration_blob.mutable_register_response()->set_device_management_token(
186 "dm_token");
187 registration_blob.mutable_register_response()->set_enrollment_type(
188 em::DeviceRegisterResponse::ENTERPRISE);
189 register_request->SendResponse(DM_STATUS_SUCCESS, registration_blob);
190
191 // UserCloudPolicyManager should not be initialized yet.
192 ASSERT_FALSE(manager_->core()->service());
193 EXPECT_TRUE(register_completed_);
194 EXPECT_TRUE(created_client_.get());
195
196 // Now call to fetch policy - this should fire off a fetch request.
181 MockDeviceManagementJob* fetch_request = NULL; 197 MockDeviceManagementJob* fetch_request = NULL;
182 EXPECT_CALL(*device_management_service_, 198 EXPECT_CALL(*device_management_service_,
183 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) 199 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH))
184 .WillOnce(device_management_service_->CreateAsyncJob(&fetch_request)); 200 .WillOnce(device_management_service_->CreateAsyncJob(&fetch_request));
185 EXPECT_CALL(*device_management_service_, StartJob(_, _, _, _, _, _, _)) 201 EXPECT_CALL(*device_management_service_, StartJob(_, _, _, _, _, _, _))
186 .Times(1); 202 .Times(1);
187 em::DeviceManagementResponse registration_blob; 203 signin_service->FetchPolicyForSignedInUser(
188 registration_blob.mutable_register_response()->set_device_management_token( 204 created_client_.Pass(),
189 "dm_token"); 205 base::Bind(&UserPolicySigninServiceTest::OnPolicyRefresh,
190 registration_blob.mutable_register_response()->set_enrollment_type( 206 base::Unretained(this)));
191 em::DeviceRegisterResponse::ENTERPRISE); 207
192 register_request->SendResponse(DM_STATUS_SUCCESS, registration_blob);
193 Mock::VerifyAndClearExpectations(this); 208 Mock::VerifyAndClearExpectations(this);
194 ASSERT_TRUE(fetch_request); 209 ASSERT_TRUE(fetch_request);
195 210
211 // UserCloudPolicyManager should now be initialized.
212 ASSERT_TRUE(manager_->core()->service());
213
196 // Make the policy fetch succeed - this should result in a write to the 214 // Make the policy fetch succeed - this should result in a write to the
197 // store and ultimately result in a call to OnPolicyRefresh(). 215 // store and ultimately result in a call to OnPolicyRefresh().
198 EXPECT_CALL(*mock_store_, Store(_)); 216 EXPECT_CALL(*mock_store_, Store(_));
199 EXPECT_CALL(*this, OnPolicyRefresh(true)).Times(1); 217 EXPECT_CALL(*this, OnPolicyRefresh(true)).Times(1);
218
200 // Create a fake policy blob to deliver to the client. 219 // Create a fake policy blob to deliver to the client.
201 em::DeviceManagementResponse policy_blob; 220 em::DeviceManagementResponse policy_blob;
202 em::PolicyData policy_data; 221 em::PolicyData policy_data;
203 policy_data.set_policy_type(dm_protocol::kChromeUserPolicyType); 222 policy_data.set_policy_type(dm_protocol::kChromeUserPolicyType);
204 em::PolicyFetchResponse* policy_response = 223 em::PolicyFetchResponse* policy_response =
205 policy_blob.mutable_policy_response()->add_response(); 224 policy_blob.mutable_policy_response()->add_response();
206 ASSERT_TRUE(policy_data.SerializeToString( 225 ASSERT_TRUE(policy_data.SerializeToString(
207 policy_response->mutable_policy_data())); 226 policy_response->mutable_policy_data()));
208 fetch_request->SendResponse(DM_STATUS_SUCCESS, policy_blob); 227 fetch_request->SendResponse(DM_STATUS_SUCCESS, policy_blob);
209 228
(...skipping 11 matching lines...) Expand all
221 240
222 // BrowserPolicyConnector and UrlFetcherFactory want to initialize and free 241 // BrowserPolicyConnector and UrlFetcherFactory want to initialize and free
223 // various components asynchronously via tasks, so create fake threads here. 242 // various components asynchronously via tasks, so create fake threads here.
224 MessageLoop loop_; 243 MessageLoop loop_;
225 content::TestBrowserThread ui_thread_; 244 content::TestBrowserThread ui_thread_;
226 content::TestBrowserThread file_thread_; 245 content::TestBrowserThread file_thread_;
227 content::TestBrowserThread io_thread_; 246 content::TestBrowserThread io_thread_;
228 247
229 net::TestURLFetcherFactory url_factory_; 248 net::TestURLFetcherFactory url_factory_;
230 249
250 // Used in conjunction with OnRegisterCompleted() to test client registration
251 // callbacks.
252 scoped_ptr<CloudPolicyClient> created_client_;
253
254 // True if OnRegisterCompleted() was called.
255 bool register_completed_;
256
231 // Weak ptr to the MockDeviceManagementService (object is owned by the 257 // Weak ptr to the MockDeviceManagementService (object is owned by the
232 // BrowserPolicyConnector). 258 // BrowserPolicyConnector).
233 MockDeviceManagementService* device_management_service_; 259 MockDeviceManagementService* device_management_service_;
234 260
235 scoped_ptr<TestingPrefServiceSimple> local_state_; 261 scoped_ptr<TestingPrefServiceSimple> local_state_;
236 }; 262 };
237 263
238 TEST_F(UserPolicySigninServiceTest, InitWhileSignedOut) { 264 TEST_F(UserPolicySigninServiceTest, InitWhileSignedOut) {
239 EXPECT_CALL(*mock_store_, Clear()); 265 EXPECT_CALL(*mock_store_, Clear());
240 // Make sure user is not signed in. 266 // Make sure user is not signed in.
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 // UserCloudPolicyManager should be initialized. 454 // UserCloudPolicyManager should be initialized.
429 ASSERT_TRUE(manager_->core()->service()); 455 ASSERT_TRUE(manager_->core()->service());
430 456
431 // Now sign out. 457 // Now sign out.
432 SigninManagerFactory::GetForProfile(profile_.get())->SignOut(); 458 SigninManagerFactory::GetForProfile(profile_.get())->SignOut();
433 459
434 // UserCloudPolicyManager should be shut down. 460 // UserCloudPolicyManager should be shut down.
435 ASSERT_FALSE(manager_->core()->service()); 461 ASSERT_FALSE(manager_->core()->service());
436 } 462 }
437 463
438 TEST_F(UserPolicySigninServiceTest, FetchPolicyOAuthFailure) { 464 TEST_F(UserPolicySigninServiceTest, RegisterPolicyClientOAuthFailure) {
439 // Set the user as signed in.
440 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername(
441 "testuser@test.com");
442
443 UserPolicySigninService* signin_service = 465 UserPolicySigninService* signin_service =
444 UserPolicySigninServiceFactory::GetForProfile(profile_.get()); 466 UserPolicySigninServiceFactory::GetForProfile(profile_.get());
445 EXPECT_CALL(*this, OnPolicyRefresh(_)).Times(0); 467 RegisterPolicyClientWithCallback(signin_service);
446 signin_service->FetchPolicyForSignedInUser(
447 "mock_token",
448 base::Bind(&UserPolicySigninServiceTest::OnPolicyRefresh,
449 base::Unretained(this)));
450 Mock::VerifyAndClearExpectations(this); 468 Mock::VerifyAndClearExpectations(this);
451 469
452 // UserCloudPolicyManager should be initialized. 470 // UserCloudPolicyManager should not be initialized.
453 ASSERT_TRUE(manager_->core()->service()); 471 ASSERT_FALSE(manager_->core()->service());
454 ASSERT_TRUE(IsRequestActive()); 472 ASSERT_TRUE(IsRequestActive());
473 EXPECT_FALSE(register_completed_);
455 474
456 // Cause the access token fetch to fail - callback should be invoked. 475 // Cause the access token fetch to fail - callback should be invoked.
457 EXPECT_CALL(*this, OnPolicyRefresh(false)).Times(1);
458 net::TestURLFetcher* fetcher = url_factory_.GetFetcherByID(0); 476 net::TestURLFetcher* fetcher = url_factory_.GetFetcherByID(0);
459 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, -1)); 477 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, -1));
460 fetcher->delegate()->OnURLFetchComplete(fetcher); 478 fetcher->delegate()->OnURLFetchComplete(fetcher);
479 EXPECT_TRUE(register_completed_);
480 EXPECT_FALSE(created_client_.get());
481 EXPECT_FALSE(IsRequestActive());
461 } 482 }
462 483
463 TEST_F(UserPolicySigninServiceTest, FetchPolicyNonHostedDomain) { 484 TEST_F(UserPolicySigninServiceTest, RegisterPolicyClientNonHostedDomain) {
464 // Set the user as signed in.
465 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername(
466 "testuser@test.com");
467
468 UserPolicySigninService* signin_service = 485 UserPolicySigninService* signin_service =
469 UserPolicySigninServiceFactory::GetForProfile(profile_.get()); 486 UserPolicySigninServiceFactory::GetForProfile(profile_.get());
470 EXPECT_CALL(*this, OnPolicyRefresh(_)).Times(0); 487 RegisterPolicyClientWithCallback(signin_service);
471 signin_service->FetchPolicyForSignedInUser(
472 "mock_token",
473 base::Bind(&UserPolicySigninServiceTest::OnPolicyRefresh,
474 base::Unretained(this)));
475 488
476 // UserCloudPolicyManager should be initialized. 489 // UserCloudPolicyManager should not be initialized.
477 ASSERT_TRUE(manager_->core()->service()); 490 ASSERT_FALSE(manager_->core()->service());
478 ASSERT_TRUE(IsRequestActive()); 491 ASSERT_TRUE(IsRequestActive());
479 492
480 // Cause the access token request to succeed. 493 // Cause the access token request to succeed.
481 MakeOAuthTokenFetchSucceed(); 494 MakeOAuthTokenFetchSucceed();
482 495
483 // Should be a follow-up fetch to check the hosted-domain status. 496 // Should be a follow-up fetch to check the hosted-domain status.
484 ASSERT_TRUE(IsRequestActive()); 497 ASSERT_TRUE(IsRequestActive());
485 Mock::VerifyAndClearExpectations(this); 498 Mock::VerifyAndClearExpectations(this);
499 EXPECT_FALSE(register_completed_);
486 500
487 // Report that the user is not on a hosted domain - callback should be 501 // Report that the user is not on a hosted domain - callback should be
488 // invoked reporting a failed fetch. 502 // invoked reporting a failed fetch.
489 EXPECT_CALL(*this, OnPolicyRefresh(false)).Times(1);
490 ReportHostedDomainStatus(false); 503 ReportHostedDomainStatus(false);
491 504
492 // Since this is not a hosted domain, we should not issue a request for a 505 // Since this is not a hosted domain, we should not issue a request for a
493 // DMToken. 506 // DMToken.
507 EXPECT_TRUE(register_completed_);
508 EXPECT_FALSE(created_client_.get());
494 ASSERT_FALSE(IsRequestActive()); 509 ASSERT_FALSE(IsRequestActive());
495 } 510 }
496 511
497 TEST_F(UserPolicySigninServiceTest, FetchPolicyFailedRegistration) { 512 TEST_F(UserPolicySigninServiceTest, RegisterPolicyClientFailedRegistration) {
498 // Set the user as signed in.
499 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername(
500 "testuser@test.com");
501
502 UserPolicySigninService* signin_service = 513 UserPolicySigninService* signin_service =
503 UserPolicySigninServiceFactory::GetForProfile(profile_.get()); 514 UserPolicySigninServiceFactory::GetForProfile(profile_.get());
504 EXPECT_CALL(*this, OnPolicyRefresh(true)).Times(0); 515 RegisterPolicyClientWithCallback(signin_service);
505 signin_service->FetchPolicyForSignedInUser(
506 "mock_token",
507 base::Bind(&UserPolicySigninServiceTest::OnPolicyRefresh,
508 base::Unretained(this)));
509 516
510 // UserCloudPolicyManager should be initialized. 517 // UserCloudPolicyManager should not be initialized.
511 ASSERT_TRUE(manager_->core()->service()); 518 ASSERT_FALSE(manager_->core()->service());
512 ASSERT_TRUE(IsRequestActive());
513 519
514 // Mimic successful oauth token fetch. 520 // Mimic successful oauth token fetch.
515 MakeOAuthTokenFetchSucceed(); 521 MakeOAuthTokenFetchSucceed();
522 EXPECT_FALSE(register_completed_);
516 523
517 // When the user is from a hosted domain, this should kick off client 524 // When the user is from a hosted domain, this should kick off client
518 // registration. 525 // registration.
519 MockDeviceManagementJob* register_request = NULL; 526 MockDeviceManagementJob* register_request = NULL;
520 EXPECT_CALL(*device_management_service_, 527 EXPECT_CALL(*device_management_service_,
521 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION)) 528 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
522 .WillOnce(device_management_service_->CreateAsyncJob(&register_request)); 529 .WillOnce(device_management_service_->CreateAsyncJob(&register_request));
523 EXPECT_CALL(*device_management_service_, StartJob(_, _, _, _, _, _, _)) 530 EXPECT_CALL(*device_management_service_, StartJob(_, _, _, _, _, _, _))
524 .Times(1); 531 .Times(1);
525 532
526 // Now mimic the user being a hosted domain - this should cause a Register() 533 // Now mimic the user being a hosted domain - this should cause a Register()
527 // call. 534 // call.
528 ReportHostedDomainStatus(true); 535 ReportHostedDomainStatus(true);
529 536
530 // Should have no more outstanding requests. 537 // Should have no more outstanding requests.
531 ASSERT_FALSE(IsRequestActive()); 538 ASSERT_FALSE(IsRequestActive());
532 Mock::VerifyAndClearExpectations(this); 539 Mock::VerifyAndClearExpectations(this);
533 ASSERT_TRUE(register_request); 540 ASSERT_TRUE(register_request);
541 EXPECT_FALSE(register_completed_);
534 542
535 EXPECT_CALL(*this, OnPolicyRefresh(false)).Times(1);
536 // Make client registration fail (hosted domain user that is not managed). 543 // Make client registration fail (hosted domain user that is not managed).
537 register_request->SendResponse(DM_STATUS_SERVICE_MANAGEMENT_NOT_SUPPORTED, 544 register_request->SendResponse(DM_STATUS_SERVICE_MANAGEMENT_NOT_SUPPORTED,
538 em::DeviceManagementResponse()); 545 em::DeviceManagementResponse());
546 EXPECT_TRUE(register_completed_);
547 EXPECT_FALSE(created_client_.get());
539 } 548 }
540 549
541 TEST_F(UserPolicySigninServiceTest, FetchPolicyPolicyFetchFailed) { 550 TEST_F(UserPolicySigninServiceTest, RegisterPolicyClientSucceeded) {
542 // Set the user as signed in.
543 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername(
544 "testuser@test.com");
545
546 UserPolicySigninService* signin_service = 551 UserPolicySigninService* signin_service =
547 UserPolicySigninServiceFactory::GetForProfile(profile_.get()); 552 UserPolicySigninServiceFactory::GetForProfile(profile_.get());
548 EXPECT_CALL(*this, OnPolicyRefresh(true)).Times(0); 553 RegisterPolicyClientWithCallback(signin_service);
549 signin_service->FetchPolicyForSignedInUser(
550 "mock_token",
551 base::Bind(&UserPolicySigninServiceTest::OnPolicyRefresh,
552 base::Unretained(this)));
553
554 // UserCloudPolicyManager should be initialized.
555 ASSERT_TRUE(manager_->core()->service());
556 ASSERT_TRUE(IsRequestActive());
557 554
558 // Mimic successful oauth token fetch. 555 // Mimic successful oauth token fetch.
559 MakeOAuthTokenFetchSucceed(); 556 MakeOAuthTokenFetchSucceed();
560 557
561 // When the user is from a hosted domain, this should kick off client 558 // When the user is from a hosted domain, this should kick off client
562 // registration. 559 // registration.
563 MockDeviceManagementJob* register_request = NULL; 560 MockDeviceManagementJob* register_request = NULL;
564 EXPECT_CALL(*device_management_service_, 561 EXPECT_CALL(*device_management_service_,
565 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION)) 562 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
566 .WillOnce(device_management_service_->CreateAsyncJob(&register_request)); 563 .WillOnce(device_management_service_->CreateAsyncJob(&register_request));
567 EXPECT_CALL(*device_management_service_, StartJob(_, _, _, _, _, _, _)) 564 EXPECT_CALL(*device_management_service_, StartJob(_, _, _, _, _, _, _))
568 .Times(1); 565 .Times(1);
569 566
570 // Now mimic the user being a hosted domain - this should cause a Register() 567 // Now mimic the user being a hosted domain - this should cause a Register()
571 // call. 568 // call.
572 ReportHostedDomainStatus(true); 569 ReportHostedDomainStatus(true);
573 570
574 // Should have no more outstanding requests. 571 // Should have no more outstanding requests.
575 ASSERT_FALSE(IsRequestActive()); 572 ASSERT_FALSE(IsRequestActive());
576 Mock::VerifyAndClearExpectations(this); 573 Mock::VerifyAndClearExpectations(this);
577 ASSERT_TRUE(register_request); 574 ASSERT_TRUE(register_request);
575 EXPECT_FALSE(register_completed_);
578 576
579 // Mimic successful client registration - this should kick off a policy
580 // fetch.
581 MockDeviceManagementJob* fetch_request = NULL;
582 EXPECT_CALL(*device_management_service_,
583 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH))
584 .WillOnce(device_management_service_->CreateAsyncJob(&fetch_request));
585 EXPECT_CALL(*device_management_service_, StartJob(_, _, _, _, _, _, _))
586 .Times(1);
587 em::DeviceManagementResponse registration_blob; 577 em::DeviceManagementResponse registration_blob;
588 registration_blob.mutable_register_response()->set_device_management_token( 578 registration_blob.mutable_register_response()->set_device_management_token(
589 "dm_token"); 579 "dm_token");
590 registration_blob.mutable_register_response()->set_enrollment_type( 580 registration_blob.mutable_register_response()->set_enrollment_type(
591 em::DeviceRegisterResponse::ENTERPRISE); 581 em::DeviceRegisterResponse::ENTERPRISE);
592 register_request->SendResponse(DM_STATUS_SUCCESS, registration_blob); 582 register_request->SendResponse(DM_STATUS_SUCCESS, registration_blob);
593 Mock::VerifyAndClearExpectations(this); 583 Mock::VerifyAndClearExpectations(this);
584 EXPECT_TRUE(register_completed_);
585 EXPECT_TRUE(created_client_.get());
586 // UserCloudPolicyManager should not be initialized.
587 ASSERT_FALSE(manager_->core()->service());
588 }
589
590 TEST_F(UserPolicySigninServiceTest, FetchPolicyFailed) {
591 scoped_ptr<CloudPolicyClient> client =
592 UserCloudPolicyManager::CreateCloudPolicyClient(
593 device_management_service_);
594 client->SetupRegistration("mock_dm_token", "mock_client_id");
595
596 // Initiate a policy fetch request.
597 MockDeviceManagementJob* fetch_request = NULL;
598 EXPECT_CALL(*device_management_service_,
599 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH))
600 .WillOnce(device_management_service_->CreateAsyncJob(&fetch_request));
601 EXPECT_CALL(*device_management_service_, StartJob(_, _, _, _, _, _, _))
602 .Times(1);
603 UserPolicySigninService* signin_service =
604 UserPolicySigninServiceFactory::GetForProfile(profile_.get());
605 signin_service->FetchPolicyForSignedInUser(
606 client.Pass(),
607 base::Bind(&UserPolicySigninServiceTest::OnPolicyRefresh,
608 base::Unretained(this)));
594 ASSERT_TRUE(fetch_request); 609 ASSERT_TRUE(fetch_request);
595 610
596 // Make the policy fetch fail. 611 // Make the policy fetch fail.
597 EXPECT_CALL(*this, OnPolicyRefresh(false)).Times(1); 612 EXPECT_CALL(*this, OnPolicyRefresh(false)).Times(1);
598 fetch_request->SendResponse(DM_STATUS_REQUEST_FAILED, 613 fetch_request->SendResponse(DM_STATUS_REQUEST_FAILED,
599 em::DeviceManagementResponse()); 614 em::DeviceManagementResponse());
615 // UserCloudPolicyManager should be initialized.
616 ASSERT_TRUE(manager_->core()->service());
600 } 617 }
601 618
602 TEST_F(UserPolicySigninServiceTest, FetchPolicySuccess) { 619 TEST_F(UserPolicySigninServiceTest, FetchPolicySuccess) {
603 TestSuccessfulSignin(); 620 TestSuccessfulSignin();
604 } 621 }
605 622
606 TEST_F(UserPolicySigninServiceTest, SignOutThenSignInAgain) { 623 TEST_F(UserPolicySigninServiceTest, SignOutThenSignInAgain) {
607 TestSuccessfulSignin(); 624 TestSuccessfulSignin();
608 625
609 // Now sign out. 626 // Now sign out.
610 SigninManagerFactory::GetForProfile(profile_.get())->SignOut(); 627 SigninManagerFactory::GetForProfile(profile_.get())->SignOut();
611 ASSERT_FALSE(manager_->core()->service()); 628 ASSERT_FALSE(manager_->core()->service());
612 629
613 // Now sign in again. 630 // Now sign in again.
614 TestSuccessfulSignin(); 631 TestSuccessfulSignin();
615 } 632 }
616 633
617 } // namespace 634 } // namespace
618 635
619 } // namespace policy 636 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/user_policy_signin_service.cc ('k') | chrome/browser/signin/signin_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698