-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathWebPushClientTest.php
More file actions
73 lines (58 loc) · 2.33 KB
/
Copy pathWebPushClientTest.php
File metadata and controls
73 lines (58 loc) · 2.33 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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Notifications\Tests\Unit;
use OCA\Notifications\WebPushClient;
use OCP\AppFramework\Services\IAppConfig;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class WebPushClientTest extends TestCase {
protected IAppConfig&MockObject $appConfig;
protected function setUp(): void {
parent::setUp();
$this->appConfig = $this->createMock(IAppConfig::class);
}
public function testConstructSucceedsWhenVapidKeysAreStored(): void {
$this->appConfig->method('getAppValueString')
->willReturnMap([
['webpush_vapid_pubkey', '', false, 'BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw'],
['webpush_vapid_privkey', '', false, 'test-private-key'],
]);
$this->appConfig->expects($this->never())->method('setAppValueString');
$client = new WebPushClient($this->appConfig);
$this->assertInstanceOf(WebPushClient::class, $client);
}
public function testConstructRegeneratesVapidKeysWhenDecryptionFails(): void {
// Simulates the case where the stored VAPID keys were encrypted with a
// different instance secret — getAppValueString throws during decryption.
$this->appConfig->method('getAppValueString')
->willThrowException(new \RuntimeException('HMAC does not match.'));
$this->appConfig->expects($this->exactly(2))
->method('setAppValueString')
->with($this->logicalOr(
$this->equalTo('webpush_vapid_pubkey'),
$this->equalTo('webpush_vapid_privkey'),
));
// Must not throw — corrupted keys should be transparently regenerated
$client = new WebPushClient($this->appConfig);
$this->assertInstanceOf(WebPushClient::class, $client);
}
public function testConstructRegeneratesVapidKeysWhenMissing(): void {
$this->appConfig->method('getAppValueString')
->willReturnMap([
['webpush_vapid_pubkey', '', false, ''],
['webpush_vapid_privkey', '', false, ''],
]);
$this->appConfig->expects($this->exactly(2))
->method('setAppValueString')
->with($this->logicalOr(
$this->equalTo('webpush_vapid_pubkey'),
$this->equalTo('webpush_vapid_privkey'),
));
$client = new WebPushClient($this->appConfig);
$this->assertInstanceOf(WebPushClient::class, $client);
}
}