| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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 <string> | |
| 6 | |
| 7 | |
| 8 #include "base/compiler_specific.h" | |
| 9 #include "base/run_loop.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "base/threading/sequenced_worker_pool.h" | |
| 12 #include "chrome/browser/chrome_notification_types.h" | |
| 13 #include "chrome/browser/chromeos/login/login_display_host_impl.h" | |
| 14 #include "chrome/browser/chromeos/login/login_manager_test.h" | |
| 15 #include "chrome/browser/chromeos/login/startup_utils.h" | |
| 16 #include "chrome/browser/chromeos/login/webui_login_view.h" | |
| 17 #include "chrome/browser/chromeos/net/network_portal_detector_test_impl.h" | |
| 18 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" | |
| 19 #include "chrome/browser/managed_mode/managed_user_registration_utility.h" | |
| 20 #include "chrome/browser/managed_mode/managed_user_registration_utility_stub.h" | |
| 21 #include "chromeos/cryptohome/mock_async_method_caller.h" | |
| 22 #include "chromeos/cryptohome/mock_homedir_methods.h" | |
| 23 #include "content/public/browser/notification_service.h" | |
| 24 #include "content/public/test/browser_test_utils.h" | |
| 25 #include "content/public/test/test_utils.h" | |
| 26 | |
| 27 using testing::_; | |
| 28 | |
| 29 namespace chromeos { | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 const char kStubEthernetServicePath[] = "eth0"; | |
| 34 | |
| 35 const char kTestManager[] = "test-manager@gmail.com"; | |
| 36 const char kTestOtherUser[] = "test-user@gmail.com"; | |
| 37 | |
| 38 const char kTestManagerPassword[] = "password"; | |
| 39 const char kSupervisedUserDisplayName[] = "John Doe"; | |
| 40 const char kSupervisedUserPassword[] = "simplepassword"; | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 class SupervisedUserTest : public chromeos::LoginManagerTest { | |
| 45 protected: | |
| 46 SupervisedUserTest() : LoginManagerTest(true), | |
| 47 mock_async_method_caller_(NULL), | |
| 48 mock_homedir_methods_(NULL), | |
| 49 network_portal_detector_(NULL), | |
| 50 registration_utility_stub_(NULL) { | |
| 51 } | |
| 52 | |
| 53 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | |
| 54 LoginManagerTest::SetUpInProcessBrowserTestFixture(); | |
| 55 mock_async_method_caller_ = new cryptohome::MockAsyncMethodCaller; | |
| 56 mock_async_method_caller_->SetUp(true, cryptohome::MOUNT_ERROR_NONE); | |
| 57 cryptohome::AsyncMethodCaller::InitializeForTesting( | |
| 58 mock_async_method_caller_); | |
| 59 | |
| 60 mock_homedir_methods_ = new cryptohome::MockHomedirMethods; | |
| 61 mock_homedir_methods_->SetUp(true, cryptohome::MOUNT_ERROR_NONE); | |
| 62 cryptohome::HomedirMethods::InitializeForTesting(mock_homedir_methods_); | |
| 63 | |
| 64 registration_utility_stub_ = new ManagedUserRegistrationUtilityStub(); | |
| 65 scoped_utility_.reset( | |
| 66 new ScopedTestingManagedUserRegistrationUtility( | |
| 67 registration_utility_stub_)); | |
| 68 | |
| 69 // Setup network portal detector to return online state for both | |
| 70 // ethernet and wifi networks. Ethernet is an active network by | |
| 71 // default. | |
| 72 network_portal_detector_ = new NetworkPortalDetectorTestImpl(); | |
| 73 NetworkPortalDetector::InitializeForTesting(network_portal_detector_); | |
| 74 NetworkPortalDetector::CaptivePortalState online_state; | |
| 75 online_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE; | |
| 76 online_state.response_code = 204; | |
| 77 network_portal_detector_->SetDefaultNetworkPathForTesting( | |
| 78 kStubEthernetServicePath); | |
| 79 network_portal_detector_->SetDetectionResultsForTesting( | |
| 80 kStubEthernetServicePath, online_state); | |
| 81 } | |
| 82 | |
| 83 virtual void CleanUpOnMainThread() OVERRIDE { | |
| 84 LoginManagerTest::CleanUpOnMainThread(); | |
| 85 scoped_utility_.reset(NULL); | |
| 86 } | |
| 87 | |
| 88 virtual void TearDown() OVERRIDE { | |
| 89 cryptohome::AsyncMethodCaller::Shutdown(); | |
| 90 cryptohome::HomedirMethods::Shutdown(); | |
| 91 mock_homedir_methods_ = NULL; | |
| 92 mock_async_method_caller_ = NULL; | |
| 93 LoginManagerTest::TearDown(); | |
| 94 } | |
| 95 | |
| 96 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE { | |
| 97 NetworkPortalDetector::Shutdown(); | |
| 98 } | |
| 99 | |
| 100 void JSEval(const std::string& script) { | |
| 101 EXPECT_TRUE(content::ExecuteScript(web_contents(), script)); | |
| 102 } | |
| 103 | |
| 104 void JSExpectAsync(const std::string& function) { | |
| 105 bool result; | |
| 106 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | |
| 107 web_contents(), | |
| 108 "(" + function + ")(function() {" | |
| 109 " window.domAutomationController.send(true);" | |
| 110 "});", | |
| 111 &result)); | |
| 112 EXPECT_TRUE(result); | |
| 113 } | |
| 114 | |
| 115 void JSSetTextField(const std::string& element_selector, | |
| 116 const std::string& value) { | |
| 117 std::string function; | |
| 118 function.append("document.querySelector('"); | |
| 119 function.append(element_selector); | |
| 120 function.append("').value = '"); | |
| 121 function.append(value); | |
| 122 function.append("'"); | |
| 123 JSEval(function); | |
| 124 } | |
| 125 | |
| 126 void PrepareUsers(); | |
| 127 void CreateSupervisedUser(); | |
| 128 void SigninAsSupervisedUser(); | |
| 129 void RemoveSupervisedUser(); | |
| 130 void LogInAsManagerAndFillUserData(); | |
| 131 | |
| 132 protected: | |
| 133 cryptohome::MockAsyncMethodCaller* mock_async_method_caller_; | |
| 134 cryptohome::MockHomedirMethods* mock_homedir_methods_; | |
| 135 NetworkPortalDetectorTestImpl* network_portal_detector_; | |
| 136 ManagedUserRegistrationUtilityStub* registration_utility_stub_; | |
| 137 scoped_ptr<ScopedTestingManagedUserRegistrationUtility> scoped_utility_; | |
| 138 | |
| 139 private: | |
| 140 DISALLOW_COPY_AND_ASSIGN(SupervisedUserTest); | |
| 141 }; | |
| 142 | |
| 143 void SupervisedUserTest::PrepareUsers() { | |
| 144 RegisterUser(kTestManager); | |
| 145 RegisterUser(kTestOtherUser); | |
| 146 chromeos::StartupUtils::MarkOobeCompleted(); | |
| 147 } | |
| 148 | |
| 149 void SupervisedUserTest::LogInAsManagerAndFillUserData() { | |
| 150 // Create supervised user. | |
| 151 | |
| 152 // Navigate to supervised user creation screen. | |
| 153 JSEval("chrome.send('showLocallyManagedUserCreationScreen')"); | |
| 154 | |
| 155 // Read intro and proceed. | |
| 156 JSExpect("$('managed-user-creation').currentPage_ == 'intro'"); | |
| 157 | |
| 158 JSEval("$('managed-user-creation-start-button').click()"); | |
| 159 | |
| 160 // Check that both users appear as managers, and test-manager@gmail.com is | |
| 161 // the first one. | |
| 162 JSExpect("$('managed-user-creation').currentPage_ == 'manager'"); | |
| 163 | |
| 164 JSExpect(std::string("document.querySelectorAll(") | |
| 165 .append("'#managed-user-creation-managers-pane .manager-pod.focused')") | |
| 166 .append(".length == 1")); | |
| 167 | |
| 168 JSExpect("$('managed-user-creation').managerList_.pods.length == 2"); | |
| 169 JSExpect(std::string("document.querySelectorAll(") | |
| 170 .append("'#managed-user-creation-managers-pane .manager-pod')") | |
| 171 .append(".length == 2")); | |
| 172 JSExpect(std::string("document.querySelectorAll(") | |
| 173 .append("'#managed-user-creation-managers-pane .manager-pod')") | |
| 174 .append("[0].user.emailAddress == '").append(kTestManager).append("'")); | |
| 175 // Select the first user as manager, and enter password. | |
| 176 JSExpect("$('managed-user-creation-next-button').disabled"); | |
| 177 JSSetTextField( | |
| 178 "#managed-user-creation .manager-pod.focused input", | |
| 179 kTestManagerPassword); | |
| 180 | |
| 181 JSEval("$('managed-user-creation').updateNextButtonForManager_()"); | |
| 182 | |
| 183 // Next button is now enabled. | |
| 184 JSExpect("!$('managed-user-creation-next-button').disabled"); | |
| 185 SetExpectedCredentials(kTestManager, kTestManagerPassword); | |
| 186 content::WindowedNotificationObserver login_observer( | |
| 187 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, | |
| 188 content::NotificationService::AllSources()); | |
| 189 // Log in as manager. | |
| 190 JSEval("$('managed-user-creation-next-button').click()"); | |
| 191 login_observer.Wait(); | |
| 192 // OAuth token is valid. | |
| 193 UserManager::Get()-> | |
| 194 SaveUserOAuthStatus(kTestManager, User::OAUTH2_TOKEN_STATUS_VALID); | |
| 195 base::RunLoop().RunUntilIdle(); | |
| 196 // Enter managed user details. | |
| 197 JSExpect("$('managed-user-creation').currentPage_ == 'username'"); | |
| 198 | |
| 199 JSExpect("$('managed-user-creation-next-button').disabled"); | |
| 200 JSSetTextField( | |
| 201 "#managed-user-creation-name", | |
| 202 kSupervisedUserDisplayName); | |
| 203 JSEval("$('managed-user-creation').checkUserName_()"); | |
| 204 | |
| 205 base::RunLoop().RunUntilIdle(); | |
| 206 | |
| 207 JSSetTextField( | |
| 208 "#managed-user-creation-password", | |
| 209 kSupervisedUserPassword); | |
| 210 JSSetTextField( | |
| 211 "#managed-user-creation-password-confirm", | |
| 212 kSupervisedUserPassword); | |
| 213 | |
| 214 JSEval("$('managed-user-creation').updateNextButtonForUser_()"); | |
| 215 JSExpect("!$('managed-user-creation-next-button').disabled"); | |
| 216 } | |
| 217 | |
| 218 void SupervisedUserTest::CreateSupervisedUser() { | |
| 219 LogInAsManagerAndFillUserData(); | |
| 220 | |
| 221 EXPECT_CALL(*mock_homedir_methods_, MountEx(_, _, _, _)).Times(1); | |
| 222 EXPECT_CALL(*mock_homedir_methods_, AddKeyEx(_, _, _, _, _)).Times(1); | |
| 223 | |
| 224 JSEval("$('managed-user-creation-next-button').click()"); | |
| 225 | |
| 226 testing::Mock::VerifyAndClearExpectations(mock_homedir_methods_); | |
| 227 | |
| 228 EXPECT_TRUE(registration_utility_stub_->register_was_called()); | |
| 229 EXPECT_EQ(registration_utility_stub_->display_name(), | |
| 230 base::UTF8ToUTF16(kSupervisedUserDisplayName)); | |
| 231 | |
| 232 registration_utility_stub_->RunSuccessCallback("token"); | |
| 233 | |
| 234 // Token writing moves control to BlockingPool and back. | |
| 235 base::RunLoop().RunUntilIdle(); | |
| 236 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | |
| 237 base::RunLoop().RunUntilIdle(); | |
| 238 | |
| 239 JSExpect("$('managed-user-creation').currentPage_ == 'created'"); | |
| 240 JSEval("$('managed-user-creation-gotit-button').click()"); | |
| 241 } | |
| 242 | |
| 243 void SupervisedUserTest::SigninAsSupervisedUser() { | |
| 244 EXPECT_CALL(*mock_homedir_methods_, MountEx(_, _, _, _)).Times(1); | |
| 245 // Log in as supervised user, make sure that everything works. | |
| 246 ASSERT_EQ(3UL, UserManager::Get()->GetUsers().size()); | |
| 247 // Created supervised user have to be first in a list. | |
| 248 const User* user = UserManager::Get()->GetUsers().at(0); | |
| 249 ASSERT_EQ(base::UTF8ToUTF16(kSupervisedUserDisplayName), | |
| 250 user->display_name()); | |
| 251 LoginUser(user->email()); | |
| 252 testing::Mock::VerifyAndClearExpectations(mock_homedir_methods_); | |
| 253 } | |
| 254 | |
| 255 void SupervisedUserTest::RemoveSupervisedUser() { | |
| 256 // Remove supervised user. | |
| 257 | |
| 258 ASSERT_EQ(3UL, UserManager::Get()->GetUsers().size()); | |
| 259 // Created supervised user have to be first in a list. | |
| 260 const User* user = UserManager::Get()->GetUsers().at(0); | |
| 261 ASSERT_EQ(base::UTF8ToUTF16(kSupervisedUserDisplayName), | |
| 262 user->display_name()); | |
| 263 | |
| 264 // Open pod menu. | |
| 265 JSExpect("!$('pod-row').pods[0].isActionBoxMenuActive"); | |
| 266 JSEval("$('pod-row').pods[0].querySelector('.action-box-button').click()"); | |
| 267 JSExpect("$('pod-row').pods[0].isActionBoxMenuActive"); | |
| 268 | |
| 269 // Select "Remove user" element. | |
| 270 JSExpect("$('pod-row').pods[0].actionBoxRemoveUserWarningElement.hidden"); | |
| 271 JSEval(std::string("$('pod-row').pods[0].") | |
| 272 .append("querySelector('.action-box-menu-remove').click()")); | |
| 273 JSExpect("!$('pod-row').pods[0].actionBoxRemoveUserWarningElement.hidden"); | |
| 274 | |
| 275 EXPECT_CALL(*mock_async_method_caller_, AsyncRemove(_, _)).Times(1); | |
| 276 | |
| 277 // Confirm deletion. | |
| 278 JSEval(std::string("$('pod-row').pods[0].") | |
| 279 .append("querySelector('.remove-warning-button').click()")); | |
| 280 | |
| 281 // Make sure there is no supervised user in list. | |
| 282 ASSERT_EQ(2UL, UserManager::Get()->GetUsers().size()); | |
| 283 } | |
| 284 | |
| 285 class SupervisedUserCreationTest : public SupervisedUserTest { | |
| 286 public: | |
| 287 SupervisedUserCreationTest() : SupervisedUserTest() {} | |
| 288 | |
| 289 private: | |
| 290 DISALLOW_COPY_AND_ASSIGN(SupervisedUserCreationTest); | |
| 291 }; | |
| 292 | |
| 293 class SupervisedUserTransactionCleanupTest : public SupervisedUserTest { | |
| 294 public: | |
| 295 SupervisedUserTransactionCleanupTest() : SupervisedUserTest () {} | |
| 296 | |
| 297 private: | |
| 298 DISALLOW_COPY_AND_ASSIGN(SupervisedUserTransactionCleanupTest); | |
| 299 }; | |
| 300 | |
| 301 class SupervisedUserOwnerCreationTest : public SupervisedUserTest { | |
| 302 public: | |
| 303 SupervisedUserOwnerCreationTest() : SupervisedUserTest() {} | |
| 304 | |
| 305 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | |
| 306 SupervisedUserTest::SetUpInProcessBrowserTestFixture(); | |
| 307 cros_settings_provider_.reset(new StubCrosSettingsProvider()); | |
| 308 cros_settings_provider_->Set(kDeviceOwner, base::StringValue(kTestManager)); | |
| 309 } | |
| 310 | |
| 311 private: | |
| 312 scoped_ptr<StubCrosSettingsProvider> cros_settings_provider_; | |
| 313 DISALLOW_COPY_AND_ASSIGN(SupervisedUserOwnerCreationTest); | |
| 314 }; | |
| 315 | |
| 316 IN_PROC_BROWSER_TEST_F(SupervisedUserCreationTest, | |
| 317 PRE_PRE_PRE_CreateAndRemoveSupervisedUser) { | |
| 318 PrepareUsers(); | |
| 319 } | |
| 320 | |
| 321 IN_PROC_BROWSER_TEST_F(SupervisedUserCreationTest, | |
| 322 PRE_PRE_CreateAndRemoveSupervisedUser) { | |
| 323 CreateSupervisedUser(); | |
| 324 } | |
| 325 | |
| 326 IN_PROC_BROWSER_TEST_F(SupervisedUserCreationTest, | |
| 327 PRE_CreateAndRemoveSupervisedUser) { | |
| 328 SigninAsSupervisedUser(); | |
| 329 } | |
| 330 | |
| 331 IN_PROC_BROWSER_TEST_F(SupervisedUserCreationTest, | |
| 332 CreateAndRemoveSupervisedUser) { | |
| 333 RemoveSupervisedUser(); | |
| 334 } | |
| 335 | |
| 336 IN_PROC_BROWSER_TEST_F(SupervisedUserOwnerCreationTest, | |
| 337 PRE_PRE_PRE_CreateAndRemoveSupervisedUser) { | |
| 338 PrepareUsers(); | |
| 339 } | |
| 340 | |
| 341 IN_PROC_BROWSER_TEST_F(SupervisedUserOwnerCreationTest, | |
| 342 PRE_PRE_CreateAndRemoveSupervisedUser) { | |
| 343 CreateSupervisedUser(); | |
| 344 } | |
| 345 | |
| 346 IN_PROC_BROWSER_TEST_F(SupervisedUserOwnerCreationTest, | |
| 347 PRE_CreateAndRemoveSupervisedUser) { | |
| 348 SigninAsSupervisedUser(); | |
| 349 } | |
| 350 | |
| 351 IN_PROC_BROWSER_TEST_F(SupervisedUserOwnerCreationTest, | |
| 352 CreateAndRemoveSupervisedUser) { | |
| 353 RemoveSupervisedUser(); | |
| 354 } | |
| 355 | |
| 356 IN_PROC_BROWSER_TEST_F(SupervisedUserTransactionCleanupTest, | |
| 357 PRE_PRE_CreateAndCancelSupervisedUser) { | |
| 358 PrepareUsers(); | |
| 359 } | |
| 360 | |
| 361 IN_PROC_BROWSER_TEST_F(SupervisedUserTransactionCleanupTest, | |
| 362 PRE_CreateAndCancelSupervisedUser) { | |
| 363 LogInAsManagerAndFillUserData(); | |
| 364 | |
| 365 EXPECT_CALL(*mock_homedir_methods_, MountEx(_, _, _, _)).Times(1); | |
| 366 EXPECT_CALL(*mock_homedir_methods_, AddKeyEx(_, _, _, _, _)).Times(1); | |
| 367 | |
| 368 JSEval("$('managed-user-creation-next-button').click()"); | |
| 369 | |
| 370 testing::Mock::VerifyAndClearExpectations(mock_homedir_methods_); | |
| 371 | |
| 372 EXPECT_TRUE(registration_utility_stub_->register_was_called()); | |
| 373 EXPECT_EQ(registration_utility_stub_->display_name(), | |
| 374 base::UTF8ToUTF16(kSupervisedUserDisplayName)); | |
| 375 | |
| 376 std::string user_id = registration_utility_stub_->managed_user_id(); | |
| 377 // Make sure user is already in list. | |
| 378 ASSERT_EQ(3UL, UserManager::Get()->GetUsers().size()); | |
| 379 // We wait for token now. Press cancel button at this point. | |
| 380 JSEval("$('cancel-add-user-button').click()"); | |
| 381 } | |
| 382 | |
| 383 IN_PROC_BROWSER_TEST_F(SupervisedUserTransactionCleanupTest, | |
| 384 CreateAndCancelSupervisedUser) { | |
| 385 // Make sure there is no supervised user in list. | |
| 386 ASSERT_EQ(2UL, UserManager::Get()->GetUsers().size()); | |
| 387 } | |
| 388 | |
| 389 } // namespace chromeos | |
| OLD | NEW |