Fix unit tests

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2021-04-01 10:08:47 +02:00
parent 2d08501be7
commit 2747ad7fd2
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
2 changed files with 64 additions and 23 deletions

View File

@ -434,6 +434,9 @@ class RegisterControllerTest extends TestCase {
$secret = '123456789'; $secret = '123456789';
$token = 'abcdefghi'; $token = 'abcdefghi';
$email = 'nextcloud@example.tld'; $email = 'nextcloud@example.tld';
$fullname = 'Full name';
$phone = '0123 / 456789';
$password = '123456';
$registration = Registration::fromParams([ $registration = Registration::fromParams([
'email' => 'nextcloud@example.tld', 'email' => 'nextcloud@example.tld',
@ -443,11 +446,17 @@ class RegisterControllerTest extends TestCase {
'validateSecretAndToken' 'validateSecretAndToken'
]); ]);
$this->config->method('getAppValue')
->willReturnMap([
['registration', 'show_fullname', 'no', 'yes'],
['registration', 'show_phone', 'no', 'yes'],
]);
$controller->expects($this->once()) $controller->expects($this->once())
->method('validateSecretAndToken') ->method('validateSecretAndToken')
->willReturn($registration); ->willReturn($registration);
$response = $controller->showUserForm($secret, $token, $username, $message); $response = $controller->showUserForm($secret, $token, $username, $fullname, $phone, $password, $message);
self::assertSame(TemplateResponse::RENDER_AS_GUEST, $response->getRenderAs()); self::assertSame(TemplateResponse::RENDER_AS_GUEST, $response->getRenderAs());
self::assertSame('form/user', $response->getTemplateName()); self::assertSame('form/user', $response->getTemplateName());
@ -455,8 +464,13 @@ class RegisterControllerTest extends TestCase {
self::assertSame([ self::assertSame([
'email' => $email, 'email' => $email,
'email_is_login' => false, 'email_is_login' => false,
'username' => $username, 'loginname' => $username,
'fullname' => $fullname,
'show_fullname' => true,
'phone' => $phone,
'show_phone' => true,
'message' => $message, 'message' => $message,
'password' => $password,
'additional_hint' => null, 'additional_hint' => null,
], $response->getParams()); ], $response->getParams());
} }
@ -500,7 +514,7 @@ class RegisterControllerTest extends TestCase {
->method('validateSecretAndTokenErrorPage') ->method('validateSecretAndTokenErrorPage')
->willReturn($response); ->willReturn($response);
self::assertSame($response, $controller->submitUserForm($secret, $token, '', '')); self::assertSame($response, $controller->submitUserForm($secret, $token, '', '', '', ''));
} }
public function testSubmitUserFormCreateAccountException(): void { public function testSubmitUserFormCreateAccountException(): void {
@ -508,6 +522,8 @@ class RegisterControllerTest extends TestCase {
$token = 'abcdefghi'; $token = 'abcdefghi';
$username = 'user'; $username = 'user';
$password = 'password'; $password = 'password';
$fullname = 'Full name';
$phone = '0123 / 456789';
$registration = Registration::fromParams([ $registration = Registration::fromParams([
'email' => 'nextcloud@example.tld', 'email' => 'nextcloud@example.tld',
@ -529,10 +545,10 @@ class RegisterControllerTest extends TestCase {
$this->registrationService->expects($this->once()) $this->registrationService->expects($this->once())
->method('createAccount') ->method('createAccount')
->with($registration, $username, $password) ->with($registration, $username, $fullname, $phone, $password)
->willThrowException(new RegistrationException('Invalid account data')); ->willThrowException(new RegistrationException('Invalid account data'));
self::assertSame($response, $controller->submitUserForm($secret, $token, $username, $password)); self::assertSame($response, $controller->submitUserForm($secret, $token, $username, $fullname, $phone, $password));
} }
public function testSubmitUserFormRequiresAdminApproval(): void { public function testSubmitUserFormRequiresAdminApproval(): void {
@ -540,6 +556,8 @@ class RegisterControllerTest extends TestCase {
$token = 'abcdefghi'; $token = 'abcdefghi';
$username = 'user'; $username = 'user';
$password = 'password'; $password = 'password';
$fullname = 'Full name';
$phone = '0123 / 456789';
$registration = Registration::fromParams([ $registration = Registration::fromParams([
'email' => 'nextcloud@example.tld', 'email' => 'nextcloud@example.tld',
@ -560,14 +578,14 @@ class RegisterControllerTest extends TestCase {
$this->registrationService->expects($this->once()) $this->registrationService->expects($this->once())
->method('createAccount') ->method('createAccount')
->with($registration, $username, $password) ->with($registration, $username, $fullname, $phone, $password)
->willReturn($user); ->willReturn($user);
$this->registrationService->expects($this->once()) $this->registrationService->expects($this->once())
->method('deleteRegistration') ->method('deleteRegistration')
->with($registration); ->with($registration);
$response = $controller->submitUserForm($secret, $token, $username, $password); $response = $controller->submitUserForm($secret, $token, $username, $fullname, $phone, $password);
self::assertInstanceOf(StandaloneTemplateResponse::class, $response); self::assertInstanceOf(StandaloneTemplateResponse::class, $response);
self::assertSame(TemplateResponse::RENDER_AS_GUEST, $response->getRenderAs()); self::assertSame(TemplateResponse::RENDER_AS_GUEST, $response->getRenderAs());
@ -579,6 +597,8 @@ class RegisterControllerTest extends TestCase {
$token = 'abcdefghi'; $token = 'abcdefghi';
$username = 'user'; $username = 'user';
$password = 'password'; $password = 'password';
$fullname = 'Full name';
$phone = '0123 / 456789';
$registration = Registration::fromParams([ $registration = Registration::fromParams([
'email' => 'nextcloud@example.tld', 'email' => 'nextcloud@example.tld',
@ -601,7 +621,7 @@ class RegisterControllerTest extends TestCase {
$this->registrationService->expects($this->once()) $this->registrationService->expects($this->once())
->method('createAccount') ->method('createAccount')
->with($registration, $username, $password) ->with($registration, $username, $fullname, $phone, $password)
->willReturn($user); ->willReturn($user);
$this->registrationService->expects($this->once()) $this->registrationService->expects($this->once())
@ -612,7 +632,7 @@ class RegisterControllerTest extends TestCase {
->method('loginUser') ->method('loginUser')
->with($username, $username, $password); ->with($username, $username, $password);
$response = $controller->submitUserForm($secret, $token, $username, $password); $response = $controller->submitUserForm($secret, $token, $username, $fullname, $phone, $password);
self::assertInstanceOf(RedirectToDefaultAppResponse::class, $response); self::assertInstanceOf(RedirectToDefaultAppResponse::class, $response);
} }
@ -622,6 +642,8 @@ class RegisterControllerTest extends TestCase {
$token = 'abcdefghi'; $token = 'abcdefghi';
$username = 'user'; $username = 'user';
$password = 'password'; $password = 'password';
$fullname = 'Full name';
$phone = '0123 / 456789';
$registration = Registration::fromParams([ $registration = Registration::fromParams([
'email' => 'nextcloud@example.tld', 'email' => 'nextcloud@example.tld',
@ -644,7 +666,7 @@ class RegisterControllerTest extends TestCase {
$this->registrationService->expects($this->once()) $this->registrationService->expects($this->once())
->method('createAccount') ->method('createAccount')
->with($registration, $username, $password) ->with($registration, $username, $fullname, $phone, $password)
->willReturn($user); ->willReturn($user);
$this->registrationService->expects($this->once()) $this->registrationService->expects($this->once())
@ -664,7 +686,7 @@ class RegisterControllerTest extends TestCase {
->with($user) ->with($user)
->willReturn($response); ->willReturn($response);
self::assertSame($response, $controller->submitUserForm($secret, $token, $username, $password)); self::assertSame($response, $controller->submitUserForm($secret, $token, $username, $fullname, $phone, $password));
} }
public function testSubmitUserFormSuccessfulLoginFlow1(): void { public function testSubmitUserFormSuccessfulLoginFlow1(): void {
@ -672,6 +694,8 @@ class RegisterControllerTest extends TestCase {
$token = 'abcdefghi'; $token = 'abcdefghi';
$username = 'user'; $username = 'user';
$password = 'password'; $password = 'password';
$fullname = 'Full name';
$phone = '0123 / 456789';
$registration = Registration::fromParams([ $registration = Registration::fromParams([
'email' => 'nextcloud@example.tld', 'email' => 'nextcloud@example.tld',
@ -694,7 +718,7 @@ class RegisterControllerTest extends TestCase {
$this->registrationService->expects($this->once()) $this->registrationService->expects($this->once())
->method('createAccount') ->method('createAccount')
->with($registration, $username, $password) ->with($registration, $username, $fullname, $phone, $password)
->willReturn($user); ->willReturn($user);
$this->registrationService->expects($this->once()) $this->registrationService->expects($this->once())
@ -715,6 +739,6 @@ class RegisterControllerTest extends TestCase {
$this->loginFlowService->method('tryLoginFlowV1') $this->loginFlowService->method('tryLoginFlowV1')
->willReturn($response); ->willReturn($response);
self::assertSame($response, $controller->submitUserForm($secret, $token, $username, $password)); self::assertSame($response, $controller->submitUserForm($secret, $token, $username, $fullname, $phone, $password));
} }
} }

View File

@ -7,6 +7,7 @@ use OCA\Registration\Db\RegistrationMapper;
use OCA\Registration\Service\MailService; use OCA\Registration\Service\MailService;
use OCA\Registration\Service\RegistrationException; use OCA\Registration\Service\RegistrationException;
use OCA\Registration\Service\RegistrationService; use OCA\Registration\Service\RegistrationService;
use OCP\Accounts\IAccountManager;
use OCP\IConfig; use OCP\IConfig;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\IL10N; use OCP\IL10N;
@ -42,13 +43,15 @@ class RegistrationServiceTest extends TestCase {
private $registrationMapper; private $registrationMapper;
/** @var IUserManager */ /** @var IUserManager */
private $userManager; private $userManager;
/** @var IAccountManager */
private $accountManager;
/** @var IConfig */ /** @var IConfig */
private $config; private $config;
/** @var IGroupManager */ /** @var IGroupManager */
private $groupManager; private $groupManager;
/** @var ISecureRandom */ /** @var ISecureRandom */
private $random; private $random;
/** @var IUserSession */ /** @var IUserSession */
private $usersession; private $usersession;
/** @var IRequest */ /** @var IRequest */
private $request; private $request;
@ -75,6 +78,7 @@ class RegistrationServiceTest extends TestCase {
$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();
$this->accountManager = $this->createMock(IAccountManager::class);
$this->config = $this->createMock(IConfig::class); $this->config = $this->createMock(IConfig::class);
$this->groupManager = \OC::$server->getGroupManager(); $this->groupManager = \OC::$server->getGroupManager();
$this->random = \OC::$server->getSecureRandom(); $this->random = \OC::$server->getSecureRandom();
@ -97,6 +101,7 @@ class RegistrationServiceTest extends TestCase {
$this->urlGenerator, $this->urlGenerator,
$this->registrationMapper, $this->registrationMapper,
$this->userManager, $this->userManager,
$this->accountManager,
$this->config, $this->config,
$this->groupManager, $this->groupManager,
$this->random, $this->random,
@ -219,7 +224,7 @@ class RegistrationServiceTest extends TestCase {
$form_input_username = 'alice1'; $form_input_username = 'alice1';
$resulting_user = $this->service->createAccount($reg, $form_input_username, 'asdf'); $resulting_user = $this->service->createAccount($reg, $form_input_username, 'Full name', '+49 800 / 1110111', 'asdf');
$this->assertInstanceOf(IUser::class, $resulting_user); $this->assertInstanceOf(IUser::class, $resulting_user);
$this->assertEquals($form_input_username, $resulting_user->getUID()); $this->assertEquals($form_input_username, $resulting_user->getUID());
@ -238,8 +243,8 @@ class RegistrationServiceTest extends TestCase {
$reg->setEmailConfirmed(true); $reg->setEmailConfirmed(true);
$this->expectException(RegistrationException::class); $this->expectException(RegistrationException::class);
$this->expectExceptionMessage('The username you have chosen already exists.'); $this->expectExceptionMessage('The login name you have chosen already exists.');
$this->service->createAccount($reg, 'alice1', 'asdf'); $this->service->createAccount($reg, 'alice1', 'Full name', '+49 800 / 1110111', 'asdf');
} }
/* /*
@ -258,12 +263,16 @@ class RegistrationServiceTest extends TestCase {
$reg->setEmail("pppp@example.com"); $reg->setEmail("pppp@example.com");
$reg->setUsername("alice1"); $reg->setUsername("alice1");
$reg->setDisplayname("Alice"); $reg->setDisplayname("Alice");
$reg->setPassword("asdf"); $reg->setPassword("crypto(asdf)");
$reg->setEmailConfirmed(true); $reg->setEmailConfirmed(true);
$this->crypto->method('decrypt')
->with('crypto(asdf)')
->willReturn('asdf');
$this->expectException(RegistrationException::class); $this->expectException(RegistrationException::class);
$this->expectExceptionMessage('The username you have chosen already exists.'); $this->expectExceptionMessage('The login name you have chosen already exists.');
$this->service->createAccount($reg); $this->service->createAccount($reg, null, 'Full name', '+49 800 / 1110111');
} }
/** /**
@ -280,12 +289,16 @@ class RegistrationServiceTest extends TestCase {
$reg->setEmail("pppp@example.com"); $reg->setEmail("pppp@example.com");
$reg->setUsername("alice23"); $reg->setUsername("alice23");
$reg->setDisplayname("Alice"); $reg->setDisplayname("Alice");
$reg->setPassword("asdf"); $reg->setPassword("crypto(asdf)");
$reg->setEmailConfirmed(true); $reg->setEmailConfirmed(true);
$this->crypto->method('decrypt')
->with('crypto(asdf)')
->willReturn('asdf');
$this->expectException(RegistrationException::class); $this->expectException(RegistrationException::class);
$this->expectExceptionMessage('Please provide a valid user name.'); $this->expectExceptionMessage('Please provide a valid login name.');
$this->service->createAccount($reg); $this->service->createAccount($reg, null, 'Full name', '+49 800 / 1110111');
} }
public function settingsCallback1($app, $key, $default) { public function settingsCallback1($app, $key, $default) {
@ -293,6 +306,10 @@ class RegistrationServiceTest extends TestCase {
'registered_user_group' => 'none', 'registered_user_group' => 'none',
'admin_approval_required' => 'no', 'admin_approval_required' => 'no',
'username_policy_regex' => '', 'username_policy_regex' => '',
'show_fullname' => 'yes',
'enforce_fullname' => 'no',
'show_phone' => 'yes',
'enforce_phone' => 'no',
]; ];
return $map[$key]; return $map[$key];