From fbb0b22155352eb81f0d8b001b3659d7b61351fa Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 14 Dec 2020 11:49:05 +0100 Subject: [PATCH] Fix and add new unit tests Signed-off-by: Joas Schilling --- lib/Service/RegistrationService.php | 6 +-- .../Controller/RegisterControllerTest.php | 1 + .../Unit/Service/RegistrationServiceTest.php | 38 +++++++++++++++++-- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/lib/Service/RegistrationService.php b/lib/Service/RegistrationService.php index 025e9ed..6d9ff14 100644 --- a/lib/Service/RegistrationService.php +++ b/lib/Service/RegistrationService.php @@ -234,10 +234,8 @@ class RegistrationService { } $regex = $this->config->getAppValue($this->appName, 'username_policy_regex', ''); - if (!($regex === '')) { - if (preg_match($regex, $username) === 0) { - throw new RegistrationException($this->l10n->t('Please provide a valid user name.')); - } + if ($regex && preg_match($regex, $username) === 0) { + throw new RegistrationException($this->l10n->t('Please provide a valid user name.')); } if ($this->registrationMapper->usernameIsPending($username) || $this->userManager->get($username) !== null) { diff --git a/tests/Unit/Controller/RegisterControllerTest.php b/tests/Unit/Controller/RegisterControllerTest.php index 25ea874..5b9324c 100644 --- a/tests/Unit/Controller/RegisterControllerTest.php +++ b/tests/Unit/Controller/RegisterControllerTest.php @@ -457,6 +457,7 @@ class RegisterControllerTest extends TestCase { 'email_is_login' => false, 'username' => $username, 'message' => $message, + 'additional_hint' => null, ], $response->getParams()); } diff --git a/tests/Unit/Service/RegistrationServiceTest.php b/tests/Unit/Service/RegistrationServiceTest.php index 7994d07..7a819cc 100644 --- a/tests/Unit/Service/RegistrationServiceTest.php +++ b/tests/Unit/Service/RegistrationServiceTest.php @@ -67,6 +67,11 @@ class RegistrationServiceTest extends TestCase { parent::setUp(); $this->mailService = $this->createMock(MailService::class); $this->l10n = $this->createMock(IL10N::class); + $this->l10n->expects($this->any()) + ->method('t') + ->willReturnCallback(function ($text, $parameters = []) { + return vsprintf($text, $parameters); + }); $this->urlGenerator = $this->createMock(IURLGenerator::class); #$this->userManager = $this->createMock(IUserManager::class); $this->userManager = \OC::$server->getUserManager(); @@ -233,7 +238,8 @@ class RegistrationServiceTest extends TestCase { $reg->setEmailConfirmed(true); $this->expectException(RegistrationException::class); - $resulting_user = $this->service->createAccount($reg, 'alice1', 'asdf'); + $this->expectExceptionMessage('The username you have chosen already exists.'); + $this->service->createAccount($reg, 'alice1', 'asdf'); } /* @@ -256,13 +262,39 @@ class RegistrationServiceTest extends TestCase { $reg->setEmailConfirmed(true); $this->expectException(RegistrationException::class); - $resulting_user = $this->service->createAccount($reg); + $this->expectExceptionMessage('The username you have chosen already exists.'); + $this->service->createAccount($reg); + } + + /** + * @depends testDuplicateUsernameApi + */ + public function testUsernameDoesntMatchPattern() { + + + $this->config->expects($this->atLeastOnce()) + ->method('getAppValue') + ->willReturnMap([ + ['registration', 'username_policy_regex', '', '/^[a-z]\.[a-z]+$/'], + ]); + + $reg = new Registration(); + $reg->setEmail("pppp@example.com"); + $reg->setUsername("alice23"); + $reg->setDisplayname("Alice"); + $reg->setPassword("asdf"); + $reg->setEmailConfirmed(true); + + $this->expectException(RegistrationException::class); + $this->expectExceptionMessage('Please provide a valid user name.'); + $this->service->createAccount($reg); } public function settingsCallback1($app, $key, $default) { $map = [ 'registered_user_group' => 'none', - 'admin_approval_required' => 'no' + 'admin_approval_required' => 'no', + 'username_policy_regex' => '', ]; return $map[$key];