-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathPushControllerTest.php
More file actions
507 lines (476 loc) Β· 15.7 KB
/
PushControllerTest.php
File metadata and controls
507 lines (476 loc) Β· 15.7 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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
<?php
/**
* @author Joas Schilling <[email protected]>
* @author Thomas MΓΌller <[email protected]>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
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 OCA\Notifications\Tests\Unit\TestCase;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\IDBConnection;
use OCP\IRequest;
use OCP\ISession;
use OCP\IUser;
use OCP\IUserSession;
class PushControllerTest extends TestCase {
/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
protected $request;
/** @var IDBConnection|\PHPUnit_Framework_MockObject_MockObject */
protected $db;
/** @var ISession|\PHPUnit_Framework_MockObject_MockObject */
protected $session;
/** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
protected $userSession;
/** @var IProvider|\PHPUnit_Framework_MockObject_MockObject */
protected $tokenProvider;
/** @var Manager|\PHPUnit_Framework_MockObject_MockObject */
protected $identityProof;
/** @var IUser|\PHPUnit_Framework_MockObject_MockObject */
protected $user;
/** @var PushController */
protected $controller;
protected $devicePublicKey = '-----BEGIN PUBLIC KEY-----
MIIBIjANBgk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iG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAz0dLlentXDUqMr7CEwbx
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->identityProof = $this->createMock(Manager::class);
}
protected function getController(array $methods = []) {
if (empty($methods)) {
return new PushController(
'notifications',
$this->request,
$this->db,
$this->session,
$this->userSession,
$this->tokenProvider,
$this->identityProof
);
}
return $this->getMockBuilder(PushController::class)
->setConstructorArgs([
'notifications',
$this->request,
$this->db,
$this->session,
$this->userSession,
$this->tokenProvider,
$this->identityProof,
])
->setMethods($methods)
->getMock();
}
public function dataRegisterDevice() {
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($this->devicePublicKey, 1),
'',
true,
0,
false,
null,
['message' => 'INVALID_DEVICE_KEY'],
Http::STATUS_BAD_REQUEST,
],
'device key invalid end' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
substr($this->devicePublicKey, 0, -1),
'',
true,
0,
false,
null,
['message' => 'INVALID_DEVICE_KEY'],
Http::STATUS_BAD_REQUEST,
],
'device key too much end' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
$this->devicePublicKey . "\n\n",
'',
true,
0,
false,
null,
['message' => 'INVALID_DEVICE_KEY'],
Http::STATUS_BAD_REQUEST,
],
'device key without trailing new line' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
$this->devicePublicKey,
'',
true,
0,
false,
null,
['message' => 'INVALID_PROXY_SERVER'],
Http::STATUS_BAD_REQUEST,
],
'device key with trailing new line' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
$this->devicePublicKey . "\n",
'',
true,
0,
false,
null,
['message' => 'INVALID_PROXY_SERVER'],
Http::STATUS_BAD_REQUEST,
],
'invalid push proxy' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
$this->devicePublicKey,
'localhost',
true,
0,
false,
null,
['message' => 'INVALID_PROXY_SERVER'],
Http::STATUS_BAD_REQUEST,
],
'using localhost' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
$this->devicePublicKey,
'http://localhost/',
true,
23,
false,
null,
['message' => 'INVALID_SESSION_TOKEN'],
Http::STATUS_BAD_REQUEST,
],
'using localhost with port' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
$this->devicePublicKey,
'http://localhost:8088/',
true,
23,
false,
null,
['message' => 'INVALID_SESSION_TOKEN'],
Http::STATUS_BAD_REQUEST,
],
'using production' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
$this->devicePublicKey,
'https://push-notifications.nextcloud.com/',
true,
23,
false,
null,
['message' => 'INVALID_SESSION_TOKEN'],
Http::STATUS_BAD_REQUEST,
],
'created or updated' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
$this->devicePublicKey,
'https://push-notifications.nextcloud.com/',
true,
23,
true,
true,
[
'publicKey' => $this->userPublicKey,
'deviceIdentifier' => 'XUCEZ1EHvTUcVhIvrQQQ1XcP0ZD2BFdFqw4EYbOhBfiEgXgirurR4x/ve4GSSyfivvbQOdOkZUM+g4m+tSb0Ew==',
'signature' => 'X9+J7NNLfG9Ft6C36zrYLVJ5aH5euIROzdV937hsU81jL7WvOwzBfc7bImzxU3Bnev5wEKwkw7Ts/2q/+UUkOxgtEZinp52s87S5obKtsVXsczHbsqg4p/ueoBPhF17VsP1e8kMtxZ4snk/iArX4Eu1cfaM3+OckmpO0MYXy0rUbYpQPAJo4VgRFKKjFvfEVOj8N74DTIJ+TjRsvvDhJbb9KpeFe3a6Rv9mIo0AqoK+deAbUkWY0aM+74noVXvPtNzExgK4mWJ02+JHEuQEUbCuQsgoBia0vC3fILbwVxHzrieWGEnE7vkRyFEzlkeo7ZSMawDPxsPN5HxwBs2SZig==',
],
Http::STATUS_CREATED,
],
'not updated' => [
'bb9b52140661ee4f2c31e02ea50a8f67ba353bffc58aa981718f90bd2aa2bd8fc08cad4c0b3ed8f7eb9d79d6a577be75d084bbeb963da1ad74d9279e0014e472',
$this->devicePublicKey,
'https://push-notifications.nextcloud.com/',
true,
42,
true,
false,
[
'publicKey' => $this->userPublicKey,
'deviceIdentifier' => 'x9vSImcGjhzR9BfZ/XbbUqqCCNC4bHKsX7vkQWNZRd1/MiY+OuF02fx8K08My0RpkNnwj/rQ/gVSU1oEdFwkww==',
'signature' => 'GFpnv3MO7mcBef2RJ4Ayrl6RQakGM7AvlKhoTr3DUWnv+iBzwGy8YV34HIPoArz4tyqonHRlLsxPYq4ENPfGO99KrIS16z4RUq0wiCBGf+S8/K8lM9cE9EBKE9yrkTsSvZGICEusvxQ+cTfVr30bnavvi1wL1UuxxDBlJebda9FJ9HfaS24j4rT7K78oMguqDVM+4hhr6BMhcpUVV+kTpOaBpluw5pRDwUP3jJBmkkOa57WRKFcu0Lr/XIx/G0c8Si+BAfM//CTMstwp5XDFn4W9EYSStjNrvsULdV+tOKFwnowqts+UFzEDvmZ1g4qIMWUUPBF4/pjaiDqtMojgrA==',
],
Http::STATUS_OK,
],
];
}
/**
* @dataProvider dataRegisterDevice
*
* @param string $pushTokenHash
* @param string $devicePublicKey
* @param string $proxyServer
* @param bool $userIsValid
* @param int $tokenId
* @param bool $tokenIsValid
* @param bool $deviceCreated
* @param array $payload
* @param int $status
*/
public function testRegisterDevice($pushTokenHash, $devicePublicKey, $proxyServer, $userIsValid, $tokenId, $tokenIsValid, $deviceCreated, $payload, $status) {
$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($this->userPrivateKey);
$key->expects($this->once())
->method('getPublic')
->willReturn($this->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());
}
$response = $controller->registerDevice($pushTokenHash, $devicePublicKey, $proxyServer);
$this->assertInstanceOf(DataResponse::class, $response);
$this->assertSame($status, $response->getStatus());
$this->assertSame($payload, $response->getData());
}
public function dataRemoveDevice() {
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 dataRemoveDevice
*
* @param bool $userIsValid
* @param int $tokenId
* @param bool $tokenIsValid
* @param bool $deviceDeleted
* @param array $payload
* @param int $status
*/
public function testRemoveDevice($userIsValid, $tokenId, $tokenIsValid, $deviceDeleted, $payload, $status) {
$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());
}
}