mailService = $this->createMock(MailService::class); $this->l10n = $this->createMock(IL10N::class); $this->urlGenerator = $this->createMock(IURLGenerator::class); #$this->userManager = $this->createMock(IUserManager::class); $this->userManager = \OC::$server->getUserManager(); $this->config = $this->createMock(IConfig::class); $this->groupManager = \OC::$server->getGroupManager(); $this->random = \OC::$server->getSecureRandom(); $this->usersession = $this->createMock(IUserSession::class); $this->request = $this->createMock(IRequest::class); $this->logger = $this->createMock(ILogger::class); $this->session = $this->createMock(ISession::class); $this->tokenProvider = $this->createMock(IProvider::class); $this->crypto = $this->createMock(ICrypto::class); $this->registrationMapper = new RegistrationMapper( \OC::$server->getDatabaseConnection(), $this->random ); $this->service = new RegistrationService( 'registration', $this->mailService, $this->l10n, $this->urlGenerator, $this->registrationMapper, $this->userManager, $this->config, $this->groupManager, $this->random, $this->usersession, $this->request, $this->logger, $this->session, $this->tokenProvider, $this->crypto ); } public function dataValidateEmail(): array { return [ ['aaaa@example.com', ''], ['aaaa@example.com', 'example.com'], ['aaaa@example.com', 'eXample.com'], ['aaaa@eXample.com', 'example.com'], ]; } /** * @dataProvider dataValidateEmail * @param string $email * @param string $allowedDomains * @throws RegistrationException */ public function testValidateEmail(string $email, string $allowedDomains) { $this->config->expects($this->once()) ->method('getAppValue') ->with('registration', 'allowed_domains', '') ->willReturn($allowedDomains); $this->service->validateEmail($email); } public function testValidateNewEmailNotWithinAllowedDomain() { $email2 = 'bbbb@gmail.com'; $this->config->expects($this->atLeastOnce()) ->method('getAppValue') ->with('registration', 'allowed_domains', '') ->willReturn('example.com'); $this->expectException(RegistrationException::class); $this->service->validateEmail($email2); } public function testValidateNewEmailWithinMultipleAllowedDomain() { $email = 'aaaa@example.com'; $email2 = 'bbbb@gmail.com'; $this->config->expects($this->atLeastOnce()) ->method('getAppValue') ->with('registration', 'allowed_domains', '') ->willReturn('example.com;gmail.com'); $this->service->validateEmail($email); $this->service->validateEmail($email2); } /** * @depends testValidateNewEmailWithinMultipleAllowedDomain */ public function testValidateNewEmailNotWithinMultipleAllowedDomain() { $email2 = 'cccc@yahoo.com'; $this->config->expects($this->atLeastOnce()) ->method('getAppValue') ->with('registration', 'allowed_domains', '') ->willReturn('example.com;gmail.com'); $this->expectException(RegistrationException::class); $this->service->validateEmail($email2); } public function testCreatePendingReg() { $email = 'aaaa@example.com'; $actual = $this->service->createRegistration($email); $expected = new Registration(); $expected->setEmail($email); $expected->setUsername(''); $expected->setDisplayname(''); $this->registrationMapper->generateNewToken($expected); $this->assertEquals($expected->getEmail(), $actual->getEmail()); $indb = $this->registrationMapper->find($email); $this->assertEquals($expected->getEmail(), $indb->getEmail()); } public function testValidatePendingReg() { $email = 'aaaa@example.com'; $this->service->createRegistration($email, 'alice'); $this->expectException(RegistrationException::class); $this->service->validateEmail($email); } public function testCreateAccountWebForm() { $reg = new Registration(); $reg->setEmail("asd@example.com"); //$reg->setUsername("alice1"); $reg->setDisplayname("Alice"); //$reg->setPassword("asdf"); $reg->setEmailConfirmed(true); $this->config->expects($this->atLeastOnce()) ->method('getAppValue') ->will($this->returnCallback([$this, 'settingsCallback1'])); $form_input_username = 'alice1'; $resulting_user = $this->service->createAccount($reg, $form_input_username, 'asdf'); $this->assertInstanceOf(IUser::class, $resulting_user); $this->assertEquals($form_input_username, $resulting_user->getUID()); $this->assertEquals('asd@example.com', $resulting_user->getEmailAddress()); } /** * @depends testCreateAccountWebForm */ public function testDuplicateUsernameWebForm() { $reg = new Registration(); $reg->setEmail("pppp@example.com"); //$reg->setUsername("alice1"); $reg->setDisplayname("Alice"); //$reg->setPassword("asdf"); $reg->setEmailConfirmed(true); $this->expectException(RegistrationException::class); $resulting_user = $this->service->createAccount($reg, 'alice1', 'asdf'); } /* * NOTE * We don't need to test for duplicate emails here, because: * In Webform, emails are validated not to be duplicate in validateEmail(), * that is, when users first fill in their email * In API, they are also validated in ApiControllerTest::validate() */ /** * @depends testCreateAccountWebForm */ public function testDuplicateUsernameApi() { $reg = new Registration(); $reg->setEmail("pppp@example.com"); $reg->setUsername("alice1"); $reg->setDisplayname("Alice"); $reg->setPassword("asdf"); $reg->setEmailConfirmed(true); $this->expectException(RegistrationException::class); $resulting_user = $this->service->createAccount($reg); } public function settingsCallback1($app, $key, $default) { $map = [ 'registered_user_group' => 'none', 'admin_approval_required' => 'no' ]; return $map[$key]; } }