cloud_bbb/tests/Unit/PermissionTest.php

162 lines
4.3 KiB
PHP
Raw Normal View History

2020-06-19 09:20:03 +02:00
<?php
2020-06-19 09:20:03 +02:00
namespace OCA\BigBlueButton\Tests;
2020-09-23 12:33:09 +02:00
use OCA\BigBlueButton\CircleHelper;
use OCA\BigBlueButton\Db\Restriction;
2020-06-19 09:20:03 +02:00
use OCA\BigBlueButton\Db\Room;
use OCA\BigBlueButton\Db\RoomShare;
use OCA\BigBlueButton\Permission;
2020-09-23 12:33:09 +02:00
use OCA\BigBlueButton\Service\RestrictionService;
use OCA\BigBlueButton\Service\RoomService;
2020-06-19 09:20:03 +02:00
use OCA\BigBlueButton\Service\RoomShareService;
use OCP\IGroupManager;
use OCP\IUser;
2020-09-23 12:33:09 +02:00
use OCP\IUserManager;
2020-06-19 09:20:03 +02:00
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class PermissionTest extends TestCase {
2020-06-19 09:20:03 +02:00
/** @var Permission */
private $permission;
/** @var IUserManager|MockObject */
private $userManager;
2020-06-19 09:20:03 +02:00
/** @var IGroupManager|MockObject */
private $groupManager;
/** @var RoomService|MockObject */
private $roomService;
2020-06-19 09:20:03 +02:00
/** @var RoomShareService|MockObject */
private $roomShareService;
/** @var RestrictionService|MockObject */
private $restrictionService;
/** @var CircleHelper|MockObject */
private $circleHelper;
public function setUp(): void {
2020-06-19 09:20:03 +02:00
parent::setUp();
/** @var IUserManager|MockObject */
$this->userManager = $this->createMock(IUserManager::class);
2020-06-19 09:20:03 +02:00
/** @var IGroupManager|MockObject */
$this->groupManager = $this->createMock(IGroupManager::class);
/** @var RoomService|MockObject */
$this->roomService = $this->createMock(RoomService::class);
/** @var RestrictionService|MockObject */
$this->restrictionService = $this->createMock(RestrictionService::class);
2020-06-19 09:20:03 +02:00
/** @var RoomShareService|MockObject */
$this->roomShareService = $this->createMock(RoomShareService::class);
/** @var CircleHelper|MockObject */
$this->circleHelper = $this->createMock(CircleHelper::class);
2020-06-19 09:20:03 +02:00
$this->permission = new Permission(
$this->userManager,
2020-06-19 09:20:03 +02:00
$this->groupManager,
$this->roomService,
$this->restrictionService,
$this->roomShareService,
$this->circleHelper
2020-06-19 09:20:03 +02:00
);
}
public function testIsUserNotAllowed() {
$this->userManager
->expects($this->once())
->method('get')
->with('foobar')
->willReturn($this->createMock(IUser::class));
$this->groupManager
->expects($this->once())
->method('getUserGroupIds')
->willReturn([]);
$this->roomService
->expects($this->once())
->method('findByUserId')
->with('foobar')
->willReturn([
$this->createRoom(1, 'foo'),
$this->createRoom(2, 'bar'),
]);
$restriction = new Restriction();
$restriction->setMaxRooms(2);
$this->restrictionService
->expects($this->once())
->method('findByGroupIds')
->willReturn($restriction);
$this->assertFalse($this->permission->isAllowedToCreateRoom('foobar'));
}
public function testIsUser() {
2020-06-19 09:20:03 +02:00
$room = $this->createRoom(1, 'foo');
$this->roomShareService
->expects($this->exactly(5))
2020-06-19 09:20:03 +02:00
->method('findAll')
->will($this->returnValueMap([
[1, [
$this->createRoomShare(RoomShare::SHARE_TYPE_USER, 'user', RoomShare::PERMISSION_ADMIN),
$this->createRoomShare(RoomShare::SHARE_TYPE_GROUP, 'group', RoomShare::PERMISSION_MODERATOR),
$this->createRoomShare(RoomShare::SHARE_TYPE_CIRCLE, 'circle', RoomShare::PERMISSION_USER),
2020-06-19 09:20:03 +02:00
]],
[2, []],
]));
$this->groupManager
->method('isInGroup')
->will($this->returnValueMap([
['bar', 'group', false],
['group_user', 'group', true],
]));
$this->circleHelper
->method('isInCircle')
->will($this->returnValueMap([
['bar', 'circle', false],
['circle_user', 'circle', true],
]));
2020-06-19 09:20:03 +02:00
$this->assertFalse($this->permission->isUser($room, null), 'Test guest user');
$this->assertFalse($this->permission->isUser($room, 'bar'), 'Test no matching share');
$this->assertFalse($this->permission->isUser($this->createRoom(2, 'foo'), 'bar'), 'Test empty shares');
$this->assertTrue($this->permission->isUser($room, 'foo'), 'Test room owner');
$this->assertTrue($this->permission->isUser($room, 'user'));
$this->assertTrue($this->permission->isUser($room, 'group_user'));
$this->assertTrue($this->permission->isUser($room, 'circle_user'));
2020-06-19 09:20:03 +02:00
}
private function createRoom(int $id, string $userId): Room {
2020-06-19 09:20:03 +02:00
$room = new Room();
$room->setId($id);
$room->setUserId($userId);
return $room;
}
private function createRoomShare(int $type, string $with, int $permission): RoomShare {
2020-06-19 09:20:03 +02:00
$share = new RoomShare();
$share->setShareType($type);
$share->setShareWith($with);
$share->setPermission($permission);
return $share;
}
}