From f4521a9d29e23225b86c6ca53f2fecd4fbfae4c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Fri, 30 Jun 2017 15:54:29 +0200 Subject: [PATCH] Add basic unit testing and tests for API controller MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- phpunit.xml | 13 +- tests/autoloader.php | 42 +---- tests/unit/controller/ApiControllerTest.php | 174 +++++++++++++++++++ tests/unit/controller/PageControllerTest.php | 53 ------ 4 files changed, 187 insertions(+), 95 deletions(-) create mode 100644 tests/unit/controller/ApiControllerTest.php delete mode 100644 tests/unit/controller/PageControllerTest.php diff --git a/phpunit.xml b/phpunit.xml index 7e5a983..aff4ae0 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1 +1,12 @@ - \ No newline at end of file + + + + ./tests/unit + + + + + ./ + + + diff --git a/tests/autoloader.php b/tests/autoloader.php index f62e632..ffaf089 100644 --- a/tests/autoloader.php +++ b/tests/autoloader.php @@ -9,44 +9,4 @@ * @copyright Pellaeon Lin 2014 */ -require_once __DIR__ . '/../../../3rdparty/Pimple/Pimple.php'; - - -class OC { - public static $server; - public static $session; -} - -// to execute without owncloud, we need to create our own classloader -spl_autoload_register(function ($className){ - if (strpos($className, 'OCA\\') === 0) { - - $path = strtolower(str_replace('\\', '/', substr($className, 3)) . '.php'); - $relPath = __DIR__ . '/../..' . $path; - - if(file_exists($relPath)){ - require_once $relPath; - } - } else if(strpos($className, 'OCP\\') === 0) { - $path = strtolower(str_replace('\\', '/', substr($className, 3)) . '.php'); - $relPath = __DIR__ . '/../../../lib/public' . $path; - - if(file_exists($relPath)){ - require_once $relPath; - } - } else if(strpos($className, 'OC_') === 0) { - $path = strtolower(str_replace('\\', '/', substr($className, 3)) . '.php'); - $relPath = __DIR__ . '/../../../lib/private/' . $path; - - if(file_exists($relPath)){ - require_once $relPath; - } - } else if(strpos($className, 'OC\\') === 0) { - $path = strtolower(str_replace('\\', '/', substr($className, 2)) . '.php'); - $relPath = __DIR__ . '/../../../lib/private' . $path; - - if(file_exists($relPath)){ - require_once $relPath; - } - } -}); \ No newline at end of file +require_once __DIR__ . '/../../../tests/bootstrap.php'; diff --git a/tests/unit/controller/ApiControllerTest.php b/tests/unit/controller/ApiControllerTest.php new file mode 100644 index 0000000..d16a43b --- /dev/null +++ b/tests/unit/controller/ApiControllerTest.php @@ -0,0 +1,174 @@ + + * @copyright Pellaeon Lin 2014 + */ + +namespace OCA\Registration\Controller; + +use OCA\Registration\Db\Registration; +use OCA\Registration\Service\MailService; +use OCA\Registration\Service\RegistrationService; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCS\OCSException; +use OCP\AppFramework\OCS\OCSNotFoundException; +use OCP\Defaults; +use OCP\IL10N; +use OCP\IRequest; +use OCP\IUser; +use \Test\TestCase; + +class ApiControllerTest extends TestCase { + + /** @var IRequest */ + private $request; + /** @var RegistrationService|\PHPUnit_Framework_MockObject_MockObject */ + private $registrationService; + /** @var MailService */ + private $mailService; + /** @var IL10N */ + private $l10n; + /** @var Defaults */ + private $defaults; + /** @var ApiController */ + private $controller; + + public function setUp () { + parent::setUp(); + $this->request = $this->createMock(IRequest::class); + $this->registrationService = $this->createMock(RegistrationService::class); + $this->mailService = $this->createMock(MailService::class); + $this->l10n = $this->createMock(IL10N::class); + $this->defaults = $this->createMock(Defaults::class); + $this->controller = new ApiController( + "registration", + $this->request, + $this->registrationService, + $this->mailService, + $this->l10n, + $this->defaults + ); + } + + public function testValidate() { + $this->registrationService + ->expects($this->once()) + ->method('validateEmail') + ->with('test@example.com'); + $this->registrationService + ->expects($this->once()) + ->method('validateDisplayname') + ->with('user test'); + $this->registrationService + ->expects($this->once()) + ->method('validateUsername') + ->with('user1'); + + $expected = new DataResponse([ + 'username' => 'user1', + 'displayname' => 'user test', + 'email' => 'test@example.com' + ], Http::STATUS_OK); + $actual = $this->controller->validate('user1', 'user test', 'test@example.com'); + $this->assertEquals($expected, $actual); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 999 + */ + public function testValidateFailEmail() { + $this->registrationService + ->expects($this->once()) + ->method('validateEmail') + ->willThrowException(new OCSException('', 999)); + $this->controller->validate('user1', 'user test', 'test@example.com'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 999 + */ + public function testValidateFailDisplayname() { + $this->registrationService + ->expects($this->once()) + ->method('validateDisplayname') + ->willThrowException(new OCSException('', 999)); + $this->controller->validate('user1', 'user test', 'test@example.com'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 999 + */ + public function testValidateFailUsername() { + $this->registrationService + ->expects($this->once()) + ->method('validateUsername') + ->willThrowException(new OCSException('', 999)); + $this->controller->validate('user1', 'user test', 'test@example.com'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException + * @expectedExceptionCode 404 + */ + public function testStatusNoRegistration() { + $this->registrationService + ->method('getRegistrationForToken') + ->with('ABCDEF') + ->willThrowException(new DoesNotExistException('')); + $this->controller->status('ABCDEF'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 403 + */ + public function testStatusPendingRegistration() { + $registration = new Registration(); + $registration->setEmailConfirmed(false); + $this->registrationService + ->method('getRegistrationForToken') + ->with('ABCDEF') + ->willReturn($registration); + $actual = $this->controller->status('ABCDEF'); + } + + public function testStatusConfirmedRegistration() { + $registration = new Registration(); + $registration->setEmailConfirmed(true); + $registration->setClientSecret('mysecret'); + $user = $this->createMock(IUser::class); + $this->registrationService + ->method('getRegistrationForToken') + ->with('ABCDEF') + ->willReturn($registration); + $this->registrationService + ->expects($this->once()) + ->method('getUserAccount') + ->with($registration) + ->willReturn($user); + $this->registrationService + ->expects($this->once()) + ->method('loginUser'); + $this->registrationService + ->expects($this->once()) + ->method('generateAppPassword'); + $actual = $this->controller->status('ABCDEF'); + $expected = new DataResponse([]); + $this->assertEquals($expected, $actual); + } + + public function testStatusConfirmedRegistrationWithSecret() { + + } + +} \ No newline at end of file diff --git a/tests/unit/controller/PageControllerTest.php b/tests/unit/controller/PageControllerTest.php deleted file mode 100644 index 0e084ed..0000000 --- a/tests/unit/controller/PageControllerTest.php +++ /dev/null @@ -1,53 +0,0 @@ - - * @copyright Pellaeon Lin 2014 - */ - -namespace OCA\Registration\Controller; - - -use \OCP\IRequest; -use \OCP\AppFramework\Http\TemplateResponse; -use \OCP\AppFramework\Http\JSONResponse; - -use \OCA\Registration\AppInfo\Application; - - -class PageControllerTest extends \PHPUnit_Framework_TestCase { - - private $container; - - public function setUp () { - $app = new Application(); - $this->container = $app->getContainer(); - } - - - public function testIndex () { - // swap out request - $this->container['Request'] = $this->getMockBuilder('\OCP\IRequest') - ->getMock(); - $this->container['UserId'] = 'john'; - - $result = $this->container['PageController']->index(); - - $this->assertEquals(array('user' => 'john'), $result->getParams()); - $this->assertEquals('main', $result->getTemplateName()); - $this->assertTrue($result instanceof TemplateResponse); - } - - - public function testEcho () { - $result = $this->container['PageController']->doEcho('hi'); - - $this->assertEquals(array('echo' => 'hi'), $result); - } - - -} \ No newline at end of file