Add basic unit testing and tests for API controller
Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
parent
c17398dede
commit
f4521a9d29
13
phpunit.xml
13
phpunit.xml
|
|
@ -1 +1,12 @@
|
|||
<phpunit bootstrap="tests/autoloader.php"></phpunit>
|
||||
<phpunit bootstrap="tests/autoloader.php" colors="true">
|
||||
<testsuites>
|
||||
<testsuite name="unit">
|
||||
<directory>./tests/unit</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory suffix=".php">./</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
</phpunit>
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
});
|
||||
require_once __DIR__ . '/../../../tests/bootstrap.php';
|
||||
|
|
|
|||
|
|
@ -0,0 +1,174 @@
|
|||
<?php
|
||||
/**
|
||||
* ownCloud - registration
|
||||
*
|
||||
* This file is licensed under the Affero General Public License version 3 or
|
||||
* later. See the COPYING file.
|
||||
*
|
||||
* @author Pellaeon Lin <pellaeon@hs.ntnu.edu.tw>
|
||||
* @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() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* ownCloud - registration
|
||||
*
|
||||
* This file is licensed under the Affero General Public License version 3 or
|
||||
* later. See the COPYING file.
|
||||
*
|
||||
* @author Pellaeon Lin <pellaeon@hs.ntnu.edu.tw>
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue