|
7 | 7 | use OCA\Text\Middleware\SessionMiddleware; |
8 | 8 | use OCA\Text\Service\DocumentService; |
9 | 9 | use OCA\Text\Service\SessionService; |
| 10 | +use OCP\Constants; |
| 11 | +use OCP\Files\File; |
10 | 12 | use OCP\Files\Folder; |
11 | 13 | use OCP\Files\IRootFolder; |
12 | 14 | use OCP\IL10N; |
13 | 15 | use OCP\IRequest; |
14 | 16 | use OCP\ISession; |
| 17 | +use OCP\IUser; |
15 | 18 | use OCP\IUserSession; |
| 19 | +use OCP\Share\Exceptions\ShareNotFound; |
16 | 20 | use OCP\Share\IManager; |
17 | 21 | use OCP\Share\IShare; |
18 | 22 | use Test\TestCase; |
@@ -89,29 +93,74 @@ public function testWrongArrayBlocked(): void { |
89 | 93 | $this->invokeMiddleware($share); |
90 | 94 | } |
91 | 95 |
|
| 96 | + public function testLoggedInUserWithInvalidToken(): void { |
| 97 | + $this->expectException(InvalidSessionException::class); |
| 98 | + |
| 99 | + $user = $this->createMock(IUser::class); |
| 100 | + $user->method('getUID')->willReturn('user1'); |
| 101 | + |
| 102 | + $share = $this->createPasswordProtectedShare('42'); |
| 103 | + $this->shareManager->method('getShareByToken')->willThrowException(new ShareNotFound()); |
| 104 | + |
| 105 | + $this->invokeMiddleware($share, $user); |
| 106 | + } |
| 107 | + |
| 108 | + public function testLoggedInUserWithValidToken(): void { |
| 109 | + $user = $this->createMock(IUser::class); |
| 110 | + $user->method('getUID')->willReturn('user1'); |
| 111 | + |
| 112 | + $share = $this->createMock(IShare::class); |
| 113 | + $share->method('getId')->willReturn('user2-share'); |
| 114 | + $share->method('getPassword')->willReturn(null); |
| 115 | + $share->method('getPermissions')->willReturn(Constants::PERMISSION_READ); |
| 116 | + $share->method('getShareOwner')->willReturn('user2'); |
| 117 | + $share->method('getAttributes')->willReturn(null); |
| 118 | + |
| 119 | + $this->shareManager->method('getShareByToken')->willReturn($share); |
| 120 | + |
| 121 | + $controller = $this->createMock(ISessionAwareController::class); |
| 122 | + $controller->expects($this->never())->method('setUserId'); |
| 123 | + $controller->expects($this->once())->method('setDocumentId'); |
| 124 | + |
| 125 | + $this->invokeMiddleware($share, $user, $controller); |
| 126 | + } |
| 127 | + |
| 128 | + public function testLoggedInUserWithValidTokenUnauthenticated(): void { |
| 129 | + $this->expectException(InvalidSessionException::class); |
| 130 | + |
| 131 | + $user = $this->createMock(IUser::class); |
| 132 | + $user->method('getUID')->willReturn('user1'); |
| 133 | + |
| 134 | + $share = $this->createPasswordProtectedShare('user2-share'); |
| 135 | + $this->session->method('get')->with('public_link_authenticated')->willReturn(null); |
| 136 | + $this->shareManager->method('getShareByToken')->willReturn($share); |
| 137 | + |
| 138 | + $this->invokeMiddleware($share, $user); |
| 139 | + } |
| 140 | + |
92 | 141 | private function createPasswordProtectedShare(string $id): IShare { |
93 | 142 | $share = $this->createMock(IShare::class); |
94 | 143 | $share->method('getId')->willReturn($id); |
95 | 144 | $share->method('getPassword')->willReturn('password'); |
96 | | - $share->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ); |
| 145 | + $share->method('getPermissions')->willReturn(Constants::PERMISSION_READ); |
97 | 146 | $share->method('getShareOwner')->willReturn('owner'); |
98 | 147 | $share->method('getAttributes')->willReturn(null); |
99 | 148 | return $share; |
100 | 149 | } |
101 | 150 |
|
102 | | - private function invokeMiddleware(IShare $share): void { |
| 151 | + private function invokeMiddleware(IShare $share, ?IUser $user = null, ?ISessionAwareController $controller = null): void { |
103 | 152 | $this->request->method('getParam')->willReturnMap([ |
104 | 153 | ['documentId', null, 999], |
105 | 154 | ['shareToken', null, 'token'], |
106 | 155 | ]); |
107 | | - $this->userSession->method('getUser')->willReturn(null); |
| 156 | + $this->userSession->method('getUser')->willReturn($user); |
108 | 157 | $this->shareManager->method('getShareByToken')->willReturn($share); |
109 | 158 |
|
110 | 159 | $folder = $this->createMock(Folder::class); |
111 | | - $folder->method('getFirstNodeById')->willReturn($this->createMock(\OCP\Files\File::class)); |
| 160 | + $folder->method('getFirstNodeById')->willReturn($this->createMock(File::class)); |
112 | 161 | $this->rootFolder->method('getUserFolder')->willReturn($folder); |
113 | 162 |
|
114 | | - $controller = $this->createMock(ISessionAwareController::class); |
| 163 | + $controller ??= $this->createMock(ISessionAwareController::class); |
115 | 164 | self::invokePrivate($this->middleware, 'assertUserOrShareToken', [$controller]); |
116 | 165 | } |
117 | 166 | } |
0 commit comments