More SharingMiddleware testing

This commit is contained in:
Julius Haertl
2016-10-30 14:05:47 +01:00
parent 9becbe43e1
commit 7969f10761
2 changed files with 69 additions and 8 deletions

View File

@@ -26,6 +26,8 @@ namespace OCA\Deck\Middleware;
use OC\AppFramework\DependencyInjection\DIContainer;
use OC\AppFramework\Utility\ControllerMethodReflector;
use OC\AppFramework\Utility\SimpleContainer;
use OCA\Deck\Db\DeckMapper;
use OCA\Deck\Db\IPermissionMapper;
use OCA\Deck\NoPermissionException;
use OCA\Deck\NotFoundException;
use OCA\Deck\Service\BoardService;
@@ -35,6 +37,7 @@ use OCP\AppFramework\Http\JSONResponse;
use OCP\IContainer;
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserSession;
use OCA\Deck\Db\AclMapper;
@@ -48,13 +51,15 @@ class SharingMiddlewareTest extends \PHPUnit_Framework_TestCase {
private $permissionService;
public function setUp() {
$this->container = new SimpleContainer();
$this->container = $this->getMockBuilder(IContainer::class)
->disableOriginalConstructor()->getMock();
$this->request = $this->getMockBuilder(IRequest::class)
->disableOriginalConstructor()->getMock();
$this->userSession = $this->getMockBuilder(IUserSession::class)
->disableOriginalConstructor()->getMock();
$this->reflector = $this->getMockBuilder(ControllerMethodReflector::class)
->disableOriginalConstructor()->getMock();
$this->reflector = new ControllerMethodReflector();
//$this->getMockBuilder(ControllerMethodReflector::class)
// ->disableOriginalConstructor()->getMock();
$this->permissionService = $this->getMockBuilder(PermissionService::class)
->disableOriginalConstructor()->getMock();
$this->sharingMiddleware = new SharingMiddleware(
@@ -66,10 +71,59 @@ class SharingMiddlewareTest extends \PHPUnit_Framework_TestCase {
);
}
public function testBeforeController() {
$controller = $this->getMockBuilder(Controller::class)
public function dataBeforeController() {
return [
['GET', '\OCA\Deck\Controller\PageController', 'index', null, true],
['GET', '\OCA\Deck\Controller\BoardController', 'index', null, true],
['GET', '\OCA\Deck\Controller\BoardController', 'read', true, true],
['GET', '\OCA\Deck\Controller\BoardController', 'read', false, true, NoPermissionException::class],
['GET', '\OCA\Deck\Controller\CardController', 'read', false, true, NoPermissionException::class],
['POST', '\OCA\Deck\Controller\CardController', 'reorder', false, true, NoPermissionException::class],
];
}
/**
* @dataProvider dataBeforeController
* @param $controllerClass
* @param $methodName
*/
public function testBeforeController($method, $controllerClass, $methodName, $getPermission, $success, $exception=null) {
$controller = $this->getMockBuilder($controllerClass)
->disableOriginalConstructor()->getMock();
$methodName = '';
$mapper = $this->getMockBuilder(IPermissionMapper::class)
->disableOriginalConstructor()->getMock();
$mapper->expects($this->any())->method('findBoardId')->willReturn(123);
$mapper->expects($this->any())->method('isOwner')->willReturn(false);
$user = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()->getMock();
$user->expects($this->once())->method('getUID')->willReturn('user1');
$this->reflector->reflect($controller, $methodName);
$this->container->expects($this->any())
->method('query')->willReturn($mapper);
$this->userSession->expects($this->exactly(2))->method('getUser')->willReturn($user);
$this->request->expects($this->once())->method('getMethod')->willReturn($method);
if($getPermission) {
$this->permissionService->expects($this->any())->method('getPermission')->willReturn($getPermission);
}
if($success) {
$this->sharingMiddleware->beforeController($controller, $methodName);
} else {
try {
$this->sharingMiddleware->beforeController($controller, $methodName);
} catch (\Exception $e) {
$this->assertInstanceOf($exception, $e);
}
}
}
public function setUpPermissions() {
$this->permissionService->expects($this->once())
->method('getPermission')
->with(123, Acl::PERMISSION_READ)
->willReturn(true);
}
@@ -91,4 +145,12 @@ class SharingMiddlewareTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals($expected, $result);
}
public function testAfterExceptionFail() {
try {
$result = $this->sharingMiddleware->afterException('Foo', 'bar', new \Exception('failed hard'));
} catch (\Exception $e) {
$this->assertEquals('failed hard', $e->getMessage());
}
}
}