add unit tests

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
Arthur Schiwon 2018-06-14 13:01:42 +02:00
parent 9a87c6728b
commit d10183c76c
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
6 changed files with 458 additions and 13 deletions

View File

@ -52,15 +52,9 @@ class DataRequestController extends OCSController {
* @PasswordConfirmationRequired
*/
public function export() {
try {
return $this->processRequest(function() {
$this->dataRequest->sendExportRequest();
return new DataResponse();
} catch(HintedRuntime $e) {
return new DataResponse(
['error' => $e->getHint()],
Http::STATUS_INTERNAL_SERVER_ERROR
);
}
});
}
/**
@ -68,8 +62,14 @@ class DataRequestController extends OCSController {
* @PasswordConfirmationRequired
*/
public function deletion() {
try {
return $this->processRequest(function() {
$this->dataRequest->sendDeleteRequest();
});
}
protected function processRequest(callable $serviceMethod) {
try {
$serviceMethod();
return new DataResponse();
} catch(HintedRuntime $e) {
return new DataResponse(

View File

@ -50,14 +50,25 @@ class Request {
private $requester;
/** @var IL10N */
private $l;
/** @var Defaults */
private $defaults;
public function __construct(IGroupManager $groupManager, IMailer $mailer, IFactory $l10nFactory, IConfig $config, IUserSession $userSession, IL10N $l) {
public function __construct(
IGroupManager $groupManager,
IMailer $mailer,
IFactory $l10nFactory,
IConfig $config,
IUserSession $userSession,
IL10N $l,
Defaults $defaults
) {
$this->groupManager = $groupManager;
$this->mailer = $mailer;
$this->l10nFactory = $l10nFactory;
$this->config = $config;
$this->requester = $userSession->getUser();
$this->l = $l;
$this->defaults = $defaults;
}
public function sendExportRequest() {
@ -95,12 +106,11 @@ class Request {
}
protected function craftEmailTo(IUser $admin, IEMailTemplate $template) {
$defaults = new Defaults();
$senderAddress = Util::getDefaultEmailAddress('no-reply');
$senderName = $defaults->getName();
$senderName = $this->defaults->getName();
$message = $this->mailer->createMessage();
$message->setTo([$admin->getEMailAddress() => $admin->getDisplayName()]);
$message->setTo([$admin->getEMailAddress () => $admin->getDisplayName()]);
$message->setSubject($template->renderSubject());
$message->setHtmlBody($template->renderHtml());
$message->setPlainBody($template->renderText());

View File

@ -0,0 +1,120 @@
<?php
/**
* @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\DataRequest\Tests\unit\Controller;
use OCA\DataRequest\Controller\DataRequestController;
use OCA\DataRequest\Exceptions\HintedRuntime;
use OCA\DataRequest\Services\Request;
use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest;
class DataRequestControllerTest extends \Test\TestCase {
/** @var Request|\PHPUnit_Framework_MockObject_MockObject */
protected $requestService;
/** @var DataRequestController */
protected $controller;
public function setUp() {
parent::setUp();
$this->requestService = $this->createMock(Request::class);
/** @var IRequest $request */
$request = $this->createMock(IRequest::class);
$this->controller = new DataRequestController(
'data_request',
$request,
'PUT, POST, GET, DELETE, PATCH',
'Authorization, Content-Type, Accept',
1728000,
$this->requestService
);
}
public function processDataProvider() {
return [
[
'sendExportRequest',
true,
500
],
[
'sendExportRequest',
false,
200
],
[
'sendDeleteRequest',
true,
500
],
[
'sendDeleteRequest',
false,
200
]
];
}
/**
* @dataProvider processDataProvider
* @param string $serviceMethod
* @param bool $causeException
* @param int $expectStatus
*/
public function testRequests($serviceMethod, $causeException, $expectStatus) {
$mocker = $this->requestService->expects($this->once())
->method($serviceMethod);
if($causeException) {
/** @var HintedRuntime|\PHPUnit_Framework_MockObject_MockObject $exception */
$exception = $this->createMock(HintedRuntime::class);
$exception->expects($this->once())
->method('getHint')
->willReturn('Some hint');
$mocker->willThrowException($exception);
}
switch($serviceMethod) {
case 'sendExportRequest':
$response = $this->controller->export();
break;
case 'sendDeleteRequest':
$response = $this->controller->deletion();
break;
default:
throw new \RuntimeException('No valid test case');
}
$this->assertInstanceOf(DataResponse::class, $response);
$this->assertSame($expectStatus, $response->getStatus());
if($expectStatus >= 500) {
$this->assertSame('Some hint', $response->getData()['error']);
}
}
}

View File

@ -0,0 +1,261 @@
<?php
use OCA\DataRequest\Exceptions\HintedRuntime;
use OCA\DataRequest\Services\Request;
use OCP\Defaults;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IUser;
use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Mail\IEMailTemplate;
use OCP\Mail\IMailer;
/**
* @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
class RequestTest extends \Test\TestCase {
/** @var IGroupManager|PHPUnit_Framework_MockObject_MockObject */
protected $groupManager;
/** @var IMailer|PHPUnit_Framework_MockObject_MockObject */
protected $mailer;
/** @var IFactory|PHPUnit_Framework_MockObject_MockObject */
protected $l10nFactory;
/** @var IConfig|PHPUnit_Framework_MockObject_MockObject */
protected $config;
/** @var IUserSession|PHPUnit_Framework_MockObject_MockObject */
protected $session;
/** @var IL10N|PHPUnit_Framework_MockObject_MockObject */
protected $l;
/** @var Request */
protected $service;
/** @var IUser|PHPUnit_Framework_MockObject_MockObject */
protected $user;
/** @var Defaults|PHPUnit_Framework_MockObject_MockObject */
protected $defaults;
public function setUp() {
parent::setUp();
$this->groupManager = $this->createMock(IGroupManager::class);
$this->mailer = $this->createMock(IMailer::class);
$this->l10nFactory = $this->createMock(IFactory::class);
$this->config = $this->createMock(IConfig::class);
$this->user = $this->createMock(IUser::class);
$this->session = $this->createMock(IUserSession::class);
$this->session->expects($this->any())
->method('getUser')
->willReturn($this->user);
$this->l = $this->createMock(IL10N::class);
$this->defaults = $this->createMock(Defaults::class);
$this->service = new Request(
$this->groupManager,
$this->mailer,
$this->l10nFactory,
$this->config,
$this->session,
$this->l,
$this->defaults
);
}
public function templateProvider() {
return [
[
'getExportTemplate',
'data_request.Export',
'Personal data export request'
],
[
'getDeletionTemplate',
'data_request.Deletion',
'Account deletion request'
]
];
}
/**
* @dataProvider templateProvider
* @param $method
* @param $templateId
* @param $expectedSubject
*/
public function testTemplates($method, $templateId, $expectedSubject) {
$adminUid = 'elu-thingol';
$adminName = 'Elu Thingol';
$adminLang = 'qya';
$admin = $this->createMock(IUser::class);
$admin->expects($this->any())
->method('getUID')
->willReturn($adminUid);
$admin->expects($this->any())
->method('getDisplayName')
->willReturn($adminName);
$this->config->expects($this->atLeastOnce())
->method('getSystemValue')
->with('default_language')
->willReturn('tlh');
$this->config->expects($this->atLeastOnce())
->method('getUserValue')
->with($adminUid, 'core', 'lang', 'tlh')
->willReturn($adminLang);
$l = $this->createMock(IL10N::class);
$l->expects($this->atLeast(3))
->method('t')
->willReturnArgument(0);
$this->l10nFactory->expects($this->once())
->method('get')
->with('data_request', $adminLang)
->willReturn($l);
$template = $this->createMock(IEMailTemplate::class);
$template->expects($this->once())
->method('setSubject')
->with($expectedSubject);
$template->expects($this->once())
->method('addHeader');
$template->expects($this->once())
->method('addHeading');
$template->expects($this->once())
->method('addBodyText');
$template->expects($this->once())
->method('addFooter');
$this->mailer->expects($this->once())
->method('createEMailTemplate')
->with($templateId, [])
->willReturn($template);
$result = $this->invokePrivate($this->service, $method, [$admin]);
$this->assertSame($template, $result);
}
public function adminProvider() {
$admin1 = $this->createMock(IUser::class);
$admin2 = $this->createMock(IUser::class);
$admin2->expects($this->any())
->method('getEMailAddress')
->willReturn('admin2@sindar.gov');
$admin3 = $this->createMock(IUser::class);
$admin3->expects($this->any())
->method('getEMailAddress')
->willReturn('admin3@sindar.gov');
return [
[
[ $admin1 ],
0
],
[
[ $admin2 ],
1
],
[
[ $admin1, $admin2, $admin3 ],
2
]
];
}
/**
* @dataProvider adminProvider
* @param $admins
* @param $adminsWithEmail
*/
public function testGetAdmins($admins, $adminsWithEmail) {
$adminGroup = $this->createMock(IGroup::class);
$adminGroup->expects($this->once())
->method('searchUsers')
->with('')
->willReturn($admins);
$this->groupManager->expects($this->once())
->method('get')
->with('admin')
->willReturn($adminGroup);
if($adminsWithEmail === 0) {
$this->expectException(HintedRuntime::class);
}
$result = $this->invokePrivate($this->service, 'getAdmins');
$this->assertSame($adminsWithEmail, count($result));
}
public function testSendMail() {
$adminName = 'Elu Thingol';
$adminMail = 'elu-thingol@sindar.gov';
$admin = $this->createMock(IUser::class);
$admin->expects($this->any())
->method('getEMailAddress')
->willReturn($adminMail);
$admin->expects($this->any())
->method('getDisplayName')
->willReturn($adminName);
$template = $this->createMock(IEMailTemplate::class);
$template->expects($this->once())
->method('renderSubject');
$template->expects($this->once())
->method('renderHtml');
$template->expects($this->once())
->method('renderText');
$message = $this->createMock(\OC\Mail\Message::class);
$message->expects($this->once())
->method('setTo')
->with([$adminMail => $adminName]);
$message->expects($this->once())
->method('setSubject');
$message->expects($this->once())
->method('setHtmlBody');
$message->expects($this->once())
->method('setPlainBody');
$message->expects($this->once())
->method('setFrom');
$this->mailer->expects($this->once())
->method('createMessage')
->willReturn($message);
$this->mailer->expects($this->once())
->method('send')
->with($message);
$this->defaults->expects($this->atLeastOnce())
->method('getName')
->willReturn('Cloud of Sindar');
$result = $this->invokePrivate($this->service, 'craftEmailTo', [$admin, $template]);
$this->assertTrue($result);
}
}

31
tests/unit/bootstrap.php Normal file
View File

@ -0,0 +1,31 @@
<?php
/**
* @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
if (!defined('PHPUNIT_RUN')) {
define('PHPUNIT_RUN', 1);
}
require_once __DIR__.'/../../../../lib/base.php';
\OC::$loader->addValidRoot(\OC::$SERVERROOT . '/tests');
\OC_App::loadApp('data_request');
if(!class_exists('PHPUnit_Framework_TestCase')) {
require_once('PHPUnit/Autoload.php');
}
OC_Hook::clear();

23
tests/unit/phpunit.xml Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpunit bootstrap="bootstrap.php"
strict="true"
verbose="true"
timeoutForSmallTests="900"
timeoutForMediumTests="900"
timeoutForLargeTests="900"
>
<testsuite name='Data Request App Tests'>
<directory suffix='Test.php'>.</directory>
</testsuite>
<!-- filters for code coverage -->
<filter>
<whitelist>
<directory suffix=".php">../../../data_request/appinfo</directory>
<directory suffix=".php">../../../data_request/lib</directory>
</whitelist>
</filter>
<logging>
<!-- and this is where your report will be written -->
<log type="coverage-clover" target="./clover.xml"/>
</logging>
</phpunit>