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

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

Powered by Google App Engine
This is Rietveld 408576698