Chromium Code Reviews| Index: chrome/browser/policy/user_policy_signin_service_unittest.cc |
| diff --git a/chrome/browser/policy/user_policy_signin_service_unittest.cc b/chrome/browser/policy/user_policy_signin_service_unittest.cc |
| index 3920bba090edcb8cde3c1a6b1fb4ad776c9dee5d..e73ab6e15293b6212cbff83e34b72c511a7c0083 100644 |
| --- a/chrome/browser/policy/user_policy_signin_service_unittest.cc |
| +++ b/chrome/browser/policy/user_policy_signin_service_unittest.cc |
| @@ -64,9 +64,14 @@ class UserPolicySigninServiceTest : public testing::Test { |
| : loop_(MessageLoop::TYPE_UI), |
| ui_thread_(content::BrowserThread::UI, &loop_), |
| file_thread_(content::BrowserThread::FILE, &loop_), |
| - io_thread_(content::BrowserThread::IO, &loop_) {} |
| + io_thread_(content::BrowserThread::IO, &loop_), |
| + register_completed_(false) {} |
| MOCK_METHOD1(OnPolicyRefresh, void(bool)); |
| + void OnRegisterCompleted(scoped_ptr<CloudPolicyClient> client) { |
| + register_completed_ = true; |
| + created_client_.swap(client); |
| + } |
| virtual void SetUp() OVERRIDE { |
| device_management_service_ = new MockDeviceManagementService(); |
| @@ -138,20 +143,14 @@ class UserPolicySigninServiceTest : public testing::Test { |
| } |
| void TestSuccessfulSignin() { |
| - // Set the user as signed in. |
| - SigninManagerFactory::GetForProfile(profile_.get())-> |
| - SetAuthenticatedUsername("testuser@test.com"); |
| - |
| UserPolicySigninService* signin_service = |
| UserPolicySigninServiceFactory::GetForProfile(profile_.get()); |
| EXPECT_CALL(*this, OnPolicyRefresh(true)).Times(0); |
| - signin_service->FetchPolicyForSignedInUser( |
| - "mock_token", |
| - base::Bind(&UserPolicySigninServiceTest::OnPolicyRefresh, |
| + signin_service->RegisterPolicyClient( |
| + "testuser@test.com", |
| + "mock_oauth_token", |
| + base::Bind(&UserPolicySigninServiceTest::OnRegisterCompleted, |
| base::Unretained(this))); |
| - |
| - // UserCloudPolicyManager should be initialized. |
| - ASSERT_TRUE(manager_->core()->service()); |
| ASSERT_TRUE(IsRequestActive()); |
| // Mimic successful oauth token fetch. |
| @@ -176,27 +175,43 @@ class UserPolicySigninServiceTest : public testing::Test { |
| Mock::VerifyAndClearExpectations(this); |
| ASSERT_TRUE(register_request); |
| - // Mimic successful client registration - this should kick off a policy |
| - // fetch. |
| - MockDeviceManagementJob* fetch_request = NULL; |
| - EXPECT_CALL(*device_management_service_, |
| - CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) |
| - .WillOnce(device_management_service_->CreateAsyncJob(&fetch_request)); |
| - EXPECT_CALL(*device_management_service_, StartJob(_, _, _, _, _, _, _)) |
| - .Times(1); |
| + // Mimic successful client registration - this should register the client |
| + // and invoke the callback. |
| em::DeviceManagementResponse registration_blob; |
| registration_blob.mutable_register_response()->set_device_management_token( |
| "dm_token"); |
| registration_blob.mutable_register_response()->set_enrollment_type( |
| em::DeviceRegisterResponse::ENTERPRISE); |
| register_request->SendResponse(DM_STATUS_SUCCESS, registration_blob); |
| + |
| + // UserCloudPolicyManager should not be initialized yet. |
| + ASSERT_FALSE(manager_->core()->service()); |
| + EXPECT_TRUE(register_completed_); |
| + EXPECT_TRUE(created_client_.get()); |
| + |
| + // Now call to fetch policy - this should fire off a fetch request. |
| + MockDeviceManagementJob* fetch_request = NULL; |
| + EXPECT_CALL(*device_management_service_, |
| + CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) |
| + .WillOnce(device_management_service_->CreateAsyncJob(&fetch_request)); |
| + EXPECT_CALL(*device_management_service_, StartJob(_, _, _, _, _, _, _)) |
| + .Times(1); |
| + signin_service->FetchPolicyForSignedInUser( |
| + created_client_.Pass(), |
| + base::Bind(&UserPolicySigninServiceTest::OnPolicyRefresh, |
| + base::Unretained(this))); |
| + |
| Mock::VerifyAndClearExpectations(this); |
| ASSERT_TRUE(fetch_request); |
| + // UserCloudPolicyManager should now be initialized. |
| + ASSERT_TRUE(manager_->core()->service()); |
| + |
| // Make the policy fetch succeed - this should result in a write to the |
| // store and ultimately result in a call to OnPolicyRefresh(). |
| EXPECT_CALL(*mock_store_, Store(_)); |
| EXPECT_CALL(*this, OnPolicyRefresh(true)).Times(1); |
| + |
| // Create a fake policy blob to deliver to the client. |
| em::DeviceManagementResponse policy_blob; |
| em::PolicyData policy_data; |
| @@ -228,6 +243,13 @@ class UserPolicySigninServiceTest : public testing::Test { |
| net::TestURLFetcherFactory url_factory_; |
| + // Used in conjunction with OnRegisterCompleted() to test client registration |
| + // callbacks. |
| + scoped_ptr<CloudPolicyClient> created_client_; |
| + |
| + // True if OnRegisterCompleted() was called. |
| + bool register_completed_; |
| + |
| // Weak ptr to the MockDeviceManagementService (object is owned by the |
| // BrowserPolicyConnector). |
| MockDeviceManagementService* device_management_service_; |
| @@ -435,46 +457,51 @@ TEST_F(UserPolicySigninServiceTest, SignOutAfterInit) { |
| ASSERT_FALSE(manager_->core()->service()); |
| } |
| -TEST_F(UserPolicySigninServiceTest, FetchPolicyOAuthFailure) { |
| +TEST_F(UserPolicySigninServiceTest, RegisterPolicyClientOAuthFailure) { |
| // Set the user as signed in. |
| SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( |
| "testuser@test.com"); |
| UserPolicySigninService* signin_service = |
| UserPolicySigninServiceFactory::GetForProfile(profile_.get()); |
| - EXPECT_CALL(*this, OnPolicyRefresh(_)).Times(0); |
| - signin_service->FetchPolicyForSignedInUser( |
| - "mock_token", |
| - base::Bind(&UserPolicySigninServiceTest::OnPolicyRefresh, |
| + signin_service->RegisterPolicyClient( |
| + SigninManagerFactory::GetForProfile(profile_.get())-> |
| + GetAuthenticatedUsername(), |
| + "mock_oauth_token", |
| + base::Bind(&UserPolicySigninServiceTest::OnRegisterCompleted, |
| base::Unretained(this))); |
| Mock::VerifyAndClearExpectations(this); |
| - // UserCloudPolicyManager should be initialized. |
| - ASSERT_TRUE(manager_->core()->service()); |
| + // UserCloudPolicyManager should not be initialized. |
| + ASSERT_FALSE(manager_->core()->service()); |
| ASSERT_TRUE(IsRequestActive()); |
| + EXPECT_FALSE(register_completed_); |
| // Cause the access token fetch to fail - callback should be invoked. |
| - EXPECT_CALL(*this, OnPolicyRefresh(false)).Times(1); |
| net::TestURLFetcher* fetcher = url_factory_.GetFetcherByID(0); |
| fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, -1)); |
| fetcher->delegate()->OnURLFetchComplete(fetcher); |
| + EXPECT_TRUE(register_completed_); |
| + EXPECT_FALSE(created_client_.get()); |
| + EXPECT_FALSE(IsRequestActive()); |
| } |
| -TEST_F(UserPolicySigninServiceTest, FetchPolicyNonHostedDomain) { |
| +TEST_F(UserPolicySigninServiceTest, RegisterPolicyClientNonHostedDomain) { |
| // Set the user as signed in. |
| SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( |
| "testuser@test.com"); |
| UserPolicySigninService* signin_service = |
| UserPolicySigninServiceFactory::GetForProfile(profile_.get()); |
| - EXPECT_CALL(*this, OnPolicyRefresh(_)).Times(0); |
| - signin_service->FetchPolicyForSignedInUser( |
| - "mock_token", |
| - base::Bind(&UserPolicySigninServiceTest::OnPolicyRefresh, |
| + signin_service->RegisterPolicyClient( |
| + SigninManagerFactory::GetForProfile( |
| + profile_.get())->GetAuthenticatedUsername(), |
| + "mock_oauth_token", |
| + base::Bind(&UserPolicySigninServiceTest::OnRegisterCompleted, |
| 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.
|
| - // UserCloudPolicyManager should be initialized. |
| - ASSERT_TRUE(manager_->core()->service()); |
| + // UserCloudPolicyManager should not be initialized. |
| + ASSERT_FALSE(manager_->core()->service()); |
| ASSERT_TRUE(IsRequestActive()); |
| // Cause the access token request to succeed. |
| @@ -483,36 +510,40 @@ TEST_F(UserPolicySigninServiceTest, FetchPolicyNonHostedDomain) { |
| // Should be a follow-up fetch to check the hosted-domain status. |
| ASSERT_TRUE(IsRequestActive()); |
| Mock::VerifyAndClearExpectations(this); |
| + EXPECT_FALSE(register_completed_); |
| // Report that the user is not on a hosted domain - callback should be |
| // invoked reporting a failed fetch. |
| - EXPECT_CALL(*this, OnPolicyRefresh(false)).Times(1); |
| ReportHostedDomainStatus(false); |
| // Since this is not a hosted domain, we should not issue a request for a |
| // DMToken. |
| + EXPECT_TRUE(register_completed_); |
| + EXPECT_FALSE(created_client_.get()); |
| ASSERT_FALSE(IsRequestActive()); |
| } |
| -TEST_F(UserPolicySigninServiceTest, FetchPolicyFailedRegistration) { |
| +TEST_F(UserPolicySigninServiceTest, RegisterPolicyClientFailedRegistration) { |
| // Set the user as signed in. |
| SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( |
| "testuser@test.com"); |
| UserPolicySigninService* signin_service = |
| UserPolicySigninServiceFactory::GetForProfile(profile_.get()); |
| - EXPECT_CALL(*this, OnPolicyRefresh(true)).Times(0); |
| - signin_service->FetchPolicyForSignedInUser( |
| - "mock_token", |
| - base::Bind(&UserPolicySigninServiceTest::OnPolicyRefresh, |
| + signin_service->RegisterPolicyClient( |
| + SigninManagerFactory::GetForProfile( |
| + profile_.get())->GetAuthenticatedUsername(), |
| + "mock_oauth_token", |
| + base::Bind(&UserPolicySigninServiceTest::OnRegisterCompleted, |
| base::Unretained(this))); |
| - // UserCloudPolicyManager should be initialized. |
| - ASSERT_TRUE(manager_->core()->service()); |
| + // UserCloudPolicyManager should not be initialized. |
| + ASSERT_FALSE(manager_->core()->service()); |
| ASSERT_TRUE(IsRequestActive()); |
| // Mimic successful oauth token fetch. |
| MakeOAuthTokenFetchSucceed(); |
| + EXPECT_FALSE(register_completed_); |
| // When the user is from a hosted domain, this should kick off client |
| // registration. |
| @@ -531,28 +562,29 @@ TEST_F(UserPolicySigninServiceTest, FetchPolicyFailedRegistration) { |
| ASSERT_FALSE(IsRequestActive()); |
| Mock::VerifyAndClearExpectations(this); |
| ASSERT_TRUE(register_request); |
| + EXPECT_FALSE(register_completed_); |
| - EXPECT_CALL(*this, OnPolicyRefresh(false)).Times(1); |
| // Make client registration fail (hosted domain user that is not managed). |
| register_request->SendResponse(DM_STATUS_SERVICE_MANAGEMENT_NOT_SUPPORTED, |
| em::DeviceManagementResponse()); |
| + EXPECT_TRUE(register_completed_); |
| + EXPECT_FALSE(created_client_.get()); |
| } |
| -TEST_F(UserPolicySigninServiceTest, FetchPolicyPolicyFetchFailed) { |
| +TEST_F(UserPolicySigninServiceTest, RegisterPolicyClientSucceeded) { |
| // Set the user as signed in. |
| SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( |
| "testuser@test.com"); |
| UserPolicySigninService* signin_service = |
| UserPolicySigninServiceFactory::GetForProfile(profile_.get()); |
| - EXPECT_CALL(*this, OnPolicyRefresh(true)).Times(0); |
| - signin_service->FetchPolicyForSignedInUser( |
| - "mock_token", |
| - base::Bind(&UserPolicySigninServiceTest::OnPolicyRefresh, |
| + signin_service->RegisterPolicyClient( |
| + SigninManagerFactory::GetForProfile( |
| + profile_.get())->GetAuthenticatedUsername(), |
| + "mock_oauth_token", |
| + base::Bind(&UserPolicySigninServiceTest::OnRegisterCompleted, |
| base::Unretained(this))); |
| - // UserCloudPolicyManager should be initialized. |
| - ASSERT_TRUE(manager_->core()->service()); |
| ASSERT_TRUE(IsRequestActive()); |
| // Mimic successful oauth token fetch. |
| @@ -575,15 +607,8 @@ TEST_F(UserPolicySigninServiceTest, FetchPolicyPolicyFetchFailed) { |
| ASSERT_FALSE(IsRequestActive()); |
| Mock::VerifyAndClearExpectations(this); |
| ASSERT_TRUE(register_request); |
| + EXPECT_FALSE(register_completed_); |
| - // Mimic successful client registration - this should kick off a policy |
| - // fetch. |
| - MockDeviceManagementJob* fetch_request = NULL; |
| - EXPECT_CALL(*device_management_service_, |
| - CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) |
| - .WillOnce(device_management_service_->CreateAsyncJob(&fetch_request)); |
| - EXPECT_CALL(*device_management_service_, StartJob(_, _, _, _, _, _, _)) |
| - .Times(1); |
| em::DeviceManagementResponse registration_blob; |
| registration_blob.mutable_register_response()->set_device_management_token( |
| "dm_token"); |
| @@ -591,12 +616,39 @@ TEST_F(UserPolicySigninServiceTest, FetchPolicyPolicyFetchFailed) { |
| em::DeviceRegisterResponse::ENTERPRISE); |
| register_request->SendResponse(DM_STATUS_SUCCESS, registration_blob); |
| Mock::VerifyAndClearExpectations(this); |
| + EXPECT_TRUE(register_completed_); |
| + EXPECT_TRUE(created_client_.get()); |
| + // UserCloudPolicyManager should not be initialized. |
| + ASSERT_FALSE(manager_->core()->service()); |
| +} |
| + |
| +TEST_F(UserPolicySigninServiceTest, FetchPolicyFailed) { |
| + scoped_ptr<CloudPolicyClient> client = |
| + UserCloudPolicyManager::CreateCloudPolicyClient( |
| + device_management_service_); |
| + client->SetupRegistration("mock_dm_token", "mock_client_id"); |
| + |
| + // Initiate a policy fetch request. |
| + MockDeviceManagementJob* fetch_request = NULL; |
| + EXPECT_CALL(*device_management_service_, |
| + CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) |
| + .WillOnce(device_management_service_->CreateAsyncJob(&fetch_request)); |
| + EXPECT_CALL(*device_management_service_, StartJob(_, _, _, _, _, _, _)) |
| + .Times(1); |
| + UserPolicySigninService* signin_service = |
| + UserPolicySigninServiceFactory::GetForProfile(profile_.get()); |
| + signin_service->FetchPolicyForSignedInUser( |
| + client.Pass(), |
| + base::Bind(&UserPolicySigninServiceTest::OnPolicyRefresh, |
| + base::Unretained(this))); |
| ASSERT_TRUE(fetch_request); |
| // Make the policy fetch fail. |
| EXPECT_CALL(*this, OnPolicyRefresh(false)).Times(1); |
| fetch_request->SendResponse(DM_STATUS_REQUEST_FAILED, |
| em::DeviceManagementResponse()); |
| + // UserCloudPolicyManager should be initialized. |
| + ASSERT_TRUE(manager_->core()->service()); |
| } |
| TEST_F(UserPolicySigninServiceTest, FetchPolicySuccess) { |