Welcome to mirror list, hosted at ThFree Co, Russian Federation.

U2FProviderTest.php « Provider « Unit « tests - github.com/nextcloud/twofactor_u2f.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6f6df289d478bc1fbdb8edc1c5a78588914299d1 (plain)
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
<?php

/**
 * Nextcloud - U2F 2FA
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
 * @copyright Christoph Wurst 2016
 */

namespace OCA\TwoFactorU2F\Tests\Unit\Provider;

use OCA\TwoFactorU2F\Provider\U2FLoginProvider;
use OCA\TwoFactorU2F\Provider\U2FProvider;
use OCA\TwoFactorU2F\Service\U2FManager;
use OCA\TwoFactorU2F\Settings\Personal;
use OCP\AppFramework\IAppContainer;
use OCP\IL10N;
use OCP\IUser;
use OCP\Template;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class U2FProviderTest extends TestCase {

	/** @var IL10N|MockObject */
	private $l10n;

	/** @var U2FManager|MockObject */
	private $manager;

	/** @var IAppContainer|MockObject */
	private $container;

	/** @var U2FProvider */
	private $provider;

	protected function setUp() {
		parent::setUp();

		$this->l10n = $this->createMock(IL10N::class);
		$this->manager = $this->createMock(U2FManager::class);
		$this->container = $this->createMock(IAppContainer::class);

		$this->provider = new U2FProvider(
			$this->l10n,
			$this->manager,
			$this->container
		);
	}

	public function testGetId() {
		$this->assertSame('u2f', $this->provider->getId());
	}

	public function testGetDisplayName() {
		$this->l10n->expects($this->once())
			->method('t')
			->with('U2F device')
			->willReturn('translated');

		$displayName = $this->provider->getDisplayName();

		$this->assertSame('translated', $displayName);
	}

	public function testGetDescription() {
		$this->l10n->expects($this->once())
			->method('t')
			->with('Authenticate with an U2F device')
			->willReturn('translated');

		$this->assertSame('translated', $this->provider->getDescription());
	}

	public function testGetTemplate() {
		$user = $this->createMock(IUser::class);
		$this->manager->expects($this->once())
			->method('startAuthenticate')
			->willReturn([]);

		$tmpl = new Template('twofactor_u2f', 'challenge');
		$tmpl->assign('reqs', []);

		$actual = $this->provider->getTemplate($user);
		$this->assertEquals($tmpl, $actual);
		$actual->fetchPage();
	}

	public function testVerifyChallenge() {
		$user = $this->createMock(IUser::class);
		$val = '123';

		$this->manager->expects($this->once())
			->method('finishAuthenticate')
			->willReturn(false);

		$this->assertFalse($this->provider->verifyChallenge($user, $val));
	}

	public function testIsTwoFactorAuthEnabledForUser() {
		$user = $this->createMock(IUser::class);
		$devices = [
			'dev1',
		];

		$this->manager->expects($this->once())
			->method('getDevices')
			->willReturn($devices);

		$this->assertTrue($this->provider->isTwoFactorAuthEnabledForUser($user));
	}

	public function testIsTwoFactorAuthDisabledForUser() {
		$user = $this->createMock(IUser::class);
		$devices = [];

		$this->manager->expects($this->once())
			->method('getDevices')
			->willReturn($devices);

		$this->assertFalse($this->provider->isTwoFactorAuthEnabledForUser($user));
	}

	public function testGetGetLightIcon() {
		$expected = image_path('twofactor_u2f', 'app-dark.svg');

		$icon = $this->provider->getDarkIcon();

		$this->assertEquals($expected, $icon);
	}

	public function testGetDarkIcon() {
		$expected = image_path('twofactor_u2f', 'app-dark.svg');

		$icon = $this->provider->getDarkIcon();

		$this->assertEquals($expected, $icon);
	}

	public function testGetPersonalSettings() {
		$expected = new Personal([]);
		$user = $this->createMock(IUser::class);

		$settings = $this->provider->getPersonalSettings($user);

		$this->assertEquals($expected, $settings);
	}

	public function testDisable() {
		$user = $this->createMock(IUser::class);
		$this->manager->expects($this->once())
			->method('removeAllDevices')
			->with($user);

		$this->provider->disableFor($user);
	}

	public function testGet() {
		$user = $this->createMock(IUser::class);
		$loginProvider = $this->createMock(U2FLoginProvider::class);
		$this->container->expects($this->once())
			->method('query')
			->with(U2FLoginProvider::class)
			->willReturn($loginProvider);

		$result = $this->provider->getLoginSetup($user);

		$this->assertSame($loginProvider, $result);
	}

}