@@ -36,26 +36,26 @@ discard block |
||
36 | 36 | use Psr\Log\LoggerInterface; |
37 | 37 | |
38 | 38 | $authBackend = new Auth( |
39 | - Server::get(ISession::class), |
|
40 | - Server::get(IUserSession::class), |
|
41 | - Server::get(IRequest::class), |
|
42 | - Server::get(\OC\Authentication\TwoFactorAuth\Manager::class), |
|
43 | - Server::get(IThrottler::class), |
|
44 | - Server::get(SetupManager::class), |
|
45 | - 'principals/' |
|
39 | + Server::get(ISession::class), |
|
40 | + Server::get(IUserSession::class), |
|
41 | + Server::get(IRequest::class), |
|
42 | + Server::get(\OC\Authentication\TwoFactorAuth\Manager::class), |
|
43 | + Server::get(IThrottler::class), |
|
44 | + Server::get(SetupManager::class), |
|
45 | + 'principals/' |
|
46 | 46 | ); |
47 | 47 | $principalBackend = new Principal( |
48 | - Server::get(IUserManager::class), |
|
49 | - Server::get(IGroupManager::class), |
|
50 | - Server::get(IAccountManager::class), |
|
51 | - Server::get(\OCP\Share\IManager::class), |
|
52 | - Server::get(IUserSession::class), |
|
53 | - Server::get(IAppManager::class), |
|
54 | - Server::get(ProxyMapper::class), |
|
55 | - Server::get(KnownUserService::class), |
|
56 | - Server::get(IConfig::class), |
|
57 | - \OC::$server->getL10NFactory(), |
|
58 | - 'principals/' |
|
48 | + Server::get(IUserManager::class), |
|
49 | + Server::get(IGroupManager::class), |
|
50 | + Server::get(IAccountManager::class), |
|
51 | + Server::get(\OCP\Share\IManager::class), |
|
52 | + Server::get(IUserSession::class), |
|
53 | + Server::get(IAppManager::class), |
|
54 | + Server::get(ProxyMapper::class), |
|
55 | + Server::get(KnownUserService::class), |
|
56 | + Server::get(IConfig::class), |
|
57 | + \OC::$server->getL10NFactory(), |
|
58 | + 'principals/' |
|
59 | 59 | ); |
60 | 60 | $db = Server::get(IDBConnection::class); |
61 | 61 | $userManager = Server::get(IUserManager::class); |
@@ -65,15 +65,15 @@ discard block |
||
65 | 65 | $config = Server::get(IConfig::class); |
66 | 66 | |
67 | 67 | $calDavBackend = new CalDavBackend( |
68 | - $db, |
|
69 | - $principalBackend, |
|
70 | - $userManager, |
|
71 | - $random, |
|
72 | - $logger, |
|
73 | - $dispatcher, |
|
74 | - $config, |
|
75 | - Server::get(\OCA\DAV\CalDAV\Sharing\Backend::class), |
|
76 | - true |
|
68 | + $db, |
|
69 | + $principalBackend, |
|
70 | + $userManager, |
|
71 | + $random, |
|
72 | + $logger, |
|
73 | + $dispatcher, |
|
74 | + $config, |
|
75 | + Server::get(\OCA\DAV\CalDAV\Sharing\Backend::class), |
|
76 | + true |
|
77 | 77 | ); |
78 | 78 | |
79 | 79 | $debugging = Server::get(IConfig::class)->getSystemValue('debug', false); |
@@ -87,8 +87,8 @@ discard block |
||
87 | 87 | $addressBookRoot->disableListing = !$debugging; // Disable listing |
88 | 88 | |
89 | 89 | $nodes = [ |
90 | - $principalCollection, |
|
91 | - $addressBookRoot, |
|
90 | + $principalCollection, |
|
91 | + $addressBookRoot, |
|
92 | 92 | ]; |
93 | 93 | |
94 | 94 | // Fire up server |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | |
105 | 105 | $server->addPlugin(new LegacyDAVACL()); |
106 | 106 | if ($debugging) { |
107 | - $server->addPlugin(new Sabre\DAV\Browser\Plugin()); |
|
107 | + $server->addPlugin(new Sabre\DAV\Browser\Plugin()); |
|
108 | 108 | } |
109 | 109 | |
110 | 110 | $server->addPlugin(new \Sabre\DAV\Sync\Plugin()); |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | $server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin(Server::get(IConfig::class), Server::get(LoggerInterface::class), Server::get(DefaultCalendarValidator::class))); |
113 | 113 | |
114 | 114 | if ($sendInvitations) { |
115 | - $server->addPlugin(Server::get(IMipPlugin::class)); |
|
115 | + $server->addPlugin(Server::get(IMipPlugin::class)); |
|
116 | 116 | } |
117 | 117 | $server->addPlugin(new ExceptionLoggerPlugin('caldav', $logger)); |
118 | 118 | $server->addPlugin(Server::get(RateLimitingPlugin::class)); |
@@ -31,582 +31,582 @@ |
||
31 | 31 | * @group DB |
32 | 32 | */ |
33 | 33 | class AuthTest extends TestCase { |
34 | - private ISession&MockObject $session; |
|
35 | - private Session&MockObject $userSession; |
|
36 | - private IRequest&MockObject $request; |
|
37 | - private Manager&MockObject $twoFactorManager; |
|
38 | - private IThrottler&MockObject $throttler; |
|
39 | - private SetupManager&MockObject $setupManager; |
|
40 | - private Auth $auth; |
|
41 | - |
|
42 | - protected function setUp(): void { |
|
43 | - parent::setUp(); |
|
44 | - $this->session = $this->createMock(ISession::class); |
|
45 | - $this->userSession = $this->createMock(Session::class); |
|
46 | - $this->request = $this->createMock(IRequest::class); |
|
47 | - $this->twoFactorManager = $this->createMock(Manager::class); |
|
48 | - $this->throttler = $this->createMock(IThrottler::class); |
|
49 | - $this->setupManager = $this->createMock(SetupManager::class); |
|
50 | - $this->auth = new Auth( |
|
51 | - $this->session, |
|
52 | - $this->userSession, |
|
53 | - $this->request, |
|
54 | - $this->twoFactorManager, |
|
55 | - $this->throttler, |
|
56 | - $this->setupManager, |
|
57 | - ); |
|
58 | - } |
|
59 | - |
|
60 | - public function testIsDavAuthenticatedWithoutDavSession(): void { |
|
61 | - $this->session |
|
62 | - ->expects($this->once()) |
|
63 | - ->method('get') |
|
64 | - ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
65 | - ->willReturn(null); |
|
66 | - |
|
67 | - $this->assertFalse(self::invokePrivate($this->auth, 'isDavAuthenticated', ['MyTestUser'])); |
|
68 | - } |
|
69 | - |
|
70 | - public function testIsDavAuthenticatedWithWrongDavSession(): void { |
|
71 | - $this->session |
|
72 | - ->expects($this->exactly(2)) |
|
73 | - ->method('get') |
|
74 | - ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
75 | - ->willReturn('AnotherUser'); |
|
76 | - |
|
77 | - $this->assertFalse(self::invokePrivate($this->auth, 'isDavAuthenticated', ['MyTestUser'])); |
|
78 | - } |
|
79 | - |
|
80 | - public function testIsDavAuthenticatedWithCorrectDavSession(): void { |
|
81 | - $this->session |
|
82 | - ->expects($this->exactly(2)) |
|
83 | - ->method('get') |
|
84 | - ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
85 | - ->willReturn('MyTestUser'); |
|
86 | - |
|
87 | - $this->assertTrue(self::invokePrivate($this->auth, 'isDavAuthenticated', ['MyTestUser'])); |
|
88 | - } |
|
89 | - |
|
90 | - public function testValidateUserPassOfAlreadyDAVAuthenticatedUser(): void { |
|
91 | - $user = $this->createMock(IUser::class); |
|
92 | - $user->expects($this->exactly(1)) |
|
93 | - ->method('getUID') |
|
94 | - ->willReturn('MyTestUser'); |
|
95 | - $this->userSession |
|
96 | - ->expects($this->once()) |
|
97 | - ->method('isLoggedIn') |
|
98 | - ->willReturn(true); |
|
99 | - $this->userSession |
|
100 | - ->expects($this->exactly(1)) |
|
101 | - ->method('getUser') |
|
102 | - ->willReturn($user); |
|
103 | - $this->session |
|
104 | - ->expects($this->exactly(2)) |
|
105 | - ->method('get') |
|
106 | - ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
107 | - ->willReturn('MyTestUser'); |
|
108 | - $this->session |
|
109 | - ->expects($this->once()) |
|
110 | - ->method('close'); |
|
111 | - |
|
112 | - $this->assertTrue(self::invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword'])); |
|
113 | - } |
|
114 | - |
|
115 | - public function testValidateUserPassOfInvalidDAVAuthenticatedUser(): void { |
|
116 | - $user = $this->createMock(IUser::class); |
|
117 | - $user->expects($this->once()) |
|
118 | - ->method('getUID') |
|
119 | - ->willReturn('MyTestUser'); |
|
120 | - $this->userSession |
|
121 | - ->expects($this->once()) |
|
122 | - ->method('isLoggedIn') |
|
123 | - ->willReturn(true); |
|
124 | - $this->userSession |
|
125 | - ->expects($this->once()) |
|
126 | - ->method('getUser') |
|
127 | - ->willReturn($user); |
|
128 | - $this->session |
|
129 | - ->expects($this->exactly(2)) |
|
130 | - ->method('get') |
|
131 | - ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
132 | - ->willReturn('AnotherUser'); |
|
133 | - $this->session |
|
134 | - ->expects($this->once()) |
|
135 | - ->method('close'); |
|
136 | - |
|
137 | - $this->assertFalse(self::invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword'])); |
|
138 | - } |
|
139 | - |
|
140 | - public function testValidateUserPassOfInvalidDAVAuthenticatedUserWithValidPassword(): void { |
|
141 | - $user = $this->createMock(IUser::class); |
|
142 | - $user->expects($this->exactly(2)) |
|
143 | - ->method('getUID') |
|
144 | - ->willReturn('MyTestUser'); |
|
145 | - $this->userSession |
|
146 | - ->expects($this->once()) |
|
147 | - ->method('isLoggedIn') |
|
148 | - ->willReturn(true); |
|
149 | - $this->userSession |
|
150 | - ->expects($this->exactly(2)) |
|
151 | - ->method('getUser') |
|
152 | - ->willReturn($user); |
|
153 | - $this->session |
|
154 | - ->expects($this->exactly(2)) |
|
155 | - ->method('get') |
|
156 | - ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
157 | - ->willReturn('AnotherUser'); |
|
158 | - $this->userSession |
|
159 | - ->expects($this->once()) |
|
160 | - ->method('logClientIn') |
|
161 | - ->with('MyTestUser', 'MyTestPassword', $this->request) |
|
162 | - ->willReturn(true); |
|
163 | - $this->session |
|
164 | - ->expects($this->once()) |
|
165 | - ->method('set') |
|
166 | - ->with('AUTHENTICATED_TO_DAV_BACKEND', 'MyTestUser'); |
|
167 | - $this->session |
|
168 | - ->expects($this->once()) |
|
169 | - ->method('close'); |
|
170 | - |
|
171 | - $this->assertTrue(self::invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword'])); |
|
172 | - } |
|
173 | - |
|
174 | - public function testValidateUserPassWithInvalidPassword(): void { |
|
175 | - $this->userSession |
|
176 | - ->expects($this->once()) |
|
177 | - ->method('isLoggedIn') |
|
178 | - ->willReturn(false); |
|
179 | - $this->userSession |
|
180 | - ->expects($this->once()) |
|
181 | - ->method('logClientIn') |
|
182 | - ->with('MyTestUser', 'MyTestPassword') |
|
183 | - ->willReturn(false); |
|
184 | - $this->session |
|
185 | - ->expects($this->once()) |
|
186 | - ->method('close'); |
|
187 | - |
|
188 | - $this->assertFalse(self::invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword'])); |
|
189 | - } |
|
190 | - |
|
191 | - |
|
192 | - public function testValidateUserPassWithPasswordLoginForbidden(): void { |
|
193 | - $this->expectException(PasswordLoginForbidden::class); |
|
194 | - |
|
195 | - $this->userSession |
|
196 | - ->expects($this->once()) |
|
197 | - ->method('isLoggedIn') |
|
198 | - ->willReturn(false); |
|
199 | - $this->userSession |
|
200 | - ->expects($this->once()) |
|
201 | - ->method('logClientIn') |
|
202 | - ->with('MyTestUser', 'MyTestPassword') |
|
203 | - ->willThrowException(new PasswordLoginForbiddenException()); |
|
204 | - $this->session |
|
205 | - ->expects($this->once()) |
|
206 | - ->method('close'); |
|
207 | - |
|
208 | - self::invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword']); |
|
209 | - } |
|
210 | - |
|
211 | - public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenForNonGet(): void { |
|
212 | - $request = $this->createMock(RequestInterface::class); |
|
213 | - $response = $this->createMock(ResponseInterface::class); |
|
214 | - $this->userSession |
|
215 | - ->expects($this->any()) |
|
216 | - ->method('isLoggedIn') |
|
217 | - ->willReturn(true); |
|
218 | - $this->request |
|
219 | - ->expects($this->any()) |
|
220 | - ->method('getMethod') |
|
221 | - ->willReturn('POST'); |
|
222 | - $this->session |
|
223 | - ->expects($this->any()) |
|
224 | - ->method('get') |
|
225 | - ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
226 | - ->willReturn(null); |
|
227 | - $user = $this->createMock(IUser::class); |
|
228 | - $user->expects($this->any()) |
|
229 | - ->method('getUID') |
|
230 | - ->willReturn('MyWrongDavUser'); |
|
231 | - $this->userSession |
|
232 | - ->expects($this->any()) |
|
233 | - ->method('getUser') |
|
234 | - ->willReturn($user); |
|
235 | - $this->request |
|
236 | - ->expects($this->once()) |
|
237 | - ->method('passesCSRFCheck') |
|
238 | - ->willReturn(false); |
|
239 | - |
|
240 | - $expectedResponse = [ |
|
241 | - false, |
|
242 | - "No 'Authorization: Basic' header found. Either the client didn't send one, or the server is misconfigured", |
|
243 | - ]; |
|
244 | - $response = $this->auth->check($request, $response); |
|
245 | - $this->assertSame($expectedResponse, $response); |
|
246 | - } |
|
247 | - |
|
248 | - public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenAndCorrectlyDavAuthenticated(): void { |
|
249 | - $request = $this->createMock(RequestInterface::class); |
|
250 | - $response = $this->createMock(ResponseInterface::class); |
|
251 | - $this->userSession |
|
252 | - ->expects($this->any()) |
|
253 | - ->method('isLoggedIn') |
|
254 | - ->willReturn(true); |
|
255 | - $this->request |
|
256 | - ->expects($this->any()) |
|
257 | - ->method('getMethod') |
|
258 | - ->willReturn('PROPFIND'); |
|
259 | - $this->request |
|
260 | - ->expects($this->any()) |
|
261 | - ->method('isUserAgent') |
|
262 | - ->willReturn(false); |
|
263 | - $this->session |
|
264 | - ->expects($this->any()) |
|
265 | - ->method('get') |
|
266 | - ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
267 | - ->willReturn('LoggedInUser'); |
|
268 | - $user = $this->createMock(IUser::class); |
|
269 | - $user->expects($this->any()) |
|
270 | - ->method('getUID') |
|
271 | - ->willReturn('LoggedInUser'); |
|
272 | - $this->userSession |
|
273 | - ->expects($this->any()) |
|
274 | - ->method('getUser') |
|
275 | - ->willReturn($user); |
|
276 | - $this->request |
|
277 | - ->expects($this->once()) |
|
278 | - ->method('passesCSRFCheck') |
|
279 | - ->willReturn(false); |
|
280 | - $this->auth->check($request, $response); |
|
281 | - } |
|
282 | - |
|
283 | - |
|
284 | - public function testAuthenticateAlreadyLoggedInWithoutTwoFactorChallengePassed(): void { |
|
285 | - $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class); |
|
286 | - $this->expectExceptionMessage('2FA challenge not passed.'); |
|
287 | - |
|
288 | - $request = $this->createMock(RequestInterface::class); |
|
289 | - $response = $this->createMock(ResponseInterface::class); |
|
290 | - $this->userSession |
|
291 | - ->expects($this->any()) |
|
292 | - ->method('isLoggedIn') |
|
293 | - ->willReturn(true); |
|
294 | - $this->request |
|
295 | - ->expects($this->any()) |
|
296 | - ->method('getMethod') |
|
297 | - ->willReturn('PROPFIND'); |
|
298 | - $this->request |
|
299 | - ->expects($this->any()) |
|
300 | - ->method('isUserAgent') |
|
301 | - ->willReturn(false); |
|
302 | - $this->session |
|
303 | - ->expects($this->any()) |
|
304 | - ->method('get') |
|
305 | - ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
306 | - ->willReturn('LoggedInUser'); |
|
307 | - $user = $this->createMock(IUser::class); |
|
308 | - $user->expects($this->any()) |
|
309 | - ->method('getUID') |
|
310 | - ->willReturn('LoggedInUser'); |
|
311 | - $this->userSession |
|
312 | - ->expects($this->any()) |
|
313 | - ->method('getUser') |
|
314 | - ->willReturn($user); |
|
315 | - $this->request |
|
316 | - ->expects($this->once()) |
|
317 | - ->method('passesCSRFCheck') |
|
318 | - ->willReturn(true); |
|
319 | - $this->twoFactorManager->expects($this->once()) |
|
320 | - ->method('needsSecondFactor') |
|
321 | - ->with($user) |
|
322 | - ->willReturn(true); |
|
323 | - $this->auth->check($request, $response); |
|
324 | - } |
|
325 | - |
|
326 | - |
|
327 | - public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenAndIncorrectlyDavAuthenticated(): void { |
|
328 | - $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class); |
|
329 | - $this->expectExceptionMessage('CSRF check not passed.'); |
|
330 | - |
|
331 | - $request = $this->createMock(RequestInterface::class); |
|
332 | - $response = $this->createMock(ResponseInterface::class); |
|
333 | - $this->userSession |
|
334 | - ->expects($this->any()) |
|
335 | - ->method('isLoggedIn') |
|
336 | - ->willReturn(true); |
|
337 | - $this->request |
|
338 | - ->expects($this->any()) |
|
339 | - ->method('getMethod') |
|
340 | - ->willReturn('PROPFIND'); |
|
341 | - $this->request |
|
342 | - ->expects($this->any()) |
|
343 | - ->method('isUserAgent') |
|
344 | - ->willReturn(false); |
|
345 | - $this->session |
|
346 | - ->expects($this->any()) |
|
347 | - ->method('get') |
|
348 | - ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
349 | - ->willReturn('AnotherUser'); |
|
350 | - $user = $this->createMock(IUser::class); |
|
351 | - $user->expects($this->any()) |
|
352 | - ->method('getUID') |
|
353 | - ->willReturn('LoggedInUser'); |
|
354 | - $this->userSession |
|
355 | - ->expects($this->any()) |
|
356 | - ->method('getUser') |
|
357 | - ->willReturn($user); |
|
358 | - $this->request |
|
359 | - ->expects($this->once()) |
|
360 | - ->method('passesCSRFCheck') |
|
361 | - ->willReturn(false); |
|
362 | - $this->auth->check($request, $response); |
|
363 | - } |
|
364 | - |
|
365 | - public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenForNonGetAndDesktopClient(): void { |
|
366 | - $request = $this->createMock(RequestInterface::class); |
|
367 | - $response = $this->createMock(ResponseInterface::class); |
|
368 | - $this->userSession |
|
369 | - ->expects($this->any()) |
|
370 | - ->method('isLoggedIn') |
|
371 | - ->willReturn(true); |
|
372 | - $this->request |
|
373 | - ->expects($this->any()) |
|
374 | - ->method('getMethod') |
|
375 | - ->willReturn('POST'); |
|
376 | - $this->request |
|
377 | - ->expects($this->any()) |
|
378 | - ->method('isUserAgent') |
|
379 | - ->willReturn(true); |
|
380 | - $this->session |
|
381 | - ->expects($this->any()) |
|
382 | - ->method('get') |
|
383 | - ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
384 | - ->willReturn(null); |
|
385 | - $user = $this->createMock(IUser::class); |
|
386 | - $user->expects($this->any()) |
|
387 | - ->method('getUID') |
|
388 | - ->willReturn('MyWrongDavUser'); |
|
389 | - $this->userSession |
|
390 | - ->expects($this->any()) |
|
391 | - ->method('getUser') |
|
392 | - ->willReturn($user); |
|
393 | - $this->request |
|
394 | - ->expects($this->once()) |
|
395 | - ->method('passesCSRFCheck') |
|
396 | - ->willReturn(false); |
|
397 | - |
|
398 | - $this->auth->check($request, $response); |
|
399 | - } |
|
400 | - |
|
401 | - public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenForGet(): void { |
|
402 | - $request = $this->createMock(RequestInterface::class); |
|
403 | - $response = $this->createMock(ResponseInterface::class); |
|
404 | - $this->userSession |
|
405 | - ->expects($this->any()) |
|
406 | - ->method('isLoggedIn') |
|
407 | - ->willReturn(true); |
|
408 | - $this->session |
|
409 | - ->expects($this->any()) |
|
410 | - ->method('get') |
|
411 | - ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
412 | - ->willReturn(null); |
|
413 | - $user = $this->createMock(IUser::class); |
|
414 | - $user->expects($this->any()) |
|
415 | - ->method('getUID') |
|
416 | - ->willReturn('MyWrongDavUser'); |
|
417 | - $this->userSession |
|
418 | - ->expects($this->any()) |
|
419 | - ->method('getUser') |
|
420 | - ->willReturn($user); |
|
421 | - $this->request |
|
422 | - ->expects($this->any()) |
|
423 | - ->method('getMethod') |
|
424 | - ->willReturn('GET'); |
|
425 | - |
|
426 | - $response = $this->auth->check($request, $response); |
|
427 | - $this->assertEquals([true, 'principals/users/MyWrongDavUser'], $response); |
|
428 | - } |
|
429 | - |
|
430 | - public function testAuthenticateAlreadyLoggedInWithCsrfTokenForGet(): void { |
|
431 | - $request = $this->createMock(RequestInterface::class); |
|
432 | - $response = $this->createMock(ResponseInterface::class); |
|
433 | - $this->userSession |
|
434 | - ->expects($this->any()) |
|
435 | - ->method('isLoggedIn') |
|
436 | - ->willReturn(true); |
|
437 | - $this->session |
|
438 | - ->expects($this->any()) |
|
439 | - ->method('get') |
|
440 | - ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
441 | - ->willReturn(null); |
|
442 | - $user = $this->createMock(IUser::class); |
|
443 | - $user->expects($this->any()) |
|
444 | - ->method('getUID') |
|
445 | - ->willReturn('MyWrongDavUser'); |
|
446 | - $this->userSession |
|
447 | - ->expects($this->any()) |
|
448 | - ->method('getUser') |
|
449 | - ->willReturn($user); |
|
450 | - $this->request |
|
451 | - ->expects($this->once()) |
|
452 | - ->method('passesCSRFCheck') |
|
453 | - ->willReturn(true); |
|
454 | - |
|
455 | - $response = $this->auth->check($request, $response); |
|
456 | - $this->assertEquals([true, 'principals/users/MyWrongDavUser'], $response); |
|
457 | - } |
|
458 | - |
|
459 | - public function testAuthenticateNoBasicAuthenticateHeadersProvided(): void { |
|
460 | - $server = $this->createMock(Server::class); |
|
461 | - $server->httpRequest = $this->createMock(RequestInterface::class); |
|
462 | - $server->httpResponse = $this->createMock(ResponseInterface::class); |
|
463 | - $response = $this->auth->check($server->httpRequest, $server->httpResponse); |
|
464 | - $this->assertEquals([false, 'No \'Authorization: Basic\' header found. Either the client didn\'t send one, or the server is misconfigured'], $response); |
|
465 | - } |
|
466 | - |
|
467 | - |
|
468 | - public function testAuthenticateNoBasicAuthenticateHeadersProvidedWithAjax(): void { |
|
469 | - $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class); |
|
470 | - $this->expectExceptionMessage('Cannot authenticate over ajax calls'); |
|
471 | - |
|
472 | - /** @var \Sabre\HTTP\RequestInterface&MockObject $httpRequest */ |
|
473 | - $httpRequest = $this->createMock(RequestInterface::class); |
|
474 | - /** @var \Sabre\HTTP\ResponseInterface&MockObject $httpResponse */ |
|
475 | - $httpResponse = $this->createMock(ResponseInterface::class); |
|
476 | - $this->userSession |
|
477 | - ->expects($this->any()) |
|
478 | - ->method('isLoggedIn') |
|
479 | - ->willReturn(false); |
|
480 | - $httpRequest |
|
481 | - ->expects($this->exactly(2)) |
|
482 | - ->method('getHeader') |
|
483 | - ->willReturnMap([ |
|
484 | - ['X-Requested-With', 'XMLHttpRequest'], |
|
485 | - ['Authorization', null], |
|
486 | - ]); |
|
487 | - |
|
488 | - $this->auth->check($httpRequest, $httpResponse); |
|
489 | - } |
|
490 | - |
|
491 | - public function testAuthenticateWithBasicAuthenticateHeadersProvidedWithAjax(): void { |
|
492 | - // No CSRF |
|
493 | - $this->request |
|
494 | - ->expects($this->once()) |
|
495 | - ->method('passesCSRFCheck') |
|
496 | - ->willReturn(false); |
|
497 | - |
|
498 | - /** @var \Sabre\HTTP\RequestInterface&MockObject $httpRequest */ |
|
499 | - $httpRequest = $this->createMock(RequestInterface::class); |
|
500 | - /** @var \Sabre\HTTP\ResponseInterface&MockObject $httpResponse */ |
|
501 | - $httpResponse = $this->createMock(ResponseInterface::class); |
|
502 | - $httpRequest |
|
503 | - ->expects($this->any()) |
|
504 | - ->method('getHeader') |
|
505 | - ->willReturnMap([ |
|
506 | - ['X-Requested-With', 'XMLHttpRequest'], |
|
507 | - ['Authorization', 'basic dXNlcm5hbWU6cGFzc3dvcmQ='], |
|
508 | - ]); |
|
509 | - |
|
510 | - $user = $this->createMock(IUser::class); |
|
511 | - $user->expects($this->any()) |
|
512 | - ->method('getUID') |
|
513 | - ->willReturn('MyDavUser'); |
|
514 | - $this->userSession |
|
515 | - ->expects($this->any()) |
|
516 | - ->method('isLoggedIn') |
|
517 | - ->willReturn(false); |
|
518 | - $this->userSession |
|
519 | - ->expects($this->once()) |
|
520 | - ->method('logClientIn') |
|
521 | - ->with('username', 'password') |
|
522 | - ->willReturn(true); |
|
523 | - $this->userSession |
|
524 | - ->expects($this->any()) |
|
525 | - ->method('getUser') |
|
526 | - ->willReturn($user); |
|
527 | - |
|
528 | - $this->auth->check($httpRequest, $httpResponse); |
|
529 | - } |
|
530 | - |
|
531 | - public function testAuthenticateNoBasicAuthenticateHeadersProvidedWithAjaxButUserIsStillLoggedIn(): void { |
|
532 | - /** @var \Sabre\HTTP\RequestInterface $httpRequest */ |
|
533 | - $httpRequest = $this->createMock(RequestInterface::class); |
|
534 | - /** @var \Sabre\HTTP\ResponseInterface $httpResponse */ |
|
535 | - $httpResponse = $this->createMock(ResponseInterface::class); |
|
536 | - $user = $this->createMock(IUser::class); |
|
537 | - $user->method('getUID')->willReturn('MyTestUser'); |
|
538 | - $this->userSession |
|
539 | - ->expects($this->any()) |
|
540 | - ->method('isLoggedIn') |
|
541 | - ->willReturn(true); |
|
542 | - $this->userSession |
|
543 | - ->expects($this->any()) |
|
544 | - ->method('getUser') |
|
545 | - ->willReturn($user); |
|
546 | - $this->session |
|
547 | - ->expects($this->atLeastOnce()) |
|
548 | - ->method('get') |
|
549 | - ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
550 | - ->willReturn('MyTestUser'); |
|
551 | - $this->request |
|
552 | - ->expects($this->once()) |
|
553 | - ->method('getMethod') |
|
554 | - ->willReturn('GET'); |
|
555 | - $httpRequest |
|
556 | - ->expects($this->atLeastOnce()) |
|
557 | - ->method('getHeader') |
|
558 | - ->with('Authorization') |
|
559 | - ->willReturn(null); |
|
560 | - $this->assertEquals( |
|
561 | - [true, 'principals/users/MyTestUser'], |
|
562 | - $this->auth->check($httpRequest, $httpResponse) |
|
563 | - ); |
|
564 | - } |
|
565 | - |
|
566 | - public function testAuthenticateValidCredentials(): void { |
|
567 | - $server = $this->createMock(Server::class); |
|
568 | - $server->httpRequest = $this->createMock(RequestInterface::class); |
|
569 | - $server->httpRequest |
|
570 | - ->expects($this->once()) |
|
571 | - ->method('getHeader') |
|
572 | - ->with('Authorization') |
|
573 | - ->willReturn('basic dXNlcm5hbWU6cGFzc3dvcmQ='); |
|
574 | - |
|
575 | - $server->httpResponse = $this->createMock(ResponseInterface::class); |
|
576 | - $this->userSession |
|
577 | - ->expects($this->once()) |
|
578 | - ->method('logClientIn') |
|
579 | - ->with('username', 'password') |
|
580 | - ->willReturn(true); |
|
581 | - $user = $this->createMock(IUser::class); |
|
582 | - $user->expects($this->exactly(2)) |
|
583 | - ->method('getUID') |
|
584 | - ->willReturn('MyTestUser'); |
|
585 | - $this->userSession |
|
586 | - ->expects($this->exactly(4)) |
|
587 | - ->method('getUser') |
|
588 | - ->willReturn($user); |
|
589 | - $response = $this->auth->check($server->httpRequest, $server->httpResponse); |
|
590 | - $this->assertEquals([true, 'principals/users/MyTestUser'], $response); |
|
591 | - } |
|
592 | - |
|
593 | - public function testAuthenticateInvalidCredentials(): void { |
|
594 | - $server = $this->createMock(Server::class); |
|
595 | - $server->httpRequest = $this->createMock(RequestInterface::class); |
|
596 | - $server->httpRequest |
|
597 | - ->expects($this->exactly(2)) |
|
598 | - ->method('getHeader') |
|
599 | - ->willReturnMap([ |
|
600 | - ['Authorization', 'basic dXNlcm5hbWU6cGFzc3dvcmQ='], |
|
601 | - ['X-Requested-With', null], |
|
602 | - ]); |
|
603 | - $server->httpResponse = $this->createMock(ResponseInterface::class); |
|
604 | - $this->userSession |
|
605 | - ->expects($this->once()) |
|
606 | - ->method('logClientIn') |
|
607 | - ->with('username', 'password') |
|
608 | - ->willReturn(false); |
|
609 | - $response = $this->auth->check($server->httpRequest, $server->httpResponse); |
|
610 | - $this->assertEquals([false, 'Username or password was incorrect'], $response); |
|
611 | - } |
|
34 | + private ISession&MockObject $session; |
|
35 | + private Session&MockObject $userSession; |
|
36 | + private IRequest&MockObject $request; |
|
37 | + private Manager&MockObject $twoFactorManager; |
|
38 | + private IThrottler&MockObject $throttler; |
|
39 | + private SetupManager&MockObject $setupManager; |
|
40 | + private Auth $auth; |
|
41 | + |
|
42 | + protected function setUp(): void { |
|
43 | + parent::setUp(); |
|
44 | + $this->session = $this->createMock(ISession::class); |
|
45 | + $this->userSession = $this->createMock(Session::class); |
|
46 | + $this->request = $this->createMock(IRequest::class); |
|
47 | + $this->twoFactorManager = $this->createMock(Manager::class); |
|
48 | + $this->throttler = $this->createMock(IThrottler::class); |
|
49 | + $this->setupManager = $this->createMock(SetupManager::class); |
|
50 | + $this->auth = new Auth( |
|
51 | + $this->session, |
|
52 | + $this->userSession, |
|
53 | + $this->request, |
|
54 | + $this->twoFactorManager, |
|
55 | + $this->throttler, |
|
56 | + $this->setupManager, |
|
57 | + ); |
|
58 | + } |
|
59 | + |
|
60 | + public function testIsDavAuthenticatedWithoutDavSession(): void { |
|
61 | + $this->session |
|
62 | + ->expects($this->once()) |
|
63 | + ->method('get') |
|
64 | + ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
65 | + ->willReturn(null); |
|
66 | + |
|
67 | + $this->assertFalse(self::invokePrivate($this->auth, 'isDavAuthenticated', ['MyTestUser'])); |
|
68 | + } |
|
69 | + |
|
70 | + public function testIsDavAuthenticatedWithWrongDavSession(): void { |
|
71 | + $this->session |
|
72 | + ->expects($this->exactly(2)) |
|
73 | + ->method('get') |
|
74 | + ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
75 | + ->willReturn('AnotherUser'); |
|
76 | + |
|
77 | + $this->assertFalse(self::invokePrivate($this->auth, 'isDavAuthenticated', ['MyTestUser'])); |
|
78 | + } |
|
79 | + |
|
80 | + public function testIsDavAuthenticatedWithCorrectDavSession(): void { |
|
81 | + $this->session |
|
82 | + ->expects($this->exactly(2)) |
|
83 | + ->method('get') |
|
84 | + ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
85 | + ->willReturn('MyTestUser'); |
|
86 | + |
|
87 | + $this->assertTrue(self::invokePrivate($this->auth, 'isDavAuthenticated', ['MyTestUser'])); |
|
88 | + } |
|
89 | + |
|
90 | + public function testValidateUserPassOfAlreadyDAVAuthenticatedUser(): void { |
|
91 | + $user = $this->createMock(IUser::class); |
|
92 | + $user->expects($this->exactly(1)) |
|
93 | + ->method('getUID') |
|
94 | + ->willReturn('MyTestUser'); |
|
95 | + $this->userSession |
|
96 | + ->expects($this->once()) |
|
97 | + ->method('isLoggedIn') |
|
98 | + ->willReturn(true); |
|
99 | + $this->userSession |
|
100 | + ->expects($this->exactly(1)) |
|
101 | + ->method('getUser') |
|
102 | + ->willReturn($user); |
|
103 | + $this->session |
|
104 | + ->expects($this->exactly(2)) |
|
105 | + ->method('get') |
|
106 | + ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
107 | + ->willReturn('MyTestUser'); |
|
108 | + $this->session |
|
109 | + ->expects($this->once()) |
|
110 | + ->method('close'); |
|
111 | + |
|
112 | + $this->assertTrue(self::invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword'])); |
|
113 | + } |
|
114 | + |
|
115 | + public function testValidateUserPassOfInvalidDAVAuthenticatedUser(): void { |
|
116 | + $user = $this->createMock(IUser::class); |
|
117 | + $user->expects($this->once()) |
|
118 | + ->method('getUID') |
|
119 | + ->willReturn('MyTestUser'); |
|
120 | + $this->userSession |
|
121 | + ->expects($this->once()) |
|
122 | + ->method('isLoggedIn') |
|
123 | + ->willReturn(true); |
|
124 | + $this->userSession |
|
125 | + ->expects($this->once()) |
|
126 | + ->method('getUser') |
|
127 | + ->willReturn($user); |
|
128 | + $this->session |
|
129 | + ->expects($this->exactly(2)) |
|
130 | + ->method('get') |
|
131 | + ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
132 | + ->willReturn('AnotherUser'); |
|
133 | + $this->session |
|
134 | + ->expects($this->once()) |
|
135 | + ->method('close'); |
|
136 | + |
|
137 | + $this->assertFalse(self::invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword'])); |
|
138 | + } |
|
139 | + |
|
140 | + public function testValidateUserPassOfInvalidDAVAuthenticatedUserWithValidPassword(): void { |
|
141 | + $user = $this->createMock(IUser::class); |
|
142 | + $user->expects($this->exactly(2)) |
|
143 | + ->method('getUID') |
|
144 | + ->willReturn('MyTestUser'); |
|
145 | + $this->userSession |
|
146 | + ->expects($this->once()) |
|
147 | + ->method('isLoggedIn') |
|
148 | + ->willReturn(true); |
|
149 | + $this->userSession |
|
150 | + ->expects($this->exactly(2)) |
|
151 | + ->method('getUser') |
|
152 | + ->willReturn($user); |
|
153 | + $this->session |
|
154 | + ->expects($this->exactly(2)) |
|
155 | + ->method('get') |
|
156 | + ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
157 | + ->willReturn('AnotherUser'); |
|
158 | + $this->userSession |
|
159 | + ->expects($this->once()) |
|
160 | + ->method('logClientIn') |
|
161 | + ->with('MyTestUser', 'MyTestPassword', $this->request) |
|
162 | + ->willReturn(true); |
|
163 | + $this->session |
|
164 | + ->expects($this->once()) |
|
165 | + ->method('set') |
|
166 | + ->with('AUTHENTICATED_TO_DAV_BACKEND', 'MyTestUser'); |
|
167 | + $this->session |
|
168 | + ->expects($this->once()) |
|
169 | + ->method('close'); |
|
170 | + |
|
171 | + $this->assertTrue(self::invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword'])); |
|
172 | + } |
|
173 | + |
|
174 | + public function testValidateUserPassWithInvalidPassword(): void { |
|
175 | + $this->userSession |
|
176 | + ->expects($this->once()) |
|
177 | + ->method('isLoggedIn') |
|
178 | + ->willReturn(false); |
|
179 | + $this->userSession |
|
180 | + ->expects($this->once()) |
|
181 | + ->method('logClientIn') |
|
182 | + ->with('MyTestUser', 'MyTestPassword') |
|
183 | + ->willReturn(false); |
|
184 | + $this->session |
|
185 | + ->expects($this->once()) |
|
186 | + ->method('close'); |
|
187 | + |
|
188 | + $this->assertFalse(self::invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword'])); |
|
189 | + } |
|
190 | + |
|
191 | + |
|
192 | + public function testValidateUserPassWithPasswordLoginForbidden(): void { |
|
193 | + $this->expectException(PasswordLoginForbidden::class); |
|
194 | + |
|
195 | + $this->userSession |
|
196 | + ->expects($this->once()) |
|
197 | + ->method('isLoggedIn') |
|
198 | + ->willReturn(false); |
|
199 | + $this->userSession |
|
200 | + ->expects($this->once()) |
|
201 | + ->method('logClientIn') |
|
202 | + ->with('MyTestUser', 'MyTestPassword') |
|
203 | + ->willThrowException(new PasswordLoginForbiddenException()); |
|
204 | + $this->session |
|
205 | + ->expects($this->once()) |
|
206 | + ->method('close'); |
|
207 | + |
|
208 | + self::invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword']); |
|
209 | + } |
|
210 | + |
|
211 | + public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenForNonGet(): void { |
|
212 | + $request = $this->createMock(RequestInterface::class); |
|
213 | + $response = $this->createMock(ResponseInterface::class); |
|
214 | + $this->userSession |
|
215 | + ->expects($this->any()) |
|
216 | + ->method('isLoggedIn') |
|
217 | + ->willReturn(true); |
|
218 | + $this->request |
|
219 | + ->expects($this->any()) |
|
220 | + ->method('getMethod') |
|
221 | + ->willReturn('POST'); |
|
222 | + $this->session |
|
223 | + ->expects($this->any()) |
|
224 | + ->method('get') |
|
225 | + ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
226 | + ->willReturn(null); |
|
227 | + $user = $this->createMock(IUser::class); |
|
228 | + $user->expects($this->any()) |
|
229 | + ->method('getUID') |
|
230 | + ->willReturn('MyWrongDavUser'); |
|
231 | + $this->userSession |
|
232 | + ->expects($this->any()) |
|
233 | + ->method('getUser') |
|
234 | + ->willReturn($user); |
|
235 | + $this->request |
|
236 | + ->expects($this->once()) |
|
237 | + ->method('passesCSRFCheck') |
|
238 | + ->willReturn(false); |
|
239 | + |
|
240 | + $expectedResponse = [ |
|
241 | + false, |
|
242 | + "No 'Authorization: Basic' header found. Either the client didn't send one, or the server is misconfigured", |
|
243 | + ]; |
|
244 | + $response = $this->auth->check($request, $response); |
|
245 | + $this->assertSame($expectedResponse, $response); |
|
246 | + } |
|
247 | + |
|
248 | + public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenAndCorrectlyDavAuthenticated(): void { |
|
249 | + $request = $this->createMock(RequestInterface::class); |
|
250 | + $response = $this->createMock(ResponseInterface::class); |
|
251 | + $this->userSession |
|
252 | + ->expects($this->any()) |
|
253 | + ->method('isLoggedIn') |
|
254 | + ->willReturn(true); |
|
255 | + $this->request |
|
256 | + ->expects($this->any()) |
|
257 | + ->method('getMethod') |
|
258 | + ->willReturn('PROPFIND'); |
|
259 | + $this->request |
|
260 | + ->expects($this->any()) |
|
261 | + ->method('isUserAgent') |
|
262 | + ->willReturn(false); |
|
263 | + $this->session |
|
264 | + ->expects($this->any()) |
|
265 | + ->method('get') |
|
266 | + ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
267 | + ->willReturn('LoggedInUser'); |
|
268 | + $user = $this->createMock(IUser::class); |
|
269 | + $user->expects($this->any()) |
|
270 | + ->method('getUID') |
|
271 | + ->willReturn('LoggedInUser'); |
|
272 | + $this->userSession |
|
273 | + ->expects($this->any()) |
|
274 | + ->method('getUser') |
|
275 | + ->willReturn($user); |
|
276 | + $this->request |
|
277 | + ->expects($this->once()) |
|
278 | + ->method('passesCSRFCheck') |
|
279 | + ->willReturn(false); |
|
280 | + $this->auth->check($request, $response); |
|
281 | + } |
|
282 | + |
|
283 | + |
|
284 | + public function testAuthenticateAlreadyLoggedInWithoutTwoFactorChallengePassed(): void { |
|
285 | + $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class); |
|
286 | + $this->expectExceptionMessage('2FA challenge not passed.'); |
|
287 | + |
|
288 | + $request = $this->createMock(RequestInterface::class); |
|
289 | + $response = $this->createMock(ResponseInterface::class); |
|
290 | + $this->userSession |
|
291 | + ->expects($this->any()) |
|
292 | + ->method('isLoggedIn') |
|
293 | + ->willReturn(true); |
|
294 | + $this->request |
|
295 | + ->expects($this->any()) |
|
296 | + ->method('getMethod') |
|
297 | + ->willReturn('PROPFIND'); |
|
298 | + $this->request |
|
299 | + ->expects($this->any()) |
|
300 | + ->method('isUserAgent') |
|
301 | + ->willReturn(false); |
|
302 | + $this->session |
|
303 | + ->expects($this->any()) |
|
304 | + ->method('get') |
|
305 | + ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
306 | + ->willReturn('LoggedInUser'); |
|
307 | + $user = $this->createMock(IUser::class); |
|
308 | + $user->expects($this->any()) |
|
309 | + ->method('getUID') |
|
310 | + ->willReturn('LoggedInUser'); |
|
311 | + $this->userSession |
|
312 | + ->expects($this->any()) |
|
313 | + ->method('getUser') |
|
314 | + ->willReturn($user); |
|
315 | + $this->request |
|
316 | + ->expects($this->once()) |
|
317 | + ->method('passesCSRFCheck') |
|
318 | + ->willReturn(true); |
|
319 | + $this->twoFactorManager->expects($this->once()) |
|
320 | + ->method('needsSecondFactor') |
|
321 | + ->with($user) |
|
322 | + ->willReturn(true); |
|
323 | + $this->auth->check($request, $response); |
|
324 | + } |
|
325 | + |
|
326 | + |
|
327 | + public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenAndIncorrectlyDavAuthenticated(): void { |
|
328 | + $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class); |
|
329 | + $this->expectExceptionMessage('CSRF check not passed.'); |
|
330 | + |
|
331 | + $request = $this->createMock(RequestInterface::class); |
|
332 | + $response = $this->createMock(ResponseInterface::class); |
|
333 | + $this->userSession |
|
334 | + ->expects($this->any()) |
|
335 | + ->method('isLoggedIn') |
|
336 | + ->willReturn(true); |
|
337 | + $this->request |
|
338 | + ->expects($this->any()) |
|
339 | + ->method('getMethod') |
|
340 | + ->willReturn('PROPFIND'); |
|
341 | + $this->request |
|
342 | + ->expects($this->any()) |
|
343 | + ->method('isUserAgent') |
|
344 | + ->willReturn(false); |
|
345 | + $this->session |
|
346 | + ->expects($this->any()) |
|
347 | + ->method('get') |
|
348 | + ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
349 | + ->willReturn('AnotherUser'); |
|
350 | + $user = $this->createMock(IUser::class); |
|
351 | + $user->expects($this->any()) |
|
352 | + ->method('getUID') |
|
353 | + ->willReturn('LoggedInUser'); |
|
354 | + $this->userSession |
|
355 | + ->expects($this->any()) |
|
356 | + ->method('getUser') |
|
357 | + ->willReturn($user); |
|
358 | + $this->request |
|
359 | + ->expects($this->once()) |
|
360 | + ->method('passesCSRFCheck') |
|
361 | + ->willReturn(false); |
|
362 | + $this->auth->check($request, $response); |
|
363 | + } |
|
364 | + |
|
365 | + public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenForNonGetAndDesktopClient(): void { |
|
366 | + $request = $this->createMock(RequestInterface::class); |
|
367 | + $response = $this->createMock(ResponseInterface::class); |
|
368 | + $this->userSession |
|
369 | + ->expects($this->any()) |
|
370 | + ->method('isLoggedIn') |
|
371 | + ->willReturn(true); |
|
372 | + $this->request |
|
373 | + ->expects($this->any()) |
|
374 | + ->method('getMethod') |
|
375 | + ->willReturn('POST'); |
|
376 | + $this->request |
|
377 | + ->expects($this->any()) |
|
378 | + ->method('isUserAgent') |
|
379 | + ->willReturn(true); |
|
380 | + $this->session |
|
381 | + ->expects($this->any()) |
|
382 | + ->method('get') |
|
383 | + ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
384 | + ->willReturn(null); |
|
385 | + $user = $this->createMock(IUser::class); |
|
386 | + $user->expects($this->any()) |
|
387 | + ->method('getUID') |
|
388 | + ->willReturn('MyWrongDavUser'); |
|
389 | + $this->userSession |
|
390 | + ->expects($this->any()) |
|
391 | + ->method('getUser') |
|
392 | + ->willReturn($user); |
|
393 | + $this->request |
|
394 | + ->expects($this->once()) |
|
395 | + ->method('passesCSRFCheck') |
|
396 | + ->willReturn(false); |
|
397 | + |
|
398 | + $this->auth->check($request, $response); |
|
399 | + } |
|
400 | + |
|
401 | + public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenForGet(): void { |
|
402 | + $request = $this->createMock(RequestInterface::class); |
|
403 | + $response = $this->createMock(ResponseInterface::class); |
|
404 | + $this->userSession |
|
405 | + ->expects($this->any()) |
|
406 | + ->method('isLoggedIn') |
|
407 | + ->willReturn(true); |
|
408 | + $this->session |
|
409 | + ->expects($this->any()) |
|
410 | + ->method('get') |
|
411 | + ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
412 | + ->willReturn(null); |
|
413 | + $user = $this->createMock(IUser::class); |
|
414 | + $user->expects($this->any()) |
|
415 | + ->method('getUID') |
|
416 | + ->willReturn('MyWrongDavUser'); |
|
417 | + $this->userSession |
|
418 | + ->expects($this->any()) |
|
419 | + ->method('getUser') |
|
420 | + ->willReturn($user); |
|
421 | + $this->request |
|
422 | + ->expects($this->any()) |
|
423 | + ->method('getMethod') |
|
424 | + ->willReturn('GET'); |
|
425 | + |
|
426 | + $response = $this->auth->check($request, $response); |
|
427 | + $this->assertEquals([true, 'principals/users/MyWrongDavUser'], $response); |
|
428 | + } |
|
429 | + |
|
430 | + public function testAuthenticateAlreadyLoggedInWithCsrfTokenForGet(): void { |
|
431 | + $request = $this->createMock(RequestInterface::class); |
|
432 | + $response = $this->createMock(ResponseInterface::class); |
|
433 | + $this->userSession |
|
434 | + ->expects($this->any()) |
|
435 | + ->method('isLoggedIn') |
|
436 | + ->willReturn(true); |
|
437 | + $this->session |
|
438 | + ->expects($this->any()) |
|
439 | + ->method('get') |
|
440 | + ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
441 | + ->willReturn(null); |
|
442 | + $user = $this->createMock(IUser::class); |
|
443 | + $user->expects($this->any()) |
|
444 | + ->method('getUID') |
|
445 | + ->willReturn('MyWrongDavUser'); |
|
446 | + $this->userSession |
|
447 | + ->expects($this->any()) |
|
448 | + ->method('getUser') |
|
449 | + ->willReturn($user); |
|
450 | + $this->request |
|
451 | + ->expects($this->once()) |
|
452 | + ->method('passesCSRFCheck') |
|
453 | + ->willReturn(true); |
|
454 | + |
|
455 | + $response = $this->auth->check($request, $response); |
|
456 | + $this->assertEquals([true, 'principals/users/MyWrongDavUser'], $response); |
|
457 | + } |
|
458 | + |
|
459 | + public function testAuthenticateNoBasicAuthenticateHeadersProvided(): void { |
|
460 | + $server = $this->createMock(Server::class); |
|
461 | + $server->httpRequest = $this->createMock(RequestInterface::class); |
|
462 | + $server->httpResponse = $this->createMock(ResponseInterface::class); |
|
463 | + $response = $this->auth->check($server->httpRequest, $server->httpResponse); |
|
464 | + $this->assertEquals([false, 'No \'Authorization: Basic\' header found. Either the client didn\'t send one, or the server is misconfigured'], $response); |
|
465 | + } |
|
466 | + |
|
467 | + |
|
468 | + public function testAuthenticateNoBasicAuthenticateHeadersProvidedWithAjax(): void { |
|
469 | + $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class); |
|
470 | + $this->expectExceptionMessage('Cannot authenticate over ajax calls'); |
|
471 | + |
|
472 | + /** @var \Sabre\HTTP\RequestInterface&MockObject $httpRequest */ |
|
473 | + $httpRequest = $this->createMock(RequestInterface::class); |
|
474 | + /** @var \Sabre\HTTP\ResponseInterface&MockObject $httpResponse */ |
|
475 | + $httpResponse = $this->createMock(ResponseInterface::class); |
|
476 | + $this->userSession |
|
477 | + ->expects($this->any()) |
|
478 | + ->method('isLoggedIn') |
|
479 | + ->willReturn(false); |
|
480 | + $httpRequest |
|
481 | + ->expects($this->exactly(2)) |
|
482 | + ->method('getHeader') |
|
483 | + ->willReturnMap([ |
|
484 | + ['X-Requested-With', 'XMLHttpRequest'], |
|
485 | + ['Authorization', null], |
|
486 | + ]); |
|
487 | + |
|
488 | + $this->auth->check($httpRequest, $httpResponse); |
|
489 | + } |
|
490 | + |
|
491 | + public function testAuthenticateWithBasicAuthenticateHeadersProvidedWithAjax(): void { |
|
492 | + // No CSRF |
|
493 | + $this->request |
|
494 | + ->expects($this->once()) |
|
495 | + ->method('passesCSRFCheck') |
|
496 | + ->willReturn(false); |
|
497 | + |
|
498 | + /** @var \Sabre\HTTP\RequestInterface&MockObject $httpRequest */ |
|
499 | + $httpRequest = $this->createMock(RequestInterface::class); |
|
500 | + /** @var \Sabre\HTTP\ResponseInterface&MockObject $httpResponse */ |
|
501 | + $httpResponse = $this->createMock(ResponseInterface::class); |
|
502 | + $httpRequest |
|
503 | + ->expects($this->any()) |
|
504 | + ->method('getHeader') |
|
505 | + ->willReturnMap([ |
|
506 | + ['X-Requested-With', 'XMLHttpRequest'], |
|
507 | + ['Authorization', 'basic dXNlcm5hbWU6cGFzc3dvcmQ='], |
|
508 | + ]); |
|
509 | + |
|
510 | + $user = $this->createMock(IUser::class); |
|
511 | + $user->expects($this->any()) |
|
512 | + ->method('getUID') |
|
513 | + ->willReturn('MyDavUser'); |
|
514 | + $this->userSession |
|
515 | + ->expects($this->any()) |
|
516 | + ->method('isLoggedIn') |
|
517 | + ->willReturn(false); |
|
518 | + $this->userSession |
|
519 | + ->expects($this->once()) |
|
520 | + ->method('logClientIn') |
|
521 | + ->with('username', 'password') |
|
522 | + ->willReturn(true); |
|
523 | + $this->userSession |
|
524 | + ->expects($this->any()) |
|
525 | + ->method('getUser') |
|
526 | + ->willReturn($user); |
|
527 | + |
|
528 | + $this->auth->check($httpRequest, $httpResponse); |
|
529 | + } |
|
530 | + |
|
531 | + public function testAuthenticateNoBasicAuthenticateHeadersProvidedWithAjaxButUserIsStillLoggedIn(): void { |
|
532 | + /** @var \Sabre\HTTP\RequestInterface $httpRequest */ |
|
533 | + $httpRequest = $this->createMock(RequestInterface::class); |
|
534 | + /** @var \Sabre\HTTP\ResponseInterface $httpResponse */ |
|
535 | + $httpResponse = $this->createMock(ResponseInterface::class); |
|
536 | + $user = $this->createMock(IUser::class); |
|
537 | + $user->method('getUID')->willReturn('MyTestUser'); |
|
538 | + $this->userSession |
|
539 | + ->expects($this->any()) |
|
540 | + ->method('isLoggedIn') |
|
541 | + ->willReturn(true); |
|
542 | + $this->userSession |
|
543 | + ->expects($this->any()) |
|
544 | + ->method('getUser') |
|
545 | + ->willReturn($user); |
|
546 | + $this->session |
|
547 | + ->expects($this->atLeastOnce()) |
|
548 | + ->method('get') |
|
549 | + ->with('AUTHENTICATED_TO_DAV_BACKEND') |
|
550 | + ->willReturn('MyTestUser'); |
|
551 | + $this->request |
|
552 | + ->expects($this->once()) |
|
553 | + ->method('getMethod') |
|
554 | + ->willReturn('GET'); |
|
555 | + $httpRequest |
|
556 | + ->expects($this->atLeastOnce()) |
|
557 | + ->method('getHeader') |
|
558 | + ->with('Authorization') |
|
559 | + ->willReturn(null); |
|
560 | + $this->assertEquals( |
|
561 | + [true, 'principals/users/MyTestUser'], |
|
562 | + $this->auth->check($httpRequest, $httpResponse) |
|
563 | + ); |
|
564 | + } |
|
565 | + |
|
566 | + public function testAuthenticateValidCredentials(): void { |
|
567 | + $server = $this->createMock(Server::class); |
|
568 | + $server->httpRequest = $this->createMock(RequestInterface::class); |
|
569 | + $server->httpRequest |
|
570 | + ->expects($this->once()) |
|
571 | + ->method('getHeader') |
|
572 | + ->with('Authorization') |
|
573 | + ->willReturn('basic dXNlcm5hbWU6cGFzc3dvcmQ='); |
|
574 | + |
|
575 | + $server->httpResponse = $this->createMock(ResponseInterface::class); |
|
576 | + $this->userSession |
|
577 | + ->expects($this->once()) |
|
578 | + ->method('logClientIn') |
|
579 | + ->with('username', 'password') |
|
580 | + ->willReturn(true); |
|
581 | + $user = $this->createMock(IUser::class); |
|
582 | + $user->expects($this->exactly(2)) |
|
583 | + ->method('getUID') |
|
584 | + ->willReturn('MyTestUser'); |
|
585 | + $this->userSession |
|
586 | + ->expects($this->exactly(4)) |
|
587 | + ->method('getUser') |
|
588 | + ->willReturn($user); |
|
589 | + $response = $this->auth->check($server->httpRequest, $server->httpResponse); |
|
590 | + $this->assertEquals([true, 'principals/users/MyTestUser'], $response); |
|
591 | + } |
|
592 | + |
|
593 | + public function testAuthenticateInvalidCredentials(): void { |
|
594 | + $server = $this->createMock(Server::class); |
|
595 | + $server->httpRequest = $this->createMock(RequestInterface::class); |
|
596 | + $server->httpRequest |
|
597 | + ->expects($this->exactly(2)) |
|
598 | + ->method('getHeader') |
|
599 | + ->willReturnMap([ |
|
600 | + ['Authorization', 'basic dXNlcm5hbWU6cGFzc3dvcmQ='], |
|
601 | + ['X-Requested-With', null], |
|
602 | + ]); |
|
603 | + $server->httpResponse = $this->createMock(ResponseInterface::class); |
|
604 | + $this->userSession |
|
605 | + ->expects($this->once()) |
|
606 | + ->method('logClientIn') |
|
607 | + ->with('username', 'password') |
|
608 | + ->willReturn(false); |
|
609 | + $response = $this->auth->check($server->httpRequest, $server->httpResponse); |
|
610 | + $this->assertEquals([false, 'Username or password was incorrect'], $response); |
|
611 | + } |
|
612 | 612 | } |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | * proper unit testing of the postAvatar call. |
14 | 14 | */ |
15 | 15 | function is_uploaded_file($filename) { |
16 | - return file_exists($filename); |
|
16 | + return file_exists($filename); |
|
17 | 17 | } |
18 | 18 | |
19 | 19 | namespace Tests\Core\Controller; |
@@ -41,409 +41,409 @@ discard block |
||
41 | 41 | * @package OC\Core\Controller |
42 | 42 | */ |
43 | 43 | class AvatarControllerTest extends \Test\TestCase { |
44 | - /** @var AvatarController */ |
|
45 | - private $avatarController; |
|
46 | - /** @var GuestAvatarController */ |
|
47 | - private $guestAvatarController; |
|
48 | - |
|
49 | - /** @var IAvatar|\PHPUnit\Framework\MockObject\MockObject */ |
|
50 | - private $avatarMock; |
|
51 | - /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */ |
|
52 | - private $userMock; |
|
53 | - /** @var ISimpleFile|\PHPUnit\Framework\MockObject\MockObject */ |
|
54 | - private $avatarFile; |
|
55 | - /** @var IAvatarManager|\PHPUnit\Framework\MockObject\MockObject */ |
|
56 | - private $avatarManager; |
|
57 | - /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */ |
|
58 | - private $l; |
|
59 | - /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */ |
|
60 | - private $userManager; |
|
61 | - /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */ |
|
62 | - private $rootFolder; |
|
63 | - /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ |
|
64 | - private $logger; |
|
65 | - /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */ |
|
66 | - private $request; |
|
67 | - /** @var TimeFactory|\PHPUnit\Framework\MockObject\MockObject */ |
|
68 | - private $timeFactory; |
|
69 | - |
|
70 | - protected function setUp(): void { |
|
71 | - parent::setUp(); |
|
72 | - |
|
73 | - $this->avatarManager = $this->getMockBuilder('OCP\IAvatarManager')->getMock(); |
|
74 | - $this->l = $this->getMockBuilder(IL10N::class)->getMock(); |
|
75 | - $this->l->method('t')->willReturnArgument(0); |
|
76 | - $this->userManager = $this->getMockBuilder(IUserManager::class)->getMock(); |
|
77 | - $this->request = $this->getMockBuilder(IRequest::class)->getMock(); |
|
78 | - $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock(); |
|
79 | - $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock(); |
|
80 | - $this->timeFactory = $this->getMockBuilder('OC\AppFramework\Utility\TimeFactory')->getMock(); |
|
81 | - |
|
82 | - $this->avatarMock = $this->getMockBuilder('OCP\IAvatar')->getMock(); |
|
83 | - $this->userMock = $this->getMockBuilder(IUser::class)->getMock(); |
|
84 | - |
|
85 | - $this->guestAvatarController = new GuestAvatarController( |
|
86 | - 'core', |
|
87 | - $this->request, |
|
88 | - $this->avatarManager, |
|
89 | - $this->logger |
|
90 | - ); |
|
91 | - |
|
92 | - $this->avatarController = new AvatarController( |
|
93 | - 'core', |
|
94 | - $this->request, |
|
95 | - $this->avatarManager, |
|
96 | - $this->l, |
|
97 | - $this->userManager, |
|
98 | - $this->rootFolder, |
|
99 | - $this->logger, |
|
100 | - 'userid', |
|
101 | - $this->timeFactory, |
|
102 | - $this->guestAvatarController, |
|
103 | - ); |
|
104 | - |
|
105 | - // Configure userMock |
|
106 | - $this->userMock->method('getDisplayName')->willReturn('displayName'); |
|
107 | - $this->userMock->method('getUID')->willReturn('userId'); |
|
108 | - $this->userManager->method('get') |
|
109 | - ->willReturnMap([['userId', $this->userMock]]); |
|
110 | - |
|
111 | - $this->avatarFile = $this->getMockBuilder(ISimpleFile::class)->getMock(); |
|
112 | - $this->avatarFile->method('getContent')->willReturn('image data'); |
|
113 | - $this->avatarFile->method('getMimeType')->willReturn('image type'); |
|
114 | - $this->avatarFile->method('getEtag')->willReturn('my etag'); |
|
115 | - $this->avatarFile->method('getName')->willReturn('my name'); |
|
116 | - $this->avatarFile->method('getMTime')->willReturn(42); |
|
117 | - } |
|
118 | - |
|
119 | - protected function tearDown(): void { |
|
120 | - parent::tearDown(); |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Fetch an avatar if a user has no avatar |
|
125 | - */ |
|
126 | - public function testGetAvatarNoAvatar(): void { |
|
127 | - $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); |
|
128 | - $this->avatarMock->method('getFile')->willThrowException(new NotFoundException()); |
|
129 | - $response = $this->avatarController->getAvatar('userId', 32); |
|
130 | - |
|
131 | - //Comment out until JS is fixed |
|
132 | - $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Fetch the user's avatar |
|
137 | - */ |
|
138 | - public function testGetAvatar(): void { |
|
139 | - $this->avatarMock->method('getFile')->willReturn($this->avatarFile); |
|
140 | - $this->avatarManager->method('getAvatar')->with('userId')->willReturn($this->avatarMock); |
|
141 | - $this->avatarMock->expects($this->once()) |
|
142 | - ->method('isCustomAvatar') |
|
143 | - ->willReturn(true); |
|
144 | - |
|
145 | - $response = $this->avatarController->getAvatar('userId', 32); |
|
146 | - |
|
147 | - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); |
|
148 | - $this->assertArrayHasKey('Content-Type', $response->getHeaders()); |
|
149 | - $this->assertEquals('image type', $response->getHeaders()['Content-Type']); |
|
150 | - $this->assertArrayHasKey('X-NC-IsCustomAvatar', $response->getHeaders()); |
|
151 | - $this->assertEquals('1', $response->getHeaders()['X-NC-IsCustomAvatar']); |
|
152 | - |
|
153 | - $this->assertEquals('my etag', $response->getETag()); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Fetch the user's avatar |
|
158 | - */ |
|
159 | - public function testGetGeneratedAvatar(): void { |
|
160 | - $this->avatarMock->method('getFile')->willReturn($this->avatarFile); |
|
161 | - $this->avatarManager->method('getAvatar')->with('userId')->willReturn($this->avatarMock); |
|
162 | - |
|
163 | - $response = $this->avatarController->getAvatar('userId', 32); |
|
164 | - |
|
165 | - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); |
|
166 | - $this->assertArrayHasKey('Content-Type', $response->getHeaders()); |
|
167 | - $this->assertEquals('image type', $response->getHeaders()['Content-Type']); |
|
168 | - $this->assertArrayHasKey('X-NC-IsCustomAvatar', $response->getHeaders()); |
|
169 | - $this->assertEquals('0', $response->getHeaders()['X-NC-IsCustomAvatar']); |
|
170 | - |
|
171 | - $this->assertEquals('my etag', $response->getETag()); |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Fetch the avatar of a non-existing user |
|
176 | - */ |
|
177 | - public function testGetAvatarNoUser(): void { |
|
178 | - $this->avatarManager |
|
179 | - ->method('getAvatar') |
|
180 | - ->with('userDoesNotExist') |
|
181 | - ->willThrowException(new \Exception('user does not exist')); |
|
182 | - |
|
183 | - $response = $this->avatarController->getAvatar('userDoesNotExist', 32); |
|
184 | - |
|
185 | - //Comment out until JS is fixed |
|
186 | - $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); |
|
187 | - } |
|
188 | - |
|
189 | - public function testGetAvatarSize64(): void { |
|
190 | - $this->avatarMock->expects($this->once()) |
|
191 | - ->method('getFile') |
|
192 | - ->with($this->equalTo(64)) |
|
193 | - ->willReturn($this->avatarFile); |
|
194 | - |
|
195 | - $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); |
|
196 | - |
|
197 | - $this->logger->expects($this->never()) |
|
198 | - ->method('debug'); |
|
199 | - |
|
200 | - $this->avatarController->getAvatar('userId', 64); |
|
201 | - } |
|
202 | - |
|
203 | - public function testGetAvatarSize512(): void { |
|
204 | - $this->avatarMock->expects($this->once()) |
|
205 | - ->method('getFile') |
|
206 | - ->with($this->equalTo(512)) |
|
207 | - ->willReturn($this->avatarFile); |
|
208 | - |
|
209 | - $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); |
|
210 | - |
|
211 | - $this->logger->expects($this->never()) |
|
212 | - ->method('debug'); |
|
213 | - |
|
214 | - $this->avatarController->getAvatar('userId', 512); |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Small sizes return 64 and generate a log |
|
219 | - */ |
|
220 | - public function testGetAvatarSizeTooSmall(): void { |
|
221 | - $this->avatarMock->expects($this->once()) |
|
222 | - ->method('getFile') |
|
223 | - ->with($this->equalTo(64)) |
|
224 | - ->willReturn($this->avatarFile); |
|
225 | - |
|
226 | - $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); |
|
227 | - |
|
228 | - $this->logger->expects($this->once()) |
|
229 | - ->method('debug') |
|
230 | - ->with('Avatar requested in deprecated size 32'); |
|
231 | - |
|
232 | - $this->avatarController->getAvatar('userId', 32); |
|
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * Avatars between 64 and 512 are upgraded to 512 |
|
237 | - */ |
|
238 | - public function testGetAvatarSizeBetween(): void { |
|
239 | - $this->avatarMock->expects($this->once()) |
|
240 | - ->method('getFile') |
|
241 | - ->with($this->equalTo(512)) |
|
242 | - ->willReturn($this->avatarFile); |
|
243 | - |
|
244 | - $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); |
|
245 | - |
|
246 | - $this->logger->expects($this->once()) |
|
247 | - ->method('debug') |
|
248 | - ->with('Avatar requested in deprecated size 65'); |
|
249 | - |
|
250 | - $this->avatarController->getAvatar('userId', 65); |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * We do not support avatars larger than 512 |
|
255 | - */ |
|
256 | - public function testGetAvatarSizeTooBig(): void { |
|
257 | - $this->avatarMock->expects($this->once()) |
|
258 | - ->method('getFile') |
|
259 | - ->with($this->equalTo(512)) |
|
260 | - ->willReturn($this->avatarFile); |
|
261 | - |
|
262 | - $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); |
|
263 | - |
|
264 | - $this->logger->expects($this->once()) |
|
265 | - ->method('debug') |
|
266 | - ->with('Avatar requested in deprecated size 513'); |
|
267 | - |
|
268 | - $this->avatarController->getAvatar('userId', 513); |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * Remove an avatar |
|
273 | - */ |
|
274 | - public function testDeleteAvatar(): void { |
|
275 | - $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); |
|
276 | - |
|
277 | - $response = $this->avatarController->deleteAvatar(); |
|
278 | - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * Test what happens if the removing of the avatar fails |
|
283 | - */ |
|
284 | - public function testDeleteAvatarException(): void { |
|
285 | - $this->avatarMock->method('remove')->willThrowException(new \Exception('foo')); |
|
286 | - $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); |
|
287 | - |
|
288 | - $this->logger->expects($this->once()) |
|
289 | - ->method('error') |
|
290 | - ->with('foo', ['exception' => new \Exception('foo'), 'app' => 'core']); |
|
291 | - $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_BAD_REQUEST); |
|
292 | - $this->assertEquals($expectedResponse, $this->avatarController->deleteAvatar()); |
|
293 | - } |
|
294 | - |
|
295 | - /** |
|
296 | - * When trying to post a new avatar a path or image should be posted. |
|
297 | - */ |
|
298 | - public function testPostAvatarNoPathOrImage(): void { |
|
299 | - $response = $this->avatarController->postAvatar(null); |
|
300 | - |
|
301 | - $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus()); |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * Test a correct post of an avatar using POST |
|
306 | - */ |
|
307 | - public function testPostAvatarFile(): void { |
|
308 | - //Create temp file |
|
309 | - $fileName = tempnam('', 'avatarTest'); |
|
310 | - $copyRes = copy(\OC::$SERVERROOT . '/tests/data/testimage.jpg', $fileName); |
|
311 | - $this->assertTrue($copyRes); |
|
312 | - |
|
313 | - //Create request return |
|
314 | - $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [filesize(\OC::$SERVERROOT . '/tests/data/testimage.jpg')]]; |
|
315 | - $this->request->method('getUploadedFile')->willReturn($reqRet); |
|
316 | - |
|
317 | - $response = $this->avatarController->postAvatar(null); |
|
318 | - |
|
319 | - //On correct upload always respond with the notsquare message |
|
320 | - $this->assertEquals('notsquare', $response->getData()['data']); |
|
321 | - |
|
322 | - //File should be deleted |
|
323 | - $this->assertFalse(file_exists($fileName)); |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * Test invalid post os an avatar using POST |
|
328 | - */ |
|
329 | - public function testPostAvatarInvalidFile(): void { |
|
330 | - //Create request return |
|
331 | - $reqRet = ['error' => [1], 'tmp_name' => ['foo']]; |
|
332 | - $this->request->method('getUploadedFile')->willReturn($reqRet); |
|
333 | - |
|
334 | - $response = $this->avatarController->postAvatar(null); |
|
335 | - |
|
336 | - $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus()); |
|
337 | - } |
|
338 | - |
|
339 | - /** |
|
340 | - * Check what happens when we upload a GIF |
|
341 | - */ |
|
342 | - public function testPostAvatarFileGif(): void { |
|
343 | - //Create temp file |
|
344 | - $fileName = tempnam('', 'avatarTest'); |
|
345 | - $copyRes = copy(\OC::$SERVERROOT . '/tests/data/testimage.gif', $fileName); |
|
346 | - $this->assertTrue($copyRes); |
|
347 | - |
|
348 | - //Create request return |
|
349 | - $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [filesize(\OC::$SERVERROOT . '/tests/data/testimage.gif')]]; |
|
350 | - $this->request->method('getUploadedFile')->willReturn($reqRet); |
|
351 | - |
|
352 | - $response = $this->avatarController->postAvatar(null); |
|
353 | - |
|
354 | - $this->assertEquals('Unknown filetype', $response->getData()['data']['message']); |
|
355 | - |
|
356 | - //File should be deleted |
|
357 | - $this->assertFalse(file_exists($fileName)); |
|
358 | - } |
|
359 | - |
|
360 | - /** |
|
361 | - * Test posting avatar from existing file |
|
362 | - */ |
|
363 | - public function testPostAvatarFromFile(): void { |
|
364 | - //Mock node API call |
|
365 | - $file = $this->getMockBuilder('OCP\Files\File') |
|
366 | - ->disableOriginalConstructor()->getMock(); |
|
367 | - $file->expects($this->once()) |
|
368 | - ->method('getContent') |
|
369 | - ->willReturn(file_get_contents(\OC::$SERVERROOT . '/tests/data/testimage.jpg')); |
|
370 | - $file->expects($this->once()) |
|
371 | - ->method('getMimeType') |
|
372 | - ->willReturn('image/jpeg'); |
|
373 | - $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock(); |
|
374 | - $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder); |
|
375 | - $userFolder->method('get')->willReturn($file); |
|
376 | - |
|
377 | - //Create request return |
|
378 | - $response = $this->avatarController->postAvatar('avatar.jpg'); |
|
379 | - |
|
380 | - //On correct upload always respond with the notsquare message |
|
381 | - $this->assertEquals('notsquare', $response->getData()['data']); |
|
382 | - } |
|
383 | - |
|
384 | - /** |
|
385 | - * Test posting avatar from existing folder |
|
386 | - */ |
|
387 | - public function testPostAvatarFromNoFile(): void { |
|
388 | - $file = $this->getMockBuilder('OCP\Files\Node')->getMock(); |
|
389 | - $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock(); |
|
390 | - $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder); |
|
391 | - $userFolder |
|
392 | - ->method('get') |
|
393 | - ->with('folder') |
|
394 | - ->willReturn($file); |
|
395 | - |
|
396 | - //Create request return |
|
397 | - $response = $this->avatarController->postAvatar('folder'); |
|
398 | - |
|
399 | - //On correct upload always respond with the notsquare message |
|
400 | - $this->assertEquals(['data' => ['message' => 'Please select a file.']], $response->getData()); |
|
401 | - } |
|
402 | - |
|
403 | - public function testPostAvatarInvalidType(): void { |
|
404 | - $file = $this->getMockBuilder('OCP\Files\File') |
|
405 | - ->disableOriginalConstructor()->getMock(); |
|
406 | - $file->expects($this->never()) |
|
407 | - ->method('getContent'); |
|
408 | - $file->expects($this->exactly(2)) |
|
409 | - ->method('getMimeType') |
|
410 | - ->willReturn('text/plain'); |
|
411 | - $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock(); |
|
412 | - $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder); |
|
413 | - $userFolder->method('get')->willReturn($file); |
|
414 | - |
|
415 | - $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'The selected file is not an image.']], Http::STATUS_BAD_REQUEST); |
|
416 | - $this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg')); |
|
417 | - } |
|
418 | - |
|
419 | - public function testPostAvatarNotPermittedException(): void { |
|
420 | - $file = $this->getMockBuilder('OCP\Files\File') |
|
421 | - ->disableOriginalConstructor()->getMock(); |
|
422 | - $file->expects($this->once()) |
|
423 | - ->method('getContent') |
|
424 | - ->willThrowException(new NotPermittedException()); |
|
425 | - $file->expects($this->once()) |
|
426 | - ->method('getMimeType') |
|
427 | - ->willReturn('image/jpeg'); |
|
428 | - $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock(); |
|
429 | - $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder); |
|
430 | - $userFolder->method('get')->willReturn($file); |
|
431 | - |
|
432 | - $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'The selected file cannot be read.']], Http::STATUS_BAD_REQUEST); |
|
433 | - $this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg')); |
|
434 | - } |
|
435 | - |
|
436 | - /** |
|
437 | - * Check for proper reply on proper crop argument |
|
438 | - */ |
|
439 | - public function testFileTooBig(): void { |
|
440 | - $fileName = \OC::$SERVERROOT . '/tests/data/testimage.jpg'; |
|
441 | - //Create request return |
|
442 | - $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [21 * 1024 * 1024]]; |
|
443 | - $this->request->method('getUploadedFile')->willReturn($reqRet); |
|
444 | - |
|
445 | - $response = $this->avatarController->postAvatar(null); |
|
446 | - |
|
447 | - $this->assertEquals('File is too big', $response->getData()['data']['message']); |
|
448 | - } |
|
44 | + /** @var AvatarController */ |
|
45 | + private $avatarController; |
|
46 | + /** @var GuestAvatarController */ |
|
47 | + private $guestAvatarController; |
|
48 | + |
|
49 | + /** @var IAvatar|\PHPUnit\Framework\MockObject\MockObject */ |
|
50 | + private $avatarMock; |
|
51 | + /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */ |
|
52 | + private $userMock; |
|
53 | + /** @var ISimpleFile|\PHPUnit\Framework\MockObject\MockObject */ |
|
54 | + private $avatarFile; |
|
55 | + /** @var IAvatarManager|\PHPUnit\Framework\MockObject\MockObject */ |
|
56 | + private $avatarManager; |
|
57 | + /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */ |
|
58 | + private $l; |
|
59 | + /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */ |
|
60 | + private $userManager; |
|
61 | + /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */ |
|
62 | + private $rootFolder; |
|
63 | + /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ |
|
64 | + private $logger; |
|
65 | + /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */ |
|
66 | + private $request; |
|
67 | + /** @var TimeFactory|\PHPUnit\Framework\MockObject\MockObject */ |
|
68 | + private $timeFactory; |
|
69 | + |
|
70 | + protected function setUp(): void { |
|
71 | + parent::setUp(); |
|
72 | + |
|
73 | + $this->avatarManager = $this->getMockBuilder('OCP\IAvatarManager')->getMock(); |
|
74 | + $this->l = $this->getMockBuilder(IL10N::class)->getMock(); |
|
75 | + $this->l->method('t')->willReturnArgument(0); |
|
76 | + $this->userManager = $this->getMockBuilder(IUserManager::class)->getMock(); |
|
77 | + $this->request = $this->getMockBuilder(IRequest::class)->getMock(); |
|
78 | + $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock(); |
|
79 | + $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock(); |
|
80 | + $this->timeFactory = $this->getMockBuilder('OC\AppFramework\Utility\TimeFactory')->getMock(); |
|
81 | + |
|
82 | + $this->avatarMock = $this->getMockBuilder('OCP\IAvatar')->getMock(); |
|
83 | + $this->userMock = $this->getMockBuilder(IUser::class)->getMock(); |
|
84 | + |
|
85 | + $this->guestAvatarController = new GuestAvatarController( |
|
86 | + 'core', |
|
87 | + $this->request, |
|
88 | + $this->avatarManager, |
|
89 | + $this->logger |
|
90 | + ); |
|
91 | + |
|
92 | + $this->avatarController = new AvatarController( |
|
93 | + 'core', |
|
94 | + $this->request, |
|
95 | + $this->avatarManager, |
|
96 | + $this->l, |
|
97 | + $this->userManager, |
|
98 | + $this->rootFolder, |
|
99 | + $this->logger, |
|
100 | + 'userid', |
|
101 | + $this->timeFactory, |
|
102 | + $this->guestAvatarController, |
|
103 | + ); |
|
104 | + |
|
105 | + // Configure userMock |
|
106 | + $this->userMock->method('getDisplayName')->willReturn('displayName'); |
|
107 | + $this->userMock->method('getUID')->willReturn('userId'); |
|
108 | + $this->userManager->method('get') |
|
109 | + ->willReturnMap([['userId', $this->userMock]]); |
|
110 | + |
|
111 | + $this->avatarFile = $this->getMockBuilder(ISimpleFile::class)->getMock(); |
|
112 | + $this->avatarFile->method('getContent')->willReturn('image data'); |
|
113 | + $this->avatarFile->method('getMimeType')->willReturn('image type'); |
|
114 | + $this->avatarFile->method('getEtag')->willReturn('my etag'); |
|
115 | + $this->avatarFile->method('getName')->willReturn('my name'); |
|
116 | + $this->avatarFile->method('getMTime')->willReturn(42); |
|
117 | + } |
|
118 | + |
|
119 | + protected function tearDown(): void { |
|
120 | + parent::tearDown(); |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Fetch an avatar if a user has no avatar |
|
125 | + */ |
|
126 | + public function testGetAvatarNoAvatar(): void { |
|
127 | + $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); |
|
128 | + $this->avatarMock->method('getFile')->willThrowException(new NotFoundException()); |
|
129 | + $response = $this->avatarController->getAvatar('userId', 32); |
|
130 | + |
|
131 | + //Comment out until JS is fixed |
|
132 | + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Fetch the user's avatar |
|
137 | + */ |
|
138 | + public function testGetAvatar(): void { |
|
139 | + $this->avatarMock->method('getFile')->willReturn($this->avatarFile); |
|
140 | + $this->avatarManager->method('getAvatar')->with('userId')->willReturn($this->avatarMock); |
|
141 | + $this->avatarMock->expects($this->once()) |
|
142 | + ->method('isCustomAvatar') |
|
143 | + ->willReturn(true); |
|
144 | + |
|
145 | + $response = $this->avatarController->getAvatar('userId', 32); |
|
146 | + |
|
147 | + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); |
|
148 | + $this->assertArrayHasKey('Content-Type', $response->getHeaders()); |
|
149 | + $this->assertEquals('image type', $response->getHeaders()['Content-Type']); |
|
150 | + $this->assertArrayHasKey('X-NC-IsCustomAvatar', $response->getHeaders()); |
|
151 | + $this->assertEquals('1', $response->getHeaders()['X-NC-IsCustomAvatar']); |
|
152 | + |
|
153 | + $this->assertEquals('my etag', $response->getETag()); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Fetch the user's avatar |
|
158 | + */ |
|
159 | + public function testGetGeneratedAvatar(): void { |
|
160 | + $this->avatarMock->method('getFile')->willReturn($this->avatarFile); |
|
161 | + $this->avatarManager->method('getAvatar')->with('userId')->willReturn($this->avatarMock); |
|
162 | + |
|
163 | + $response = $this->avatarController->getAvatar('userId', 32); |
|
164 | + |
|
165 | + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); |
|
166 | + $this->assertArrayHasKey('Content-Type', $response->getHeaders()); |
|
167 | + $this->assertEquals('image type', $response->getHeaders()['Content-Type']); |
|
168 | + $this->assertArrayHasKey('X-NC-IsCustomAvatar', $response->getHeaders()); |
|
169 | + $this->assertEquals('0', $response->getHeaders()['X-NC-IsCustomAvatar']); |
|
170 | + |
|
171 | + $this->assertEquals('my etag', $response->getETag()); |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Fetch the avatar of a non-existing user |
|
176 | + */ |
|
177 | + public function testGetAvatarNoUser(): void { |
|
178 | + $this->avatarManager |
|
179 | + ->method('getAvatar') |
|
180 | + ->with('userDoesNotExist') |
|
181 | + ->willThrowException(new \Exception('user does not exist')); |
|
182 | + |
|
183 | + $response = $this->avatarController->getAvatar('userDoesNotExist', 32); |
|
184 | + |
|
185 | + //Comment out until JS is fixed |
|
186 | + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); |
|
187 | + } |
|
188 | + |
|
189 | + public function testGetAvatarSize64(): void { |
|
190 | + $this->avatarMock->expects($this->once()) |
|
191 | + ->method('getFile') |
|
192 | + ->with($this->equalTo(64)) |
|
193 | + ->willReturn($this->avatarFile); |
|
194 | + |
|
195 | + $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); |
|
196 | + |
|
197 | + $this->logger->expects($this->never()) |
|
198 | + ->method('debug'); |
|
199 | + |
|
200 | + $this->avatarController->getAvatar('userId', 64); |
|
201 | + } |
|
202 | + |
|
203 | + public function testGetAvatarSize512(): void { |
|
204 | + $this->avatarMock->expects($this->once()) |
|
205 | + ->method('getFile') |
|
206 | + ->with($this->equalTo(512)) |
|
207 | + ->willReturn($this->avatarFile); |
|
208 | + |
|
209 | + $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); |
|
210 | + |
|
211 | + $this->logger->expects($this->never()) |
|
212 | + ->method('debug'); |
|
213 | + |
|
214 | + $this->avatarController->getAvatar('userId', 512); |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Small sizes return 64 and generate a log |
|
219 | + */ |
|
220 | + public function testGetAvatarSizeTooSmall(): void { |
|
221 | + $this->avatarMock->expects($this->once()) |
|
222 | + ->method('getFile') |
|
223 | + ->with($this->equalTo(64)) |
|
224 | + ->willReturn($this->avatarFile); |
|
225 | + |
|
226 | + $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); |
|
227 | + |
|
228 | + $this->logger->expects($this->once()) |
|
229 | + ->method('debug') |
|
230 | + ->with('Avatar requested in deprecated size 32'); |
|
231 | + |
|
232 | + $this->avatarController->getAvatar('userId', 32); |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * Avatars between 64 and 512 are upgraded to 512 |
|
237 | + */ |
|
238 | + public function testGetAvatarSizeBetween(): void { |
|
239 | + $this->avatarMock->expects($this->once()) |
|
240 | + ->method('getFile') |
|
241 | + ->with($this->equalTo(512)) |
|
242 | + ->willReturn($this->avatarFile); |
|
243 | + |
|
244 | + $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); |
|
245 | + |
|
246 | + $this->logger->expects($this->once()) |
|
247 | + ->method('debug') |
|
248 | + ->with('Avatar requested in deprecated size 65'); |
|
249 | + |
|
250 | + $this->avatarController->getAvatar('userId', 65); |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * We do not support avatars larger than 512 |
|
255 | + */ |
|
256 | + public function testGetAvatarSizeTooBig(): void { |
|
257 | + $this->avatarMock->expects($this->once()) |
|
258 | + ->method('getFile') |
|
259 | + ->with($this->equalTo(512)) |
|
260 | + ->willReturn($this->avatarFile); |
|
261 | + |
|
262 | + $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); |
|
263 | + |
|
264 | + $this->logger->expects($this->once()) |
|
265 | + ->method('debug') |
|
266 | + ->with('Avatar requested in deprecated size 513'); |
|
267 | + |
|
268 | + $this->avatarController->getAvatar('userId', 513); |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * Remove an avatar |
|
273 | + */ |
|
274 | + public function testDeleteAvatar(): void { |
|
275 | + $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); |
|
276 | + |
|
277 | + $response = $this->avatarController->deleteAvatar(); |
|
278 | + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * Test what happens if the removing of the avatar fails |
|
283 | + */ |
|
284 | + public function testDeleteAvatarException(): void { |
|
285 | + $this->avatarMock->method('remove')->willThrowException(new \Exception('foo')); |
|
286 | + $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); |
|
287 | + |
|
288 | + $this->logger->expects($this->once()) |
|
289 | + ->method('error') |
|
290 | + ->with('foo', ['exception' => new \Exception('foo'), 'app' => 'core']); |
|
291 | + $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_BAD_REQUEST); |
|
292 | + $this->assertEquals($expectedResponse, $this->avatarController->deleteAvatar()); |
|
293 | + } |
|
294 | + |
|
295 | + /** |
|
296 | + * When trying to post a new avatar a path or image should be posted. |
|
297 | + */ |
|
298 | + public function testPostAvatarNoPathOrImage(): void { |
|
299 | + $response = $this->avatarController->postAvatar(null); |
|
300 | + |
|
301 | + $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus()); |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * Test a correct post of an avatar using POST |
|
306 | + */ |
|
307 | + public function testPostAvatarFile(): void { |
|
308 | + //Create temp file |
|
309 | + $fileName = tempnam('', 'avatarTest'); |
|
310 | + $copyRes = copy(\OC::$SERVERROOT . '/tests/data/testimage.jpg', $fileName); |
|
311 | + $this->assertTrue($copyRes); |
|
312 | + |
|
313 | + //Create request return |
|
314 | + $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [filesize(\OC::$SERVERROOT . '/tests/data/testimage.jpg')]]; |
|
315 | + $this->request->method('getUploadedFile')->willReturn($reqRet); |
|
316 | + |
|
317 | + $response = $this->avatarController->postAvatar(null); |
|
318 | + |
|
319 | + //On correct upload always respond with the notsquare message |
|
320 | + $this->assertEquals('notsquare', $response->getData()['data']); |
|
321 | + |
|
322 | + //File should be deleted |
|
323 | + $this->assertFalse(file_exists($fileName)); |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * Test invalid post os an avatar using POST |
|
328 | + */ |
|
329 | + public function testPostAvatarInvalidFile(): void { |
|
330 | + //Create request return |
|
331 | + $reqRet = ['error' => [1], 'tmp_name' => ['foo']]; |
|
332 | + $this->request->method('getUploadedFile')->willReturn($reqRet); |
|
333 | + |
|
334 | + $response = $this->avatarController->postAvatar(null); |
|
335 | + |
|
336 | + $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus()); |
|
337 | + } |
|
338 | + |
|
339 | + /** |
|
340 | + * Check what happens when we upload a GIF |
|
341 | + */ |
|
342 | + public function testPostAvatarFileGif(): void { |
|
343 | + //Create temp file |
|
344 | + $fileName = tempnam('', 'avatarTest'); |
|
345 | + $copyRes = copy(\OC::$SERVERROOT . '/tests/data/testimage.gif', $fileName); |
|
346 | + $this->assertTrue($copyRes); |
|
347 | + |
|
348 | + //Create request return |
|
349 | + $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [filesize(\OC::$SERVERROOT . '/tests/data/testimage.gif')]]; |
|
350 | + $this->request->method('getUploadedFile')->willReturn($reqRet); |
|
351 | + |
|
352 | + $response = $this->avatarController->postAvatar(null); |
|
353 | + |
|
354 | + $this->assertEquals('Unknown filetype', $response->getData()['data']['message']); |
|
355 | + |
|
356 | + //File should be deleted |
|
357 | + $this->assertFalse(file_exists($fileName)); |
|
358 | + } |
|
359 | + |
|
360 | + /** |
|
361 | + * Test posting avatar from existing file |
|
362 | + */ |
|
363 | + public function testPostAvatarFromFile(): void { |
|
364 | + //Mock node API call |
|
365 | + $file = $this->getMockBuilder('OCP\Files\File') |
|
366 | + ->disableOriginalConstructor()->getMock(); |
|
367 | + $file->expects($this->once()) |
|
368 | + ->method('getContent') |
|
369 | + ->willReturn(file_get_contents(\OC::$SERVERROOT . '/tests/data/testimage.jpg')); |
|
370 | + $file->expects($this->once()) |
|
371 | + ->method('getMimeType') |
|
372 | + ->willReturn('image/jpeg'); |
|
373 | + $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock(); |
|
374 | + $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder); |
|
375 | + $userFolder->method('get')->willReturn($file); |
|
376 | + |
|
377 | + //Create request return |
|
378 | + $response = $this->avatarController->postAvatar('avatar.jpg'); |
|
379 | + |
|
380 | + //On correct upload always respond with the notsquare message |
|
381 | + $this->assertEquals('notsquare', $response->getData()['data']); |
|
382 | + } |
|
383 | + |
|
384 | + /** |
|
385 | + * Test posting avatar from existing folder |
|
386 | + */ |
|
387 | + public function testPostAvatarFromNoFile(): void { |
|
388 | + $file = $this->getMockBuilder('OCP\Files\Node')->getMock(); |
|
389 | + $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock(); |
|
390 | + $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder); |
|
391 | + $userFolder |
|
392 | + ->method('get') |
|
393 | + ->with('folder') |
|
394 | + ->willReturn($file); |
|
395 | + |
|
396 | + //Create request return |
|
397 | + $response = $this->avatarController->postAvatar('folder'); |
|
398 | + |
|
399 | + //On correct upload always respond with the notsquare message |
|
400 | + $this->assertEquals(['data' => ['message' => 'Please select a file.']], $response->getData()); |
|
401 | + } |
|
402 | + |
|
403 | + public function testPostAvatarInvalidType(): void { |
|
404 | + $file = $this->getMockBuilder('OCP\Files\File') |
|
405 | + ->disableOriginalConstructor()->getMock(); |
|
406 | + $file->expects($this->never()) |
|
407 | + ->method('getContent'); |
|
408 | + $file->expects($this->exactly(2)) |
|
409 | + ->method('getMimeType') |
|
410 | + ->willReturn('text/plain'); |
|
411 | + $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock(); |
|
412 | + $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder); |
|
413 | + $userFolder->method('get')->willReturn($file); |
|
414 | + |
|
415 | + $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'The selected file is not an image.']], Http::STATUS_BAD_REQUEST); |
|
416 | + $this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg')); |
|
417 | + } |
|
418 | + |
|
419 | + public function testPostAvatarNotPermittedException(): void { |
|
420 | + $file = $this->getMockBuilder('OCP\Files\File') |
|
421 | + ->disableOriginalConstructor()->getMock(); |
|
422 | + $file->expects($this->once()) |
|
423 | + ->method('getContent') |
|
424 | + ->willThrowException(new NotPermittedException()); |
|
425 | + $file->expects($this->once()) |
|
426 | + ->method('getMimeType') |
|
427 | + ->willReturn('image/jpeg'); |
|
428 | + $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock(); |
|
429 | + $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder); |
|
430 | + $userFolder->method('get')->willReturn($file); |
|
431 | + |
|
432 | + $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'The selected file cannot be read.']], Http::STATUS_BAD_REQUEST); |
|
433 | + $this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg')); |
|
434 | + } |
|
435 | + |
|
436 | + /** |
|
437 | + * Check for proper reply on proper crop argument |
|
438 | + */ |
|
439 | + public function testFileTooBig(): void { |
|
440 | + $fileName = \OC::$SERVERROOT . '/tests/data/testimage.jpg'; |
|
441 | + //Create request return |
|
442 | + $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [21 * 1024 * 1024]]; |
|
443 | + $this->request->method('getUploadedFile')->willReturn($reqRet); |
|
444 | + |
|
445 | + $response = $this->avatarController->postAvatar(null); |
|
446 | + |
|
447 | + $this->assertEquals('File is too big', $response->getData()['data']['message']); |
|
448 | + } |
|
449 | 449 | } |
@@ -307,11 +307,11 @@ discard block |
||
307 | 307 | public function testPostAvatarFile(): void { |
308 | 308 | //Create temp file |
309 | 309 | $fileName = tempnam('', 'avatarTest'); |
310 | - $copyRes = copy(\OC::$SERVERROOT . '/tests/data/testimage.jpg', $fileName); |
|
310 | + $copyRes = copy(\OC::$SERVERROOT.'/tests/data/testimage.jpg', $fileName); |
|
311 | 311 | $this->assertTrue($copyRes); |
312 | 312 | |
313 | 313 | //Create request return |
314 | - $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [filesize(\OC::$SERVERROOT . '/tests/data/testimage.jpg')]]; |
|
314 | + $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [filesize(\OC::$SERVERROOT.'/tests/data/testimage.jpg')]]; |
|
315 | 315 | $this->request->method('getUploadedFile')->willReturn($reqRet); |
316 | 316 | |
317 | 317 | $response = $this->avatarController->postAvatar(null); |
@@ -342,11 +342,11 @@ discard block |
||
342 | 342 | public function testPostAvatarFileGif(): void { |
343 | 343 | //Create temp file |
344 | 344 | $fileName = tempnam('', 'avatarTest'); |
345 | - $copyRes = copy(\OC::$SERVERROOT . '/tests/data/testimage.gif', $fileName); |
|
345 | + $copyRes = copy(\OC::$SERVERROOT.'/tests/data/testimage.gif', $fileName); |
|
346 | 346 | $this->assertTrue($copyRes); |
347 | 347 | |
348 | 348 | //Create request return |
349 | - $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [filesize(\OC::$SERVERROOT . '/tests/data/testimage.gif')]]; |
|
349 | + $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [filesize(\OC::$SERVERROOT.'/tests/data/testimage.gif')]]; |
|
350 | 350 | $this->request->method('getUploadedFile')->willReturn($reqRet); |
351 | 351 | |
352 | 352 | $response = $this->avatarController->postAvatar(null); |
@@ -366,7 +366,7 @@ discard block |
||
366 | 366 | ->disableOriginalConstructor()->getMock(); |
367 | 367 | $file->expects($this->once()) |
368 | 368 | ->method('getContent') |
369 | - ->willReturn(file_get_contents(\OC::$SERVERROOT . '/tests/data/testimage.jpg')); |
|
369 | + ->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg')); |
|
370 | 370 | $file->expects($this->once()) |
371 | 371 | ->method('getMimeType') |
372 | 372 | ->willReturn('image/jpeg'); |
@@ -437,7 +437,7 @@ discard block |
||
437 | 437 | * Check for proper reply on proper crop argument |
438 | 438 | */ |
439 | 439 | public function testFileTooBig(): void { |
440 | - $fileName = \OC::$SERVERROOT . '/tests/data/testimage.jpg'; |
|
440 | + $fileName = \OC::$SERVERROOT.'/tests/data/testimage.jpg'; |
|
441 | 441 | //Create request return |
442 | 442 | $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [21 * 1024 * 1024]]; |
443 | 443 | $this->request->method('getUploadedFile')->willReturn($reqRet); |