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,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) {

View File

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

View File

@ -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];