From 891fa7b7d5896c7e3fbf5ef09d0da4c226351ad3 Mon Sep 17 00:00:00 2001 From: Ryan Fletcher Date: Sun, 15 Jul 2018 15:35:48 -0400 Subject: [PATCH] Created index tests for StackApiTestController Signed-off-by: Ryan Fletcher --- lib/Controller/Helper/ApiHelper.php | 2 +- lib/Controller/StackApiController.php | 22 ++-- lib/Service/PermissionService.php | 4 +- .../controller/BoardApiControllerTest.php | 26 ---- .../controller/StackApiControllerTest.php | 113 ++++++++++++++++++ 5 files changed, 125 insertions(+), 42 deletions(-) create mode 100644 tests/unit/controller/StackApiControllerTest.php diff --git a/lib/Controller/Helper/ApiHelper.php b/lib/Controller/Helper/ApiHelper.php index 81aa003a5..c19baaaf8 100644 --- a/lib/Controller/Helper/ApiHelper.php +++ b/lib/Controller/Helper/ApiHelper.php @@ -38,7 +38,7 @@ class ApiHelper { $entity = $service->find($entityId); if ($entity === false || $entity === null) { - $error['message'] = $entityName . ' does not exist'; + $error['message'] = $entityName . ' not found'; $error['status'] = HTTP::STATUS_NOT_FOUND; return $error; } diff --git a/lib/Controller/StackApiController.php b/lib/Controller/StackApiController.php index 0cc57f9f5..137de1e6d 100644 --- a/lib/Controller/StackApiController.php +++ b/lib/Controller/StackApiController.php @@ -66,17 +66,13 @@ class StackApiController extends ApiController { * Return all of the stacks in the specified board. */ public function index() { - $boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService); + $boardError = $this->apiHelper->entityHasError($this->request->getParam('boardId'), 'board', $this->boardService); if ($boardError) { return new DataResponse($boardError['message'], $boardError['status']); } - - $stacks = $this->service->findAll($this->request->params['boardId']); - if ($stacks === false || $stacks === null) { - return new DataResponse('No Stacks Found', HTTP::STATUS_NOT_FOUND); - } + $stacks = $this->service->findAll($this->request->getParam('boardId')); return new DataResponse($stacks, HTTP::STATUS_OK); } @@ -88,8 +84,8 @@ class StackApiController extends ApiController { * * Return all of the stacks in the specified board. */ - public function get() { - $boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService); + public function get() { + $boardError = $this->apiHelper->entityHasError($this->request->getParam('boardId'), 'board', $this->boardService); if ($boardError) { return new DataResponse($boardError['message'], $boardError['status']); @@ -116,7 +112,7 @@ class StackApiController extends ApiController { */ public function create($title, $order) { - $boardError = $this->apiHelper->entityHasError( $this->request->params['boardId'], 'board', $this->boardService ); + $boardError = $this->apiHelper->entityHasError( $this->request->getParam('boardId'), 'board', $this->boardService ); if ($boardError) { return new DataResponse($boardError['message'], $boardError['status']); @@ -127,7 +123,7 @@ class StackApiController extends ApiController { } try { - $stack = $this->service->create($title, $this->request->params['boardId'], $order); + $stack = $this->service->create($title, $this->request->getParam('boardId'), $order); } catch (StatusException $e) { $errorMessage['error'] = $e->getMessage(); return new DataResponse($errorMessage, HTTP::STATUS_INTERNAL_SERVER_ERROR); @@ -148,7 +144,7 @@ class StackApiController extends ApiController { */ public function update($title, $order) { - $boardError = $this->apiHelper->entityHasError( $this->request->params['boardId'], 'board', $this->boardService ); + $boardError = $this->apiHelper->entityHasError( $this->request->getParam('boardId'), 'board', $this->boardService ); if ($boardError) { return new DataResponse($boardError['message'], $boardError['status']); @@ -163,7 +159,7 @@ class StackApiController extends ApiController { } try { - $stack = $this->service->update($this->request->params['stackId'], $title, $this->request->params['boardId'], $order); + $stack = $this->service->update($this->request->params['stackId'], $title, $this->request->getParam('boardId'), $order); if ($stack === false || $stack === null) { return new DataResponse('Stack not found', HTTP::STATUS_NOT_FOUND); @@ -184,7 +180,7 @@ class StackApiController extends ApiController { */ public function delete() { - $boardError = $this->apiHelper->entityHasError( $this->request->params['boardId'], 'board', $this->boardService ); + $boardError = $this->apiHelper->entityHasError( $this->request->getParam('boardId'), 'board', $this->boardService ); if ($boardError) { return new DataResponse($boardError['message'], $boardError['status']); diff --git a/lib/Service/PermissionService.php b/lib/Service/PermissionService.php index 5dff31be8..8bd0623aa 100644 --- a/lib/Service/PermissionService.php +++ b/lib/Service/PermissionService.php @@ -146,11 +146,11 @@ class PermissionService { public function userIsBoardOwner($boardId) { try { $board = $this->boardMapper->find($boardId); + return $board && $this->userId === $board->getOwner(); } catch (DoesNotExistException $e) { } catch (MultipleObjectsReturnedException $e) { return false; - } - return $board && $this->userId === $board->getOwner(); + } } /** diff --git a/tests/unit/controller/BoardApiControllerTest.php b/tests/unit/controller/BoardApiControllerTest.php index 1cd0f84ba..df3783f1a 100644 --- a/tests/unit/controller/BoardApiControllerTest.php +++ b/tests/unit/controller/BoardApiControllerTest.php @@ -119,32 +119,6 @@ class BoardApiControllerTest extends \Test\TestCase { $this->assertEquals($expected, $actual); } - - public function testGetNoPermission() { - - $board = new Board(); - $board->setId($this->deniedBoard['id']); - $board->setOwner($this->deniedBoard['owner']); - $this->boardService->expects($this->once()) - ->method('find') - ->willReturn($board); - - // permission service check. - // ------ there be dragons here ----- - // $this->permissionsService->expect($this->once()) - // ->method('matchPermissions') - // ->with($board) - // ->will($this->) - - $this->request->expects($this->any()) - ->method('getParam') - ->with('boardId') - ->will($this->returnValue('999')); - - $expected = new DataResponse("Access Denied: User has no access rights to board", HTTP::STATUS_FORBIDDEN); - $actual = $this->controller->get(); - $this->assertEquals($expected, $actual); - } public function testCreate() { $board = new Board(); diff --git a/tests/unit/controller/StackApiControllerTest.php b/tests/unit/controller/StackApiControllerTest.php new file mode 100644 index 000000000..4a8bb93d1 --- /dev/null +++ b/tests/unit/controller/StackApiControllerTest.php @@ -0,0 +1,113 @@ + + * + * @author Ryan Fletcher + * + * @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 . + * + */ +namespace OCA\Deck\Controller; + +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; +use OCP\IRequest; + +use OCA\Deck\Service\BoardService; +use OCA\Deck\Service\StackService; +use OCA\Deck\Db\Board; +use OCA\Deck\Db\Stack; + +class StackApiControllerTest extends \Test\TestCase { + + private $appName = 'deck'; + private $userId = 'admin'; + private $controller; + private $boardService; + private $stackService; + private $exampleStack; + private $exampleBoard; + + public function setUp() { + parent::setUp(); + $this->request = $this->createMock(IRequest::class); + $this->boardService = $this->createMock(BoardService::class); + $this->stackService = $this->createMock(StackService::class); + + $this->exampleStack['id'] = 345; + $this->exampleStack['boardId'] = 245; + $this->exampleStack['order'] = 0; + + $this->exampleBoard['boardId'] = '89'; + + $this->controller = new StackApiController( + $this->appName, + $this->request, + $this->stackService, + $this->boardService + ); + } + + public function testIndex() { + $stack = new Stack(); + $stack->setId($this->exampleStack['id']); + $stack->setBoardId($this->exampleStack['boardId']); + $stack->setOrder($this->exampleStack['order']); + $stacks = [$stack]; + + $board = new Board(); + $board->setId($this->exampleBoard['boardId']); + $this->boardService->expects($this->once()) + ->method('find') + ->willReturn($board); + + $this->stackService->expects($this->once()) + ->method('findAll') + ->willReturn($stacks); + + $this->request->expects($this->any()) + ->method('getParam') + ->with('boardId') + ->will($this->returnValue($this->exampleBoard['boardId'])); + + $expected = new DataResponse($stacks, HTTP::STATUS_OK); + $actual = $this->controller->index(); + $this->assertEquals($expected, $actual); + } + + public function testIndexBadBoardId() { + $this->request->expects($this->any()) + ->method('getParam') + ->with('boardId') + ->will($this->returnValue('bad board id')); + + $expected = new DataResponse('board id must be a number', HTTP::STATUS_BAD_REQUEST); + $actual = $this->controller->index(); + $this->assertEquals($expected, $actual); + } + + public function testIndexBoardNotFound() { + $this->request->expects($this->any()) + ->method('getParam') + ->with('boardId') + ->will($this->returnValue(689)); + + $expected = new DataResponse('board not found', HTTP::STATUS_NOT_FOUND); + $actual = $this->controller->index(); + $this->assertEquals($expected, $actual); + } + +} \ No newline at end of file