Fix and add new unit tests

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2020-12-14 11:49:05 +01:00
parent 378f4e78fc
commit fbb0b22155
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
3 changed files with 38 additions and 7 deletions

View File

@ -234,11 +234,9 @@ class RegistrationService {
} }
$regex = $this->config->getAppValue($this->appName, 'username_policy_regex', ''); $regex = $this->config->getAppValue($this->appName, 'username_policy_regex', '');
if (!($regex === '')) { if ($regex && preg_match($regex, $username) === 0) {
if (preg_match($regex, $username) === 0) {
throw new RegistrationException($this->l10n->t('Please provide a valid user name.')); throw new RegistrationException($this->l10n->t('Please provide a valid user name.'));
} }
}
if ($this->registrationMapper->usernameIsPending($username) || $this->userManager->get($username) !== null) { if ($this->registrationMapper->usernameIsPending($username) || $this->userManager->get($username) !== null) {
throw new RegistrationException($this->l10n->t('The username you have chosen already exists.')); throw new RegistrationException($this->l10n->t('The username you have chosen already exists.'));

View File

@ -457,6 +457,7 @@ class RegisterControllerTest extends TestCase {
'email_is_login' => false, 'email_is_login' => false,
'username' => $username, 'username' => $username,
'message' => $message, 'message' => $message,
'additional_hint' => null,
], $response->getParams()); ], $response->getParams());
} }

View File

@ -67,6 +67,11 @@ class RegistrationServiceTest extends TestCase {
parent::setUp(); parent::setUp();
$this->mailService = $this->createMock(MailService::class); $this->mailService = $this->createMock(MailService::class);
$this->l10n = $this->createMock(IL10N::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->urlGenerator = $this->createMock(IURLGenerator::class);
#$this->userManager = $this->createMock(IUserManager::class); #$this->userManager = $this->createMock(IUserManager::class);
$this->userManager = \OC::$server->getUserManager(); $this->userManager = \OC::$server->getUserManager();
@ -233,7 +238,8 @@ class RegistrationServiceTest extends TestCase {
$reg->setEmailConfirmed(true); $reg->setEmailConfirmed(true);
$this->expectException(RegistrationException::class); $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); $reg->setEmailConfirmed(true);
$this->expectException(RegistrationException::class); $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) { public function settingsCallback1($app, $key, $default) {
$map = [ $map = [
'registered_user_group' => 'none', 'registered_user_group' => 'none',
'admin_approval_required' => 'no' 'admin_approval_required' => 'no',
'username_policy_regex' => '',
]; ];
return $map[$key]; return $map[$key];