Fix other tests

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2020-07-29 18:06:05 +02:00
parent 906b1f8db2
commit 150e58880e
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
6 changed files with 45 additions and 164 deletions

View File

@ -34,7 +34,6 @@ use OCP\IL10N;
use OCP\IRequest; use OCP\IRequest;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\IConfig; use OCP\IConfig;
use OCP\IL10N;
class RegisterController extends Controller { class RegisterController extends Controller {

View File

@ -215,14 +215,14 @@ class RegistrationService {
* @return bool * @return bool
*/ */
public function checkAllowedDomains(string $email): bool { public function checkAllowedDomains(string $email): bool {
$allowed_domains = $this->config->getAppValue($this->appName, 'allowed_domains', ''); $allowedDomains = $this->config->getAppValue($this->appName, 'allowed_domains', '');
if ($allowed_domains !== '') { if ($allowedDomains !== '') {
$allowed_domains = explode(';', $allowed_domains); $allowedDomains = explode(';', $allowedDomains);
$allowed = false; $allowed = false;
foreach ($allowed_domains as $domain) { foreach ($allowedDomains as $domain) {
$maildomain = explode("@", $email)[1]; [,$mailDomain] = explode('@', $email, 2);
// valid domain, everythings fine // valid domain, everything's fine
if ($maildomain === $domain) { if ($mailDomain === $domain) {
$allowed = true; $allowed = true;
break; break;
} }
@ -236,9 +236,9 @@ class RegistrationService {
* @return string[] * @return string[]
*/ */
public function getAllowedDomains(): array { public function getAllowedDomains(): array {
$allowed_domains = $this->config->getAppValue($this->appName, 'allowed_domains', ''); $allowedDomains = $this->config->getAppValue($this->appName, 'allowed_domains', '');
$allowed_domains = explode(';', $allowed_domains); $allowedDomains = explode(';', $allowedDomains);
return $allowed_domains; return $allowedDomains;
} }
/** /**

View File

@ -1,138 +0,0 @@
<?php
namespace OCA\Registration\Tests\Integration\Controller;
use OCA\Registration\Controller\RegisterController;
use OCA\Registration\Db\RegistrationMapper;
use OCA\Registration\Service\MailService;
use OCA\Registration\Service\RegistrationService;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\ILogger;
use OC\Authentication\Token\IProvider;
use OCP\IRequest;
use OCP\Security\ISecureRandom;
use OCP\Security\ICrypto;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\IUserSession;
use \OCP\AppFramework\Http\TemplateResponse;
use ChristophWurst\Nextcloud\Testing\DatabaseTransaction;
use ChristophWurst\Nextcloud\Testing\TestCase;
/**
* class RegistrationControllerTest
*
* @group DB
*/
class RegisterControllerTest extends TestCase {
use DatabaseTransaction;
/** @var MailService */
private $mailService;
/** @var IL10N */
private $l10n;
/** @var IURLGenerator */
private $urlGenerator;
/** @var RegistrationMapper */
private $registrationMapper;
/** @var IUserManager */
private $userManager;
/** @var IConfig */
private $config;
/** @var IGroupManager */
private $groupManager;
/** @var \OCP\Defaults */
private $defaults;
/** @var ISecureRandom */
private $random;
/** @var IUserSession */
private $usersession;
/** @var IRequest */
private $request;
/** @var ILogger */
private $logger;
/** @var ISession */
private $session;
/** @var IProvider */
private $tokenProvider;
/** @var ICrypto */
private $crypto;
public function setUp(): void {
parent::setUp();
$this->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->registrationService = 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
);
$this->controller = new RegisterController(
'registration',
$this->request,
$this->l10n,
$this->urlGenerator,
$this->config,
$this->registrationService,
$this->mailService
);
}
public function testValidateEmailNormal() {
$email = 'aaaa@example.com';
$this->config->expects($this->atLeastOnce())
->method('getAppValue')
->with("registration", 'allowed_domains', '')
->willReturn('');
$this->mailService->expects($this->once())
->method('sendTokenByMail');
$this->assertEquals($this->registrationService->validateEmail($email), true);
$ret = $this->controller->validateEmail($email);
$expected = new TemplateResponse('registration', 'message', ['msg' =>
$this->l10n->t('Verification email successfully sent.')
], 'guest');
$this->assertEquals($expected, $ret, print_r($ret, true));
}
}

View File

@ -61,6 +61,9 @@ class RegistrationServiceTest extends TestCase {
/** @var ICrypto */ /** @var ICrypto */
private $crypto; private $crypto;
/** @var RegistrationService */
private $service;
public function setUp(): void { public function setUp(): void {
parent::setUp(); parent::setUp();
$this->mailService = $this->createMock(MailService::class); $this->mailService = $this->createMock(MailService::class);
@ -107,13 +110,10 @@ class RegistrationServiceTest extends TestCase {
$this->config->expects($this->once()) $this->config->expects($this->once())
->method('getAppValue') ->method('getAppValue')
->with("registration", 'allowed_domains', '') ->with('registration', 'allowed_domains', '')
->willReturn(''); ->willReturn('');
$ret = $this->service->validateEmail($email); $this->service->validateEmail($email);
//$this->assertInstanceOf(Registration::class, $ret);
$this->assertTrue($ret);
} }
public function testValidateNewEmailWithinAllowedDomain() { public function testValidateNewEmailWithinAllowedDomain() {
@ -121,18 +121,23 @@ class RegistrationServiceTest extends TestCase {
$this->config->expects($this->atLeastOnce()) $this->config->expects($this->atLeastOnce())
->method('getAppValue') ->method('getAppValue')
->with("registration", 'allowed_domains', '') ->with('registration', 'allowed_domains', '')
->willReturn('example.com'); ->willReturn('example.com');
$ret = $this->service->validateEmail($email); $this->service->validateEmail($email);
$this->assertTrue($ret, print_r($ret, true));
} }
/** /**
* @depends testValidateNewEmailWithinAllowedDomain * @depends testValidateNewEmailWithinAllowedDomain
*/ */
public function testValidateNewEmailNotWithinAllowedDomain() { public function testValidateNewEmailNotWithinAllowedDomain() {
$email2 = 'bbbb@gmail.com'; $email2 = 'bbbb@gmail.com';
$this->config->expects($this->atLeastOnce())
->method('getAppValue')
->with('registration', 'allowed_domains', '')
->willReturn('example.com');
$this->expectException(RegistrationException::class); $this->expectException(RegistrationException::class);
$this->service->validateEmail($email2); $this->service->validateEmail($email2);
} }
@ -143,18 +148,24 @@ class RegistrationServiceTest extends TestCase {
$this->config->expects($this->atLeastOnce()) $this->config->expects($this->atLeastOnce())
->method('getAppValue') ->method('getAppValue')
->with("registration", 'allowed_domains', '') ->with('registration', 'allowed_domains', '')
->willReturn('example.com;gmail.com'); ->willReturn('example.com;gmail.com');
$this->assertTrue($this->service->validateEmail($email)); $this->service->validateEmail($email);
$this->assertTrue($this->service->validateEmail($email2)); $this->service->validateEmail($email2);
} }
/** /**
* @depends testValidateNewEmailWithinMultipleAllowedDomain * @depends testValidateNewEmailWithinMultipleAllowedDomain
*/ */
public function testValidateNewEmailNotWithinMultipleAllowedDomain() { public function testValidateNewEmailNotWithinMultipleAllowedDomain() {
$email2 = 'cccc@yahoo.com'; $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->expectException(RegistrationException::class);
$this->service->validateEmail($email2); $this->service->validateEmail($email2);
} }
@ -180,10 +191,8 @@ class RegistrationServiceTest extends TestCase {
$email = 'aaaa@example.com'; $email = 'aaaa@example.com';
$this->service->createRegistration($email, 'alice'); $this->service->createRegistration($email, 'alice');
$ret = $this->service->validateEmail($email); $this->expectException(RegistrationException::class);
$this->service->validateEmail($email);
$this->assertInstanceOf(Registration::class, $ret);
$this->assertEquals($email, $ret->getEmail());
} }
public function testCreateAccountWebForm() { public function testCreateAccountWebForm() {

View File

@ -157,7 +157,11 @@ class ApiControllerTest extends TestCase {
$registration = new Registration(); $registration = new Registration();
$registration->setEmailConfirmed(true); $registration->setEmailConfirmed(true);
$registration->setClientSecret('mysecret'); $registration->setClientSecret('mysecret');
$registration->setUsername('user');
$registration->setPassword('password');
$user = $this->createMock(IUser::class); $user = $this->createMock(IUser::class);
$user->method('getUID')
->willReturn('user');
$this->registrationService $this->registrationService
->method('getRegistrationForSecret') ->method('getRegistrationForSecret')
->with('mysecret') ->with('mysecret')

View File

@ -35,6 +35,7 @@ use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\RedirectToDefaultAppResponse; use OCP\AppFramework\Http\RedirectToDefaultAppResponse;
use OCP\AppFramework\Http\StandaloneTemplateResponse; use OCP\AppFramework\Http\StandaloneTemplateResponse;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use OCP\IL10N; use OCP\IL10N;
use OCP\IRequest; use OCP\IRequest;
use OCP\IURLGenerator; use OCP\IURLGenerator;
@ -50,6 +51,8 @@ class RegisterControllerTest extends TestCase {
private $l10n; private $l10n;
/** @var IURLGenerator|MockObject */ /** @var IURLGenerator|MockObject */
private $urlGenerator; private $urlGenerator;
/** @var IConfig|MockObject */
private $config;
/** @var RegistrationService|MockObject */ /** @var RegistrationService|MockObject */
private $registrationService; private $registrationService;
/** @var LoginFlowService|MockObject */ /** @var LoginFlowService|MockObject */
@ -62,6 +65,7 @@ class RegisterControllerTest extends TestCase {
$this->request = $this->createMock(IRequest::class); $this->request = $this->createMock(IRequest::class);
$this->l10n = $this->createMock(IL10N::class); $this->l10n = $this->createMock(IL10N::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class); $this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->config = $this->createMock(IConfig::class);
$this->registrationService = $this->createMock(RegistrationService::class); $this->registrationService = $this->createMock(RegistrationService::class);
$this->loginFlowService = $this->createMock(LoginFlowService::class); $this->loginFlowService = $this->createMock(LoginFlowService::class);
$this->mailService = $this->createMock(MailService::class); $this->mailService = $this->createMock(MailService::class);
@ -84,6 +88,7 @@ class RegisterControllerTest extends TestCase {
$this->request, $this->request,
$this->l10n, $this->l10n,
$this->urlGenerator, $this->urlGenerator,
$this->config,
$this->registrationService, $this->registrationService,
$this->loginFlowService, $this->loginFlowService,
$this->mailService $this->mailService
@ -97,6 +102,7 @@ class RegisterControllerTest extends TestCase {
$this->request, $this->request,
$this->l10n, $this->l10n,
$this->urlGenerator, $this->urlGenerator,
$this->config,
$this->registrationService, $this->registrationService,
$this->loginFlowService, $this->loginFlowService,
$this->mailService, $this->mailService,
@ -446,6 +452,7 @@ class RegisterControllerTest extends TestCase {
self::assertSame([ self::assertSame([
'email' => $email, 'email' => $email,
'email_is_login' => false,
'username' => $username, 'username' => $username,
'message' => $message, 'message' => $message,
], $response->getParams()); ], $response->getParams());