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
|
<?php
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Encryption\Tests;
use OC\Files\View;
use OCA\Encryption\Crypto\Crypt;
use OCA\Encryption\Util;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Storage\IStorage;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class UtilTest extends TestCase {
private static $tempStorage = [];
/** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */
private $configMock;
/** @var \OC\Files\View|\PHPUnit\Framework\MockObject\MockObject */
private $filesMock;
/** @var \OCP\IUserManager|\PHPUnit\Framework\MockObject\MockObject */
private $userManagerMock;
/** @var \OCP\Files\Mount\IMountPoint|\PHPUnit\Framework\MockObject\MockObject */
private $mountMock;
/** @var Util */
private $instance;
public function testSetRecoveryForUser(): void {
$this->instance->setRecoveryForUser('1');
$this->assertArrayHasKey('recoveryEnabled', self::$tempStorage);
}
public function testIsRecoveryEnabledForUser(): void {
$this->assertTrue($this->instance->isRecoveryEnabledForUser('admin'));
// Assert recovery will return default value if not set
unset(self::$tempStorage['recoveryEnabled']);
$this->assertEquals(0, $this->instance->isRecoveryEnabledForUser('admin'));
}
public function testUserHasFiles(): void {
$this->filesMock->expects($this->once())
->method('file_exists')
->willReturn(true);
$this->assertTrue($this->instance->userHasFiles('admin'));
}
protected function setUp(): void {
parent::setUp();
$this->mountMock = $this->createMock(IMountPoint::class);
$this->filesMock = $this->createMock(View::class);
$this->userManagerMock = $this->createMock(IUserManager::class);
/** @var Crypt $cryptMock */
$cryptMock = $this->getMockBuilder(Crypt::class)
->disableOriginalConstructor()
->getMock();
$user = $this->createMock(IUser::class);
$user->expects($this->any())
->method('getUID')
->willReturn('admin');
/** @var IUserSession|MockObject $userSessionMock */
$userSessionMock = $this->createMock(IUserSession::class);
$userSessionMock->expects($this->any())
->method('getUser')
->willReturn($user);
$userSessionMock->expects($this->any())
->method('isLoggedIn')
->willReturn(true);
$this->configMock = $this->createMock(IConfig::class);
$this->configMock->expects($this->any())
->method('getUserValue')
->willReturnCallback([$this, 'getValueTester']);
$this->configMock->expects($this->any())
->method('setUserValue')
->willReturnCallback([$this, 'setValueTester']);
$this->instance = new Util($this->filesMock, $cryptMock, $userSessionMock, $this->configMock, $this->userManagerMock);
}
/**
* @param $userId
* @param $app
* @param $key
* @param $value
*/
public function setValueTester($userId, $app, $key, $value) {
self::$tempStorage[$key] = $value;
}
/**
* @param $userId
* @param $app
* @param $key
* @param $default
* @return mixed
*/
public function getValueTester($userId, $app, $key, $default) {
if (!empty(self::$tempStorage[$key])) {
return self::$tempStorage[$key];
}
return $default ?: null;
}
/**
* @dataProvider dataTestIsMasterKeyEnabled
*
* @param string $value
* @param bool $expect
*/
public function testIsMasterKeyEnabled($value, $expect): void {
$this->configMock->expects($this->once())->method('getAppValue')
->with('encryption', 'useMasterKey', '1')->willReturn($value);
$this->assertSame($expect,
$this->instance->isMasterKeyEnabled()
);
}
public function dataTestIsMasterKeyEnabled() {
return [
['0', false],
['1', true]
];
}
/**
* @dataProvider dataTestShouldEncryptHomeStorage
* @param string $returnValue return value from getAppValue()
* @param bool $expected
*/
public function testShouldEncryptHomeStorage($returnValue, $expected): void {
$this->configMock->expects($this->once())->method('getAppValue')
->with('encryption', 'encryptHomeStorage', '1')
->willReturn($returnValue);
$this->assertSame($expected,
$this->instance->shouldEncryptHomeStorage());
}
public function dataTestShouldEncryptHomeStorage() {
return [
['1', true],
['0', false]
];
}
/**
* @dataProvider dataTestSetEncryptHomeStorage
* @param $value
* @param $expected
*/
public function testSetEncryptHomeStorage($value, $expected): void {
$this->configMock->expects($this->once())->method('setAppValue')
->with('encryption', 'encryptHomeStorage', $expected);
$this->instance->setEncryptHomeStorage($value);
}
public function dataTestSetEncryptHomeStorage() {
return [
[true, '1'],
[false, '0']
];
}
public function testGetStorage(): void {
$return = $this->getMockBuilder(IStorage::class)
->disableOriginalConstructor()
->getMock();
$path = '/foo/bar.txt';
$this->filesMock->expects($this->once())->method('getMount')->with($path)
->willReturn($this->mountMock);
$this->mountMock->expects($this->once())->method('getStorage')->willReturn($return);
$this->assertEquals($return, $this->instance->getStorage($path));
}
}
|