| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/policy/consumer_enrollment_handler.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "base/strings/utf_string_conversions.h" | |
| 13 #include "chrome/browser/browser_process.h" | |
| 14 #include "chrome/browser/browser_process_platform_part.h" | |
| 15 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" | |
| 16 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | |
| 17 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" | |
| 18 #include "chrome/browser/chromeos/policy/consumer_management_service.h" | |
| 19 #include "chrome/browser/chromeos/policy/consumer_management_stage.h" | |
| 20 #include "chrome/browser/chromeos/policy/enrollment_status_chromeos.h" | |
| 21 #include "chrome/browser/chromeos/policy/fake_consumer_management_service.h" | |
| 22 #include "chrome/browser/chromeos/policy/fake_device_cloud_policy_initializer.h" | |
| 23 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" | |
| 24 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 25 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 26 #include "chrome/test/base/testing_browser_process.h" | |
| 27 #include "chrome/test/base/testing_profile_manager.h" | |
| 28 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | |
| 29 #include "components/signin/core/browser/profile_oauth2_token_service.h" | |
| 30 #include "components/signin/core/browser/signin_manager_base.h" | |
| 31 #include "components/syncable_prefs/pref_service_syncable.h" | |
| 32 #include "content/public/test/test_browser_thread_bundle.h" | |
| 33 #include "google_apis/gaia/google_service_auth_error.h" | |
| 34 #include "testing/gtest/include/gtest/gtest.h" | |
| 35 | |
| 36 namespace { | |
| 37 const char* kTestOwner = "test.owner@chromium.org.test"; | |
| 38 const char* kTestUser = "test.user@chromium.org.test"; | |
| 39 } | |
| 40 | |
| 41 namespace policy { | |
| 42 | |
| 43 class ConsumerEnrollmentHandlerTest : public testing::Test { | |
| 44 public: | |
| 45 ConsumerEnrollmentHandlerTest() | |
| 46 : fake_service_(new FakeConsumerManagementService()), | |
| 47 fake_initializer_(new FakeDeviceCloudPolicyInitializer()), | |
| 48 fake_user_manager_(new chromeos::FakeChromeUserManager()), | |
| 49 scoped_user_manager_enabler_(fake_user_manager_), | |
| 50 testing_profile_manager_( | |
| 51 new TestingProfileManager(TestingBrowserProcess::GetGlobal())) { | |
| 52 // Set up FakeConsumerManagementService. | |
| 53 fake_service_->SetStatusAndStage( | |
| 54 ConsumerManagementService::STATUS_ENROLLING, | |
| 55 ConsumerManagementStage::EnrollmentOwnerStored()); | |
| 56 | |
| 57 // Inject fake objects. | |
| 58 BrowserPolicyConnectorChromeOS* connector = | |
| 59 g_browser_process->platform_part()->browser_policy_connector_chromeos(); | |
| 60 connector->SetConsumerManagementServiceForTesting( | |
| 61 base::WrapUnique(fake_service_)); | |
| 62 connector->SetDeviceCloudPolicyInitializerForTesting( | |
| 63 base::WrapUnique(fake_initializer_)); | |
| 64 | |
| 65 // Set up FakeChromeUserManager. | |
| 66 fake_user_manager_->AddUser(AccountId::FromUserEmail(kTestOwner)); | |
| 67 fake_user_manager_->AddUser(AccountId::FromUserEmail(kTestUser)); | |
| 68 fake_user_manager_->set_owner_id(AccountId::FromUserEmail(kTestOwner)); | |
| 69 } | |
| 70 | |
| 71 void SetUp() override { | |
| 72 ASSERT_TRUE(testing_profile_manager_->SetUp()); | |
| 73 TestingProfile::TestingFactories factories; | |
| 74 factories.push_back( | |
| 75 std::make_pair(ProfileOAuth2TokenServiceFactory::GetInstance(), | |
| 76 BuildAutoIssuingFakeProfileOAuth2TokenService)); | |
| 77 profile_ = testing_profile_manager_->CreateTestingProfile( | |
| 78 kTestUser, std::unique_ptr<syncable_prefs::PrefServiceSyncable>(), | |
| 79 base::UTF8ToUTF16(kTestUser), 0, std::string(), factories); | |
| 80 | |
| 81 // Set up the authenticated user name and ID. | |
| 82 SigninManagerFactory::GetForProfile(profile_) | |
| 83 ->SetAuthenticatedAccountInfo(kTestOwner, kTestOwner); | |
| 84 | |
| 85 // Issue a fake refresh token. | |
| 86 GetFakeProfileOAuth2TokenService()->UpdateCredentials(kTestOwner, | |
| 87 "fake_token"); | |
| 88 } | |
| 89 | |
| 90 FakeProfileOAuth2TokenService* GetFakeProfileOAuth2TokenService() { | |
| 91 return static_cast<FakeProfileOAuth2TokenService*>( | |
| 92 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)); | |
| 93 } | |
| 94 | |
| 95 void RunEnrollmentTest() { | |
| 96 handler_.reset( | |
| 97 new ConsumerEnrollmentHandler(profile_, fake_service_, NULL)); | |
| 98 base::RunLoop().RunUntilIdle(); | |
| 99 } | |
| 100 | |
| 101 content::TestBrowserThreadBundle thread_bundle; | |
| 102 FakeConsumerManagementService* fake_service_; | |
| 103 FakeDeviceCloudPolicyInitializer* fake_initializer_; | |
| 104 chromeos::FakeChromeUserManager* fake_user_manager_; | |
| 105 chromeos::ScopedUserManagerEnabler scoped_user_manager_enabler_; | |
| 106 std::unique_ptr<TestingProfileManager> testing_profile_manager_; | |
| 107 Profile* profile_; | |
| 108 std::unique_ptr<ConsumerEnrollmentHandler> handler_; | |
| 109 }; | |
| 110 | |
| 111 TEST_F(ConsumerEnrollmentHandlerTest, EnrollsSuccessfully) { | |
| 112 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called()); | |
| 113 | |
| 114 RunEnrollmentTest(); | |
| 115 | |
| 116 EXPECT_TRUE(fake_initializer_->was_start_enrollment_called()); | |
| 117 EXPECT_EQ(ConsumerManagementStage::EnrollmentSuccess(), | |
| 118 fake_service_->GetStage()); | |
| 119 } | |
| 120 | |
| 121 TEST_F(ConsumerEnrollmentHandlerTest, FailsToGetAccessToken) { | |
| 122 // Disable auto-posting so that RunEnrollmentTest() should stop and wait for | |
| 123 // the access token to be available. | |
| 124 GetFakeProfileOAuth2TokenService()-> | |
| 125 set_auto_post_fetch_response_on_message_loop(false); | |
| 126 | |
| 127 RunEnrollmentTest(); | |
| 128 | |
| 129 // The service should have a pending token request. | |
| 130 OAuth2TokenService::Request* token_request = | |
| 131 handler_->GetTokenRequestForTesting(); | |
| 132 EXPECT_TRUE(token_request); | |
| 133 | |
| 134 // Tell the service that the access token is not available because of some | |
| 135 // backend issue. | |
| 136 handler_->OnGetTokenFailure( | |
| 137 token_request, | |
| 138 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR)); | |
| 139 | |
| 140 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called()); | |
| 141 EXPECT_EQ(ConsumerManagementStage::EnrollmentGetTokenFailed(), | |
| 142 fake_service_->GetStage()); | |
| 143 } | |
| 144 | |
| 145 TEST_F(ConsumerEnrollmentHandlerTest, FailsToRegister) { | |
| 146 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called()); | |
| 147 fake_initializer_->set_enrollment_status(EnrollmentStatus::ForStatus( | |
| 148 EnrollmentStatus::STATUS_REGISTRATION_FAILED)); | |
| 149 | |
| 150 RunEnrollmentTest(); | |
| 151 | |
| 152 EXPECT_TRUE(fake_initializer_->was_start_enrollment_called()); | |
| 153 EXPECT_EQ(ConsumerManagementStage::EnrollmentDMServerFailed(), | |
| 154 fake_service_->GetStage()); | |
| 155 } | |
| 156 | |
| 157 } // namespace policy | |
| OLD | NEW |