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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..821de544cfd9dba33329db46cbc0d09ab150e8cf 100644
--- a/chrome/browser/policy/user_policy_signin_service_unittest.cc
+++ b/chrome/browser/policy/user_policy_signin_service_unittest.cc
@@ -64,9 +64,23 @@ 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);
+ }
+
+ void RegisterPolicyClientWithCallback(UserPolicySigninService* service) {
+ service->RegisterPolicyClient(
+ "testuser@test.com",
+ "mock_oauth_token",
+ base::Bind(&UserPolicySigninServiceTest::OnRegisterCompleted,
+ base::Unretained(this)));
+ ASSERT_TRUE(IsRequestActive());
+ }
virtual void SetUp() OVERRIDE {
device_management_service_ = new MockDeviceManagementService();
@@ -138,21 +152,10 @@ 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,
- base::Unretained(this)));
-
- // UserCloudPolicyManager should be initialized.
- ASSERT_TRUE(manager_->core()->service());
- ASSERT_TRUE(IsRequestActive());
+ RegisterPolicyClientWithCallback(signin_service);
// Mimic successful oauth token fetch.
MakeOAuthTokenFetchSucceed();
@@ -176,27 +179,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 +247,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 +461,33 @@ TEST_F(UserPolicySigninServiceTest, SignOutAfterInit) {
ASSERT_FALSE(manager_->core()->service());
}
-TEST_F(UserPolicySigninServiceTest, FetchPolicyOAuthFailure) {
- // Set the user as signed in.
- SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername(
- "testuser@test.com");
-
+TEST_F(UserPolicySigninServiceTest, RegisterPolicyClientOAuthFailure) {
UserPolicySigninService* signin_service =
UserPolicySigninServiceFactory::GetForProfile(profile_.get());
- EXPECT_CALL(*this, OnPolicyRefresh(_)).Times(0);
- signin_service->FetchPolicyForSignedInUser(
- "mock_token",
- base::Bind(&UserPolicySigninServiceTest::OnPolicyRefresh,
- base::Unretained(this)));
+ RegisterPolicyClientWithCallback(signin_service);
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) {
- // Set the user as signed in.
- SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername(
- "testuser@test.com");
-
+TEST_F(UserPolicySigninServiceTest, RegisterPolicyClientNonHostedDomain) {
UserPolicySigninService* signin_service =
UserPolicySigninServiceFactory::GetForProfile(profile_.get());
- EXPECT_CALL(*this, OnPolicyRefresh(_)).Times(0);
- signin_service->FetchPolicyForSignedInUser(
- "mock_token",
- base::Bind(&UserPolicySigninServiceTest::OnPolicyRefresh,
- base::Unretained(this)));
+ RegisterPolicyClientWithCallback(signin_service);
- // 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 +496,30 @@ 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) {
- // Set the user as signed in.
- SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername(
- "testuser@test.com");
-
+TEST_F(UserPolicySigninServiceTest, RegisterPolicyClientFailedRegistration) {
UserPolicySigninService* signin_service =
UserPolicySigninServiceFactory::GetForProfile(profile_.get());
- EXPECT_CALL(*this, OnPolicyRefresh(true)).Times(0);
- signin_service->FetchPolicyForSignedInUser(
- "mock_token",
- base::Bind(&UserPolicySigninServiceTest::OnPolicyRefresh,
- base::Unretained(this)));
+ RegisterPolicyClientWithCallback(signin_service);
- // UserCloudPolicyManager should be initialized.
- ASSERT_TRUE(manager_->core()->service());
- ASSERT_TRUE(IsRequestActive());
+ // UserCloudPolicyManager should not be initialized.
+ ASSERT_FALSE(manager_->core()->service());
// 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,29 +538,19 @@ 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) {
- // Set the user as signed in.
- SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername(
- "testuser@test.com");
-
+TEST_F(UserPolicySigninServiceTest, RegisterPolicyClientSucceeded) {
UserPolicySigninService* signin_service =
UserPolicySigninServiceFactory::GetForProfile(profile_.get());
- EXPECT_CALL(*this, OnPolicyRefresh(true)).Times(0);
- signin_service->FetchPolicyForSignedInUser(
- "mock_token",
- base::Bind(&UserPolicySigninServiceTest::OnPolicyRefresh,
- base::Unretained(this)));
-
- // UserCloudPolicyManager should be initialized.
- ASSERT_TRUE(manager_->core()->service());
- ASSERT_TRUE(IsRequestActive());
+ RegisterPolicyClientWithCallback(signin_service);
// Mimic successful oauth token fetch.
MakeOAuthTokenFetchSucceed();
@@ -575,15 +572,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 +581,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) {
« 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