OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/chromeos/policy/consumer_enrollment_handler.h" | 5 #include "chrome/browser/chromeos/policy/consumer_enrollment_handler.h" |
6 | 6 |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
9 #include "chrome/browser/browser_process_platform_part.h" | 9 #include "chrome/browser/browser_process_platform_part.h" |
| 10 #include "chrome/browser/chromeos/login/users/fake_user_manager.h" |
| 11 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
10 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" | 12 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" |
11 #include "chrome/browser/chromeos/policy/consumer_management_service.h" | 13 #include "chrome/browser/chromeos/policy/consumer_management_service.h" |
12 #include "chrome/browser/chromeos/policy/enrollment_status_chromeos.h" | 14 #include "chrome/browser/chromeos/policy/enrollment_status_chromeos.h" |
13 #include "chrome/browser/chromeos/policy/fake_consumer_management_service.h" | 15 #include "chrome/browser/chromeos/policy/fake_consumer_management_service.h" |
14 #include "chrome/browser/chromeos/policy/fake_device_cloud_policy_initializer.h" | 16 #include "chrome/browser/chromeos/policy/fake_device_cloud_policy_initializer.h" |
15 #include "chrome/browser/notifications/notification_ui_manager.h" | |
16 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" | 17 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" |
17 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" | 18 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" |
18 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 19 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
19 #include "chrome/browser/signin/signin_manager_factory.h" | 20 #include "chrome/browser/signin/signin_manager_factory.h" |
20 #include "chrome/test/base/browser_with_test_window_test.h" | |
21 #include "chrome/test/base/testing_browser_process.h" | 21 #include "chrome/test/base/testing_browser_process.h" |
22 #include "chrome/test/base/testing_profile_manager.h" | 22 #include "chrome/test/base/testing_profile_manager.h" |
23 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 23 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
24 #include "components/signin/core/browser/signin_manager_base.h" | 24 #include "components/signin/core/browser/signin_manager_base.h" |
| 25 #include "content/public/test/test_browser_thread_bundle.h" |
25 #include "google_apis/gaia/google_service_auth_error.h" | 26 #include "google_apis/gaia/google_service_auth_error.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
27 | 28 |
28 namespace { | 29 namespace { |
29 const char* kTestOwner = "test@chromium.org.test"; | 30 const char* kTestOwner = "test.owner@chromium.org.test"; |
| 31 const char* kTestUser = "test.user@chromium.org.test"; |
30 } | 32 } |
31 | 33 |
32 namespace policy { | 34 namespace policy { |
33 | 35 |
34 class ConsumerEnrollmentHandlerTest : public BrowserWithTestWindowTest { | 36 class ConsumerEnrollmentHandlerTest : public testing::Test { |
35 public: | 37 public: |
36 ConsumerEnrollmentHandlerTest() | 38 ConsumerEnrollmentHandlerTest() |
37 : fake_initializer_(new FakeDeviceCloudPolicyInitializer()), | 39 : fake_service_(new FakeConsumerManagementService()), |
38 fake_service_(new FakeConsumerManagementService()) { | 40 fake_initializer_(new FakeDeviceCloudPolicyInitializer()), |
| 41 fake_user_manager_(new chromeos::FakeUserManager()), |
| 42 scoped_user_manager_enabler_(fake_user_manager_), |
| 43 testing_profile_manager_(new TestingProfileManager( |
| 44 TestingBrowserProcess::GetGlobal())) { |
39 // Set up FakeConsumerManagementService. | 45 // Set up FakeConsumerManagementService. |
40 fake_service_->set_status(ConsumerManagementService::STATUS_ENROLLING); | 46 fake_service_->SetStatusAndEnrollmentStage( |
41 fake_service_->SetEnrollmentStage( | 47 ConsumerManagementService::STATUS_ENROLLING, |
42 ConsumerManagementService::ENROLLMENT_STAGE_OWNER_STORED); | 48 ConsumerManagementService::ENROLLMENT_STAGE_OWNER_STORED); |
43 | 49 |
44 // Inject fake objects. | 50 // Inject fake objects. |
45 BrowserPolicyConnectorChromeOS* connector = | 51 BrowserPolicyConnectorChromeOS* connector = |
46 g_browser_process->platform_part()->browser_policy_connector_chromeos(); | 52 g_browser_process->platform_part()->browser_policy_connector_chromeos(); |
47 connector->SetConsumerManagementServiceForTesting( | 53 connector->SetConsumerManagementServiceForTesting( |
48 make_scoped_ptr(fake_service_)); | 54 make_scoped_ptr(fake_service_)); |
49 connector->SetDeviceCloudPolicyInitializerForTesting( | 55 connector->SetDeviceCloudPolicyInitializerForTesting( |
50 make_scoped_ptr(fake_initializer_)); | 56 make_scoped_ptr(fake_initializer_)); |
| 57 |
| 58 // Set up FakeUserManager. |
| 59 fake_user_manager_->AddUser(kTestOwner); |
| 60 fake_user_manager_->AddUser(kTestUser); |
| 61 fake_user_manager_->set_owner_email(kTestOwner); |
51 } | 62 } |
52 | 63 |
53 virtual void SetUp() override { | 64 virtual void SetUp() override { |
54 BrowserWithTestWindowTest::SetUp(); | |
55 | |
56 // Set up TestingProfileManager. This is required for NotificationUIManager. | |
57 testing_profile_manager_.reset(new TestingProfileManager( | |
58 TestingBrowserProcess::GetGlobal())); | |
59 ASSERT_TRUE(testing_profile_manager_->SetUp()); | 65 ASSERT_TRUE(testing_profile_manager_->SetUp()); |
| 66 profile_ = testing_profile_manager_->CreateTestingProfile(kTestUser); |
60 | 67 |
61 // Set up FakeProfileOAuth2TokenService and issue a fake refresh token. | 68 // Set up FakeProfileOAuth2TokenService and issue a fake refresh token. |
62 ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory( | 69 ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory( |
63 profile(), &BuildAutoIssuingFakeProfileOAuth2TokenService); | 70 profile_, &BuildAutoIssuingFakeProfileOAuth2TokenService); |
64 GetFakeProfileOAuth2TokenService()-> | 71 GetFakeProfileOAuth2TokenService()-> |
65 IssueRefreshTokenForUser(kTestOwner, "fake_token"); | 72 IssueRefreshTokenForUser(kTestOwner, "fake_token"); |
66 | 73 |
67 // Set up the authenticated user name and ID. | 74 // Set up the authenticated user name and ID. |
68 SigninManagerFactory::GetForProfile(profile())-> | 75 SigninManagerFactory::GetForProfile(profile_)-> |
69 SetAuthenticatedUsername(kTestOwner); | 76 SetAuthenticatedUsername(kTestOwner); |
70 } | 77 } |
71 | 78 |
72 virtual void TearDown() override { | |
73 g_browser_process->notification_ui_manager()->CancelAll(); | |
74 testing_profile_manager_.reset(); | |
75 | |
76 BrowserWithTestWindowTest::TearDown(); | |
77 } | |
78 | |
79 FakeProfileOAuth2TokenService* GetFakeProfileOAuth2TokenService() { | 79 FakeProfileOAuth2TokenService* GetFakeProfileOAuth2TokenService() { |
80 return static_cast<FakeProfileOAuth2TokenService*>( | 80 return static_cast<FakeProfileOAuth2TokenService*>( |
81 ProfileOAuth2TokenServiceFactory::GetForProfile(profile())); | 81 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)); |
82 } | |
83 | |
84 bool HasEnrollmentNotification() { | |
85 return g_browser_process->notification_ui_manager()->FindById( | |
86 "consumer_management.enroll", | |
87 NotificationUIManager::GetProfileID(profile())); | |
88 } | 82 } |
89 | 83 |
90 void RunEnrollmentTest() { | 84 void RunEnrollmentTest() { |
91 handler_.reset( | 85 handler_.reset( |
92 new ConsumerEnrollmentHandler(profile(), fake_service_, NULL)); | 86 new ConsumerEnrollmentHandler(profile_, fake_service_, NULL)); |
93 base::RunLoop().RunUntilIdle(); | 87 base::RunLoop().RunUntilIdle(); |
94 } | 88 } |
95 | 89 |
| 90 content::TestBrowserThreadBundle thread_bundle; |
| 91 FakeConsumerManagementService* fake_service_; |
96 FakeDeviceCloudPolicyInitializer* fake_initializer_; | 92 FakeDeviceCloudPolicyInitializer* fake_initializer_; |
97 FakeConsumerManagementService* fake_service_; | 93 chromeos::FakeUserManager* fake_user_manager_; |
| 94 chromeos::ScopedUserManagerEnabler scoped_user_manager_enabler_; |
98 scoped_ptr<TestingProfileManager> testing_profile_manager_; | 95 scoped_ptr<TestingProfileManager> testing_profile_manager_; |
| 96 Profile* profile_; |
99 scoped_ptr<ConsumerEnrollmentHandler> handler_; | 97 scoped_ptr<ConsumerEnrollmentHandler> handler_; |
100 }; | 98 }; |
101 | 99 |
102 TEST_F(ConsumerEnrollmentHandlerTest, EnrollsSuccessfully) { | 100 TEST_F(ConsumerEnrollmentHandlerTest, EnrollsSuccessfully) { |
103 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called()); | 101 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called()); |
104 EXPECT_FALSE(HasEnrollmentNotification()); | |
105 | 102 |
106 RunEnrollmentTest(); | 103 RunEnrollmentTest(); |
107 | 104 |
108 EXPECT_TRUE(fake_initializer_->was_start_enrollment_called()); | 105 EXPECT_TRUE(fake_initializer_->was_start_enrollment_called()); |
109 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_STAGE_SUCCESS, | 106 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_STAGE_SUCCESS, |
110 fake_service_->enrollment_stage_before_reset()); | |
111 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_STAGE_NONE, | |
112 fake_service_->GetEnrollmentStage()); | 107 fake_service_->GetEnrollmentStage()); |
113 EXPECT_TRUE(HasEnrollmentNotification()); | |
114 } | 108 } |
115 | 109 |
116 TEST_F(ConsumerEnrollmentHandlerTest, FailsToGetAccessToken) { | 110 TEST_F(ConsumerEnrollmentHandlerTest, FailsToGetAccessToken) { |
117 // Disable auto-posting so that RunEnrollmentTest() should stop and wait for | 111 // Disable auto-posting so that RunEnrollmentTest() should stop and wait for |
118 // the access token to be available. | 112 // the access token to be available. |
119 GetFakeProfileOAuth2TokenService()-> | 113 GetFakeProfileOAuth2TokenService()-> |
120 set_auto_post_fetch_response_on_message_loop(false); | 114 set_auto_post_fetch_response_on_message_loop(false); |
121 EXPECT_FALSE(HasEnrollmentNotification()); | |
122 | 115 |
123 RunEnrollmentTest(); | 116 RunEnrollmentTest(); |
124 | 117 |
125 // The service should have a pending token request. | 118 // The service should have a pending token request. |
126 OAuth2TokenService::Request* token_request = | 119 OAuth2TokenService::Request* token_request = |
127 handler_->GetTokenRequestForTesting(); | 120 handler_->GetTokenRequestForTesting(); |
128 EXPECT_TRUE(token_request); | 121 EXPECT_TRUE(token_request); |
129 | 122 |
130 // Tell the service that the access token is not available because of some | 123 // Tell the service that the access token is not available because of some |
131 // backend issue. | 124 // backend issue. |
132 handler_->OnGetTokenFailure( | 125 handler_->OnGetTokenFailure( |
133 token_request, | 126 token_request, |
134 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR)); | 127 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR)); |
135 | 128 |
136 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called()); | 129 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called()); |
137 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_STAGE_GET_TOKEN_FAILED, | 130 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_STAGE_GET_TOKEN_FAILED, |
138 fake_service_->enrollment_stage_before_reset()); | |
139 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_STAGE_NONE, | |
140 fake_service_->GetEnrollmentStage()); | 131 fake_service_->GetEnrollmentStage()); |
141 EXPECT_TRUE(HasEnrollmentNotification()); | |
142 } | 132 } |
143 | 133 |
144 TEST_F(ConsumerEnrollmentHandlerTest, FailsToRegister) { | 134 TEST_F(ConsumerEnrollmentHandlerTest, FailsToRegister) { |
145 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called()); | 135 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called()); |
146 fake_initializer_->set_enrollment_status(EnrollmentStatus::ForStatus( | 136 fake_initializer_->set_enrollment_status(EnrollmentStatus::ForStatus( |
147 EnrollmentStatus::STATUS_REGISTRATION_FAILED)); | 137 EnrollmentStatus::STATUS_REGISTRATION_FAILED)); |
148 EXPECT_FALSE(HasEnrollmentNotification()); | |
149 | 138 |
150 RunEnrollmentTest(); | 139 RunEnrollmentTest(); |
151 | 140 |
152 EXPECT_TRUE(fake_initializer_->was_start_enrollment_called()); | 141 EXPECT_TRUE(fake_initializer_->was_start_enrollment_called()); |
153 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_STAGE_DM_SERVER_FAILED, | 142 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_STAGE_DM_SERVER_FAILED, |
154 fake_service_->enrollment_stage_before_reset()); | |
155 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_STAGE_NONE, | |
156 fake_service_->GetEnrollmentStage()); | 143 fake_service_->GetEnrollmentStage()); |
157 EXPECT_TRUE(HasEnrollmentNotification()); | |
158 } | |
159 | |
160 TEST_F(ConsumerEnrollmentHandlerTest, | |
161 ShowsDesktopNotificationOnlyIfEnrollmentIsAlreadyCompleted) { | |
162 fake_service_->set_status(ConsumerManagementService::STATUS_UNENROLLED); | |
163 fake_service_->SetEnrollmentStage( | |
164 ConsumerManagementService::ENROLLMENT_STAGE_CANCELED); | |
165 EXPECT_FALSE(HasEnrollmentNotification()); | |
166 | |
167 RunEnrollmentTest(); | |
168 | |
169 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called()); | |
170 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_STAGE_NONE, | |
171 fake_service_->GetEnrollmentStage()); | |
172 EXPECT_TRUE(HasEnrollmentNotification()); | |
173 } | 144 } |
174 | 145 |
175 } // namespace policy | 146 } // namespace policy |
OLD | NEW |