|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OCA\Workspace\Tests\Unit\Middleware; |
| 4 | + |
| 5 | +use OCA\Workspace\Controller\WorkspaceController; |
| 6 | +use OCA\Workspace\Db\Space; |
| 7 | +use OCA\Workspace\Middleware\Exceptions\AccessDeniedException; |
| 8 | +use OCA\Workspace\Middleware\WorkspaceMemberAccessMiddleware; |
| 9 | +use OCA\Workspace\Service\SpaceService; |
| 10 | +use OCA\Workspace\Service\UserService; |
| 11 | +use OCP\AppFramework\Http; |
| 12 | +use OCP\IRequest; |
| 13 | +use PHPUnit\Framework\MockObject\MockObject; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +class WorkspaceMemberAccessMiddlewareTest extends TestCase { |
| 17 | + |
| 18 | + private MockObject&IRequest $request; |
| 19 | + private MockObject&UserService $userService; |
| 20 | + private MockObject&SpaceService $spaceService; |
| 21 | + private MockObject&WorkspaceController $controller; |
| 22 | + private WorkspaceMemberAccessMiddleware $middleware; |
| 23 | + |
| 24 | + public function setUp(): void { |
| 25 | + parent::setUp(); |
| 26 | + |
| 27 | + $this->request = $this->createMock(IRequest::class); |
| 28 | + $this->userService = $this->createMock(UserService::class); |
| 29 | + $this->spaceService = $this->createMock(SpaceService::class); |
| 30 | + |
| 31 | + // No method is mocked, so the attributes of the real methods stay readable by reflection. |
| 32 | + $this->controller = $this->getMockBuilder(WorkspaceController::class) |
| 33 | + ->disableOriginalConstructor() |
| 34 | + ->onlyMethods([]) |
| 35 | + ->getMock() |
| 36 | + ; |
| 37 | + |
| 38 | + $this->middleware = new WorkspaceMemberAccessMiddleware( |
| 39 | + $this->request, |
| 40 | + $this->userService, |
| 41 | + $this->spaceService, |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + private function mockSpace(int $spaceId): void { |
| 46 | + /** @var MockObject&Space */ |
| 47 | + $space = $this->createMock(Space::class); |
| 48 | + $space |
| 49 | + ->method('jsonSerialize') |
| 50 | + ->willReturn(['id' => $spaceId]) |
| 51 | + ; |
| 52 | + |
| 53 | + $this->request |
| 54 | + ->method('getParam') |
| 55 | + ->with('spaceId') |
| 56 | + ->willReturn((string)$spaceId) |
| 57 | + ; |
| 58 | + |
| 59 | + $this->spaceService |
| 60 | + ->method('find') |
| 61 | + ->with($spaceId) |
| 62 | + ->willReturn($space) |
| 63 | + ; |
| 64 | + } |
| 65 | + |
| 66 | + public function testMethodWithoutAttributeIsNotChecked(): void { |
| 67 | + $this->userService |
| 68 | + ->expects($this->never()) |
| 69 | + ->method('isUserGeneralAdmin') |
| 70 | + ; |
| 71 | + |
| 72 | + $this->request |
| 73 | + ->expects($this->never()) |
| 74 | + ->method('getParam') |
| 75 | + ; |
| 76 | + |
| 77 | + $this->middleware->beforeController($this->controller, 'findAll'); |
| 78 | + } |
| 79 | + |
| 80 | + public function testGeneralManagerIsAllowed(): void { |
| 81 | + $this->userService |
| 82 | + ->expects($this->once()) |
| 83 | + ->method('isUserGeneralAdmin') |
| 84 | + ->willReturn(true) |
| 85 | + ; |
| 86 | + |
| 87 | + $this->spaceService |
| 88 | + ->expects($this->never()) |
| 89 | + ->method('find') |
| 90 | + ; |
| 91 | + |
| 92 | + $this->userService |
| 93 | + ->expects($this->never()) |
| 94 | + ->method('isSpaceManagerOfSpace') |
| 95 | + ; |
| 96 | + |
| 97 | + $this->middleware->beforeController($this->controller, 'getUsers'); |
| 98 | + } |
| 99 | + |
| 100 | + public function testWorkspaceManagerOfTheSpaceIsAllowed(): void { |
| 101 | + $this->mockSpace(6); |
| 102 | + |
| 103 | + $this->userService |
| 104 | + ->expects($this->once()) |
| 105 | + ->method('isSpaceManagerOfSpace') |
| 106 | + ->with(['id' => 6]) |
| 107 | + ->willReturn(true) |
| 108 | + ; |
| 109 | + |
| 110 | + // A manager is accepted without checking the user group. |
| 111 | + $this->userService |
| 112 | + ->expects($this->never()) |
| 113 | + ->method('isUserOfSpace') |
| 114 | + ; |
| 115 | + |
| 116 | + $this->middleware->beforeController($this->controller, 'getAdmins'); |
| 117 | + } |
| 118 | + |
| 119 | + public function testMemberOfTheSpaceIsAllowed(): void { |
| 120 | + $this->mockSpace(6); |
| 121 | + |
| 122 | + $this->userService |
| 123 | + ->expects($this->once()) |
| 124 | + ->method('isSpaceManagerOfSpace') |
| 125 | + ->with(['id' => 6]) |
| 126 | + ->willReturn(false) |
| 127 | + ; |
| 128 | + |
| 129 | + $this->userService |
| 130 | + ->expects($this->once()) |
| 131 | + ->method('isUserOfSpace') |
| 132 | + ->with(['id' => 6]) |
| 133 | + ->willReturn(true) |
| 134 | + ; |
| 135 | + |
| 136 | + $this->middleware->beforeController($this->controller, 'getUsers'); |
| 137 | + } |
| 138 | + |
| 139 | + public function testUserOutsideTheSpaceIsDenied(): void { |
| 140 | + $this->mockSpace(4); |
| 141 | + |
| 142 | + $this->userService->method('isSpaceManagerOfSpace')->willReturn(false); |
| 143 | + $this->userService->method('isUserOfSpace')->willReturn(false); |
| 144 | + |
| 145 | + $this->expectException(AccessDeniedException::class); |
| 146 | + |
| 147 | + $this->middleware->beforeController($this->controller, 'getUsers'); |
| 148 | + } |
| 149 | + |
| 150 | + public function testUnknownSpaceIsDenied(): void { |
| 151 | + $this->request |
| 152 | + ->method('getParam') |
| 153 | + ->with('spaceId') |
| 154 | + ->willReturn('999') |
| 155 | + ; |
| 156 | + |
| 157 | + $this->spaceService |
| 158 | + ->method('find') |
| 159 | + ->with(999) |
| 160 | + ->willReturn(null) |
| 161 | + ; |
| 162 | + |
| 163 | + $this->expectException(AccessDeniedException::class); |
| 164 | + |
| 165 | + $this->middleware->beforeController($this->controller, 'getAdmins'); |
| 166 | + } |
| 167 | + |
| 168 | + public function testAccessDeniedIsTurnedIntoForbiddenResponse(): void { |
| 169 | + $response = $this->middleware->afterException($this->controller, 'getUsers', new AccessDeniedException()); |
| 170 | + |
| 171 | + $this->assertSame(Http::STATUS_FORBIDDEN, $response->getStatus()); |
| 172 | + } |
| 173 | +} |
0 commit comments