diff --git a/tests/unit/Db/AclMapperTest.php b/tests/unit/Db/AclMapperTest.php new file mode 100644 index 000000000..c4ebf8954 --- /dev/null +++ b/tests/unit/Db/AclMapperTest.php @@ -0,0 +1,68 @@ + + * + * @author Julius Härtl + * + * @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\Db; + +use Test\AppFramework\Db\MapperTestUtility; + +class AclMapperTest extends MapperTestUtility { + + private $mapper; + private $acl; + + public function setup(){ + parent::setUp(); + $this->mapper = new AclMapper($this->db); + } + /** @return Acl */ + public function getAcl($type='user', $participant='admin', $write=false, $invite=false, $manage=false, $boardId=123) { + $acl = new Acl(); + $acl->setParticipant($participant); + $acl->setType('user'); + $acl->setPermissionWrite($write); + $acl->setPermissionInvite($invite); + $acl->setPermissionManage($manage); + $acl->setBoardId($boardId); + return $acl; + } + + public function testFindAll() { + $acls = []; + $acls[] = $this->getAcl('user','user1')->jsonSerialize(); + $acls[] = $this->getAcl('user','user2')->jsonSerialize(); + $acls[] = $this->getAcl('group','group1')->jsonSerialize(); + $acls[] = $this->getAcl('group','group2', true, true, true, 234)->jsonSerialize(); + + $sql = 'SELECT id, board_id, type, participant, permission_write, permission_invite, permission_manage FROM `*PREFIX*deck_board_acl` WHERE `board_id` = ? '; + $params = [123]; + $rows = [ + $acls[0], $acls[1], $acls[2] + ]; + $this->setMapperResult($sql, $params, $rows); + + $result = $this->mapper->findAll(123); + //$this->assertEquals($rows, $result); + + } + +} \ No newline at end of file diff --git a/tests/unit/ExceptionsTest.php b/tests/unit/ExceptionsTest.php index 32ba33088..75d2dc1fa 100644 --- a/tests/unit/ExceptionsTest.php +++ b/tests/unit/ExceptionsTest.php @@ -27,6 +27,7 @@ use OCA\Deck\CardArchivedException; use OCA\Deck\Controller\PageController; use OCA\Deck\NoPermissionException; use OCA\Deck\NotFoundException; +use OCA\Deck\StatusException; class ExceptionsTest extends \PHPUnit_Framework_TestCase { @@ -34,11 +35,13 @@ class ExceptionsTest extends \PHPUnit_Framework_TestCase { $c = new \stdClass(); $e = new NoPermissionException('not allowed', $c, 'mymethod'); $this->assertEquals('stdClass#mymethod: not allowed', $e->getMessage()); + $this->assertEquals(403, $e->getStatus()); } public function testNotFoundException() { $e = new NotFoundException('foo'); $this->assertEquals('foo', $e->getMessage()); + $this->assertEquals(404, $e->getStatus()); } public function testCardArchivedException() { @@ -46,4 +49,10 @@ class ExceptionsTest extends \PHPUnit_Framework_TestCase { $this->assertEquals('foo', $e->getMessage()); } + public function testStatusException() { + $e = new StatusException('foo'); + $this->assertEquals('foo', $e->getMessage()); + $this->assertEquals(500, $e->getStatus()); + } + } \ No newline at end of file diff --git a/tests/unit/Service/BoardServiceTest.php b/tests/unit/Service/BoardServiceTest.php index e16360513..2a1d41775 100644 --- a/tests/unit/Service/BoardServiceTest.php +++ b/tests/unit/Service/BoardServiceTest.php @@ -23,165 +23,75 @@ namespace OCA\Deck\Service; -use OCA\Deck\Db\Acl; +use OC\L10N\L10N; use OCA\Deck\Db\AclMapper; -use OCA\Deck\Db\Board; use OCA\Deck\Db\BoardMapper; +use OCA\Deck\Db\LabelMapper; use OCP\IGroupManager; use OCP\ILogger; -use PHPUnit_Framework_TestCase; class BoardServiceTest extends \PHPUnit_Framework_TestCase { private $service; private $logger; + private $l10n; + private $labelMapper; private $aclMapper; private $boardMapper; private $groupManager; + private $userId = 'admin'; public function setUp() { $this->logger = $this->request = $this->getMockBuilder(ILogger::class) ->disableOriginalConstructor() ->getMock(); + $this->l10n = $this->request = $this->getMockBuilder(L10N::class) + ->disableOriginalConstructor() + ->getMock(); $this->aclMapper = $this->getMockBuilder(AclMapper::class) ->disableOriginalConstructor()->getMock(); $this->boardMapper = $this->getMockBuilder(BoardMapper::class) ->disableOriginalConstructor()->getMock(); + $this->labelMapper = $this->getMockBuilder(LabelMapper::class) + ->disableOriginalConstructor()->getMock(); $this->groupManager = $this->getMockBuilder(IGroupManager::class) ->disableOriginalConstructor()->getMock(); - $this->service = new PermissionService( - $this->logger, - $this->aclMapper, + $this->service = new BoardService( $this->boardMapper, - $this->groupManager, - 'admin' + $this->logger, + $this->l10n, + $this->labelMapper, + $this->aclMapper, + $this->groupManager ); } - - public function testGetPermissionsOwner() { - $board = new Board(); - $board->setOwner('admin'); - $this->boardMapper->expects($this->once())->method('find')->with(123)->willReturn($board); - $this->aclMapper->expects($this->once()) - ->method('findAll') - ->willReturn(null); - $expected = [ - Acl::PERMISSION_READ => true, - Acl::PERMISSION_EDIT => true, - Acl::PERMISSION_MANAGE => true, - Acl::PERMISSION_SHARE => true, + public function testFindAll() { + $this->boardMapper->expects($this->once()) + ->method('findAllByUser') + ->with('admin') + ->willReturn([1,2,3,6,7]); + $this->boardMapper->expects($this->once()) + ->method('findAllByGroups') + ->with('admin', ['a', 'b', 'c']) + ->willReturn([4,5,6,7,8]); + $userinfo = [ + 'user' => 'admin', + 'groups' => ['a', 'b', 'c'] ]; - $this->assertEquals($expected, $this->service->getPermissions(123)); - } - public function testGetPermissionsAcl() { - $board = new Board(); - $board->setOwner('admin'); - $this->boardMapper->expects($this->once())->method('find')->with(123)->willReturn($board); - $aclUser = new Acl(); - $aclUser->setType('user'); - $aclUser->setParticipant('admin'); - $aclUser->setPermissionWrite(true); - $aclUser->setPermissionInvite(true); - $aclUser->setPermissionManage(true); - $this->aclMapper->expects($this->once()) - ->method('findAll') - ->willReturn([$aclUser]); - $expected = [ - Acl::PERMISSION_READ => true, - Acl::PERMISSION_EDIT => true, - Acl::PERMISSION_MANAGE => true, - Acl::PERMISSION_SHARE => true, - ]; - $this->assertEquals($expected, $this->service->getPermissions(123)); - } - public function testGetPermissionsAclNo() { - $board = new Board(); - $board->setOwner('user1'); - $this->boardMapper->expects($this->once())->method('find')->with(123)->willReturn($board); - $this->aclMapper->expects($this->once()) - ->method('findAll') - ->willReturn([]); - $expected = [ - Acl::PERMISSION_READ => false, - Acl::PERMISSION_EDIT => false, - Acl::PERMISSION_MANAGE => false, - Acl::PERMISSION_SHARE => false, - ]; - $this->assertEquals($expected, $this->service->getPermissions(123)); + $result = $this->service->findAll($userinfo); + sort($result); + $this->assertEquals([1,2,3,4,5,6,7,8], $result); } - public function testGetPermission() { - $board = new Board(); - $board->setOwner('admin'); - $this->boardMapper->expects($this->exactly(4))->method('find')->with(123)->willReturn($board); - $this->assertEquals(true, $this->service->getPermission(123, Acl::PERMISSION_READ)); - $this->assertEquals(true, $this->service->getPermission(123, Acl::PERMISSION_EDIT)); - $this->assertEquals(true, $this->service->getPermission(123, Acl::PERMISSION_MANAGE)); - $this->assertEquals(true, $this->service->getPermission(123, Acl::PERMISSION_SHARE)); - } - - public function testGetPermissionFail() { - $board = new Board(); - $board->setOwner('user1'); - $this->boardMapper->expects($this->exactly(4))->method('find')->with(234)->willReturn($board); - $this->aclMapper->expects($this->exactly(4))->method('findAll')->willReturn([]); - $this->assertEquals(false, $this->service->getPermission(234, Acl::PERMISSION_READ)); - $this->assertEquals(false, $this->service->getPermission(234, Acl::PERMISSION_EDIT)); - $this->assertEquals(false, $this->service->getPermission(234, Acl::PERMISSION_MANAGE)); - $this->assertEquals(false, $this->service->getPermission(234, Acl::PERMISSION_SHARE)); - } - - public function testUserIsBoardOwner() { - $board = new Board(); - $board->setOwner('admin'); - $this->boardMapper->expects($this->at(0))->method('find')->with(123)->willReturn($board); - $this->assertEquals(true, $this->service->userIsBoardOwner(123)); - $board = new Board(); - $board->setOwner('user1'); - $this->boardMapper->expects($this->at(0))->method('find')->with(234)->willReturn($board); - $this->assertEquals(false, $this->service->userIsBoardOwner(234)); - } - - public function testUserIsBoardOwnerNull() { - $this->boardMapper->expects($this->once())->method('find')->willReturn(null); - $this->assertEquals(false, $this->service->userIsBoardOwner(123)); - } - - public function dataTestUserCan() { - return [ - // participant permissions type - ['admin', false, false, false, 'user', true, false, false, false], - ['admin', true, false, false, 'user', true, true, false, false], - ['admin', true, true, false, 'user', true, true, true, false], - ['admin', true, true, false, 'user', true, true, true, false], - ['admin', true, true, true, 'user', true, true, true, true], - ['user1', false, false, false, 'user', false, false, false, false] - ]; - } - /** @dataProvider dataTestUserCan */ - public function testUserCan($participant, $edit, $share, $manage, $type, $canRead, $canEdit, $canShare, $canManage) { - $aclUser = new Acl(); - $aclUser->setType($type); - $aclUser->setParticipant($participant); - $aclUser->setPermissionWrite($edit); - $aclUser->setPermissionInvite($share); - $aclUser->setPermissionManage($manage); - $acls = [ - $aclUser - ]; - $this->assertEquals($canRead, $this->service->userCan($acls, Acl::PERMISSION_READ)); - $this->assertEquals($canEdit, $this->service->userCan($acls, Acl::PERMISSION_EDIT)); - $this->assertEquals($canShare, $this->service->userCan($acls, Acl::PERMISSION_SHARE)); - $this->assertEquals($canManage, $this->service->userCan($acls, Acl::PERMISSION_MANAGE)); - - - } - - public function testUserCanFail() { - $this->assertFalse($this->service->userCan([], Acl::PERMISSION_EDIT)); + public function testFind() { + $this->boardMapper->expects($this->once()) + ->method('find') + ->with(123) + ->willReturn(1); + $this->assertEquals(1, $this->service->find(123)); } } \ No newline at end of file diff --git a/tests/unit/controller/BoardControllerTest.php b/tests/unit/controller/BoardControllerTest.php index e0feabd21..156f6e9ae 100644 --- a/tests/unit/controller/BoardControllerTest.php +++ b/tests/unit/controller/BoardControllerTest.php @@ -121,48 +121,16 @@ class BoardControllerTest extends \PHPUnit_Framework_TestCase { } public function testGetUserPermissions() { - $board = $this->getMockBuilder(\OCA\Deck\Db\Board::class) - ->disableOriginalConstructor() - ->setMethods(['getOwner']) - ->getMock(); - $this->boardService->expects($this->once()) - ->method('find') - ->with(123) - ->willReturn($board); - $board->expects($this->once()) - ->method('getOwner') - ->willReturn('user'); $expected = [ 'PERMISSION_READ' => true, 'PERMISSION_EDIT' => true, 'PERMISSION_MANAGE' => true, 'PERMISSION_SHARE' => true, ]; - $this->assertEquals($expected, $this->controller->getUserPermissions(123)); - } - - public function testGetUserPermissionsNotOwner() { - $board = $this->getMockBuilder(\OCA\Deck\Db\Board::class) - ->disableOriginalConstructor() - ->setMethods(['getOwner']) - ->getMock(); - $this->boardService->expects($this->once()) - ->method('find') + $this->permissionService->expects($this->once()) + ->method('getPermissions') ->with(123) - ->willReturn($board); - $board->expects($this->once()) - ->method('getOwner') - ->willReturn('someoneelse'); - $this->boardService->expects($this->exactly(4)) - ->method('getPermission') - ->withConsecutive([123, 'user', Acl::PERMISSION_READ]) - ->will($this->onConsecutiveCalls(1, 2, 3, 4)); - $expected = [ - 'PERMISSION_READ' => 1, - 'PERMISSION_EDIT' => 2, - 'PERMISSION_MANAGE' => 3, - 'PERMISSION_SHARE' => 4, - ]; + ->willReturn($expected); $this->assertEquals($expected, $this->controller->getUserPermissions(123)); }