Fix tests for group restrictions

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2018-12-04 09:25:35 +01:00
Родитель 7c95783ab5
Коммит 0528bd59d1
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4C614C6ED2CDE6DF
4 изменённых файлов: 59 добавлений и 30 удалений

Просмотреть файл

@ -37,6 +37,7 @@ use OCP\IConfig;
use OCP\IGroupManager;
use OCP\ILogger;
use OCP\IUserManager;
use OCP\Share\IManager;
class PermissionService {
@ -53,6 +54,8 @@ class PermissionService {
private $groupManager;
/** @var IConfig */
private $config;
/** @var IManager */
private $shareManager;
/** @var string */
private $userId;
/** @var array */
@ -64,6 +67,7 @@ class PermissionService {
BoardMapper $boardMapper,
IUserManager $userManager,
IGroupManager $groupManager,
IManager $shareManager,
IConfig $config,
$userId
) {
@ -72,6 +76,7 @@ class PermissionService {
$this->logger = $logger;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->shareManager = $shareManager;
$this->config = $config;
$this->userId = $userId;
}
@ -90,7 +95,7 @@ class PermissionService {
Acl::PERMISSION_EDIT => $owner || $this->userCan($acls, Acl::PERMISSION_EDIT),
Acl::PERMISSION_MANAGE => $owner || $this->userCan($acls, Acl::PERMISSION_MANAGE),
Acl::PERMISSION_SHARE => ($owner || $this->userCan($acls, Acl::PERMISSION_SHARE))
&& (!\OC::$server->getShareManager()->sharingDisabledForUser($this->userId))
&& (!$this->shareManager->sharingDisabledForUser($this->userId))
];
}
@ -109,7 +114,7 @@ class PermissionService {
Acl::PERMISSION_EDIT => $owner || $this->userCan($acls, Acl::PERMISSION_EDIT),
Acl::PERMISSION_MANAGE => $owner || $this->userCan($acls, Acl::PERMISSION_MANAGE),
Acl::PERMISSION_SHARE => ($owner || $this->userCan($acls, Acl::PERMISSION_SHARE))
&& (!\OC::$server->getShareManager()->sharingDisabledForUser($this->userId))
&& (!$this->shareManager->sharingDisabledForUser($this->userId))
];
}
@ -132,7 +137,7 @@ class PermissionService {
throw new NoPermissionException('Permission denied');
}
if ($permission === Acl::PERMISSION_SHARE && !\OC::$server->getShareManager()->sharingDisabledForUser($this->userId)) {
if ($permission === Acl::PERMISSION_SHARE && $this->shareManager->sharingDisabledForUser($this->userId)) {
return false;
}

Просмотреть файл

@ -150,6 +150,9 @@ class BoardServiceTest extends TestCase {
$this->boardMapper->expects($this->once())
->method('insert')
->willReturn($board);
$this->permissionService->expects($this->once())
->method('canCreate')
->willReturn(true);
$b = $this->service->create('MyBoard', 'admin', '00ff00');
$this->assertEquals($b->getTitle(), 'MyBoard');
@ -158,6 +161,20 @@ class BoardServiceTest extends TestCase {
$this->assertCount(4, $b->getLabels());
}
/**
* @expectedException \OCA\Deck\NoPermissionException
*/
public function testCreateDenied() {
$board = new Board();
$board->setTitle('MyBoard');
$board->setOwner('admin');
$board->setColor('00ff00');
$this->permissionService->expects($this->once())
->method('canCreate')
->willReturn(false);
$b = $this->service->create('MyBoard', 'admin', '00ff00');
}
public function testUpdate() {
$board = new Board();
$board->setTitle('MyBoard');

Просмотреть файл

@ -31,11 +31,13 @@ use OCA\Deck\Db\IPermissionMapper;
use OCA\Deck\Db\User;
use OCA\Deck\NoPermissionException;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\ILogger;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Share\IManager;
class PermissionServiceTest extends \Test\TestCase {
@ -51,21 +53,22 @@ class PermissionServiceTest extends \Test\TestCase {
private $userManager;
/** @var IGroupManager */
private $groupManager;
/** @var IManager */
private $shareManager;
/** @var IConfig */
private $config;
/** @var string */
private $userId = 'admin';
public function setUp() {
parent::setUp();
$this->logger = $this->request = $this->getMockBuilder(ILogger::class)
->disableOriginalConstructor()
->getMock();
$this->aclMapper = $this->getMockBuilder(AclMapper::class)
->disableOriginalConstructor()->getMock();
$this->boardMapper = $this->getMockBuilder(BoardMapper::class)
->disableOriginalConstructor()->getMock();
$this->logger = $this->request = $this->createMock(ILogger::class);
$this->aclMapper = $this->createMock(AclMapper::class);
$this->boardMapper = $this->createMock(BoardMapper::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->groupManager = $this->getMockBuilder(IGroupManager::class)
->disableOriginalConstructor()->getMock();
$this->groupManager = $this->createMock(IGroupManager::class);
$this->shareManager = $this->createMock(IManager::class);
$this->config = $this->createMock(IConfig::class);
$this->service = new PermissionService(
$this->logger,
@ -73,6 +76,8 @@ class PermissionServiceTest extends \Test\TestCase {
$this->boardMapper,
$this->userManager,
$this->groupManager,
$this->shareManager,
$this->config,
'admin'
);
}
@ -226,6 +231,9 @@ class PermissionServiceTest extends \Test\TestCase {
$acls = $this->getAcls($boardId);
$this->aclMapper->expects($this->any())->method('findAll')->willReturn($acls);
$this->shareManager->expects($this->any())
->method('sharingDisabledForUser')
->willReturn(false);
if($result) {
$actual = $this->service->checkPermission($mapper, 1234, $permission);
@ -251,6 +259,7 @@ class PermissionServiceTest extends \Test\TestCase {
$acls = $this->getAcls($boardId);
$this->aclMapper->expects($this->any())->method('findAll')->willReturn($acls);
if($result) {
$actual = $this->service->checkPermission($mapper, 1234, $permission);
$this->assertTrue($actual);
@ -263,7 +272,7 @@ class PermissionServiceTest extends \Test\TestCase {
public function testCheckPermissionNotFound() {
$mapper = $this->getMockBuilder(IPermissionMapper::class)->getMock();
$mapper->expects($this->once())->method('findBoardId')->willThrowException(new NoPermissionException(null));
$mapper->expects($this->once())->method('findBoardId')->willThrowException(new NoPermissionException(null));
$this->expectException(NoPermissionException::class);
$this->service->checkPermission($mapper, 1234, Acl::PERMISSION_READ);
}

Просмотреть файл

@ -6,24 +6,27 @@
* @author Ryan Fletcher <ryan.fletcher@codepassion.ca>
*
* @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 <http://www.gnu.org/licenses/>.
*
*
*/
namespace OCA\Deck\Controller;
use OCA\Deck\Service\PermissionService;
use OCP\IL10N;
use OCP\IRequest;
use PHPUnit_Framework_TestCase;
use OCA\Deck\Service\DefaultBoardService;
use OCA\Deck\Db\Board;
@ -36,26 +39,21 @@ class PageControllerTest extends \Test\TestCase {
private $l10n;
private $userId = 'john';
private $defaultBoardService;
private $permissionService;
private $config;
public function setUp() {
$this->l10n = $this->request = $this->getMockBuilder(
'\OCP\IL10n')
->disableOriginalConstructor()
->getMock();
$this->request = $this->getMockBuilder(
'\OCP\IRequest')
->disableOriginalConstructor()
->getMock();
$this->l10n = $this->createMock(IL10N::class);
$this->request = $this->createMock(IRequest::class);
$this->defaultBoardService = $this->createMock(DefaultBoardService::class);
$this->permissionService = $this->createMock(PermissionService::class);
$this->config = $this->createMock(IConfig::class);
$this->controller = new PageController(
'deck', $this->request, $this->defaultBoardService, $this->l10n, $this->userId
'deck', $this->request, $this->defaultBoardService, $this->permissionService, $this->l10n, $this->userId
);
}
public function testIndexOnFirstRun() {
$board = new Board();
@ -72,7 +70,7 @@ class PageControllerTest extends \Test\TestCase {
->willReturn($board);
$response = $this->controller->index();
$this->assertEquals('main', $response->getTemplateName());
$this->assertEquals('main', $response->getTemplateName());
}
public function testIndexOnSecondRun() {
@ -87,4 +85,4 @@ class PageControllerTest extends \Test\TestCase {
$this->assertEquals('main', $response->getTemplateName());
}
}
}