Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / consumer_enrollment_handler_unittest.cc
blob15ecd7e3bc72a19d0a6f7685d95f08f204e23c3c
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/policy/consumer_enrollment_handler.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/browser_process_platform_part.h"
11 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
12 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
13 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
14 #include "chrome/browser/chromeos/policy/consumer_management_service.h"
15 #include "chrome/browser/chromeos/policy/consumer_management_stage.h"
16 #include "chrome/browser/chromeos/policy/enrollment_status_chromeos.h"
17 #include "chrome/browser/chromeos/policy/fake_consumer_management_service.h"
18 #include "chrome/browser/chromeos/policy/fake_device_cloud_policy_initializer.h"
19 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
20 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
21 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
22 #include "chrome/browser/signin/signin_manager_factory.h"
23 #include "chrome/test/base/testing_browser_process.h"
24 #include "chrome/test/base/testing_profile_manager.h"
25 #include "components/signin/core/browser/profile_oauth2_token_service.h"
26 #include "components/signin/core/browser/signin_manager_base.h"
27 #include "content/public/test/test_browser_thread_bundle.h"
28 #include "google_apis/gaia/google_service_auth_error.h"
29 #include "testing/gtest/include/gtest/gtest.h"
31 namespace {
32 const char* kTestOwner = "test.owner@chromium.org.test";
33 const char* kTestUser = "test.user@chromium.org.test";
36 namespace policy {
38 class ConsumerEnrollmentHandlerTest : public testing::Test {
39 public:
40 ConsumerEnrollmentHandlerTest()
41 : fake_service_(new FakeConsumerManagementService()),
42 fake_initializer_(new FakeDeviceCloudPolicyInitializer()),
43 fake_user_manager_(new chromeos::FakeChromeUserManager()),
44 scoped_user_manager_enabler_(fake_user_manager_),
45 testing_profile_manager_(
46 new TestingProfileManager(TestingBrowserProcess::GetGlobal())) {
47 // Set up FakeConsumerManagementService.
48 fake_service_->SetStatusAndStage(
49 ConsumerManagementService::STATUS_ENROLLING,
50 ConsumerManagementStage::EnrollmentOwnerStored());
52 // Inject fake objects.
53 BrowserPolicyConnectorChromeOS* connector =
54 g_browser_process->platform_part()->browser_policy_connector_chromeos();
55 connector->SetConsumerManagementServiceForTesting(
56 make_scoped_ptr(fake_service_));
57 connector->SetDeviceCloudPolicyInitializerForTesting(
58 make_scoped_ptr(fake_initializer_));
60 // Set up FakeChromeUserManager.
61 fake_user_manager_->AddUser(kTestOwner);
62 fake_user_manager_->AddUser(kTestUser);
63 fake_user_manager_->set_owner_email(kTestOwner);
66 void SetUp() override {
67 ASSERT_TRUE(testing_profile_manager_->SetUp());
68 profile_ = testing_profile_manager_->CreateTestingProfile(kTestUser);
70 // Set up FakeProfileOAuth2TokenService and issue a fake refresh token.
71 ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory(
72 profile_, &BuildAutoIssuingFakeProfileOAuth2TokenService);
73 GetFakeProfileOAuth2TokenService()->
74 IssueRefreshTokenForUser(kTestOwner, "fake_token");
76 // Set up the authenticated user name and ID.
77 SigninManagerFactory::GetForProfile(profile_)->
78 SetAuthenticatedUsername(kTestOwner);
81 FakeProfileOAuth2TokenService* GetFakeProfileOAuth2TokenService() {
82 return static_cast<FakeProfileOAuth2TokenService*>(
83 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_));
86 void RunEnrollmentTest() {
87 handler_.reset(
88 new ConsumerEnrollmentHandler(profile_, fake_service_, NULL));
89 base::RunLoop().RunUntilIdle();
92 content::TestBrowserThreadBundle thread_bundle;
93 FakeConsumerManagementService* fake_service_;
94 FakeDeviceCloudPolicyInitializer* fake_initializer_;
95 chromeos::FakeChromeUserManager* fake_user_manager_;
96 chromeos::ScopedUserManagerEnabler scoped_user_manager_enabler_;
97 scoped_ptr<TestingProfileManager> testing_profile_manager_;
98 Profile* profile_;
99 scoped_ptr<ConsumerEnrollmentHandler> handler_;
102 TEST_F(ConsumerEnrollmentHandlerTest, EnrollsSuccessfully) {
103 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called());
105 RunEnrollmentTest();
107 EXPECT_TRUE(fake_initializer_->was_start_enrollment_called());
108 EXPECT_EQ(ConsumerManagementStage::EnrollmentSuccess(),
109 fake_service_->GetStage());
112 TEST_F(ConsumerEnrollmentHandlerTest, FailsToGetAccessToken) {
113 // Disable auto-posting so that RunEnrollmentTest() should stop and wait for
114 // the access token to be available.
115 GetFakeProfileOAuth2TokenService()->
116 set_auto_post_fetch_response_on_message_loop(false);
118 RunEnrollmentTest();
120 // The service should have a pending token request.
121 OAuth2TokenService::Request* token_request =
122 handler_->GetTokenRequestForTesting();
123 EXPECT_TRUE(token_request);
125 // Tell the service that the access token is not available because of some
126 // backend issue.
127 handler_->OnGetTokenFailure(
128 token_request,
129 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR));
131 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called());
132 EXPECT_EQ(ConsumerManagementStage::EnrollmentGetTokenFailed(),
133 fake_service_->GetStage());
136 TEST_F(ConsumerEnrollmentHandlerTest, FailsToRegister) {
137 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called());
138 fake_initializer_->set_enrollment_status(EnrollmentStatus::ForStatus(
139 EnrollmentStatus::STATUS_REGISTRATION_FAILED));
141 RunEnrollmentTest();
143 EXPECT_TRUE(fake_initializer_->was_start_enrollment_called());
144 EXPECT_EQ(ConsumerManagementStage::EnrollmentDMServerFailed(),
145 fake_service_->GetStage());
148 } // namespace policy