-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathWebAuthnProviderTest.php
More file actions
221 lines (173 loc) · 5.98 KB
/
WebAuthnProviderTest.php
File metadata and controls
221 lines (173 loc) · 5.98 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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\TwoFactorWebauthn\Tests\Unit\Provider;
use OCA\TwoFactorWebauthn\Model\Device;
use OCA\TwoFactorWebauthn\Provider\WebAuthnLoginProvider;
use OCA\TwoFactorWebauthn\Provider\WebAuthnProvider;
use OCA\TwoFactorWebauthn\Service\WebAuthnManager;
use OCA\TwoFactorWebauthn\Settings\Personal;
use OCP\AppFramework\Services\IInitialState;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Template;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Webauthn\PublicKeyCredentialRequestOptions;
class WebAuthnProviderTest extends TestCase {
/** @var IL10N|MockObject */
private $l10n;
/** @var WebAuthnManager|MockObject */
private $manager;
/** @var IURLGenerator|MockObject */
private $urlGenerator;
/** @var IInitialState|MockObject */
private $initialState;
/** @var IRequest|MockObject */
private $request;
/** @var WebAuthnProvider */
private $provider;
/** @var ContainerInterface */
private $container;
protected function setUp(): void {
parent::setUp();
$this->l10n = $this->createMock(IL10N::class);
$this->manager = $this->createMock(WebAuthnManager::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->initialState = $this->createMock(IInitialState::class);
$this->request = $this->createMock(IRequest::class);
$this->container = $this->createMock(ContainerInterface::class);
$this->provider = new WebAuthnProvider(
$this->l10n,
$this->manager,
$this->initialState,
$this->urlGenerator,
$this->request,
$this->container,
);
}
public function testGetId(): void {
self::assertSame('webauthn', $this->provider->getId());
}
public function testGetDisplayName(): void {
$this->l10n->expects(self::once())
->method('t')
->with('Security key')
->willReturn('translated');
$displayName = $this->provider->getDisplayName();
self::assertSame('translated', $displayName);
}
public function testGetDescription(): void {
$this->l10n->expects(self::once())
->method('t')
->with('Use WebAuthn for second factor authentication')
->willReturn('translated');
self::assertSame('translated', $this->provider->getDescription());
}
public function testGetTemplate(): void {
$user = $this->createMock(IUser::class);
$key = new PublicKeyCredentialRequestOptions('challenge');
$serverHost = 'my.next.cloud';
$this->request->expects(self::once())
->method('getServerHost')
->willReturn($serverHost);
$this->manager->expects(self::once())
->method('startAuthenticate')
->with($user, $serverHost)
->willReturn($key);
$this->initialState->expects(self::once())
->method('provideInitialState')
->with('credential-request-options', $key);
$tmpl = new Template('twofactor_webauthn', 'challenge');
$actual = $this->provider->getTemplate($user);
self::assertEquals($tmpl, $actual);
$actual->fetchPage();
}
public function testVerifyChallenge(): void {
$user = $this->createMock(IUser::class);
$val = '123';
$this->manager->expects(self::once())
->method('finishAuthenticate')
->willReturn(false);
self::assertFalse($this->provider->verifyChallenge($user, $val));
}
public function testIsTwoFactorAuthEnabledForUser(): void {
$user = $this->createMock(IUser::class);
$this->manager->expects(self::once())
->method('getDevices')
->willReturn([
new Device(1, 'k1', 'n1', null, true),
]);
self::assertTrue($this->provider->isTwoFactorAuthEnabledForUser($user));
}
public function testIsTwoFactorAuthDisabledForUserBecauseDisabledDevice(): void {
$user = $this->createMock(IUser::class);
$this->manager->expects(self::once())
->method('getDevices')
->willReturn([
new Device(1, 'k1', 'n1', null, false),
]);
self::assertFalse($this->provider->isTwoFactorAuthEnabledForUser($user));
}
public function testIsTwoFactorAuthDisabledForUserBecauseNoDevice(): void {
$user = $this->createMock(IUser::class);
$devices = [];
$this->manager->expects(self::once())
->method('getDevices')
->willReturn($devices);
self::assertFalse($this->provider->isTwoFactorAuthEnabledForUser($user));
}
public function testGetGetLightIcon(): void {
$this->urlGenerator->expects(self::once())
->method('imagePath')
->with('twofactor_webauthn', 'app.svg')
->willReturn('/apps/twofactor_webauthn/img/app.svg');
$icon = $this->provider->getLightIcon();
self::assertEquals('/apps/twofactor_webauthn/img/app.svg', $icon);
}
public function testGetDarkIcon(): void {
$this->urlGenerator->expects(self::once())
->method('imagePath')
->with('twofactor_webauthn', 'app-dark.svg')
->willReturn('/apps/twofactor_webauthn/img/app-dark.svg');
$icon = $this->provider->getDarkIcon();
self::assertEquals('/apps/twofactor_webauthn/img/app-dark.svg', $icon);
}
public function testGetPersonalSettings(): void {
$expected = new Personal();
$this->initialState->expects(self::once())
->method('provideInitialState')
->with(
'devices',
['my', 'devices']
);
$user = $this->createMock(IUser::class);
$this->manager->method('getDevices')
->willReturn(['my', 'devices']);
$settings = $this->provider->getPersonalSettings($user);
self::assertEquals($expected, $settings);
}
public function testDisable(): void {
$user = $this->createMock(IUser::class);
$this->manager->expects(self::once())
->method('deactivateAllDevices')
->with($user);
$this->provider->disableFor($user);
}
public function testGetLoginSetupProvider(): void {
$user = $this->createMock(IUser::class);
$loginProvider = $this->createMock(WebAuthnLoginProvider::class);
$this->container->expects($this->once())
->method('get')
->with(WebAuthnLoginProvider::class)
->willReturn($loginProvider);
$result = $this->provider->getLoginSetup($user);
$this->assertSame($loginProvider, $result);
}
}