-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathPushControllerTest.php
More file actions
473 lines (442 loc) Β· 14.9 KB
/
Copy pathPushControllerTest.php
File metadata and controls
473 lines (442 loc) Β· 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Notifications\Tests\Unit\Controller;
use OC\Authentication\Exceptions\InvalidTokenException;
use OC\Authentication\Token\IProvider;
use OC\Authentication\Token\IToken;
use OC\Security\IdentityProof\Key;
use OC\Security\IdentityProof\Manager;
use OCA\Notifications\Controller\PushController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\IDBConnection;
use OCP\IRequest;
use OCP\ISession;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Security\IRemoteHostValidator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class PushControllerTest extends TestCase {
protected IRequest&MockObject $request;
protected IDBConnection&MockObject $db;
protected ISession&MockObject $session;
protected IUserSession&MockObject $userSession;
protected IProvider&MockObject $tokenProvider;
protected IRemoteHostValidator&MockObject $hostValidator;
protected Manager&MockObject $identityProof;
protected IUser&MockObject $user;
protected PushController $controller;
protected static string $devicePublicKey = '-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2Or1KumSDfk8dT0MuCW9
WS5wkVOpNsbz2OIJFBYrBvu6joC2iQo9StONMaXoTQj5Ucak9UBtC60PHyTkIDFb
HOpCST5onmIAtZdqHN/3ABOBeHVU/notdRIl/menGM64jiqGWvE06F1+yZ8GGcGQ
8RKzabqMd2K1iUohXP625uzTABVaiwz3u8nGEwui5R6Pf5Fy6DccuqdUMtJIfW21
Z4Tj48Tw+pR+fUrGpa1Wg+wiwlg7ISK8Symml1Rd6hSRXK2t8Opm/kjH9ZX8oVwn
RSO1ehjzRpTY+gdw/5gvwMZI0XmrIanZmZHwePRR4HC6FLPrL2OQG3gWikDIPyTS
hQIDAQAB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6hclG6/gE3fwn4
mL33IIzQ9BCoXxr3tcS0r4iQjbGKorUNJW1OwmkqyMZ4POF9BSkLXpTTcJaM5WxU
8JU9PmLX3wKBgFNpuLMX27j8MUQQ2xwuttp7w48zCgLlzRWsldiP9ZxbZhzOBQcL
glmLYmJ/79OAmisduqP/R7X2x7kpqK3FwKFrUGtNouVttB+x73+ZGC1FTD5mcUXi
D+3BIp002EpRsi+Wi7+M+w1JZCUjAkmZV6f8xndq11MNlNFm96sUBXvBAoGAJ9hc
tgYYARDprrfN0RdI6eLKzMbS2IAUHaJuJadZNv+B0rJSUTlfVSn32oFGRiBbNWHX
RhcFD2mU+LfN2DzozMkEvbdnf/WUUBrVqJagcILwcvx0TpJ/451PKGIGrB0/EJcW
Vmk3R+NnYvdvHElOgjbNPMdF+sTL/EzGOZxc9QECgYBNY4LAAKqrw47p+lcRi31O
X4fhdGWAIFyiUliPDkxzEl8857FbT5c6qhdes3Gyc9tSF1wh0X7lpCDquWXYLP1V
9WNvdon+YMRi9BKpO0SlE07lwFANBpz+wJkhONVJBMzvKbxEnMRPRJ4lWa0VAAGE
j2ZL3j2Nwefj3HrR/AkeFA==
-----END PRIVATE KEY-----
';
protected static string $userPublicKey = '-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAz0dLlentXDUqMr7CEwbx
smKTiJ/byO86RIZOxRhsHTk3dnccSdopHoESeha9jo3HteSt834mrFz3p0wELhRF
3nBqD/VmPg0uV9S3wqY/RHgDgX1RinkA7Lec/xqF3lPmKlm2siU6erxUY+6OV+kA
wgl8tuSreLJzQ1kxz5THOOAS42esPlo9A9skpc9XRqbp3NL8GDf7hMo6/c7ZojfN
k2bWR4MzeNwyUr+7S7n7GYBrFp1ZcrV5rIuELggCnJvdl9mbSF5J+Z4jFg8SSXqa
hce6QtyC1zGSmKJQLbfdVby4hkI81EYauxwOEHarxcil6dLizdqymY6kNNp1Mwt+
FwIDAQAB
-----END PUBLIC KEY-----
';
protected function setUp(): void {
parent::setUp();
$this->request = $this->createMock(IRequest::class);
$this->db = $this->createMock(IDBConnection::class);
$this->session = $this->createMock(ISession::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->tokenProvider = $this->createMock(IProvider::class);
$this->hostValidator = $this->createMock(IRemoteHostValidator::class);
$this->identityProof = $this->createMock(Manager::class);
}
protected function getController(array $methods = []): PushController|MockObject {
if (empty($methods)) {
return new PushController(
'notifications',
$this->request,
$this->db,
$this->session,
$this->userSession,
$this->tokenProvider,
$this->hostValidator,
$this->identityProof,
);
}
return $this->getMockBuilder(PushController::class)
->setConstructorArgs([
'notifications',
$this->request,
$this->db,
$this->session,
$this->userSession,
$this->tokenProvider,
$this->hostValidator,
$this->identityProof,
])
->onlyMethods($methods)
->getMock();
}
public static function dataRegisterDevice(): array {
return [
'not authenticated' => [
'',
'',
'',
false,
0,
false,
null,
[],
Http::STATUS_UNAUTHORIZED
],
'too short token hash' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e47',
'',
'',
true,
0,
false,
null,
['message' => 'INVALID_PUSHTOKEN_HASH'],
Http::STATUS_BAD_REQUEST,
],
'too long token hash' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e4722',
'',
'',
true,
0,
false,
null,
['message' => 'INVALID_PUSHTOKEN_HASH'],
Http::STATUS_BAD_REQUEST,
],
'invalid char in token hash' => [
'rb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
'',
'',
true,
0,
false,
null,
['message' => 'INVALID_PUSHTOKEN_HASH'],
Http::STATUS_BAD_REQUEST,
],
'device key invalid start' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
substr(self::$devicePublicKey, 1),
'',
true,
0,
false,
null,
['message' => 'INVALID_DEVICE_KEY'],
Http::STATUS_BAD_REQUEST,
],
'device key invalid end' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
substr(self::$devicePublicKey, 0, -1),
'',
true,
0,
false,
null,
['message' => 'INVALID_DEVICE_KEY'],
Http::STATUS_BAD_REQUEST,
],
'device key too much end' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
self::$devicePublicKey . "\n\n",
'',
true,
0,
false,
null,
['message' => 'INVALID_DEVICE_KEY'],
Http::STATUS_BAD_REQUEST,
],
'device key without trailing new line' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
self::$devicePublicKey,
'',
true,
0,
false,
null,
['message' => 'INVALID_PROXY_SERVER'],
Http::STATUS_BAD_REQUEST,
],
'device key with trailing new line' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
self::$devicePublicKey . "\n",
'',
true,
0,
false,
null,
['message' => 'INVALID_PROXY_SERVER'],
Http::STATUS_BAD_REQUEST,
],
'invalid push proxy' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
self::$devicePublicKey,
'localhost',
true,
0,
false,
null,
['message' => 'INVALID_PROXY_SERVER'],
Http::STATUS_BAD_REQUEST,
],
'using localhost' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
self::$devicePublicKey,
'http://localhost/',
true,
23,
false,
null,
['message' => 'INVALID_SESSION_TOKEN'],
Http::STATUS_BAD_REQUEST,
],
'using localhost with port' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
self::$devicePublicKey,
'http://localhost:8088/',
true,
23,
false,
null,
['message' => 'INVALID_SESSION_TOKEN'],
Http::STATUS_BAD_REQUEST,
],
'using production' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
self::$devicePublicKey,
'https://push-notifications.nextcloud.com/',
true,
23,
false,
null,
['message' => 'INVALID_SESSION_TOKEN'],
Http::STATUS_BAD_REQUEST,
],
'created or updated' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
self::$devicePublicKey,
'https://push-notifications.nextcloud.com/',
true,
23,
true,
true,
[
'publicKey' => self::$userPublicKey,
'deviceIdentifier' => 'XUCEZ1EHvTUcVhIvrQQQ1XcP0ZD2BFdFqw4EYbOhBfiEgXgirurR4x/ve4GSSyfivvbQOdOkZUM+g4m+tSb0Ew==',
'signature' => 'LRhbXO71WYX9qqDbQX7C+87YaaFfWoT/vG0DlaXdBz6+lhyOA0dw/1Ggz3fd7RerCQ0MfgnnTyxO+cSeRpUaPdA2yPjfoiPpfYA5SOJQGF3comS/HYna3fHiFDbOoM3BJOnjvqiSZdxA/ICdyl2mEEC5wO7AZ4OZKBTa5XfL7eSCXZLEv1YldqcLOStbXrI7voDQocTMJxoQZI/j8BVcf2i3D6F454aXIFDrYYzC2PQY+CKJoXZW0m0RMWaTM2B8tBmFFwrmaGLDqcjjpd33TsTtsV5DB7WimffLBPpOuGV4Z1Kiagp/mxpPLz2NImNV79mDX9gY3ZppCZTwChP5qQ==',
],
Http::STATUS_CREATED,
],
'not updated' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
self::$devicePublicKey,
'https://push-notifications.nextcloud.com/',
true,
42,
true,
false,
[
'publicKey' => self::$userPublicKey,
'deviceIdentifier' => 'x9vSImcGjhzR9BfZ/XbbUqqCCNC4bHKsX7vkQWNZRd1/MiY+OuF02fx8K08My0RpkNnwj/rQ/gVSU1oEdFwkww==',
'signature' => 'J9AcdJt5youJmMnBhS+Cc9ytArynIKtCRoNf/m0oOFO/e0hWHqs1NRdQBe81qzYIjf0+bj0Q97X9Xv1rnVJesPkQUbGaa4nAPt+viGSfvzTptjX4LKgqm8B3UkduBA262IcaWgM5P84gUqelkQIC1nIqq/MJTuC6oQ5lUwIV1a92ZurDjhwH4b3f7/ZLTTOTRD0DWN9W/yOyF1qECivgePR3eu+mkcBzXVU/TDZDJic9G7xhqcTnWV6qk+aKyzdNo1tu5W7mF+v5vF6rrGZrq55vPLWAHApTD7P+NFV01BnaCuN7/qGJNVs7m7EH03jpOw7y3jqNMmcmonYrJSMVqg==',
],
Http::STATUS_OK,
],
];
}
#[DataProvider(methodName: 'dataRegisterDevice')]
public function testRegisterDevice(string $pushTokenHash, string $devicePublicKey, string $proxyServer, bool $userIsValid, int $tokenId, bool $tokenIsValid, ?bool $deviceCreated, array $payload, int $status): void {
$controller = $this->getController([
'savePushToken',
]);
$user = $this->createMock(IUser::class);
if ($userIsValid) {
$this->userSession->expects($this->any())
->method('getUser')
->willReturn($user);
} else {
$this->userSession->expects($this->any())
->method('getUser')
->willReturn(null);
}
$this->session->expects($tokenId > 0 ? $this->once() : $this->never())
->method('get')
->with('token-id')
->willReturn($tokenId);
if ($tokenIsValid) {
$token = $this->createMock(IToken::class);
$token->expects($this->once())
->method('getId')
->willReturn($tokenId);
$this->tokenProvider->expects($this->any())
->method('getTokenById')
->with($tokenId)
->willReturn($token);
$key = $this->createMock(Key::class);
$key->expects($this->once())
->method('getPrivate')
->willReturn(self::$userPrivateKey);
$key->expects($this->once())
->method('getPublic')
->willReturn(self::$userPublicKey);
$this->identityProof->expects($this->once())
->method('getKey')
->with($user)
->willReturn($key);
$controller->expects($this->once())
->method('savePushToken')
->with($user, $token, $this->anything(), $devicePublicKey, $pushTokenHash, $proxyServer)
->willReturn($deviceCreated);
} else {
$controller->expects($this->never())
->method('savePushToken');
$this->tokenProvider->expects($this->any())
->method('getTokenById')
->with($tokenId)
->willThrowException(new InvalidTokenException());
}
$this->hostValidator->expects($this->any())
->method('isValid')
->willReturn(true);
$response = $controller->registerDevice($pushTokenHash, $devicePublicKey, $proxyServer);
$this->assertInstanceOf(DataResponse::class, $response);
$this->assertSame($status, $response->getStatus());
$this->assertSame($payload, $response->getData());
}
public static function dataRemoveDevice(): array {
return [
'not authenticated' => [
false,
0,
false,
null,
[],
Http::STATUS_UNAUTHORIZED
],
'invalid token' => [
true,
23,
false,
null,
['message' => 'INVALID_SESSION_TOKEN'],
Http::STATUS_BAD_REQUEST,
],
'using production' => [
true,
23,
false,
null,
['message' => 'INVALID_SESSION_TOKEN'],
Http::STATUS_BAD_REQUEST,
],
'created or updated' => [
true,
23,
true,
true,
[],
Http::STATUS_ACCEPTED,
],
'not updated' => [
true,
42,
true,
false,
[],
Http::STATUS_OK,
],
];
}
#[DataProvider(methodName: 'dataRemoveDevice')]
public function testRemoveDevice(bool $userIsValid, int $tokenId, bool $tokenIsValid, ?bool $deviceDeleted, array $payload, int $status): void {
$controller = $this->getController([
'deletePushToken',
]);
$user = $this->createMock(IUser::class);
if ($userIsValid) {
$this->userSession->expects($this->any())
->method('getUser')
->willReturn($user);
} else {
$this->userSession->expects($this->any())
->method('getUser')
->willReturn(null);
}
$this->session->expects($tokenId > 0 ? $this->once() : $this->never())
->method('get')
->with('token-id')
->willReturn($tokenId);
if ($tokenIsValid) {
$token = $this->createMock(IToken::class);
$this->tokenProvider->expects($this->any())
->method('getTokenById')
->with($tokenId)
->willReturn($token);
$controller->expects($this->once())
->method('deletePushToken')
->with($user, $token)
->willReturn($deviceDeleted);
} else {
$controller->expects($this->never())
->method('deletePushToken');
$this->tokenProvider->expects($this->any())
->method('getTokenById')
->with($tokenId)
->willThrowException(new InvalidTokenException());
}
$response = $controller->removeDevice();
$this->assertInstanceOf(DataResponse::class, $response);
$this->assertSame($status, $response->getStatus());
$this->assertSame($payload, $response->getData());
}
}