@@ -16,93 +16,93 @@ |
||
16 | 16 | |
17 | 17 | class ProvisioningApiMiddlewareTest extends TestCase { |
18 | 18 | |
19 | - /** @var IControllerMethodReflector|\PHPUnit\Framework\MockObject\MockObject */ |
|
20 | - private $reflector; |
|
19 | + /** @var IControllerMethodReflector|\PHPUnit\Framework\MockObject\MockObject */ |
|
20 | + private $reflector; |
|
21 | 21 | |
22 | - protected function setUp(): void { |
|
23 | - parent::setUp(); |
|
22 | + protected function setUp(): void { |
|
23 | + parent::setUp(); |
|
24 | 24 | |
25 | - $this->reflector = $this->createMock(IControllerMethodReflector::class); |
|
26 | - } |
|
25 | + $this->reflector = $this->createMock(IControllerMethodReflector::class); |
|
26 | + } |
|
27 | 27 | |
28 | - public static function dataAnnotation(): array { |
|
29 | - return [ |
|
30 | - [false, false, false, false, false], |
|
31 | - [false, false, true, false, false], |
|
32 | - [false, true, true, false, false], |
|
33 | - [true, false, false, false, true], |
|
34 | - [true, false, true, false, false], |
|
35 | - [true, true, false, false, false], |
|
36 | - [true, true, true, false, false], |
|
37 | - [false, false, false, true, false], |
|
38 | - [false, false, true, true, false], |
|
39 | - [false, true, true, true, false], |
|
40 | - [true, false, false, true, false], |
|
41 | - [true, false, true, true, false], |
|
42 | - [true, true, false, true, false], |
|
43 | - [true, true, true, true, false], |
|
44 | - ]; |
|
45 | - } |
|
28 | + public static function dataAnnotation(): array { |
|
29 | + return [ |
|
30 | + [false, false, false, false, false], |
|
31 | + [false, false, true, false, false], |
|
32 | + [false, true, true, false, false], |
|
33 | + [true, false, false, false, true], |
|
34 | + [true, false, true, false, false], |
|
35 | + [true, true, false, false, false], |
|
36 | + [true, true, true, false, false], |
|
37 | + [false, false, false, true, false], |
|
38 | + [false, false, true, true, false], |
|
39 | + [false, true, true, true, false], |
|
40 | + [true, false, false, true, false], |
|
41 | + [true, false, true, true, false], |
|
42 | + [true, true, false, true, false], |
|
43 | + [true, true, true, true, false], |
|
44 | + ]; |
|
45 | + } |
|
46 | 46 | |
47 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataAnnotation')] |
|
48 | - public function testBeforeController(bool $subadminRequired, bool $isAdmin, bool $isSubAdmin, bool $hasSettingAuthorizationAnnotation, bool $shouldThrowException): void { |
|
49 | - $middleware = new ProvisioningApiMiddleware( |
|
50 | - $this->reflector, |
|
51 | - $isAdmin, |
|
52 | - $isSubAdmin |
|
53 | - ); |
|
47 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataAnnotation')] |
|
48 | + public function testBeforeController(bool $subadminRequired, bool $isAdmin, bool $isSubAdmin, bool $hasSettingAuthorizationAnnotation, bool $shouldThrowException): void { |
|
49 | + $middleware = new ProvisioningApiMiddleware( |
|
50 | + $this->reflector, |
|
51 | + $isAdmin, |
|
52 | + $isSubAdmin |
|
53 | + ); |
|
54 | 54 | |
55 | - $this->reflector->method('hasAnnotation') |
|
56 | - ->willReturnCallback(function ($annotation) use ($subadminRequired, $hasSettingAuthorizationAnnotation) { |
|
57 | - if ($annotation === 'NoSubAdminRequired') { |
|
58 | - return !$subadminRequired; |
|
59 | - } |
|
60 | - if ($annotation === 'AuthorizedAdminSetting') { |
|
61 | - return $hasSettingAuthorizationAnnotation; |
|
62 | - } |
|
63 | - return false; |
|
64 | - }); |
|
55 | + $this->reflector->method('hasAnnotation') |
|
56 | + ->willReturnCallback(function ($annotation) use ($subadminRequired, $hasSettingAuthorizationAnnotation) { |
|
57 | + if ($annotation === 'NoSubAdminRequired') { |
|
58 | + return !$subadminRequired; |
|
59 | + } |
|
60 | + if ($annotation === 'AuthorizedAdminSetting') { |
|
61 | + return $hasSettingAuthorizationAnnotation; |
|
62 | + } |
|
63 | + return false; |
|
64 | + }); |
|
65 | 65 | |
66 | - try { |
|
67 | - $middleware->beforeController( |
|
68 | - $this->createMock(Controller::class), |
|
69 | - 'myMethod' |
|
70 | - ); |
|
71 | - $this->assertFalse($shouldThrowException); |
|
72 | - } catch (NotSubAdminException $e) { |
|
73 | - $this->assertTrue($shouldThrowException); |
|
74 | - } |
|
75 | - } |
|
66 | + try { |
|
67 | + $middleware->beforeController( |
|
68 | + $this->createMock(Controller::class), |
|
69 | + 'myMethod' |
|
70 | + ); |
|
71 | + $this->assertFalse($shouldThrowException); |
|
72 | + } catch (NotSubAdminException $e) { |
|
73 | + $this->assertTrue($shouldThrowException); |
|
74 | + } |
|
75 | + } |
|
76 | 76 | |
77 | - public static function dataAfterException(): array { |
|
78 | - return [ |
|
79 | - [new NotSubAdminException(), false], |
|
80 | - [new \Exception('test', 42), true], |
|
81 | - ]; |
|
82 | - } |
|
77 | + public static function dataAfterException(): array { |
|
78 | + return [ |
|
79 | + [new NotSubAdminException(), false], |
|
80 | + [new \Exception('test', 42), true], |
|
81 | + ]; |
|
82 | + } |
|
83 | 83 | |
84 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataAfterException')] |
|
85 | - public function testAfterException(\Exception $exception, bool $forwared): void { |
|
86 | - $middleware = new ProvisioningApiMiddleware( |
|
87 | - $this->reflector, |
|
88 | - false, |
|
89 | - false |
|
90 | - ); |
|
84 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataAfterException')] |
|
85 | + public function testAfterException(\Exception $exception, bool $forwared): void { |
|
86 | + $middleware = new ProvisioningApiMiddleware( |
|
87 | + $this->reflector, |
|
88 | + false, |
|
89 | + false |
|
90 | + ); |
|
91 | 91 | |
92 | - try { |
|
93 | - $middleware->afterException( |
|
94 | - $this->createMock(Controller::class), |
|
95 | - 'myMethod', |
|
96 | - $exception |
|
97 | - ); |
|
98 | - $this->fail(); |
|
99 | - } catch (OCSException $e) { |
|
100 | - $this->assertFalse($forwared); |
|
101 | - $this->assertSame($exception->getMessage(), $e->getMessage()); |
|
102 | - $this->assertSame(Http::STATUS_FORBIDDEN, $e->getCode()); |
|
103 | - } catch (\Exception $e) { |
|
104 | - $this->assertTrue($forwared); |
|
105 | - $this->assertSame($exception, $e); |
|
106 | - } |
|
107 | - } |
|
92 | + try { |
|
93 | + $middleware->afterException( |
|
94 | + $this->createMock(Controller::class), |
|
95 | + 'myMethod', |
|
96 | + $exception |
|
97 | + ); |
|
98 | + $this->fail(); |
|
99 | + } catch (OCSException $e) { |
|
100 | + $this->assertFalse($forwared); |
|
101 | + $this->assertSame($exception->getMessage(), $e->getMessage()); |
|
102 | + $this->assertSame(Http::STATUS_FORBIDDEN, $e->getCode()); |
|
103 | + } catch (\Exception $e) { |
|
104 | + $this->assertTrue($forwared); |
|
105 | + $this->assertSame($exception, $e); |
|
106 | + } |
|
107 | + } |
|
108 | 108 | } |
@@ -23,55 +23,55 @@ |
||
23 | 23 | */ |
24 | 24 | class CapabilitiesTest extends TestCase { |
25 | 25 | |
26 | - protected IAppManager&MockObject $appManager; |
|
27 | - protected Capabilities $capabilities; |
|
26 | + protected IAppManager&MockObject $appManager; |
|
27 | + protected Capabilities $capabilities; |
|
28 | 28 | |
29 | - public function setUp(): void { |
|
30 | - parent::setUp(); |
|
31 | - $this->appManager = $this->createMock(IAppManager::class); |
|
32 | - $this->capabilities = new Capabilities($this->appManager); |
|
29 | + public function setUp(): void { |
|
30 | + parent::setUp(); |
|
31 | + $this->appManager = $this->createMock(IAppManager::class); |
|
32 | + $this->capabilities = new Capabilities($this->appManager); |
|
33 | 33 | |
34 | - $this->appManager->expects($this->once()) |
|
35 | - ->method('getAppVersion') |
|
36 | - ->with('provisioning_api') |
|
37 | - ->willReturn('1.12'); |
|
38 | - } |
|
34 | + $this->appManager->expects($this->once()) |
|
35 | + ->method('getAppVersion') |
|
36 | + ->with('provisioning_api') |
|
37 | + ->willReturn('1.12'); |
|
38 | + } |
|
39 | 39 | |
40 | - public static function getCapabilitiesProvider(): array { |
|
41 | - return [ |
|
42 | - [true, false, false, true, false], |
|
43 | - [true, true, false, true, false], |
|
44 | - [true, true, true, true, true], |
|
45 | - [false, false, false, false, false], |
|
46 | - [false, true, false, false, false], |
|
47 | - [false, true, true, false, true], |
|
48 | - ]; |
|
49 | - } |
|
40 | + public static function getCapabilitiesProvider(): array { |
|
41 | + return [ |
|
42 | + [true, false, false, true, false], |
|
43 | + [true, true, false, true, false], |
|
44 | + [true, true, true, true, true], |
|
45 | + [false, false, false, false, false], |
|
46 | + [false, true, false, false, false], |
|
47 | + [false, true, true, false, true], |
|
48 | + ]; |
|
49 | + } |
|
50 | 50 | |
51 | - #[\PHPUnit\Framework\Attributes\DataProvider('getCapabilitiesProvider')] |
|
52 | - public function testGetCapabilities(bool $federationAppEnabled, bool $federatedFileSharingAppEnabled, bool $lookupServerEnabled, bool $expectedFederatedScopeEnabled, bool $expectedPublishedScopeEnabled): void { |
|
53 | - $this->appManager->expects($this->any()) |
|
54 | - ->method('isEnabledForUser') |
|
55 | - ->willReturnMap([ |
|
56 | - ['federation', null, $federationAppEnabled], |
|
57 | - ['federatedfilesharing', null, $federatedFileSharingAppEnabled], |
|
58 | - ]); |
|
51 | + #[\PHPUnit\Framework\Attributes\DataProvider('getCapabilitiesProvider')] |
|
52 | + public function testGetCapabilities(bool $federationAppEnabled, bool $federatedFileSharingAppEnabled, bool $lookupServerEnabled, bool $expectedFederatedScopeEnabled, bool $expectedPublishedScopeEnabled): void { |
|
53 | + $this->appManager->expects($this->any()) |
|
54 | + ->method('isEnabledForUser') |
|
55 | + ->willReturnMap([ |
|
56 | + ['federation', null, $federationAppEnabled], |
|
57 | + ['federatedfilesharing', null, $federatedFileSharingAppEnabled], |
|
58 | + ]); |
|
59 | 59 | |
60 | - $federatedShareProvider = $this->createMock(FederatedShareProvider::class); |
|
61 | - $this->overwriteService(FederatedShareProvider::class, $federatedShareProvider); |
|
60 | + $federatedShareProvider = $this->createMock(FederatedShareProvider::class); |
|
61 | + $this->overwriteService(FederatedShareProvider::class, $federatedShareProvider); |
|
62 | 62 | |
63 | - $federatedShareProvider->expects($this->any()) |
|
64 | - ->method('isLookupServerUploadEnabled') |
|
65 | - ->willReturn($lookupServerEnabled); |
|
63 | + $federatedShareProvider->expects($this->any()) |
|
64 | + ->method('isLookupServerUploadEnabled') |
|
65 | + ->willReturn($lookupServerEnabled); |
|
66 | 66 | |
67 | - $expected = [ |
|
68 | - 'provisioning_api' => [ |
|
69 | - 'version' => '1.12', |
|
70 | - 'AccountPropertyScopesVersion' => 2, |
|
71 | - 'AccountPropertyScopesFederatedEnabled' => $expectedFederatedScopeEnabled, |
|
72 | - 'AccountPropertyScopesPublishedEnabled' => $expectedPublishedScopeEnabled, |
|
73 | - ], |
|
74 | - ]; |
|
75 | - $this->assertSame($expected, $this->capabilities->getCapabilities()); |
|
76 | - } |
|
67 | + $expected = [ |
|
68 | + 'provisioning_api' => [ |
|
69 | + 'version' => '1.12', |
|
70 | + 'AccountPropertyScopesVersion' => 2, |
|
71 | + 'AccountPropertyScopesFederatedEnabled' => $expectedFederatedScopeEnabled, |
|
72 | + 'AccountPropertyScopesPublishedEnabled' => $expectedPublishedScopeEnabled, |
|
73 | + ], |
|
74 | + ]; |
|
75 | + $this->assertSame($expected, $this->capabilities->getCapabilities()); |
|
76 | + } |
|
77 | 77 | } |
@@ -16,190 +16,190 @@ |
||
16 | 16 | use Test\TestCase; |
17 | 17 | |
18 | 18 | class SessionTest extends TestCase { |
19 | - private static $tempStorage = []; |
|
20 | - |
|
21 | - protected Session $instance; |
|
22 | - protected ISession&MockObject $sessionMock; |
|
23 | - |
|
24 | - public function testThatGetPrivateKeyThrowsExceptionWhenNotSet(): void { |
|
25 | - $this->expectException(PrivateKeyMissingException::class); |
|
26 | - $this->expectExceptionMessage('Private Key missing for user: please try to log-out and log-in again'); |
|
27 | - |
|
28 | - $this->instance->getPrivateKey(); |
|
29 | - } |
|
30 | - |
|
31 | - /** |
|
32 | - * @depends testThatGetPrivateKeyThrowsExceptionWhenNotSet |
|
33 | - */ |
|
34 | - public function testSetAndGetPrivateKey(): void { |
|
35 | - $this->instance->setPrivateKey('dummyPrivateKey'); |
|
36 | - $this->assertEquals('dummyPrivateKey', $this->instance->getPrivateKey()); |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * @depends testSetAndGetPrivateKey |
|
41 | - */ |
|
42 | - public function testIsPrivateKeySet(): void { |
|
43 | - $this->instance->setPrivateKey('dummyPrivateKey'); |
|
44 | - $this->assertTrue($this->instance->isPrivateKeySet()); |
|
45 | - |
|
46 | - unset(self::$tempStorage['privateKey']); |
|
47 | - $this->assertFalse($this->instance->isPrivateKeySet()); |
|
48 | - |
|
49 | - // Set private key back so we can test clear method |
|
50 | - self::$tempStorage['privateKey'] = 'dummyPrivateKey'; |
|
51 | - } |
|
52 | - |
|
53 | - public function testDecryptAllModeActivated(): void { |
|
54 | - $this->instance->prepareDecryptAll('user1', 'usersKey'); |
|
55 | - $this->assertTrue($this->instance->decryptAllModeActivated()); |
|
56 | - $this->assertSame('user1', $this->instance->getDecryptAllUid()); |
|
57 | - $this->assertSame('usersKey', $this->instance->getDecryptAllKey()); |
|
58 | - } |
|
59 | - |
|
60 | - public function testDecryptAllModeDeactivated(): void { |
|
61 | - $this->assertFalse($this->instance->decryptAllModeActivated()); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * @expectExceptionMessage 'Please activate decrypt all mode first' |
|
66 | - */ |
|
67 | - public function testGetDecryptAllUidException(): void { |
|
68 | - $this->expectException(\Exception::class); |
|
69 | - |
|
70 | - $this->instance->getDecryptAllUid(); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * @expectExceptionMessage 'No uid found while in decrypt all mode' |
|
75 | - */ |
|
76 | - public function testGetDecryptAllUidException2(): void { |
|
77 | - $this->expectException(\Exception::class); |
|
78 | - |
|
79 | - $this->instance->prepareDecryptAll(null, 'key'); |
|
80 | - $this->instance->getDecryptAllUid(); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * @expectExceptionMessage 'Please activate decrypt all mode first' |
|
85 | - */ |
|
86 | - public function testGetDecryptAllKeyException(): void { |
|
87 | - $this->expectException(PrivateKeyMissingException::class); |
|
88 | - |
|
89 | - $this->instance->getDecryptAllKey(); |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * @expectExceptionMessage 'No key found while in decrypt all mode' |
|
94 | - */ |
|
95 | - public function testGetDecryptAllKeyException2(): void { |
|
96 | - $this->expectException(PrivateKeyMissingException::class); |
|
97 | - |
|
98 | - $this->instance->prepareDecryptAll('user', null); |
|
99 | - $this->instance->getDecryptAllKey(); |
|
100 | - } |
|
101 | - |
|
102 | - |
|
103 | - public function testSetAndGetStatusWillSetAndReturn(): void { |
|
104 | - // Check if get status will return 0 if it has not been set before |
|
105 | - $this->assertEquals(0, $this->instance->getStatus()); |
|
106 | - |
|
107 | - $this->instance->setStatus(Session::NOT_INITIALIZED); |
|
108 | - $this->assertEquals(0, $this->instance->getStatus()); |
|
109 | - |
|
110 | - $this->instance->setStatus(Session::INIT_EXECUTED); |
|
111 | - $this->assertEquals(1, $this->instance->getStatus()); |
|
112 | - |
|
113 | - $this->instance->setStatus(Session::INIT_SUCCESSFUL); |
|
114 | - $this->assertEquals(2, $this->instance->getStatus()); |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * |
|
119 | - * @param int $status |
|
120 | - * @param bool $expected |
|
121 | - */ |
|
122 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataTestIsReady')] |
|
123 | - public function testIsReady($status, $expected): void { |
|
124 | - /** @var Session&MockObject $instance */ |
|
125 | - $instance = $this->getMockBuilder(Session::class) |
|
126 | - ->setConstructorArgs([$this->sessionMock]) |
|
127 | - ->onlyMethods(['getStatus']) |
|
128 | - ->getMock(); |
|
129 | - |
|
130 | - $instance->expects($this->once())->method('getStatus') |
|
131 | - ->willReturn($status); |
|
132 | - |
|
133 | - $this->assertSame($expected, $instance->isReady()); |
|
134 | - } |
|
135 | - |
|
136 | - public static function dataTestIsReady(): array { |
|
137 | - return [ |
|
138 | - [Session::INIT_SUCCESSFUL, true], |
|
139 | - [Session::INIT_EXECUTED, false], |
|
140 | - [Session::NOT_INITIALIZED, false], |
|
141 | - ]; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * @param $key |
|
146 | - * @param $value |
|
147 | - */ |
|
148 | - public function setValueTester($key, $value) { |
|
149 | - self::$tempStorage[$key] = $value; |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * @param $key |
|
154 | - */ |
|
155 | - public function removeValueTester($key) { |
|
156 | - unset(self::$tempStorage[$key]); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * @param $key |
|
161 | - * @return mixed |
|
162 | - */ |
|
163 | - public function getValueTester($key) { |
|
164 | - if (!empty(self::$tempStorage[$key])) { |
|
165 | - return self::$tempStorage[$key]; |
|
166 | - } |
|
167 | - return null; |
|
168 | - } |
|
169 | - |
|
170 | - |
|
171 | - public function testClearWillRemoveValues(): void { |
|
172 | - $this->instance->setPrivateKey('privateKey'); |
|
173 | - $this->instance->setStatus('initStatus'); |
|
174 | - $this->instance->prepareDecryptAll('user', 'key'); |
|
175 | - $this->assertNotEmpty(self::$tempStorage); |
|
176 | - $this->instance->clear(); |
|
177 | - $this->assertEmpty(self::$tempStorage); |
|
178 | - } |
|
179 | - |
|
180 | - |
|
181 | - protected function setUp(): void { |
|
182 | - parent::setUp(); |
|
183 | - $this->sessionMock = $this->createMock(ISession::class); |
|
184 | - |
|
185 | - $this->sessionMock->expects($this->any()) |
|
186 | - ->method('set') |
|
187 | - ->willReturnCallback([$this, 'setValueTester']); |
|
188 | - |
|
189 | - $this->sessionMock->expects($this->any()) |
|
190 | - ->method('get') |
|
191 | - ->willReturnCallback([$this, 'getValueTester']); |
|
192 | - |
|
193 | - $this->sessionMock->expects($this->any()) |
|
194 | - ->method('remove') |
|
195 | - ->willReturnCallback([$this, 'removeValueTester']); |
|
196 | - |
|
197 | - |
|
198 | - $this->instance = new Session($this->sessionMock); |
|
199 | - } |
|
200 | - |
|
201 | - protected function tearDown(): void { |
|
202 | - self::$tempStorage = []; |
|
203 | - parent::tearDown(); |
|
204 | - } |
|
19 | + private static $tempStorage = []; |
|
20 | + |
|
21 | + protected Session $instance; |
|
22 | + protected ISession&MockObject $sessionMock; |
|
23 | + |
|
24 | + public function testThatGetPrivateKeyThrowsExceptionWhenNotSet(): void { |
|
25 | + $this->expectException(PrivateKeyMissingException::class); |
|
26 | + $this->expectExceptionMessage('Private Key missing for user: please try to log-out and log-in again'); |
|
27 | + |
|
28 | + $this->instance->getPrivateKey(); |
|
29 | + } |
|
30 | + |
|
31 | + /** |
|
32 | + * @depends testThatGetPrivateKeyThrowsExceptionWhenNotSet |
|
33 | + */ |
|
34 | + public function testSetAndGetPrivateKey(): void { |
|
35 | + $this->instance->setPrivateKey('dummyPrivateKey'); |
|
36 | + $this->assertEquals('dummyPrivateKey', $this->instance->getPrivateKey()); |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * @depends testSetAndGetPrivateKey |
|
41 | + */ |
|
42 | + public function testIsPrivateKeySet(): void { |
|
43 | + $this->instance->setPrivateKey('dummyPrivateKey'); |
|
44 | + $this->assertTrue($this->instance->isPrivateKeySet()); |
|
45 | + |
|
46 | + unset(self::$tempStorage['privateKey']); |
|
47 | + $this->assertFalse($this->instance->isPrivateKeySet()); |
|
48 | + |
|
49 | + // Set private key back so we can test clear method |
|
50 | + self::$tempStorage['privateKey'] = 'dummyPrivateKey'; |
|
51 | + } |
|
52 | + |
|
53 | + public function testDecryptAllModeActivated(): void { |
|
54 | + $this->instance->prepareDecryptAll('user1', 'usersKey'); |
|
55 | + $this->assertTrue($this->instance->decryptAllModeActivated()); |
|
56 | + $this->assertSame('user1', $this->instance->getDecryptAllUid()); |
|
57 | + $this->assertSame('usersKey', $this->instance->getDecryptAllKey()); |
|
58 | + } |
|
59 | + |
|
60 | + public function testDecryptAllModeDeactivated(): void { |
|
61 | + $this->assertFalse($this->instance->decryptAllModeActivated()); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * @expectExceptionMessage 'Please activate decrypt all mode first' |
|
66 | + */ |
|
67 | + public function testGetDecryptAllUidException(): void { |
|
68 | + $this->expectException(\Exception::class); |
|
69 | + |
|
70 | + $this->instance->getDecryptAllUid(); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * @expectExceptionMessage 'No uid found while in decrypt all mode' |
|
75 | + */ |
|
76 | + public function testGetDecryptAllUidException2(): void { |
|
77 | + $this->expectException(\Exception::class); |
|
78 | + |
|
79 | + $this->instance->prepareDecryptAll(null, 'key'); |
|
80 | + $this->instance->getDecryptAllUid(); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * @expectExceptionMessage 'Please activate decrypt all mode first' |
|
85 | + */ |
|
86 | + public function testGetDecryptAllKeyException(): void { |
|
87 | + $this->expectException(PrivateKeyMissingException::class); |
|
88 | + |
|
89 | + $this->instance->getDecryptAllKey(); |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * @expectExceptionMessage 'No key found while in decrypt all mode' |
|
94 | + */ |
|
95 | + public function testGetDecryptAllKeyException2(): void { |
|
96 | + $this->expectException(PrivateKeyMissingException::class); |
|
97 | + |
|
98 | + $this->instance->prepareDecryptAll('user', null); |
|
99 | + $this->instance->getDecryptAllKey(); |
|
100 | + } |
|
101 | + |
|
102 | + |
|
103 | + public function testSetAndGetStatusWillSetAndReturn(): void { |
|
104 | + // Check if get status will return 0 if it has not been set before |
|
105 | + $this->assertEquals(0, $this->instance->getStatus()); |
|
106 | + |
|
107 | + $this->instance->setStatus(Session::NOT_INITIALIZED); |
|
108 | + $this->assertEquals(0, $this->instance->getStatus()); |
|
109 | + |
|
110 | + $this->instance->setStatus(Session::INIT_EXECUTED); |
|
111 | + $this->assertEquals(1, $this->instance->getStatus()); |
|
112 | + |
|
113 | + $this->instance->setStatus(Session::INIT_SUCCESSFUL); |
|
114 | + $this->assertEquals(2, $this->instance->getStatus()); |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * |
|
119 | + * @param int $status |
|
120 | + * @param bool $expected |
|
121 | + */ |
|
122 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestIsReady')] |
|
123 | + public function testIsReady($status, $expected): void { |
|
124 | + /** @var Session&MockObject $instance */ |
|
125 | + $instance = $this->getMockBuilder(Session::class) |
|
126 | + ->setConstructorArgs([$this->sessionMock]) |
|
127 | + ->onlyMethods(['getStatus']) |
|
128 | + ->getMock(); |
|
129 | + |
|
130 | + $instance->expects($this->once())->method('getStatus') |
|
131 | + ->willReturn($status); |
|
132 | + |
|
133 | + $this->assertSame($expected, $instance->isReady()); |
|
134 | + } |
|
135 | + |
|
136 | + public static function dataTestIsReady(): array { |
|
137 | + return [ |
|
138 | + [Session::INIT_SUCCESSFUL, true], |
|
139 | + [Session::INIT_EXECUTED, false], |
|
140 | + [Session::NOT_INITIALIZED, false], |
|
141 | + ]; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * @param $key |
|
146 | + * @param $value |
|
147 | + */ |
|
148 | + public function setValueTester($key, $value) { |
|
149 | + self::$tempStorage[$key] = $value; |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * @param $key |
|
154 | + */ |
|
155 | + public function removeValueTester($key) { |
|
156 | + unset(self::$tempStorage[$key]); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * @param $key |
|
161 | + * @return mixed |
|
162 | + */ |
|
163 | + public function getValueTester($key) { |
|
164 | + if (!empty(self::$tempStorage[$key])) { |
|
165 | + return self::$tempStorage[$key]; |
|
166 | + } |
|
167 | + return null; |
|
168 | + } |
|
169 | + |
|
170 | + |
|
171 | + public function testClearWillRemoveValues(): void { |
|
172 | + $this->instance->setPrivateKey('privateKey'); |
|
173 | + $this->instance->setStatus('initStatus'); |
|
174 | + $this->instance->prepareDecryptAll('user', 'key'); |
|
175 | + $this->assertNotEmpty(self::$tempStorage); |
|
176 | + $this->instance->clear(); |
|
177 | + $this->assertEmpty(self::$tempStorage); |
|
178 | + } |
|
179 | + |
|
180 | + |
|
181 | + protected function setUp(): void { |
|
182 | + parent::setUp(); |
|
183 | + $this->sessionMock = $this->createMock(ISession::class); |
|
184 | + |
|
185 | + $this->sessionMock->expects($this->any()) |
|
186 | + ->method('set') |
|
187 | + ->willReturnCallback([$this, 'setValueTester']); |
|
188 | + |
|
189 | + $this->sessionMock->expects($this->any()) |
|
190 | + ->method('get') |
|
191 | + ->willReturnCallback([$this, 'getValueTester']); |
|
192 | + |
|
193 | + $this->sessionMock->expects($this->any()) |
|
194 | + ->method('remove') |
|
195 | + ->willReturnCallback([$this, 'removeValueTester']); |
|
196 | + |
|
197 | + |
|
198 | + $this->instance = new Session($this->sessionMock); |
|
199 | + } |
|
200 | + |
|
201 | + protected function tearDown(): void { |
|
202 | + self::$tempStorage = []; |
|
203 | + parent::tearDown(); |
|
204 | + } |
|
205 | 205 | } |
@@ -23,167 +23,167 @@ |
||
23 | 23 | |
24 | 24 | class UtilTest extends TestCase { |
25 | 25 | |
26 | - protected Util $instance; |
|
27 | - protected static $tempStorage = []; |
|
28 | - |
|
29 | - protected IConfig&MockObject $configMock; |
|
30 | - protected View&MockObject $filesMock; |
|
31 | - protected IUserManager&MockObject $userManagerMock; |
|
32 | - protected IMountPoint&MockObject $mountMock; |
|
33 | - |
|
34 | - public function testSetRecoveryForUser(): void { |
|
35 | - $this->instance->setRecoveryForUser('1'); |
|
36 | - $this->assertArrayHasKey('recoveryEnabled', self::$tempStorage); |
|
37 | - } |
|
38 | - |
|
39 | - public function testIsRecoveryEnabledForUser(): void { |
|
40 | - $this->assertTrue($this->instance->isRecoveryEnabledForUser('admin')); |
|
41 | - |
|
42 | - // Assert recovery will return default value if not set |
|
43 | - unset(self::$tempStorage['recoveryEnabled']); |
|
44 | - $this->assertEquals(0, $this->instance->isRecoveryEnabledForUser('admin')); |
|
45 | - } |
|
46 | - |
|
47 | - public function testUserHasFiles(): void { |
|
48 | - $this->filesMock->expects($this->once()) |
|
49 | - ->method('file_exists') |
|
50 | - ->willReturn(true); |
|
51 | - |
|
52 | - $this->assertTrue($this->instance->userHasFiles('admin')); |
|
53 | - } |
|
54 | - |
|
55 | - protected function setUp(): void { |
|
56 | - parent::setUp(); |
|
57 | - $this->mountMock = $this->createMock(IMountPoint::class); |
|
58 | - $this->filesMock = $this->createMock(View::class); |
|
59 | - $this->userManagerMock = $this->createMock(IUserManager::class); |
|
60 | - |
|
61 | - /** @var Crypt $cryptMock */ |
|
62 | - $cryptMock = $this->getMockBuilder(Crypt::class) |
|
63 | - ->disableOriginalConstructor() |
|
64 | - ->getMock(); |
|
65 | - |
|
66 | - $user = $this->createMock(IUser::class); |
|
67 | - $user->expects($this->any()) |
|
68 | - ->method('getUID') |
|
69 | - ->willReturn('admin'); |
|
70 | - |
|
71 | - /** @var IUserSession|MockObject $userSessionMock */ |
|
72 | - $userSessionMock = $this->createMock(IUserSession::class); |
|
73 | - $userSessionMock->expects($this->any()) |
|
74 | - ->method('getUser') |
|
75 | - ->willReturn($user); |
|
76 | - $userSessionMock->expects($this->any()) |
|
77 | - ->method('isLoggedIn') |
|
78 | - ->willReturn(true); |
|
79 | - |
|
80 | - $this->configMock = $this->createMock(IConfig::class); |
|
81 | - |
|
82 | - $this->configMock->expects($this->any()) |
|
83 | - ->method('getUserValue') |
|
84 | - ->willReturnCallback([$this, 'getValueTester']); |
|
85 | - |
|
86 | - $this->configMock->expects($this->any()) |
|
87 | - ->method('setUserValue') |
|
88 | - ->willReturnCallback([$this, 'setValueTester']); |
|
89 | - |
|
90 | - $this->instance = new Util($this->filesMock, $cryptMock, $userSessionMock, $this->configMock, $this->userManagerMock); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @param $userId |
|
95 | - * @param $app |
|
96 | - * @param $key |
|
97 | - * @param $value |
|
98 | - */ |
|
99 | - public function setValueTester($userId, $app, $key, $value) { |
|
100 | - self::$tempStorage[$key] = $value; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * @param $userId |
|
105 | - * @param $app |
|
106 | - * @param $key |
|
107 | - * @param $default |
|
108 | - * @return mixed |
|
109 | - */ |
|
110 | - public function getValueTester($userId, $app, $key, $default) { |
|
111 | - if (!empty(self::$tempStorage[$key])) { |
|
112 | - return self::$tempStorage[$key]; |
|
113 | - } |
|
114 | - return $default ?: null; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * |
|
119 | - * @param string $value |
|
120 | - * @param bool $expect |
|
121 | - */ |
|
122 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataTestIsMasterKeyEnabled')] |
|
123 | - public function testIsMasterKeyEnabled($value, $expect): void { |
|
124 | - $this->configMock->expects($this->once())->method('getAppValue') |
|
125 | - ->with('encryption', 'useMasterKey', '1')->willReturn($value); |
|
126 | - $this->assertSame($expect, |
|
127 | - $this->instance->isMasterKeyEnabled() |
|
128 | - ); |
|
129 | - } |
|
130 | - |
|
131 | - public static function dataTestIsMasterKeyEnabled(): array { |
|
132 | - return [ |
|
133 | - ['0', false], |
|
134 | - ['1', true] |
|
135 | - ]; |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * @param string $returnValue return value from getAppValue() |
|
140 | - * @param bool $expected |
|
141 | - */ |
|
142 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataTestShouldEncryptHomeStorage')] |
|
143 | - public function testShouldEncryptHomeStorage($returnValue, $expected): void { |
|
144 | - $this->configMock->expects($this->once())->method('getAppValue') |
|
145 | - ->with('encryption', 'encryptHomeStorage', '1') |
|
146 | - ->willReturn($returnValue); |
|
147 | - |
|
148 | - $this->assertSame($expected, |
|
149 | - $this->instance->shouldEncryptHomeStorage()); |
|
150 | - } |
|
151 | - |
|
152 | - public static function dataTestShouldEncryptHomeStorage(): array { |
|
153 | - return [ |
|
154 | - ['1', true], |
|
155 | - ['0', false] |
|
156 | - ]; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * @param $value |
|
161 | - * @param $expected |
|
162 | - */ |
|
163 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataTestSetEncryptHomeStorage')] |
|
164 | - public function testSetEncryptHomeStorage($value, $expected): void { |
|
165 | - $this->configMock->expects($this->once())->method('setAppValue') |
|
166 | - ->with('encryption', 'encryptHomeStorage', $expected); |
|
167 | - $this->instance->setEncryptHomeStorage($value); |
|
168 | - } |
|
169 | - |
|
170 | - public static function dataTestSetEncryptHomeStorage(): array { |
|
171 | - return [ |
|
172 | - [true, '1'], |
|
173 | - [false, '0'] |
|
174 | - ]; |
|
175 | - } |
|
176 | - |
|
177 | - public function testGetStorage(): void { |
|
178 | - $return = $this->getMockBuilder(IStorage::class) |
|
179 | - ->disableOriginalConstructor() |
|
180 | - ->getMock(); |
|
181 | - |
|
182 | - $path = '/foo/bar.txt'; |
|
183 | - $this->filesMock->expects($this->once())->method('getMount')->with($path) |
|
184 | - ->willReturn($this->mountMock); |
|
185 | - $this->mountMock->expects($this->once())->method('getStorage')->willReturn($return); |
|
186 | - |
|
187 | - $this->assertEquals($return, $this->instance->getStorage($path)); |
|
188 | - } |
|
26 | + protected Util $instance; |
|
27 | + protected static $tempStorage = []; |
|
28 | + |
|
29 | + protected IConfig&MockObject $configMock; |
|
30 | + protected View&MockObject $filesMock; |
|
31 | + protected IUserManager&MockObject $userManagerMock; |
|
32 | + protected IMountPoint&MockObject $mountMock; |
|
33 | + |
|
34 | + public function testSetRecoveryForUser(): void { |
|
35 | + $this->instance->setRecoveryForUser('1'); |
|
36 | + $this->assertArrayHasKey('recoveryEnabled', self::$tempStorage); |
|
37 | + } |
|
38 | + |
|
39 | + public function testIsRecoveryEnabledForUser(): void { |
|
40 | + $this->assertTrue($this->instance->isRecoveryEnabledForUser('admin')); |
|
41 | + |
|
42 | + // Assert recovery will return default value if not set |
|
43 | + unset(self::$tempStorage['recoveryEnabled']); |
|
44 | + $this->assertEquals(0, $this->instance->isRecoveryEnabledForUser('admin')); |
|
45 | + } |
|
46 | + |
|
47 | + public function testUserHasFiles(): void { |
|
48 | + $this->filesMock->expects($this->once()) |
|
49 | + ->method('file_exists') |
|
50 | + ->willReturn(true); |
|
51 | + |
|
52 | + $this->assertTrue($this->instance->userHasFiles('admin')); |
|
53 | + } |
|
54 | + |
|
55 | + protected function setUp(): void { |
|
56 | + parent::setUp(); |
|
57 | + $this->mountMock = $this->createMock(IMountPoint::class); |
|
58 | + $this->filesMock = $this->createMock(View::class); |
|
59 | + $this->userManagerMock = $this->createMock(IUserManager::class); |
|
60 | + |
|
61 | + /** @var Crypt $cryptMock */ |
|
62 | + $cryptMock = $this->getMockBuilder(Crypt::class) |
|
63 | + ->disableOriginalConstructor() |
|
64 | + ->getMock(); |
|
65 | + |
|
66 | + $user = $this->createMock(IUser::class); |
|
67 | + $user->expects($this->any()) |
|
68 | + ->method('getUID') |
|
69 | + ->willReturn('admin'); |
|
70 | + |
|
71 | + /** @var IUserSession|MockObject $userSessionMock */ |
|
72 | + $userSessionMock = $this->createMock(IUserSession::class); |
|
73 | + $userSessionMock->expects($this->any()) |
|
74 | + ->method('getUser') |
|
75 | + ->willReturn($user); |
|
76 | + $userSessionMock->expects($this->any()) |
|
77 | + ->method('isLoggedIn') |
|
78 | + ->willReturn(true); |
|
79 | + |
|
80 | + $this->configMock = $this->createMock(IConfig::class); |
|
81 | + |
|
82 | + $this->configMock->expects($this->any()) |
|
83 | + ->method('getUserValue') |
|
84 | + ->willReturnCallback([$this, 'getValueTester']); |
|
85 | + |
|
86 | + $this->configMock->expects($this->any()) |
|
87 | + ->method('setUserValue') |
|
88 | + ->willReturnCallback([$this, 'setValueTester']); |
|
89 | + |
|
90 | + $this->instance = new Util($this->filesMock, $cryptMock, $userSessionMock, $this->configMock, $this->userManagerMock); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @param $userId |
|
95 | + * @param $app |
|
96 | + * @param $key |
|
97 | + * @param $value |
|
98 | + */ |
|
99 | + public function setValueTester($userId, $app, $key, $value) { |
|
100 | + self::$tempStorage[$key] = $value; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * @param $userId |
|
105 | + * @param $app |
|
106 | + * @param $key |
|
107 | + * @param $default |
|
108 | + * @return mixed |
|
109 | + */ |
|
110 | + public function getValueTester($userId, $app, $key, $default) { |
|
111 | + if (!empty(self::$tempStorage[$key])) { |
|
112 | + return self::$tempStorage[$key]; |
|
113 | + } |
|
114 | + return $default ?: null; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * |
|
119 | + * @param string $value |
|
120 | + * @param bool $expect |
|
121 | + */ |
|
122 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestIsMasterKeyEnabled')] |
|
123 | + public function testIsMasterKeyEnabled($value, $expect): void { |
|
124 | + $this->configMock->expects($this->once())->method('getAppValue') |
|
125 | + ->with('encryption', 'useMasterKey', '1')->willReturn($value); |
|
126 | + $this->assertSame($expect, |
|
127 | + $this->instance->isMasterKeyEnabled() |
|
128 | + ); |
|
129 | + } |
|
130 | + |
|
131 | + public static function dataTestIsMasterKeyEnabled(): array { |
|
132 | + return [ |
|
133 | + ['0', false], |
|
134 | + ['1', true] |
|
135 | + ]; |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * @param string $returnValue return value from getAppValue() |
|
140 | + * @param bool $expected |
|
141 | + */ |
|
142 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestShouldEncryptHomeStorage')] |
|
143 | + public function testShouldEncryptHomeStorage($returnValue, $expected): void { |
|
144 | + $this->configMock->expects($this->once())->method('getAppValue') |
|
145 | + ->with('encryption', 'encryptHomeStorage', '1') |
|
146 | + ->willReturn($returnValue); |
|
147 | + |
|
148 | + $this->assertSame($expected, |
|
149 | + $this->instance->shouldEncryptHomeStorage()); |
|
150 | + } |
|
151 | + |
|
152 | + public static function dataTestShouldEncryptHomeStorage(): array { |
|
153 | + return [ |
|
154 | + ['1', true], |
|
155 | + ['0', false] |
|
156 | + ]; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * @param $value |
|
161 | + * @param $expected |
|
162 | + */ |
|
163 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestSetEncryptHomeStorage')] |
|
164 | + public function testSetEncryptHomeStorage($value, $expected): void { |
|
165 | + $this->configMock->expects($this->once())->method('setAppValue') |
|
166 | + ->with('encryption', 'encryptHomeStorage', $expected); |
|
167 | + $this->instance->setEncryptHomeStorage($value); |
|
168 | + } |
|
169 | + |
|
170 | + public static function dataTestSetEncryptHomeStorage(): array { |
|
171 | + return [ |
|
172 | + [true, '1'], |
|
173 | + [false, '0'] |
|
174 | + ]; |
|
175 | + } |
|
176 | + |
|
177 | + public function testGetStorage(): void { |
|
178 | + $return = $this->getMockBuilder(IStorage::class) |
|
179 | + ->disableOriginalConstructor() |
|
180 | + ->getMock(); |
|
181 | + |
|
182 | + $path = '/foo/bar.txt'; |
|
183 | + $this->filesMock->expects($this->once())->method('getMount')->with($path) |
|
184 | + ->willReturn($this->mountMock); |
|
185 | + $this->mountMock->expects($this->once())->method('getStorage')->willReturn($return); |
|
186 | + |
|
187 | + $this->assertEquals($return, $this->instance->getStorage($path)); |
|
188 | + } |
|
189 | 189 | } |
@@ -19,74 +19,74 @@ |
||
19 | 19 | |
20 | 20 | class TestEnableMasterKey extends TestCase { |
21 | 21 | |
22 | - /** @var EnableMasterKey */ |
|
23 | - protected $enableMasterKey; |
|
24 | - |
|
25 | - /** @var Util | \PHPUnit\Framework\MockObject\MockObject */ |
|
26 | - protected $util; |
|
27 | - |
|
28 | - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ |
|
29 | - protected $config; |
|
30 | - |
|
31 | - /** @var \Symfony\Component\Console\Helper\QuestionHelper | \PHPUnit\Framework\MockObject\MockObject */ |
|
32 | - protected $questionHelper; |
|
33 | - |
|
34 | - /** @var \Symfony\Component\Console\Output\OutputInterface | \PHPUnit\Framework\MockObject\MockObject */ |
|
35 | - protected $output; |
|
36 | - |
|
37 | - /** @var \Symfony\Component\Console\Input\InputInterface | \PHPUnit\Framework\MockObject\MockObject */ |
|
38 | - protected $input; |
|
39 | - |
|
40 | - protected function setUp(): void { |
|
41 | - parent::setUp(); |
|
42 | - |
|
43 | - $this->util = $this->getMockBuilder(Util::class) |
|
44 | - ->disableOriginalConstructor()->getMock(); |
|
45 | - $this->config = $this->getMockBuilder(IConfig::class) |
|
46 | - ->disableOriginalConstructor()->getMock(); |
|
47 | - $this->questionHelper = $this->getMockBuilder(QuestionHelper::class) |
|
48 | - ->disableOriginalConstructor()->getMock(); |
|
49 | - $this->output = $this->getMockBuilder(OutputInterface::class) |
|
50 | - ->disableOriginalConstructor()->getMock(); |
|
51 | - $this->input = $this->getMockBuilder(InputInterface::class) |
|
52 | - ->disableOriginalConstructor()->getMock(); |
|
53 | - |
|
54 | - $this->enableMasterKey = new EnableMasterKey($this->util, $this->config, $this->questionHelper); |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * |
|
59 | - * @param bool $isAlreadyEnabled |
|
60 | - * @param string $answer |
|
61 | - */ |
|
62 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataTestExecute')] |
|
63 | - public function testExecute($isAlreadyEnabled, $answer): void { |
|
64 | - $this->util->expects($this->once())->method('isMasterKeyEnabled') |
|
65 | - ->willReturn($isAlreadyEnabled); |
|
66 | - |
|
67 | - if ($isAlreadyEnabled) { |
|
68 | - $this->output->expects($this->once())->method('writeln') |
|
69 | - ->with('Master key already enabled'); |
|
70 | - } else { |
|
71 | - if ($answer === 'y') { |
|
72 | - $this->questionHelper->expects($this->once())->method('ask')->willReturn(true); |
|
73 | - $this->config->expects($this->once())->method('setAppValue') |
|
74 | - ->with('encryption', 'useMasterKey', '1'); |
|
75 | - } else { |
|
76 | - $this->questionHelper->expects($this->once())->method('ask')->willReturn(false); |
|
77 | - $this->config->expects($this->never())->method('setAppValue'); |
|
78 | - } |
|
79 | - } |
|
80 | - |
|
81 | - $this->invokePrivate($this->enableMasterKey, 'execute', [$this->input, $this->output]); |
|
82 | - } |
|
83 | - |
|
84 | - public static function dataTestExecute() { |
|
85 | - return [ |
|
86 | - [true, ''], |
|
87 | - [false, 'y'], |
|
88 | - [false, 'n'], |
|
89 | - [false, ''] |
|
90 | - ]; |
|
91 | - } |
|
22 | + /** @var EnableMasterKey */ |
|
23 | + protected $enableMasterKey; |
|
24 | + |
|
25 | + /** @var Util | \PHPUnit\Framework\MockObject\MockObject */ |
|
26 | + protected $util; |
|
27 | + |
|
28 | + /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ |
|
29 | + protected $config; |
|
30 | + |
|
31 | + /** @var \Symfony\Component\Console\Helper\QuestionHelper | \PHPUnit\Framework\MockObject\MockObject */ |
|
32 | + protected $questionHelper; |
|
33 | + |
|
34 | + /** @var \Symfony\Component\Console\Output\OutputInterface | \PHPUnit\Framework\MockObject\MockObject */ |
|
35 | + protected $output; |
|
36 | + |
|
37 | + /** @var \Symfony\Component\Console\Input\InputInterface | \PHPUnit\Framework\MockObject\MockObject */ |
|
38 | + protected $input; |
|
39 | + |
|
40 | + protected function setUp(): void { |
|
41 | + parent::setUp(); |
|
42 | + |
|
43 | + $this->util = $this->getMockBuilder(Util::class) |
|
44 | + ->disableOriginalConstructor()->getMock(); |
|
45 | + $this->config = $this->getMockBuilder(IConfig::class) |
|
46 | + ->disableOriginalConstructor()->getMock(); |
|
47 | + $this->questionHelper = $this->getMockBuilder(QuestionHelper::class) |
|
48 | + ->disableOriginalConstructor()->getMock(); |
|
49 | + $this->output = $this->getMockBuilder(OutputInterface::class) |
|
50 | + ->disableOriginalConstructor()->getMock(); |
|
51 | + $this->input = $this->getMockBuilder(InputInterface::class) |
|
52 | + ->disableOriginalConstructor()->getMock(); |
|
53 | + |
|
54 | + $this->enableMasterKey = new EnableMasterKey($this->util, $this->config, $this->questionHelper); |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * |
|
59 | + * @param bool $isAlreadyEnabled |
|
60 | + * @param string $answer |
|
61 | + */ |
|
62 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestExecute')] |
|
63 | + public function testExecute($isAlreadyEnabled, $answer): void { |
|
64 | + $this->util->expects($this->once())->method('isMasterKeyEnabled') |
|
65 | + ->willReturn($isAlreadyEnabled); |
|
66 | + |
|
67 | + if ($isAlreadyEnabled) { |
|
68 | + $this->output->expects($this->once())->method('writeln') |
|
69 | + ->with('Master key already enabled'); |
|
70 | + } else { |
|
71 | + if ($answer === 'y') { |
|
72 | + $this->questionHelper->expects($this->once())->method('ask')->willReturn(true); |
|
73 | + $this->config->expects($this->once())->method('setAppValue') |
|
74 | + ->with('encryption', 'useMasterKey', '1'); |
|
75 | + } else { |
|
76 | + $this->questionHelper->expects($this->once())->method('ask')->willReturn(false); |
|
77 | + $this->config->expects($this->never())->method('setAppValue'); |
|
78 | + } |
|
79 | + } |
|
80 | + |
|
81 | + $this->invokePrivate($this->enableMasterKey, 'execute', [$this->input, $this->output]); |
|
82 | + } |
|
83 | + |
|
84 | + public static function dataTestExecute() { |
|
85 | + return [ |
|
86 | + [true, ''], |
|
87 | + [false, 'y'], |
|
88 | + [false, 'n'], |
|
89 | + [false, ''] |
|
90 | + ]; |
|
91 | + } |
|
92 | 92 | } |
@@ -19,56 +19,56 @@ |
||
19 | 19 | |
20 | 20 | class StatusControllerTest extends TestCase { |
21 | 21 | |
22 | - protected IRequest&MockObject $requestMock; |
|
23 | - protected IL10N&MockObject $l10nMock; |
|
24 | - protected Session&MockObject $sessionMock; |
|
25 | - protected IManager&MockObject $encryptionManagerMock; |
|
22 | + protected IRequest&MockObject $requestMock; |
|
23 | + protected IL10N&MockObject $l10nMock; |
|
24 | + protected Session&MockObject $sessionMock; |
|
25 | + protected IManager&MockObject $encryptionManagerMock; |
|
26 | 26 | |
27 | - protected StatusController $controller; |
|
27 | + protected StatusController $controller; |
|
28 | 28 | |
29 | - protected function setUp(): void { |
|
30 | - parent::setUp(); |
|
29 | + protected function setUp(): void { |
|
30 | + parent::setUp(); |
|
31 | 31 | |
32 | - $this->sessionMock = $this->getMockBuilder(Session::class) |
|
33 | - ->disableOriginalConstructor()->getMock(); |
|
34 | - $this->requestMock = $this->createMock(IRequest::class); |
|
32 | + $this->sessionMock = $this->getMockBuilder(Session::class) |
|
33 | + ->disableOriginalConstructor()->getMock(); |
|
34 | + $this->requestMock = $this->createMock(IRequest::class); |
|
35 | 35 | |
36 | - $this->l10nMock = $this->getMockBuilder(IL10N::class) |
|
37 | - ->disableOriginalConstructor()->getMock(); |
|
38 | - $this->l10nMock->expects($this->any()) |
|
39 | - ->method('t') |
|
40 | - ->willReturnCallback(function ($message) { |
|
41 | - return $message; |
|
42 | - }); |
|
43 | - $this->encryptionManagerMock = $this->createMock(IManager::class); |
|
36 | + $this->l10nMock = $this->getMockBuilder(IL10N::class) |
|
37 | + ->disableOriginalConstructor()->getMock(); |
|
38 | + $this->l10nMock->expects($this->any()) |
|
39 | + ->method('t') |
|
40 | + ->willReturnCallback(function ($message) { |
|
41 | + return $message; |
|
42 | + }); |
|
43 | + $this->encryptionManagerMock = $this->createMock(IManager::class); |
|
44 | 44 | |
45 | - $this->controller = new StatusController('encryptionTest', |
|
46 | - $this->requestMock, |
|
47 | - $this->l10nMock, |
|
48 | - $this->sessionMock, |
|
49 | - $this->encryptionManagerMock); |
|
50 | - } |
|
45 | + $this->controller = new StatusController('encryptionTest', |
|
46 | + $this->requestMock, |
|
47 | + $this->l10nMock, |
|
48 | + $this->sessionMock, |
|
49 | + $this->encryptionManagerMock); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * |
|
54 | - * @param string $status |
|
55 | - * @param string $expectedStatus |
|
56 | - */ |
|
57 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataTestGetStatus')] |
|
58 | - public function testGetStatus($status, $expectedStatus): void { |
|
59 | - $this->sessionMock->expects($this->once()) |
|
60 | - ->method('getStatus')->willReturn($status); |
|
61 | - $result = $this->controller->getStatus(); |
|
62 | - $data = $result->getData(); |
|
63 | - $this->assertSame($expectedStatus, $data['status']); |
|
64 | - } |
|
52 | + /** |
|
53 | + * |
|
54 | + * @param string $status |
|
55 | + * @param string $expectedStatus |
|
56 | + */ |
|
57 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestGetStatus')] |
|
58 | + public function testGetStatus($status, $expectedStatus): void { |
|
59 | + $this->sessionMock->expects($this->once()) |
|
60 | + ->method('getStatus')->willReturn($status); |
|
61 | + $result = $this->controller->getStatus(); |
|
62 | + $data = $result->getData(); |
|
63 | + $this->assertSame($expectedStatus, $data['status']); |
|
64 | + } |
|
65 | 65 | |
66 | - public static function dataTestGetStatus(): array { |
|
67 | - return [ |
|
68 | - [Session::INIT_EXECUTED, 'interactionNeeded'], |
|
69 | - [Session::INIT_SUCCESSFUL, 'success'], |
|
70 | - [Session::NOT_INITIALIZED, 'interactionNeeded'], |
|
71 | - ['unknown', 'error'], |
|
72 | - ]; |
|
73 | - } |
|
66 | + public static function dataTestGetStatus(): array { |
|
67 | + return [ |
|
68 | + [Session::INIT_EXECUTED, 'interactionNeeded'], |
|
69 | + [Session::INIT_SUCCESSFUL, 'success'], |
|
70 | + [Session::NOT_INITIALIZED, 'interactionNeeded'], |
|
71 | + ['unknown', 'error'], |
|
72 | + ]; |
|
73 | + } |
|
74 | 74 | } |
@@ -19,141 +19,141 @@ |
||
19 | 19 | use Test\TestCase; |
20 | 20 | |
21 | 21 | class RecoveryControllerTest extends TestCase { |
22 | - protected RecoveryController $controller; |
|
23 | - |
|
24 | - protected IRequest&MockObject $requestMock; |
|
25 | - protected IConfig&MockObject $configMock; |
|
26 | - protected IL10N&MockObject $l10nMock; |
|
27 | - protected Recovery&MockObject $recoveryMock; |
|
28 | - |
|
29 | - public static function adminRecoveryProvider(): array { |
|
30 | - return [ |
|
31 | - ['test', 'test', '1', 'Recovery key successfully enabled', Http::STATUS_OK], |
|
32 | - ['', 'test', '1', 'Missing recovery key password', Http::STATUS_BAD_REQUEST], |
|
33 | - ['test', '', '1', 'Please repeat the recovery key password', Http::STATUS_BAD_REQUEST], |
|
34 | - ['test', 'something that doesn\'t match', '1', 'Repeated recovery key password does not match the provided recovery key password', Http::STATUS_BAD_REQUEST], |
|
35 | - ['test', 'test', '0', 'Recovery key successfully disabled', Http::STATUS_OK], |
|
36 | - ]; |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * @param $recoveryPassword |
|
41 | - * @param $passConfirm |
|
42 | - * @param $enableRecovery |
|
43 | - * @param $expectedMessage |
|
44 | - * @param $expectedStatus |
|
45 | - */ |
|
46 | - #[\PHPUnit\Framework\Attributes\DataProvider('adminRecoveryProvider')] |
|
47 | - public function testAdminRecovery($recoveryPassword, $passConfirm, $enableRecovery, $expectedMessage, $expectedStatus): void { |
|
48 | - $this->recoveryMock->expects($this->any()) |
|
49 | - ->method('enableAdminRecovery') |
|
50 | - ->willReturn(true); |
|
51 | - |
|
52 | - $this->recoveryMock->expects($this->any()) |
|
53 | - ->method('disableAdminRecovery') |
|
54 | - ->willReturn(true); |
|
55 | - |
|
56 | - $response = $this->controller->adminRecovery($recoveryPassword, |
|
57 | - $passConfirm, |
|
58 | - $enableRecovery); |
|
59 | - |
|
60 | - |
|
61 | - $this->assertEquals($expectedMessage, $response->getData()['data']['message']); |
|
62 | - $this->assertEquals($expectedStatus, $response->getStatus()); |
|
63 | - } |
|
64 | - |
|
65 | - public static function changeRecoveryPasswordProvider(): array { |
|
66 | - return [ |
|
67 | - ['test', 'test', 'oldtestFail', 'Could not change the password. Maybe the old password was not correct.', Http::STATUS_BAD_REQUEST], |
|
68 | - ['test', 'test', 'oldtest', 'Password successfully changed.', Http::STATUS_OK], |
|
69 | - ['test', 'notmatch', 'oldtest', 'Repeated recovery key password does not match the provided recovery key password', Http::STATUS_BAD_REQUEST], |
|
70 | - ['', 'test', 'oldtest', 'Please provide a new recovery password', Http::STATUS_BAD_REQUEST], |
|
71 | - ['test', 'test', '', 'Please provide the old recovery password', Http::STATUS_BAD_REQUEST] |
|
72 | - ]; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @param $password |
|
77 | - * @param $confirmPassword |
|
78 | - * @param $oldPassword |
|
79 | - * @param $expectedMessage |
|
80 | - * @param $expectedStatus |
|
81 | - */ |
|
82 | - #[\PHPUnit\Framework\Attributes\DataProvider('changeRecoveryPasswordProvider')] |
|
83 | - public function testChangeRecoveryPassword($password, $confirmPassword, $oldPassword, $expectedMessage, $expectedStatus): void { |
|
84 | - $this->recoveryMock->expects($this->any()) |
|
85 | - ->method('changeRecoveryKeyPassword') |
|
86 | - ->with($password, $oldPassword) |
|
87 | - ->willReturnMap([ |
|
88 | - ['test', 'oldTestFail', false], |
|
89 | - ['test', 'oldtest', true] |
|
90 | - ]); |
|
91 | - |
|
92 | - $response = $this->controller->changeRecoveryPassword($password, |
|
93 | - $oldPassword, |
|
94 | - $confirmPassword); |
|
95 | - |
|
96 | - $this->assertEquals($expectedMessage, $response->getData()['data']['message']); |
|
97 | - $this->assertEquals($expectedStatus, $response->getStatus()); |
|
98 | - } |
|
99 | - |
|
100 | - public static function userSetRecoveryProvider(): array { |
|
101 | - return [ |
|
102 | - ['1', 'Recovery Key enabled', Http::STATUS_OK], |
|
103 | - ['0', 'Could not enable the recovery key, please try again or contact your administrator', Http::STATUS_BAD_REQUEST] |
|
104 | - ]; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * @param $enableRecovery |
|
109 | - * @param $expectedMessage |
|
110 | - * @param $expectedStatus |
|
111 | - */ |
|
112 | - #[\PHPUnit\Framework\Attributes\DataProvider('userSetRecoveryProvider')] |
|
113 | - public function testUserSetRecovery($enableRecovery, $expectedMessage, $expectedStatus): void { |
|
114 | - $this->recoveryMock->expects($this->any()) |
|
115 | - ->method('setRecoveryForUser') |
|
116 | - ->with($enableRecovery) |
|
117 | - ->willReturnMap([ |
|
118 | - ['1', true], |
|
119 | - ['0', false] |
|
120 | - ]); |
|
121 | - |
|
122 | - |
|
123 | - $response = $this->controller->userSetRecovery($enableRecovery); |
|
124 | - |
|
125 | - $this->assertEquals($expectedMessage, $response->getData()['data']['message']); |
|
126 | - $this->assertEquals($expectedStatus, $response->getStatus()); |
|
127 | - } |
|
128 | - |
|
129 | - protected function setUp(): void { |
|
130 | - parent::setUp(); |
|
131 | - |
|
132 | - $this->requestMock = $this->getMockBuilder(IRequest::class) |
|
133 | - ->disableOriginalConstructor() |
|
134 | - ->getMock(); |
|
135 | - |
|
136 | - $this->configMock = $this->getMockBuilder(IConfig::class) |
|
137 | - ->disableOriginalConstructor() |
|
138 | - ->getMock(); |
|
139 | - |
|
140 | - $this->l10nMock = $this->getMockBuilder(IL10N::class) |
|
141 | - ->disableOriginalConstructor() |
|
142 | - ->getMock(); |
|
143 | - |
|
144 | - // Make l10n work in our tests |
|
145 | - $this->l10nMock->expects($this->any()) |
|
146 | - ->method('t') |
|
147 | - ->willReturnArgument(0); |
|
148 | - |
|
149 | - $this->recoveryMock = $this->getMockBuilder(Recovery::class) |
|
150 | - ->disableOriginalConstructor() |
|
151 | - ->getMock(); |
|
152 | - |
|
153 | - $this->controller = new RecoveryController('encryption', |
|
154 | - $this->requestMock, |
|
155 | - $this->configMock, |
|
156 | - $this->l10nMock, |
|
157 | - $this->recoveryMock); |
|
158 | - } |
|
22 | + protected RecoveryController $controller; |
|
23 | + |
|
24 | + protected IRequest&MockObject $requestMock; |
|
25 | + protected IConfig&MockObject $configMock; |
|
26 | + protected IL10N&MockObject $l10nMock; |
|
27 | + protected Recovery&MockObject $recoveryMock; |
|
28 | + |
|
29 | + public static function adminRecoveryProvider(): array { |
|
30 | + return [ |
|
31 | + ['test', 'test', '1', 'Recovery key successfully enabled', Http::STATUS_OK], |
|
32 | + ['', 'test', '1', 'Missing recovery key password', Http::STATUS_BAD_REQUEST], |
|
33 | + ['test', '', '1', 'Please repeat the recovery key password', Http::STATUS_BAD_REQUEST], |
|
34 | + ['test', 'something that doesn\'t match', '1', 'Repeated recovery key password does not match the provided recovery key password', Http::STATUS_BAD_REQUEST], |
|
35 | + ['test', 'test', '0', 'Recovery key successfully disabled', Http::STATUS_OK], |
|
36 | + ]; |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * @param $recoveryPassword |
|
41 | + * @param $passConfirm |
|
42 | + * @param $enableRecovery |
|
43 | + * @param $expectedMessage |
|
44 | + * @param $expectedStatus |
|
45 | + */ |
|
46 | + #[\PHPUnit\Framework\Attributes\DataProvider('adminRecoveryProvider')] |
|
47 | + public function testAdminRecovery($recoveryPassword, $passConfirm, $enableRecovery, $expectedMessage, $expectedStatus): void { |
|
48 | + $this->recoveryMock->expects($this->any()) |
|
49 | + ->method('enableAdminRecovery') |
|
50 | + ->willReturn(true); |
|
51 | + |
|
52 | + $this->recoveryMock->expects($this->any()) |
|
53 | + ->method('disableAdminRecovery') |
|
54 | + ->willReturn(true); |
|
55 | + |
|
56 | + $response = $this->controller->adminRecovery($recoveryPassword, |
|
57 | + $passConfirm, |
|
58 | + $enableRecovery); |
|
59 | + |
|
60 | + |
|
61 | + $this->assertEquals($expectedMessage, $response->getData()['data']['message']); |
|
62 | + $this->assertEquals($expectedStatus, $response->getStatus()); |
|
63 | + } |
|
64 | + |
|
65 | + public static function changeRecoveryPasswordProvider(): array { |
|
66 | + return [ |
|
67 | + ['test', 'test', 'oldtestFail', 'Could not change the password. Maybe the old password was not correct.', Http::STATUS_BAD_REQUEST], |
|
68 | + ['test', 'test', 'oldtest', 'Password successfully changed.', Http::STATUS_OK], |
|
69 | + ['test', 'notmatch', 'oldtest', 'Repeated recovery key password does not match the provided recovery key password', Http::STATUS_BAD_REQUEST], |
|
70 | + ['', 'test', 'oldtest', 'Please provide a new recovery password', Http::STATUS_BAD_REQUEST], |
|
71 | + ['test', 'test', '', 'Please provide the old recovery password', Http::STATUS_BAD_REQUEST] |
|
72 | + ]; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @param $password |
|
77 | + * @param $confirmPassword |
|
78 | + * @param $oldPassword |
|
79 | + * @param $expectedMessage |
|
80 | + * @param $expectedStatus |
|
81 | + */ |
|
82 | + #[\PHPUnit\Framework\Attributes\DataProvider('changeRecoveryPasswordProvider')] |
|
83 | + public function testChangeRecoveryPassword($password, $confirmPassword, $oldPassword, $expectedMessage, $expectedStatus): void { |
|
84 | + $this->recoveryMock->expects($this->any()) |
|
85 | + ->method('changeRecoveryKeyPassword') |
|
86 | + ->with($password, $oldPassword) |
|
87 | + ->willReturnMap([ |
|
88 | + ['test', 'oldTestFail', false], |
|
89 | + ['test', 'oldtest', true] |
|
90 | + ]); |
|
91 | + |
|
92 | + $response = $this->controller->changeRecoveryPassword($password, |
|
93 | + $oldPassword, |
|
94 | + $confirmPassword); |
|
95 | + |
|
96 | + $this->assertEquals($expectedMessage, $response->getData()['data']['message']); |
|
97 | + $this->assertEquals($expectedStatus, $response->getStatus()); |
|
98 | + } |
|
99 | + |
|
100 | + public static function userSetRecoveryProvider(): array { |
|
101 | + return [ |
|
102 | + ['1', 'Recovery Key enabled', Http::STATUS_OK], |
|
103 | + ['0', 'Could not enable the recovery key, please try again or contact your administrator', Http::STATUS_BAD_REQUEST] |
|
104 | + ]; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * @param $enableRecovery |
|
109 | + * @param $expectedMessage |
|
110 | + * @param $expectedStatus |
|
111 | + */ |
|
112 | + #[\PHPUnit\Framework\Attributes\DataProvider('userSetRecoveryProvider')] |
|
113 | + public function testUserSetRecovery($enableRecovery, $expectedMessage, $expectedStatus): void { |
|
114 | + $this->recoveryMock->expects($this->any()) |
|
115 | + ->method('setRecoveryForUser') |
|
116 | + ->with($enableRecovery) |
|
117 | + ->willReturnMap([ |
|
118 | + ['1', true], |
|
119 | + ['0', false] |
|
120 | + ]); |
|
121 | + |
|
122 | + |
|
123 | + $response = $this->controller->userSetRecovery($enableRecovery); |
|
124 | + |
|
125 | + $this->assertEquals($expectedMessage, $response->getData()['data']['message']); |
|
126 | + $this->assertEquals($expectedStatus, $response->getStatus()); |
|
127 | + } |
|
128 | + |
|
129 | + protected function setUp(): void { |
|
130 | + parent::setUp(); |
|
131 | + |
|
132 | + $this->requestMock = $this->getMockBuilder(IRequest::class) |
|
133 | + ->disableOriginalConstructor() |
|
134 | + ->getMock(); |
|
135 | + |
|
136 | + $this->configMock = $this->getMockBuilder(IConfig::class) |
|
137 | + ->disableOriginalConstructor() |
|
138 | + ->getMock(); |
|
139 | + |
|
140 | + $this->l10nMock = $this->getMockBuilder(IL10N::class) |
|
141 | + ->disableOriginalConstructor() |
|
142 | + ->getMock(); |
|
143 | + |
|
144 | + // Make l10n work in our tests |
|
145 | + $this->l10nMock->expects($this->any()) |
|
146 | + ->method('t') |
|
147 | + ->willReturnArgument(0); |
|
148 | + |
|
149 | + $this->recoveryMock = $this->getMockBuilder(Recovery::class) |
|
150 | + ->disableOriginalConstructor() |
|
151 | + ->getMock(); |
|
152 | + |
|
153 | + $this->controller = new RecoveryController('encryption', |
|
154 | + $this->requestMock, |
|
155 | + $this->configMock, |
|
156 | + $this->l10nMock, |
|
157 | + $this->recoveryMock); |
|
158 | + } |
|
159 | 159 | } |
@@ -29,378 +29,378 @@ |
||
29 | 29 | |
30 | 30 | class EncryptionTest extends TestCase { |
31 | 31 | |
32 | - protected Encryption $instance; |
|
33 | - |
|
34 | - protected KeyManager&MockObject $keyManagerMock; |
|
35 | - protected EncryptAll&MockObject $encryptAllMock; |
|
36 | - protected DecryptAll&MockObject $decryptAllMock; |
|
37 | - protected Session&MockObject $sessionMock; |
|
38 | - protected Crypt&MockObject $cryptMock; |
|
39 | - protected Util&MockObject $utilMock; |
|
40 | - protected LoggerInterface&MockObject $loggerMock; |
|
41 | - protected IL10N&MockObject $l10nMock; |
|
42 | - protected IStorage&MockObject $storageMock; |
|
43 | - |
|
44 | - protected function setUp(): void { |
|
45 | - parent::setUp(); |
|
46 | - |
|
47 | - $this->storageMock = $this->getMockBuilder(IStorage::class) |
|
48 | - ->disableOriginalConstructor()->getMock(); |
|
49 | - $this->cryptMock = $this->getMockBuilder(Crypt::class) |
|
50 | - ->disableOriginalConstructor() |
|
51 | - ->getMock(); |
|
52 | - $this->utilMock = $this->getMockBuilder(Util::class) |
|
53 | - ->disableOriginalConstructor() |
|
54 | - ->getMock(); |
|
55 | - $this->keyManagerMock = $this->getMockBuilder(KeyManager::class) |
|
56 | - ->disableOriginalConstructor() |
|
57 | - ->getMock(); |
|
58 | - $this->sessionMock = $this->getMockBuilder(Session::class) |
|
59 | - ->disableOriginalConstructor() |
|
60 | - ->getMock(); |
|
61 | - $this->encryptAllMock = $this->getMockBuilder(EncryptAll::class) |
|
62 | - ->disableOriginalConstructor() |
|
63 | - ->getMock(); |
|
64 | - $this->decryptAllMock = $this->getMockBuilder(DecryptAll::class) |
|
65 | - ->disableOriginalConstructor() |
|
66 | - ->getMock(); |
|
67 | - $this->loggerMock = $this->getMockBuilder(LoggerInterface::class) |
|
68 | - ->disableOriginalConstructor() |
|
69 | - ->getMock(); |
|
70 | - $this->l10nMock = $this->getMockBuilder(IL10N::class) |
|
71 | - ->disableOriginalConstructor() |
|
72 | - ->getMock(); |
|
73 | - $this->l10nMock->expects($this->any()) |
|
74 | - ->method('t') |
|
75 | - ->with($this->anything()) |
|
76 | - ->willReturnArgument(0); |
|
77 | - |
|
78 | - $this->instance = new Encryption( |
|
79 | - $this->cryptMock, |
|
80 | - $this->keyManagerMock, |
|
81 | - $this->utilMock, |
|
82 | - $this->sessionMock, |
|
83 | - $this->encryptAllMock, |
|
84 | - $this->decryptAllMock, |
|
85 | - $this->loggerMock, |
|
86 | - $this->l10nMock |
|
87 | - ); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * test if public key from one of the recipients is missing |
|
92 | - */ |
|
93 | - public function testEndUser1(): void { |
|
94 | - $this->sessionMock->expects($this->once()) |
|
95 | - ->method('decryptAllModeActivated') |
|
96 | - ->willReturn(false); |
|
97 | - |
|
98 | - $this->instance->begin('/foo/bar', 'user1', 'r', [], ['users' => ['user1', 'user2', 'user3']]); |
|
99 | - $this->endTest(); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * test if public key from owner is missing |
|
104 | - * |
|
105 | - */ |
|
106 | - public function testEndUser2(): void { |
|
107 | - $this->sessionMock->expects($this->once()) |
|
108 | - ->method('decryptAllModeActivated') |
|
109 | - ->willReturn(false); |
|
110 | - |
|
111 | - $this->expectException(PublicKeyMissingException::class); |
|
112 | - |
|
113 | - $this->instance->begin('/foo/bar', 'user2', 'r', [], ['users' => ['user1', 'user2', 'user3']]); |
|
114 | - $this->endTest(); |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * common part of testEndUser1 and testEndUser2 |
|
119 | - * |
|
120 | - * @throws PublicKeyMissingException |
|
121 | - */ |
|
122 | - public function endTest() { |
|
123 | - // prepare internal variables |
|
124 | - self::invokePrivate($this->instance, 'isWriteOperation', [true]); |
|
125 | - self::invokePrivate($this->instance, 'writeCache', ['']); |
|
126 | - |
|
127 | - $this->keyManagerMock->expects($this->any()) |
|
128 | - ->method('getPublicKey') |
|
129 | - ->willReturnCallback([$this, 'getPublicKeyCallback']); |
|
130 | - $this->keyManagerMock->expects($this->any()) |
|
131 | - ->method('addSystemKeys') |
|
132 | - ->willReturnCallback([$this, 'addSystemKeysCallback']); |
|
133 | - $this->cryptMock->expects($this->any()) |
|
134 | - ->method('multiKeyEncrypt') |
|
135 | - ->willReturn([]); |
|
136 | - |
|
137 | - $this->instance->end('/foo/bar'); |
|
138 | - } |
|
139 | - |
|
140 | - |
|
141 | - public function getPublicKeyCallback($uid) { |
|
142 | - if ($uid === 'user2') { |
|
143 | - throw new PublicKeyMissingException($uid); |
|
144 | - } |
|
145 | - return $uid; |
|
146 | - } |
|
147 | - |
|
148 | - public function addSystemKeysCallback($accessList, $publicKeys) { |
|
149 | - $this->assertSame(2, count($publicKeys)); |
|
150 | - $this->assertArrayHasKey('user1', $publicKeys); |
|
151 | - $this->assertArrayHasKey('user3', $publicKeys); |
|
152 | - return $publicKeys; |
|
153 | - } |
|
154 | - |
|
155 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataProviderForTestGetPathToRealFile')] |
|
156 | - public function testGetPathToRealFile($path, $expected): void { |
|
157 | - $this->assertSame($expected, |
|
158 | - self::invokePrivate($this->instance, 'getPathToRealFile', [$path]) |
|
159 | - ); |
|
160 | - } |
|
161 | - |
|
162 | - public static function dataProviderForTestGetPathToRealFile(): array { |
|
163 | - return [ |
|
164 | - ['/user/files/foo/bar.txt', '/user/files/foo/bar.txt'], |
|
165 | - ['/user/files/foo.txt', '/user/files/foo.txt'], |
|
166 | - ['/user/files_versions/foo.txt.v543534', '/user/files/foo.txt'], |
|
167 | - ['/user/files_versions/foo/bar.txt.v5454', '/user/files/foo/bar.txt'], |
|
168 | - ]; |
|
169 | - } |
|
170 | - |
|
171 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataTestBegin')] |
|
172 | - public function testBegin($mode, $header, $legacyCipher, $defaultCipher, $fileKey, $expected): void { |
|
173 | - $this->sessionMock->expects($this->once()) |
|
174 | - ->method('decryptAllModeActivated') |
|
175 | - ->willReturn(false); |
|
176 | - |
|
177 | - $this->sessionMock->expects($this->never())->method('getDecryptAllUid'); |
|
178 | - $this->sessionMock->expects($this->never())->method('getDecryptAllKey'); |
|
179 | - $this->keyManagerMock->expects($this->never())->method('getEncryptedFileKey'); |
|
180 | - $this->keyManagerMock->expects($this->never())->method('getShareKey'); |
|
181 | - $this->cryptMock->expects($this->never())->method('multiKeyDecrypt'); |
|
182 | - |
|
183 | - $this->cryptMock->expects($this->any()) |
|
184 | - ->method('getCipher') |
|
185 | - ->willReturn($defaultCipher); |
|
186 | - $this->cryptMock->expects($this->any()) |
|
187 | - ->method('getLegacyCipher') |
|
188 | - ->willReturn($legacyCipher); |
|
189 | - if (empty($fileKey)) { |
|
190 | - $this->cryptMock->expects($this->once()) |
|
191 | - ->method('generateFileKey') |
|
192 | - ->willReturn('fileKey'); |
|
193 | - } else { |
|
194 | - $this->cryptMock->expects($this->never()) |
|
195 | - ->method('generateFileKey'); |
|
196 | - } |
|
197 | - |
|
198 | - $this->keyManagerMock->expects($this->once()) |
|
199 | - ->method('getFileKey') |
|
200 | - ->willReturn($fileKey); |
|
201 | - |
|
202 | - $result = $this->instance->begin('/user/files/foo.txt', 'user', $mode, $header, []); |
|
203 | - |
|
204 | - $this->assertArrayHasKey('cipher', $result); |
|
205 | - $this->assertSame($expected, $result['cipher']); |
|
206 | - if ($mode === 'w') { |
|
207 | - $this->assertTrue(self::invokePrivate($this->instance, 'isWriteOperation')); |
|
208 | - } else { |
|
209 | - $this->assertFalse(self::invokePrivate($this->instance, 'isWriteOperation')); |
|
210 | - } |
|
211 | - } |
|
212 | - |
|
213 | - public static function dataTestBegin(): array { |
|
214 | - return [ |
|
215 | - ['w', ['cipher' => 'myCipher'], 'legacyCipher', 'defaultCipher', 'fileKey', 'defaultCipher'], |
|
216 | - ['r', ['cipher' => 'myCipher'], 'legacyCipher', 'defaultCipher', 'fileKey', 'myCipher'], |
|
217 | - ['w', [], 'legacyCipher', 'defaultCipher', '', 'defaultCipher'], |
|
218 | - ['r', [], 'legacyCipher', 'defaultCipher', 'file_key', 'legacyCipher'], |
|
219 | - ]; |
|
220 | - } |
|
221 | - |
|
222 | - |
|
223 | - /** |
|
224 | - * test begin() if decryptAll mode was activated |
|
225 | - */ |
|
226 | - public function testBeginDecryptAll(): void { |
|
227 | - $path = '/user/files/foo.txt'; |
|
228 | - $fileKey = 'fileKey'; |
|
229 | - |
|
230 | - $this->sessionMock->expects($this->once()) |
|
231 | - ->method('decryptAllModeActivated') |
|
232 | - ->willReturn(true); |
|
233 | - $this->keyManagerMock->expects($this->once()) |
|
234 | - ->method('getFileKey') |
|
235 | - ->with($path, 'user', null, true) |
|
236 | - ->willReturn($fileKey); |
|
237 | - |
|
238 | - $this->instance->begin($path, 'user', 'r', [], []); |
|
239 | - |
|
240 | - $this->assertSame($fileKey, |
|
241 | - $this->invokePrivate($this->instance, 'fileKey') |
|
242 | - ); |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * test begin() if encryption is not initialized but the master key is enabled |
|
247 | - * in this case we can initialize the encryption without a username/password |
|
248 | - * and continue |
|
249 | - */ |
|
250 | - public function testBeginInitMasterKey(): void { |
|
251 | - $this->sessionMock->expects($this->once()) |
|
252 | - ->method('decryptAllModeActivated') |
|
253 | - ->willReturn(false); |
|
254 | - |
|
255 | - $this->sessionMock->expects($this->once())->method('isReady')->willReturn(false); |
|
256 | - $this->utilMock->expects($this->once())->method('isMasterKeyEnabled') |
|
257 | - ->willReturn(true); |
|
258 | - $this->keyManagerMock->expects($this->once())->method('init')->with('', ''); |
|
259 | - |
|
260 | - $this->instance->begin('/user/files/welcome.txt', 'user', 'r', [], []); |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * |
|
265 | - * @param string $fileKey |
|
266 | - * @param boolean $expected |
|
267 | - */ |
|
268 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataTestUpdate')] |
|
269 | - public function testUpdate($fileKey, $expected): void { |
|
270 | - $this->keyManagerMock->expects($this->once()) |
|
271 | - ->method('getFileKey')->willReturn($fileKey); |
|
272 | - |
|
273 | - $this->keyManagerMock->expects($this->any()) |
|
274 | - ->method('getPublicKey')->willReturn('publicKey'); |
|
275 | - |
|
276 | - $this->keyManagerMock->expects($this->any()) |
|
277 | - ->method('addSystemKeys') |
|
278 | - ->willReturnCallback(function ($accessList, $publicKeys) { |
|
279 | - return $publicKeys; |
|
280 | - }); |
|
281 | - |
|
282 | - $this->keyManagerMock->expects($this->never())->method('getVersion'); |
|
283 | - $this->keyManagerMock->expects($this->never())->method('setVersion'); |
|
284 | - |
|
285 | - $this->assertSame($expected, |
|
286 | - $this->instance->update('path', 'user1', ['users' => ['user1']]) |
|
287 | - ); |
|
288 | - } |
|
289 | - |
|
290 | - public static function dataTestUpdate(): array { |
|
291 | - return [ |
|
292 | - ['', false], |
|
293 | - ['fileKey', true] |
|
294 | - ]; |
|
295 | - } |
|
296 | - |
|
297 | - public function testUpdateNoUsers(): void { |
|
298 | - $this->invokePrivate($this->instance, 'rememberVersion', [['path' => 2]]); |
|
299 | - |
|
300 | - $this->keyManagerMock->expects($this->never())->method('getFileKey'); |
|
301 | - $this->keyManagerMock->expects($this->never())->method('getPublicKey'); |
|
302 | - $this->keyManagerMock->expects($this->never())->method('addSystemKeys'); |
|
303 | - $this->keyManagerMock->expects($this->once())->method('setVersion') |
|
304 | - ->willReturnCallback(function ($path, $version, $view): void { |
|
305 | - $this->assertSame('path', $path); |
|
306 | - $this->assertSame(2, $version); |
|
307 | - $this->assertTrue($view instanceof View); |
|
308 | - }); |
|
309 | - $this->instance->update('path', 'user1', []); |
|
310 | - } |
|
311 | - |
|
312 | - /** |
|
313 | - * Test case if the public key is missing. Nextcloud should still encrypt |
|
314 | - * the file for the remaining users |
|
315 | - */ |
|
316 | - public function testUpdateMissingPublicKey(): void { |
|
317 | - $this->keyManagerMock->expects($this->once()) |
|
318 | - ->method('getFileKey')->willReturn('fileKey'); |
|
319 | - |
|
320 | - $this->keyManagerMock->expects($this->any()) |
|
321 | - ->method('getPublicKey')->willReturnCallback( |
|
322 | - function ($user): void { |
|
323 | - throw new PublicKeyMissingException($user); |
|
324 | - } |
|
325 | - ); |
|
326 | - |
|
327 | - $this->keyManagerMock->expects($this->any()) |
|
328 | - ->method('addSystemKeys') |
|
329 | - ->willReturnCallback(function ($accessList, $publicKeys) { |
|
330 | - return $publicKeys; |
|
331 | - }); |
|
332 | - |
|
333 | - $this->cryptMock->expects($this->once())->method('multiKeyEncrypt') |
|
334 | - ->willReturnCallback( |
|
335 | - function ($fileKey, $publicKeys) { |
|
336 | - $this->assertEmpty($publicKeys); |
|
337 | - $this->assertSame('fileKey', $fileKey); |
|
338 | - return []; |
|
339 | - } |
|
340 | - ); |
|
341 | - |
|
342 | - $this->keyManagerMock->expects($this->never())->method('getVersion'); |
|
343 | - $this->keyManagerMock->expects($this->never())->method('setVersion'); |
|
344 | - |
|
345 | - $this->assertTrue( |
|
346 | - $this->instance->update('path', 'user1', ['users' => ['user1']]) |
|
347 | - ); |
|
348 | - } |
|
349 | - |
|
350 | - /** |
|
351 | - * by default the encryption module should encrypt regular files, files in |
|
352 | - * files_versions and files in files_trashbin |
|
353 | - */ |
|
354 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataTestShouldEncrypt')] |
|
355 | - public function testShouldEncrypt($path, $shouldEncryptHomeStorage, $isHomeStorage, $expected): void { |
|
356 | - $this->utilMock->expects($this->once())->method('shouldEncryptHomeStorage') |
|
357 | - ->willReturn($shouldEncryptHomeStorage); |
|
358 | - |
|
359 | - if ($shouldEncryptHomeStorage === false) { |
|
360 | - $this->storageMock->expects($this->once())->method('instanceOfStorage') |
|
361 | - ->with('\OCP\Files\IHomeStorage')->willReturn($isHomeStorage); |
|
362 | - $this->utilMock->expects($this->once())->method('getStorage')->with($path) |
|
363 | - ->willReturn($this->storageMock); |
|
364 | - } |
|
365 | - |
|
366 | - $this->assertSame($expected, |
|
367 | - $this->instance->shouldEncrypt($path) |
|
368 | - ); |
|
369 | - } |
|
370 | - |
|
371 | - public static function dataTestShouldEncrypt(): array { |
|
372 | - return [ |
|
373 | - ['/user1/files/foo.txt', true, true, true], |
|
374 | - ['/user1/files_versions/foo.txt', true, true, true], |
|
375 | - ['/user1/files_trashbin/foo.txt', true, true, true], |
|
376 | - ['/user1/some_folder/foo.txt', true, true, false], |
|
377 | - ['/user1/foo.txt', true, true, false], |
|
378 | - ['/user1/files', true, true, false], |
|
379 | - ['/user1/files_trashbin', true, true, false], |
|
380 | - ['/user1/files_versions', true, true, false], |
|
381 | - // test if shouldEncryptHomeStorage is set to false |
|
382 | - ['/user1/files/foo.txt', false, true, false], |
|
383 | - ['/user1/files_versions/foo.txt', false, false, true], |
|
384 | - ]; |
|
385 | - } |
|
386 | - |
|
387 | - |
|
388 | - public function testDecrypt(): void { |
|
389 | - $this->expectException(DecryptionFailedException::class); |
|
390 | - $this->expectExceptionMessage('Cannot decrypt this file, probably this is a shared file. Please ask the file owner to reshare the file with you.'); |
|
391 | - |
|
392 | - $this->instance->decrypt('abc'); |
|
393 | - } |
|
394 | - |
|
395 | - public function testPrepareDecryptAll(): void { |
|
396 | - /** @var \Symfony\Component\Console\Input\InputInterface $input */ |
|
397 | - $input = $this->createMock(InputInterface::class); |
|
398 | - /** @var \Symfony\Component\Console\Output\OutputInterface $output */ |
|
399 | - $output = $this->createMock(OutputInterface::class); |
|
400 | - |
|
401 | - $this->decryptAllMock->expects($this->once())->method('prepare') |
|
402 | - ->with($input, $output, 'user'); |
|
403 | - |
|
404 | - $this->instance->prepareDecryptAll($input, $output, 'user'); |
|
405 | - } |
|
32 | + protected Encryption $instance; |
|
33 | + |
|
34 | + protected KeyManager&MockObject $keyManagerMock; |
|
35 | + protected EncryptAll&MockObject $encryptAllMock; |
|
36 | + protected DecryptAll&MockObject $decryptAllMock; |
|
37 | + protected Session&MockObject $sessionMock; |
|
38 | + protected Crypt&MockObject $cryptMock; |
|
39 | + protected Util&MockObject $utilMock; |
|
40 | + protected LoggerInterface&MockObject $loggerMock; |
|
41 | + protected IL10N&MockObject $l10nMock; |
|
42 | + protected IStorage&MockObject $storageMock; |
|
43 | + |
|
44 | + protected function setUp(): void { |
|
45 | + parent::setUp(); |
|
46 | + |
|
47 | + $this->storageMock = $this->getMockBuilder(IStorage::class) |
|
48 | + ->disableOriginalConstructor()->getMock(); |
|
49 | + $this->cryptMock = $this->getMockBuilder(Crypt::class) |
|
50 | + ->disableOriginalConstructor() |
|
51 | + ->getMock(); |
|
52 | + $this->utilMock = $this->getMockBuilder(Util::class) |
|
53 | + ->disableOriginalConstructor() |
|
54 | + ->getMock(); |
|
55 | + $this->keyManagerMock = $this->getMockBuilder(KeyManager::class) |
|
56 | + ->disableOriginalConstructor() |
|
57 | + ->getMock(); |
|
58 | + $this->sessionMock = $this->getMockBuilder(Session::class) |
|
59 | + ->disableOriginalConstructor() |
|
60 | + ->getMock(); |
|
61 | + $this->encryptAllMock = $this->getMockBuilder(EncryptAll::class) |
|
62 | + ->disableOriginalConstructor() |
|
63 | + ->getMock(); |
|
64 | + $this->decryptAllMock = $this->getMockBuilder(DecryptAll::class) |
|
65 | + ->disableOriginalConstructor() |
|
66 | + ->getMock(); |
|
67 | + $this->loggerMock = $this->getMockBuilder(LoggerInterface::class) |
|
68 | + ->disableOriginalConstructor() |
|
69 | + ->getMock(); |
|
70 | + $this->l10nMock = $this->getMockBuilder(IL10N::class) |
|
71 | + ->disableOriginalConstructor() |
|
72 | + ->getMock(); |
|
73 | + $this->l10nMock->expects($this->any()) |
|
74 | + ->method('t') |
|
75 | + ->with($this->anything()) |
|
76 | + ->willReturnArgument(0); |
|
77 | + |
|
78 | + $this->instance = new Encryption( |
|
79 | + $this->cryptMock, |
|
80 | + $this->keyManagerMock, |
|
81 | + $this->utilMock, |
|
82 | + $this->sessionMock, |
|
83 | + $this->encryptAllMock, |
|
84 | + $this->decryptAllMock, |
|
85 | + $this->loggerMock, |
|
86 | + $this->l10nMock |
|
87 | + ); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * test if public key from one of the recipients is missing |
|
92 | + */ |
|
93 | + public function testEndUser1(): void { |
|
94 | + $this->sessionMock->expects($this->once()) |
|
95 | + ->method('decryptAllModeActivated') |
|
96 | + ->willReturn(false); |
|
97 | + |
|
98 | + $this->instance->begin('/foo/bar', 'user1', 'r', [], ['users' => ['user1', 'user2', 'user3']]); |
|
99 | + $this->endTest(); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * test if public key from owner is missing |
|
104 | + * |
|
105 | + */ |
|
106 | + public function testEndUser2(): void { |
|
107 | + $this->sessionMock->expects($this->once()) |
|
108 | + ->method('decryptAllModeActivated') |
|
109 | + ->willReturn(false); |
|
110 | + |
|
111 | + $this->expectException(PublicKeyMissingException::class); |
|
112 | + |
|
113 | + $this->instance->begin('/foo/bar', 'user2', 'r', [], ['users' => ['user1', 'user2', 'user3']]); |
|
114 | + $this->endTest(); |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * common part of testEndUser1 and testEndUser2 |
|
119 | + * |
|
120 | + * @throws PublicKeyMissingException |
|
121 | + */ |
|
122 | + public function endTest() { |
|
123 | + // prepare internal variables |
|
124 | + self::invokePrivate($this->instance, 'isWriteOperation', [true]); |
|
125 | + self::invokePrivate($this->instance, 'writeCache', ['']); |
|
126 | + |
|
127 | + $this->keyManagerMock->expects($this->any()) |
|
128 | + ->method('getPublicKey') |
|
129 | + ->willReturnCallback([$this, 'getPublicKeyCallback']); |
|
130 | + $this->keyManagerMock->expects($this->any()) |
|
131 | + ->method('addSystemKeys') |
|
132 | + ->willReturnCallback([$this, 'addSystemKeysCallback']); |
|
133 | + $this->cryptMock->expects($this->any()) |
|
134 | + ->method('multiKeyEncrypt') |
|
135 | + ->willReturn([]); |
|
136 | + |
|
137 | + $this->instance->end('/foo/bar'); |
|
138 | + } |
|
139 | + |
|
140 | + |
|
141 | + public function getPublicKeyCallback($uid) { |
|
142 | + if ($uid === 'user2') { |
|
143 | + throw new PublicKeyMissingException($uid); |
|
144 | + } |
|
145 | + return $uid; |
|
146 | + } |
|
147 | + |
|
148 | + public function addSystemKeysCallback($accessList, $publicKeys) { |
|
149 | + $this->assertSame(2, count($publicKeys)); |
|
150 | + $this->assertArrayHasKey('user1', $publicKeys); |
|
151 | + $this->assertArrayHasKey('user3', $publicKeys); |
|
152 | + return $publicKeys; |
|
153 | + } |
|
154 | + |
|
155 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataProviderForTestGetPathToRealFile')] |
|
156 | + public function testGetPathToRealFile($path, $expected): void { |
|
157 | + $this->assertSame($expected, |
|
158 | + self::invokePrivate($this->instance, 'getPathToRealFile', [$path]) |
|
159 | + ); |
|
160 | + } |
|
161 | + |
|
162 | + public static function dataProviderForTestGetPathToRealFile(): array { |
|
163 | + return [ |
|
164 | + ['/user/files/foo/bar.txt', '/user/files/foo/bar.txt'], |
|
165 | + ['/user/files/foo.txt', '/user/files/foo.txt'], |
|
166 | + ['/user/files_versions/foo.txt.v543534', '/user/files/foo.txt'], |
|
167 | + ['/user/files_versions/foo/bar.txt.v5454', '/user/files/foo/bar.txt'], |
|
168 | + ]; |
|
169 | + } |
|
170 | + |
|
171 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestBegin')] |
|
172 | + public function testBegin($mode, $header, $legacyCipher, $defaultCipher, $fileKey, $expected): void { |
|
173 | + $this->sessionMock->expects($this->once()) |
|
174 | + ->method('decryptAllModeActivated') |
|
175 | + ->willReturn(false); |
|
176 | + |
|
177 | + $this->sessionMock->expects($this->never())->method('getDecryptAllUid'); |
|
178 | + $this->sessionMock->expects($this->never())->method('getDecryptAllKey'); |
|
179 | + $this->keyManagerMock->expects($this->never())->method('getEncryptedFileKey'); |
|
180 | + $this->keyManagerMock->expects($this->never())->method('getShareKey'); |
|
181 | + $this->cryptMock->expects($this->never())->method('multiKeyDecrypt'); |
|
182 | + |
|
183 | + $this->cryptMock->expects($this->any()) |
|
184 | + ->method('getCipher') |
|
185 | + ->willReturn($defaultCipher); |
|
186 | + $this->cryptMock->expects($this->any()) |
|
187 | + ->method('getLegacyCipher') |
|
188 | + ->willReturn($legacyCipher); |
|
189 | + if (empty($fileKey)) { |
|
190 | + $this->cryptMock->expects($this->once()) |
|
191 | + ->method('generateFileKey') |
|
192 | + ->willReturn('fileKey'); |
|
193 | + } else { |
|
194 | + $this->cryptMock->expects($this->never()) |
|
195 | + ->method('generateFileKey'); |
|
196 | + } |
|
197 | + |
|
198 | + $this->keyManagerMock->expects($this->once()) |
|
199 | + ->method('getFileKey') |
|
200 | + ->willReturn($fileKey); |
|
201 | + |
|
202 | + $result = $this->instance->begin('/user/files/foo.txt', 'user', $mode, $header, []); |
|
203 | + |
|
204 | + $this->assertArrayHasKey('cipher', $result); |
|
205 | + $this->assertSame($expected, $result['cipher']); |
|
206 | + if ($mode === 'w') { |
|
207 | + $this->assertTrue(self::invokePrivate($this->instance, 'isWriteOperation')); |
|
208 | + } else { |
|
209 | + $this->assertFalse(self::invokePrivate($this->instance, 'isWriteOperation')); |
|
210 | + } |
|
211 | + } |
|
212 | + |
|
213 | + public static function dataTestBegin(): array { |
|
214 | + return [ |
|
215 | + ['w', ['cipher' => 'myCipher'], 'legacyCipher', 'defaultCipher', 'fileKey', 'defaultCipher'], |
|
216 | + ['r', ['cipher' => 'myCipher'], 'legacyCipher', 'defaultCipher', 'fileKey', 'myCipher'], |
|
217 | + ['w', [], 'legacyCipher', 'defaultCipher', '', 'defaultCipher'], |
|
218 | + ['r', [], 'legacyCipher', 'defaultCipher', 'file_key', 'legacyCipher'], |
|
219 | + ]; |
|
220 | + } |
|
221 | + |
|
222 | + |
|
223 | + /** |
|
224 | + * test begin() if decryptAll mode was activated |
|
225 | + */ |
|
226 | + public function testBeginDecryptAll(): void { |
|
227 | + $path = '/user/files/foo.txt'; |
|
228 | + $fileKey = 'fileKey'; |
|
229 | + |
|
230 | + $this->sessionMock->expects($this->once()) |
|
231 | + ->method('decryptAllModeActivated') |
|
232 | + ->willReturn(true); |
|
233 | + $this->keyManagerMock->expects($this->once()) |
|
234 | + ->method('getFileKey') |
|
235 | + ->with($path, 'user', null, true) |
|
236 | + ->willReturn($fileKey); |
|
237 | + |
|
238 | + $this->instance->begin($path, 'user', 'r', [], []); |
|
239 | + |
|
240 | + $this->assertSame($fileKey, |
|
241 | + $this->invokePrivate($this->instance, 'fileKey') |
|
242 | + ); |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * test begin() if encryption is not initialized but the master key is enabled |
|
247 | + * in this case we can initialize the encryption without a username/password |
|
248 | + * and continue |
|
249 | + */ |
|
250 | + public function testBeginInitMasterKey(): void { |
|
251 | + $this->sessionMock->expects($this->once()) |
|
252 | + ->method('decryptAllModeActivated') |
|
253 | + ->willReturn(false); |
|
254 | + |
|
255 | + $this->sessionMock->expects($this->once())->method('isReady')->willReturn(false); |
|
256 | + $this->utilMock->expects($this->once())->method('isMasterKeyEnabled') |
|
257 | + ->willReturn(true); |
|
258 | + $this->keyManagerMock->expects($this->once())->method('init')->with('', ''); |
|
259 | + |
|
260 | + $this->instance->begin('/user/files/welcome.txt', 'user', 'r', [], []); |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * |
|
265 | + * @param string $fileKey |
|
266 | + * @param boolean $expected |
|
267 | + */ |
|
268 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestUpdate')] |
|
269 | + public function testUpdate($fileKey, $expected): void { |
|
270 | + $this->keyManagerMock->expects($this->once()) |
|
271 | + ->method('getFileKey')->willReturn($fileKey); |
|
272 | + |
|
273 | + $this->keyManagerMock->expects($this->any()) |
|
274 | + ->method('getPublicKey')->willReturn('publicKey'); |
|
275 | + |
|
276 | + $this->keyManagerMock->expects($this->any()) |
|
277 | + ->method('addSystemKeys') |
|
278 | + ->willReturnCallback(function ($accessList, $publicKeys) { |
|
279 | + return $publicKeys; |
|
280 | + }); |
|
281 | + |
|
282 | + $this->keyManagerMock->expects($this->never())->method('getVersion'); |
|
283 | + $this->keyManagerMock->expects($this->never())->method('setVersion'); |
|
284 | + |
|
285 | + $this->assertSame($expected, |
|
286 | + $this->instance->update('path', 'user1', ['users' => ['user1']]) |
|
287 | + ); |
|
288 | + } |
|
289 | + |
|
290 | + public static function dataTestUpdate(): array { |
|
291 | + return [ |
|
292 | + ['', false], |
|
293 | + ['fileKey', true] |
|
294 | + ]; |
|
295 | + } |
|
296 | + |
|
297 | + public function testUpdateNoUsers(): void { |
|
298 | + $this->invokePrivate($this->instance, 'rememberVersion', [['path' => 2]]); |
|
299 | + |
|
300 | + $this->keyManagerMock->expects($this->never())->method('getFileKey'); |
|
301 | + $this->keyManagerMock->expects($this->never())->method('getPublicKey'); |
|
302 | + $this->keyManagerMock->expects($this->never())->method('addSystemKeys'); |
|
303 | + $this->keyManagerMock->expects($this->once())->method('setVersion') |
|
304 | + ->willReturnCallback(function ($path, $version, $view): void { |
|
305 | + $this->assertSame('path', $path); |
|
306 | + $this->assertSame(2, $version); |
|
307 | + $this->assertTrue($view instanceof View); |
|
308 | + }); |
|
309 | + $this->instance->update('path', 'user1', []); |
|
310 | + } |
|
311 | + |
|
312 | + /** |
|
313 | + * Test case if the public key is missing. Nextcloud should still encrypt |
|
314 | + * the file for the remaining users |
|
315 | + */ |
|
316 | + public function testUpdateMissingPublicKey(): void { |
|
317 | + $this->keyManagerMock->expects($this->once()) |
|
318 | + ->method('getFileKey')->willReturn('fileKey'); |
|
319 | + |
|
320 | + $this->keyManagerMock->expects($this->any()) |
|
321 | + ->method('getPublicKey')->willReturnCallback( |
|
322 | + function ($user): void { |
|
323 | + throw new PublicKeyMissingException($user); |
|
324 | + } |
|
325 | + ); |
|
326 | + |
|
327 | + $this->keyManagerMock->expects($this->any()) |
|
328 | + ->method('addSystemKeys') |
|
329 | + ->willReturnCallback(function ($accessList, $publicKeys) { |
|
330 | + return $publicKeys; |
|
331 | + }); |
|
332 | + |
|
333 | + $this->cryptMock->expects($this->once())->method('multiKeyEncrypt') |
|
334 | + ->willReturnCallback( |
|
335 | + function ($fileKey, $publicKeys) { |
|
336 | + $this->assertEmpty($publicKeys); |
|
337 | + $this->assertSame('fileKey', $fileKey); |
|
338 | + return []; |
|
339 | + } |
|
340 | + ); |
|
341 | + |
|
342 | + $this->keyManagerMock->expects($this->never())->method('getVersion'); |
|
343 | + $this->keyManagerMock->expects($this->never())->method('setVersion'); |
|
344 | + |
|
345 | + $this->assertTrue( |
|
346 | + $this->instance->update('path', 'user1', ['users' => ['user1']]) |
|
347 | + ); |
|
348 | + } |
|
349 | + |
|
350 | + /** |
|
351 | + * by default the encryption module should encrypt regular files, files in |
|
352 | + * files_versions and files in files_trashbin |
|
353 | + */ |
|
354 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestShouldEncrypt')] |
|
355 | + public function testShouldEncrypt($path, $shouldEncryptHomeStorage, $isHomeStorage, $expected): void { |
|
356 | + $this->utilMock->expects($this->once())->method('shouldEncryptHomeStorage') |
|
357 | + ->willReturn($shouldEncryptHomeStorage); |
|
358 | + |
|
359 | + if ($shouldEncryptHomeStorage === false) { |
|
360 | + $this->storageMock->expects($this->once())->method('instanceOfStorage') |
|
361 | + ->with('\OCP\Files\IHomeStorage')->willReturn($isHomeStorage); |
|
362 | + $this->utilMock->expects($this->once())->method('getStorage')->with($path) |
|
363 | + ->willReturn($this->storageMock); |
|
364 | + } |
|
365 | + |
|
366 | + $this->assertSame($expected, |
|
367 | + $this->instance->shouldEncrypt($path) |
|
368 | + ); |
|
369 | + } |
|
370 | + |
|
371 | + public static function dataTestShouldEncrypt(): array { |
|
372 | + return [ |
|
373 | + ['/user1/files/foo.txt', true, true, true], |
|
374 | + ['/user1/files_versions/foo.txt', true, true, true], |
|
375 | + ['/user1/files_trashbin/foo.txt', true, true, true], |
|
376 | + ['/user1/some_folder/foo.txt', true, true, false], |
|
377 | + ['/user1/foo.txt', true, true, false], |
|
378 | + ['/user1/files', true, true, false], |
|
379 | + ['/user1/files_trashbin', true, true, false], |
|
380 | + ['/user1/files_versions', true, true, false], |
|
381 | + // test if shouldEncryptHomeStorage is set to false |
|
382 | + ['/user1/files/foo.txt', false, true, false], |
|
383 | + ['/user1/files_versions/foo.txt', false, false, true], |
|
384 | + ]; |
|
385 | + } |
|
386 | + |
|
387 | + |
|
388 | + public function testDecrypt(): void { |
|
389 | + $this->expectException(DecryptionFailedException::class); |
|
390 | + $this->expectExceptionMessage('Cannot decrypt this file, probably this is a shared file. Please ask the file owner to reshare the file with you.'); |
|
391 | + |
|
392 | + $this->instance->decrypt('abc'); |
|
393 | + } |
|
394 | + |
|
395 | + public function testPrepareDecryptAll(): void { |
|
396 | + /** @var \Symfony\Component\Console\Input\InputInterface $input */ |
|
397 | + $input = $this->createMock(InputInterface::class); |
|
398 | + /** @var \Symfony\Component\Console\Output\OutputInterface $output */ |
|
399 | + $output = $this->createMock(OutputInterface::class); |
|
400 | + |
|
401 | + $this->decryptAllMock->expects($this->once())->method('prepare') |
|
402 | + ->with($input, $output, 'user'); |
|
403 | + |
|
404 | + $this->instance->prepareDecryptAll($input, $output, 'user'); |
|
405 | + } |
|
406 | 406 | } |
@@ -32,348 +32,348 @@ |
||
32 | 32 | |
33 | 33 | class EncryptAllTest extends TestCase { |
34 | 34 | |
35 | - protected KeyManager&MockObject $keyManager; |
|
36 | - protected Util&MockObject $util; |
|
37 | - protected IUserManager&MockObject $userManager; |
|
38 | - protected Setup&MockObject $setupUser; |
|
39 | - protected View&MockObject $view; |
|
40 | - protected IConfig&MockObject $config; |
|
41 | - protected IMailer&MockObject $mailer; |
|
42 | - protected IL10N&MockObject $l; |
|
43 | - protected IFactory&MockObject $l10nFactory; |
|
44 | - protected \Symfony\Component\Console\Helper\QuestionHelper&MockObject $questionHelper; |
|
45 | - protected \Symfony\Component\Console\Input\InputInterface&MockObject $inputInterface; |
|
46 | - protected \Symfony\Component\Console\Output\OutputInterface&MockObject $outputInterface; |
|
47 | - protected UserInterface&MockObject $userInterface; |
|
48 | - protected ISecureRandom&MockObject $secureRandom; |
|
49 | - |
|
50 | - protected EncryptAll $encryptAll; |
|
51 | - |
|
52 | - protected function setUp(): void { |
|
53 | - parent::setUp(); |
|
54 | - $this->setupUser = $this->getMockBuilder(Setup::class) |
|
55 | - ->disableOriginalConstructor()->getMock(); |
|
56 | - $this->keyManager = $this->getMockBuilder(KeyManager::class) |
|
57 | - ->disableOriginalConstructor()->getMock(); |
|
58 | - $this->util = $this->getMockBuilder(Util::class) |
|
59 | - ->disableOriginalConstructor()->getMock(); |
|
60 | - $this->userManager = $this->getMockBuilder(IUserManager::class) |
|
61 | - ->disableOriginalConstructor()->getMock(); |
|
62 | - $this->view = $this->getMockBuilder(View::class) |
|
63 | - ->disableOriginalConstructor()->getMock(); |
|
64 | - $this->config = $this->getMockBuilder(IConfig::class) |
|
65 | - ->disableOriginalConstructor()->getMock(); |
|
66 | - $this->mailer = $this->getMockBuilder(IMailer::class) |
|
67 | - ->disableOriginalConstructor()->getMock(); |
|
68 | - $this->l10nFactory = $this->createMock(IFactory::class); |
|
69 | - $this->l = $this->getMockBuilder(IL10N::class) |
|
70 | - ->disableOriginalConstructor()->getMock(); |
|
71 | - $this->questionHelper = $this->getMockBuilder(QuestionHelper::class) |
|
72 | - ->disableOriginalConstructor()->getMock(); |
|
73 | - $this->inputInterface = $this->getMockBuilder(InputInterface::class) |
|
74 | - ->disableOriginalConstructor()->getMock(); |
|
75 | - $this->outputInterface = $this->getMockBuilder(OutputInterface::class) |
|
76 | - ->disableOriginalConstructor()->getMock(); |
|
77 | - $this->userInterface = $this->getMockBuilder(UserInterface::class) |
|
78 | - ->disableOriginalConstructor()->getMock(); |
|
79 | - |
|
80 | - /** |
|
81 | - * We need format method to return a string |
|
82 | - * @var OutputFormatterInterface|\PHPUnit\Framework\MockObject\MockObject |
|
83 | - */ |
|
84 | - $outputFormatter = $this->createMock(OutputFormatterInterface::class); |
|
85 | - $outputFormatter->method('isDecorated')->willReturn(false); |
|
86 | - $outputFormatter->method('format')->willReturnArgument(0); |
|
87 | - |
|
88 | - $this->outputInterface->expects($this->any())->method('getFormatter') |
|
89 | - ->willReturn($outputFormatter); |
|
90 | - |
|
91 | - $this->userManager->expects($this->any())->method('getBackends')->willReturn([$this->userInterface]); |
|
92 | - $this->userInterface->expects($this->any())->method('getUsers')->willReturn(['user1', 'user2']); |
|
93 | - |
|
94 | - $this->secureRandom = $this->getMockBuilder(ISecureRandom::class)->disableOriginalConstructor()->getMock(); |
|
95 | - $this->secureRandom->expects($this->any())->method('generate')->willReturn('12345678'); |
|
96 | - |
|
97 | - |
|
98 | - $this->encryptAll = new EncryptAll( |
|
99 | - $this->setupUser, |
|
100 | - $this->userManager, |
|
101 | - $this->view, |
|
102 | - $this->keyManager, |
|
103 | - $this->util, |
|
104 | - $this->config, |
|
105 | - $this->mailer, |
|
106 | - $this->l, |
|
107 | - $this->l10nFactory, |
|
108 | - $this->questionHelper, |
|
109 | - $this->secureRandom |
|
110 | - ); |
|
111 | - } |
|
112 | - |
|
113 | - public function testEncryptAll(): void { |
|
114 | - /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */ |
|
115 | - $encryptAll = $this->getMockBuilder(EncryptAll::class) |
|
116 | - ->setConstructorArgs( |
|
117 | - [ |
|
118 | - $this->setupUser, |
|
119 | - $this->userManager, |
|
120 | - $this->view, |
|
121 | - $this->keyManager, |
|
122 | - $this->util, |
|
123 | - $this->config, |
|
124 | - $this->mailer, |
|
125 | - $this->l, |
|
126 | - $this->l10nFactory, |
|
127 | - $this->questionHelper, |
|
128 | - $this->secureRandom |
|
129 | - ] |
|
130 | - ) |
|
131 | - ->onlyMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords']) |
|
132 | - ->getMock(); |
|
133 | - |
|
134 | - $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false); |
|
135 | - $encryptAll->expects($this->once())->method('createKeyPairs')->with(); |
|
136 | - $encryptAll->expects($this->once())->method('outputPasswords')->with(); |
|
137 | - $encryptAll->expects($this->once())->method('encryptAllUsersFiles')->with(); |
|
138 | - |
|
139 | - $encryptAll->encryptAll($this->inputInterface, $this->outputInterface); |
|
140 | - } |
|
141 | - |
|
142 | - public function testEncryptAllWithMasterKey(): void { |
|
143 | - /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */ |
|
144 | - $encryptAll = $this->getMockBuilder(EncryptAll::class) |
|
145 | - ->setConstructorArgs( |
|
146 | - [ |
|
147 | - $this->setupUser, |
|
148 | - $this->userManager, |
|
149 | - $this->view, |
|
150 | - $this->keyManager, |
|
151 | - $this->util, |
|
152 | - $this->config, |
|
153 | - $this->mailer, |
|
154 | - $this->l, |
|
155 | - $this->l10nFactory, |
|
156 | - $this->questionHelper, |
|
157 | - $this->secureRandom |
|
158 | - ] |
|
159 | - ) |
|
160 | - ->onlyMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords']) |
|
161 | - ->getMock(); |
|
162 | - |
|
163 | - $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(true); |
|
164 | - $encryptAll->expects($this->never())->method('createKeyPairs'); |
|
165 | - $this->keyManager->expects($this->once())->method('validateMasterKey'); |
|
166 | - $encryptAll->expects($this->once())->method('encryptAllUsersFiles')->with(); |
|
167 | - $encryptAll->expects($this->never())->method('outputPasswords'); |
|
168 | - |
|
169 | - $encryptAll->encryptAll($this->inputInterface, $this->outputInterface); |
|
170 | - } |
|
171 | - |
|
172 | - public function testCreateKeyPairs(): void { |
|
173 | - /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */ |
|
174 | - $encryptAll = $this->getMockBuilder(EncryptAll::class) |
|
175 | - ->setConstructorArgs( |
|
176 | - [ |
|
177 | - $this->setupUser, |
|
178 | - $this->userManager, |
|
179 | - $this->view, |
|
180 | - $this->keyManager, |
|
181 | - $this->util, |
|
182 | - $this->config, |
|
183 | - $this->mailer, |
|
184 | - $this->l, |
|
185 | - $this->l10nFactory, |
|
186 | - $this->questionHelper, |
|
187 | - $this->secureRandom |
|
188 | - ] |
|
189 | - ) |
|
190 | - ->onlyMethods(['setupUserFS', 'generateOneTimePassword']) |
|
191 | - ->getMock(); |
|
192 | - |
|
193 | - |
|
194 | - // set protected property $output |
|
195 | - $this->invokePrivate($encryptAll, 'output', [$this->outputInterface]); |
|
196 | - |
|
197 | - $this->keyManager->expects($this->exactly(2))->method('userHasKeys') |
|
198 | - ->willReturnCallback( |
|
199 | - function ($user) { |
|
200 | - if ($user === 'user1') { |
|
201 | - return false; |
|
202 | - } |
|
203 | - return true; |
|
204 | - } |
|
205 | - ); |
|
206 | - |
|
207 | - $encryptAll->expects($this->once())->method('setupUserFS')->with('user1'); |
|
208 | - $encryptAll->expects($this->once())->method('generateOneTimePassword')->with('user1')->willReturn('password'); |
|
209 | - $this->setupUser->expects($this->once())->method('setupUser')->with('user1', 'password'); |
|
210 | - |
|
211 | - $this->invokePrivate($encryptAll, 'createKeyPairs'); |
|
212 | - |
|
213 | - $userPasswords = $this->invokePrivate($encryptAll, 'userPasswords'); |
|
214 | - |
|
215 | - // we only expect the skipped user, because generateOneTimePassword which |
|
216 | - // would set the user with the new password was mocked. |
|
217 | - // This method will be tested separately |
|
218 | - $this->assertSame(1, count($userPasswords)); |
|
219 | - $this->assertSame('', $userPasswords['user2']); |
|
220 | - } |
|
221 | - |
|
222 | - public function testEncryptAllUsersFiles(): void { |
|
223 | - /** @var EncryptAll&MockObject $encryptAll */ |
|
224 | - $encryptAll = $this->getMockBuilder(EncryptAll::class) |
|
225 | - ->setConstructorArgs( |
|
226 | - [ |
|
227 | - $this->setupUser, |
|
228 | - $this->userManager, |
|
229 | - $this->view, |
|
230 | - $this->keyManager, |
|
231 | - $this->util, |
|
232 | - $this->config, |
|
233 | - $this->mailer, |
|
234 | - $this->l, |
|
235 | - $this->l10nFactory, |
|
236 | - $this->questionHelper, |
|
237 | - $this->secureRandom |
|
238 | - ] |
|
239 | - ) |
|
240 | - ->onlyMethods(['encryptUsersFiles']) |
|
241 | - ->getMock(); |
|
242 | - |
|
243 | - $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false); |
|
244 | - |
|
245 | - // set protected property $output |
|
246 | - $this->invokePrivate($encryptAll, 'output', [$this->outputInterface]); |
|
247 | - $this->invokePrivate($encryptAll, 'userPasswords', [['user1' => 'pwd1', 'user2' => 'pwd2']]); |
|
248 | - |
|
249 | - $encryptAllCalls = []; |
|
250 | - $encryptAll->expects($this->exactly(2)) |
|
251 | - ->method('encryptUsersFiles') |
|
252 | - ->willReturnCallback(function ($uid) use (&$encryptAllCalls): void { |
|
253 | - $encryptAllCalls[] = $uid; |
|
254 | - }); |
|
255 | - |
|
256 | - $this->invokePrivate($encryptAll, 'encryptAllUsersFiles'); |
|
257 | - self::assertEquals([ |
|
258 | - 'user1', |
|
259 | - 'user2', |
|
260 | - ], $encryptAllCalls); |
|
261 | - } |
|
262 | - |
|
263 | - public function testEncryptUsersFiles(): void { |
|
264 | - /** @var EncryptAll&MockObject $encryptAll */ |
|
265 | - $encryptAll = $this->getMockBuilder(EncryptAll::class) |
|
266 | - ->setConstructorArgs( |
|
267 | - [ |
|
268 | - $this->setupUser, |
|
269 | - $this->userManager, |
|
270 | - $this->view, |
|
271 | - $this->keyManager, |
|
272 | - $this->util, |
|
273 | - $this->config, |
|
274 | - $this->mailer, |
|
275 | - $this->l, |
|
276 | - $this->l10nFactory, |
|
277 | - $this->questionHelper, |
|
278 | - $this->secureRandom |
|
279 | - ] |
|
280 | - ) |
|
281 | - ->onlyMethods(['encryptFile', 'setupUserFS']) |
|
282 | - ->getMock(); |
|
283 | - |
|
284 | - $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false); |
|
285 | - |
|
286 | - $this->view->expects($this->exactly(2))->method('getDirectoryContent') |
|
287 | - ->willReturnMap([ |
|
288 | - [ |
|
289 | - '/user1/files', |
|
290 | - '', |
|
291 | - null, |
|
292 | - [ |
|
293 | - ['name' => 'foo', 'type' => 'dir'], |
|
294 | - ['name' => 'bar', 'type' => 'file'], |
|
295 | - ], |
|
296 | - ], |
|
297 | - [ |
|
298 | - '/user1/files/foo', |
|
299 | - '', |
|
300 | - null, |
|
301 | - [ |
|
302 | - ['name' => 'subfile', 'type' => 'file'] |
|
303 | - ], |
|
304 | - ], |
|
305 | - ]); |
|
306 | - |
|
307 | - $this->view->expects($this->any())->method('is_dir') |
|
308 | - ->willReturnCallback( |
|
309 | - function ($path) { |
|
310 | - if ($path === '/user1/files/foo') { |
|
311 | - return true; |
|
312 | - } |
|
313 | - return false; |
|
314 | - } |
|
315 | - ); |
|
316 | - |
|
317 | - $encryptAllCalls = []; |
|
318 | - $encryptAll->expects($this->exactly(2)) |
|
319 | - ->method('encryptFile') |
|
320 | - ->willReturnCallback(function (string $path) use (&$encryptAllCalls): void { |
|
321 | - $encryptAllCalls[] = $path; |
|
322 | - }); |
|
323 | - |
|
324 | - $outputFormatter = $this->createMock(OutputFormatterInterface::class); |
|
325 | - $outputFormatter->method('isDecorated')->willReturn(false); |
|
326 | - $this->outputInterface->expects($this->any()) |
|
327 | - ->method('getFormatter') |
|
328 | - ->willReturn($outputFormatter); |
|
329 | - $progressBar = new ProgressBar($this->outputInterface); |
|
330 | - |
|
331 | - $this->invokePrivate($encryptAll, 'encryptUsersFiles', ['user1', $progressBar, '']); |
|
332 | - self::assertEquals([ |
|
333 | - '/user1/files/bar', |
|
334 | - '/user1/files/foo/subfile', |
|
335 | - ], $encryptAllCalls); |
|
336 | - } |
|
337 | - |
|
338 | - public function testGenerateOneTimePassword(): void { |
|
339 | - $password = $this->invokePrivate($this->encryptAll, 'generateOneTimePassword', ['user1']); |
|
340 | - $this->assertTrue(is_string($password)); |
|
341 | - $this->assertSame(8, strlen($password)); |
|
342 | - |
|
343 | - $userPasswords = $this->invokePrivate($this->encryptAll, 'userPasswords'); |
|
344 | - $this->assertSame(1, count($userPasswords)); |
|
345 | - $this->assertSame($password, $userPasswords['user1']); |
|
346 | - } |
|
347 | - |
|
348 | - /** |
|
349 | - * @param $isEncrypted |
|
350 | - */ |
|
351 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataTestEncryptFile')] |
|
352 | - public function testEncryptFile($isEncrypted): void { |
|
353 | - $fileInfo = $this->createMock(FileInfo::class); |
|
354 | - $fileInfo->expects($this->any())->method('isEncrypted') |
|
355 | - ->willReturn($isEncrypted); |
|
356 | - $this->view->expects($this->any())->method('getFileInfo') |
|
357 | - ->willReturn($fileInfo); |
|
358 | - |
|
359 | - |
|
360 | - if ($isEncrypted) { |
|
361 | - $this->view->expects($this->never())->method('copy'); |
|
362 | - $this->view->expects($this->never())->method('rename'); |
|
363 | - } else { |
|
364 | - $this->view->expects($this->once())->method('copy'); |
|
365 | - $this->view->expects($this->once())->method('rename'); |
|
366 | - } |
|
367 | - |
|
368 | - $this->assertTrue( |
|
369 | - $this->invokePrivate($this->encryptAll, 'encryptFile', ['foo.txt']) |
|
370 | - ); |
|
371 | - } |
|
372 | - |
|
373 | - public static function dataTestEncryptFile(): array { |
|
374 | - return [ |
|
375 | - [true], |
|
376 | - [false], |
|
377 | - ]; |
|
378 | - } |
|
35 | + protected KeyManager&MockObject $keyManager; |
|
36 | + protected Util&MockObject $util; |
|
37 | + protected IUserManager&MockObject $userManager; |
|
38 | + protected Setup&MockObject $setupUser; |
|
39 | + protected View&MockObject $view; |
|
40 | + protected IConfig&MockObject $config; |
|
41 | + protected IMailer&MockObject $mailer; |
|
42 | + protected IL10N&MockObject $l; |
|
43 | + protected IFactory&MockObject $l10nFactory; |
|
44 | + protected \Symfony\Component\Console\Helper\QuestionHelper&MockObject $questionHelper; |
|
45 | + protected \Symfony\Component\Console\Input\InputInterface&MockObject $inputInterface; |
|
46 | + protected \Symfony\Component\Console\Output\OutputInterface&MockObject $outputInterface; |
|
47 | + protected UserInterface&MockObject $userInterface; |
|
48 | + protected ISecureRandom&MockObject $secureRandom; |
|
49 | + |
|
50 | + protected EncryptAll $encryptAll; |
|
51 | + |
|
52 | + protected function setUp(): void { |
|
53 | + parent::setUp(); |
|
54 | + $this->setupUser = $this->getMockBuilder(Setup::class) |
|
55 | + ->disableOriginalConstructor()->getMock(); |
|
56 | + $this->keyManager = $this->getMockBuilder(KeyManager::class) |
|
57 | + ->disableOriginalConstructor()->getMock(); |
|
58 | + $this->util = $this->getMockBuilder(Util::class) |
|
59 | + ->disableOriginalConstructor()->getMock(); |
|
60 | + $this->userManager = $this->getMockBuilder(IUserManager::class) |
|
61 | + ->disableOriginalConstructor()->getMock(); |
|
62 | + $this->view = $this->getMockBuilder(View::class) |
|
63 | + ->disableOriginalConstructor()->getMock(); |
|
64 | + $this->config = $this->getMockBuilder(IConfig::class) |
|
65 | + ->disableOriginalConstructor()->getMock(); |
|
66 | + $this->mailer = $this->getMockBuilder(IMailer::class) |
|
67 | + ->disableOriginalConstructor()->getMock(); |
|
68 | + $this->l10nFactory = $this->createMock(IFactory::class); |
|
69 | + $this->l = $this->getMockBuilder(IL10N::class) |
|
70 | + ->disableOriginalConstructor()->getMock(); |
|
71 | + $this->questionHelper = $this->getMockBuilder(QuestionHelper::class) |
|
72 | + ->disableOriginalConstructor()->getMock(); |
|
73 | + $this->inputInterface = $this->getMockBuilder(InputInterface::class) |
|
74 | + ->disableOriginalConstructor()->getMock(); |
|
75 | + $this->outputInterface = $this->getMockBuilder(OutputInterface::class) |
|
76 | + ->disableOriginalConstructor()->getMock(); |
|
77 | + $this->userInterface = $this->getMockBuilder(UserInterface::class) |
|
78 | + ->disableOriginalConstructor()->getMock(); |
|
79 | + |
|
80 | + /** |
|
81 | + * We need format method to return a string |
|
82 | + * @var OutputFormatterInterface|\PHPUnit\Framework\MockObject\MockObject |
|
83 | + */ |
|
84 | + $outputFormatter = $this->createMock(OutputFormatterInterface::class); |
|
85 | + $outputFormatter->method('isDecorated')->willReturn(false); |
|
86 | + $outputFormatter->method('format')->willReturnArgument(0); |
|
87 | + |
|
88 | + $this->outputInterface->expects($this->any())->method('getFormatter') |
|
89 | + ->willReturn($outputFormatter); |
|
90 | + |
|
91 | + $this->userManager->expects($this->any())->method('getBackends')->willReturn([$this->userInterface]); |
|
92 | + $this->userInterface->expects($this->any())->method('getUsers')->willReturn(['user1', 'user2']); |
|
93 | + |
|
94 | + $this->secureRandom = $this->getMockBuilder(ISecureRandom::class)->disableOriginalConstructor()->getMock(); |
|
95 | + $this->secureRandom->expects($this->any())->method('generate')->willReturn('12345678'); |
|
96 | + |
|
97 | + |
|
98 | + $this->encryptAll = new EncryptAll( |
|
99 | + $this->setupUser, |
|
100 | + $this->userManager, |
|
101 | + $this->view, |
|
102 | + $this->keyManager, |
|
103 | + $this->util, |
|
104 | + $this->config, |
|
105 | + $this->mailer, |
|
106 | + $this->l, |
|
107 | + $this->l10nFactory, |
|
108 | + $this->questionHelper, |
|
109 | + $this->secureRandom |
|
110 | + ); |
|
111 | + } |
|
112 | + |
|
113 | + public function testEncryptAll(): void { |
|
114 | + /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */ |
|
115 | + $encryptAll = $this->getMockBuilder(EncryptAll::class) |
|
116 | + ->setConstructorArgs( |
|
117 | + [ |
|
118 | + $this->setupUser, |
|
119 | + $this->userManager, |
|
120 | + $this->view, |
|
121 | + $this->keyManager, |
|
122 | + $this->util, |
|
123 | + $this->config, |
|
124 | + $this->mailer, |
|
125 | + $this->l, |
|
126 | + $this->l10nFactory, |
|
127 | + $this->questionHelper, |
|
128 | + $this->secureRandom |
|
129 | + ] |
|
130 | + ) |
|
131 | + ->onlyMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords']) |
|
132 | + ->getMock(); |
|
133 | + |
|
134 | + $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false); |
|
135 | + $encryptAll->expects($this->once())->method('createKeyPairs')->with(); |
|
136 | + $encryptAll->expects($this->once())->method('outputPasswords')->with(); |
|
137 | + $encryptAll->expects($this->once())->method('encryptAllUsersFiles')->with(); |
|
138 | + |
|
139 | + $encryptAll->encryptAll($this->inputInterface, $this->outputInterface); |
|
140 | + } |
|
141 | + |
|
142 | + public function testEncryptAllWithMasterKey(): void { |
|
143 | + /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */ |
|
144 | + $encryptAll = $this->getMockBuilder(EncryptAll::class) |
|
145 | + ->setConstructorArgs( |
|
146 | + [ |
|
147 | + $this->setupUser, |
|
148 | + $this->userManager, |
|
149 | + $this->view, |
|
150 | + $this->keyManager, |
|
151 | + $this->util, |
|
152 | + $this->config, |
|
153 | + $this->mailer, |
|
154 | + $this->l, |
|
155 | + $this->l10nFactory, |
|
156 | + $this->questionHelper, |
|
157 | + $this->secureRandom |
|
158 | + ] |
|
159 | + ) |
|
160 | + ->onlyMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords']) |
|
161 | + ->getMock(); |
|
162 | + |
|
163 | + $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(true); |
|
164 | + $encryptAll->expects($this->never())->method('createKeyPairs'); |
|
165 | + $this->keyManager->expects($this->once())->method('validateMasterKey'); |
|
166 | + $encryptAll->expects($this->once())->method('encryptAllUsersFiles')->with(); |
|
167 | + $encryptAll->expects($this->never())->method('outputPasswords'); |
|
168 | + |
|
169 | + $encryptAll->encryptAll($this->inputInterface, $this->outputInterface); |
|
170 | + } |
|
171 | + |
|
172 | + public function testCreateKeyPairs(): void { |
|
173 | + /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */ |
|
174 | + $encryptAll = $this->getMockBuilder(EncryptAll::class) |
|
175 | + ->setConstructorArgs( |
|
176 | + [ |
|
177 | + $this->setupUser, |
|
178 | + $this->userManager, |
|
179 | + $this->view, |
|
180 | + $this->keyManager, |
|
181 | + $this->util, |
|
182 | + $this->config, |
|
183 | + $this->mailer, |
|
184 | + $this->l, |
|
185 | + $this->l10nFactory, |
|
186 | + $this->questionHelper, |
|
187 | + $this->secureRandom |
|
188 | + ] |
|
189 | + ) |
|
190 | + ->onlyMethods(['setupUserFS', 'generateOneTimePassword']) |
|
191 | + ->getMock(); |
|
192 | + |
|
193 | + |
|
194 | + // set protected property $output |
|
195 | + $this->invokePrivate($encryptAll, 'output', [$this->outputInterface]); |
|
196 | + |
|
197 | + $this->keyManager->expects($this->exactly(2))->method('userHasKeys') |
|
198 | + ->willReturnCallback( |
|
199 | + function ($user) { |
|
200 | + if ($user === 'user1') { |
|
201 | + return false; |
|
202 | + } |
|
203 | + return true; |
|
204 | + } |
|
205 | + ); |
|
206 | + |
|
207 | + $encryptAll->expects($this->once())->method('setupUserFS')->with('user1'); |
|
208 | + $encryptAll->expects($this->once())->method('generateOneTimePassword')->with('user1')->willReturn('password'); |
|
209 | + $this->setupUser->expects($this->once())->method('setupUser')->with('user1', 'password'); |
|
210 | + |
|
211 | + $this->invokePrivate($encryptAll, 'createKeyPairs'); |
|
212 | + |
|
213 | + $userPasswords = $this->invokePrivate($encryptAll, 'userPasswords'); |
|
214 | + |
|
215 | + // we only expect the skipped user, because generateOneTimePassword which |
|
216 | + // would set the user with the new password was mocked. |
|
217 | + // This method will be tested separately |
|
218 | + $this->assertSame(1, count($userPasswords)); |
|
219 | + $this->assertSame('', $userPasswords['user2']); |
|
220 | + } |
|
221 | + |
|
222 | + public function testEncryptAllUsersFiles(): void { |
|
223 | + /** @var EncryptAll&MockObject $encryptAll */ |
|
224 | + $encryptAll = $this->getMockBuilder(EncryptAll::class) |
|
225 | + ->setConstructorArgs( |
|
226 | + [ |
|
227 | + $this->setupUser, |
|
228 | + $this->userManager, |
|
229 | + $this->view, |
|
230 | + $this->keyManager, |
|
231 | + $this->util, |
|
232 | + $this->config, |
|
233 | + $this->mailer, |
|
234 | + $this->l, |
|
235 | + $this->l10nFactory, |
|
236 | + $this->questionHelper, |
|
237 | + $this->secureRandom |
|
238 | + ] |
|
239 | + ) |
|
240 | + ->onlyMethods(['encryptUsersFiles']) |
|
241 | + ->getMock(); |
|
242 | + |
|
243 | + $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false); |
|
244 | + |
|
245 | + // set protected property $output |
|
246 | + $this->invokePrivate($encryptAll, 'output', [$this->outputInterface]); |
|
247 | + $this->invokePrivate($encryptAll, 'userPasswords', [['user1' => 'pwd1', 'user2' => 'pwd2']]); |
|
248 | + |
|
249 | + $encryptAllCalls = []; |
|
250 | + $encryptAll->expects($this->exactly(2)) |
|
251 | + ->method('encryptUsersFiles') |
|
252 | + ->willReturnCallback(function ($uid) use (&$encryptAllCalls): void { |
|
253 | + $encryptAllCalls[] = $uid; |
|
254 | + }); |
|
255 | + |
|
256 | + $this->invokePrivate($encryptAll, 'encryptAllUsersFiles'); |
|
257 | + self::assertEquals([ |
|
258 | + 'user1', |
|
259 | + 'user2', |
|
260 | + ], $encryptAllCalls); |
|
261 | + } |
|
262 | + |
|
263 | + public function testEncryptUsersFiles(): void { |
|
264 | + /** @var EncryptAll&MockObject $encryptAll */ |
|
265 | + $encryptAll = $this->getMockBuilder(EncryptAll::class) |
|
266 | + ->setConstructorArgs( |
|
267 | + [ |
|
268 | + $this->setupUser, |
|
269 | + $this->userManager, |
|
270 | + $this->view, |
|
271 | + $this->keyManager, |
|
272 | + $this->util, |
|
273 | + $this->config, |
|
274 | + $this->mailer, |
|
275 | + $this->l, |
|
276 | + $this->l10nFactory, |
|
277 | + $this->questionHelper, |
|
278 | + $this->secureRandom |
|
279 | + ] |
|
280 | + ) |
|
281 | + ->onlyMethods(['encryptFile', 'setupUserFS']) |
|
282 | + ->getMock(); |
|
283 | + |
|
284 | + $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false); |
|
285 | + |
|
286 | + $this->view->expects($this->exactly(2))->method('getDirectoryContent') |
|
287 | + ->willReturnMap([ |
|
288 | + [ |
|
289 | + '/user1/files', |
|
290 | + '', |
|
291 | + null, |
|
292 | + [ |
|
293 | + ['name' => 'foo', 'type' => 'dir'], |
|
294 | + ['name' => 'bar', 'type' => 'file'], |
|
295 | + ], |
|
296 | + ], |
|
297 | + [ |
|
298 | + '/user1/files/foo', |
|
299 | + '', |
|
300 | + null, |
|
301 | + [ |
|
302 | + ['name' => 'subfile', 'type' => 'file'] |
|
303 | + ], |
|
304 | + ], |
|
305 | + ]); |
|
306 | + |
|
307 | + $this->view->expects($this->any())->method('is_dir') |
|
308 | + ->willReturnCallback( |
|
309 | + function ($path) { |
|
310 | + if ($path === '/user1/files/foo') { |
|
311 | + return true; |
|
312 | + } |
|
313 | + return false; |
|
314 | + } |
|
315 | + ); |
|
316 | + |
|
317 | + $encryptAllCalls = []; |
|
318 | + $encryptAll->expects($this->exactly(2)) |
|
319 | + ->method('encryptFile') |
|
320 | + ->willReturnCallback(function (string $path) use (&$encryptAllCalls): void { |
|
321 | + $encryptAllCalls[] = $path; |
|
322 | + }); |
|
323 | + |
|
324 | + $outputFormatter = $this->createMock(OutputFormatterInterface::class); |
|
325 | + $outputFormatter->method('isDecorated')->willReturn(false); |
|
326 | + $this->outputInterface->expects($this->any()) |
|
327 | + ->method('getFormatter') |
|
328 | + ->willReturn($outputFormatter); |
|
329 | + $progressBar = new ProgressBar($this->outputInterface); |
|
330 | + |
|
331 | + $this->invokePrivate($encryptAll, 'encryptUsersFiles', ['user1', $progressBar, '']); |
|
332 | + self::assertEquals([ |
|
333 | + '/user1/files/bar', |
|
334 | + '/user1/files/foo/subfile', |
|
335 | + ], $encryptAllCalls); |
|
336 | + } |
|
337 | + |
|
338 | + public function testGenerateOneTimePassword(): void { |
|
339 | + $password = $this->invokePrivate($this->encryptAll, 'generateOneTimePassword', ['user1']); |
|
340 | + $this->assertTrue(is_string($password)); |
|
341 | + $this->assertSame(8, strlen($password)); |
|
342 | + |
|
343 | + $userPasswords = $this->invokePrivate($this->encryptAll, 'userPasswords'); |
|
344 | + $this->assertSame(1, count($userPasswords)); |
|
345 | + $this->assertSame($password, $userPasswords['user1']); |
|
346 | + } |
|
347 | + |
|
348 | + /** |
|
349 | + * @param $isEncrypted |
|
350 | + */ |
|
351 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestEncryptFile')] |
|
352 | + public function testEncryptFile($isEncrypted): void { |
|
353 | + $fileInfo = $this->createMock(FileInfo::class); |
|
354 | + $fileInfo->expects($this->any())->method('isEncrypted') |
|
355 | + ->willReturn($isEncrypted); |
|
356 | + $this->view->expects($this->any())->method('getFileInfo') |
|
357 | + ->willReturn($fileInfo); |
|
358 | + |
|
359 | + |
|
360 | + if ($isEncrypted) { |
|
361 | + $this->view->expects($this->never())->method('copy'); |
|
362 | + $this->view->expects($this->never())->method('rename'); |
|
363 | + } else { |
|
364 | + $this->view->expects($this->once())->method('copy'); |
|
365 | + $this->view->expects($this->once())->method('rename'); |
|
366 | + } |
|
367 | + |
|
368 | + $this->assertTrue( |
|
369 | + $this->invokePrivate($this->encryptAll, 'encryptFile', ['foo.txt']) |
|
370 | + ); |
|
371 | + } |
|
372 | + |
|
373 | + public static function dataTestEncryptFile(): array { |
|
374 | + return [ |
|
375 | + [true], |
|
376 | + [false], |
|
377 | + ]; |
|
378 | + } |
|
379 | 379 | } |
@@ -196,7 +196,7 @@ discard block |
||
196 | 196 | |
197 | 197 | $this->keyManager->expects($this->exactly(2))->method('userHasKeys') |
198 | 198 | ->willReturnCallback( |
199 | - function ($user) { |
|
199 | + function($user) { |
|
200 | 200 | if ($user === 'user1') { |
201 | 201 | return false; |
202 | 202 | } |
@@ -249,7 +249,7 @@ discard block |
||
249 | 249 | $encryptAllCalls = []; |
250 | 250 | $encryptAll->expects($this->exactly(2)) |
251 | 251 | ->method('encryptUsersFiles') |
252 | - ->willReturnCallback(function ($uid) use (&$encryptAllCalls): void { |
|
252 | + ->willReturnCallback(function($uid) use (&$encryptAllCalls): void { |
|
253 | 253 | $encryptAllCalls[] = $uid; |
254 | 254 | }); |
255 | 255 | |
@@ -306,7 +306,7 @@ discard block |
||
306 | 306 | |
307 | 307 | $this->view->expects($this->any())->method('is_dir') |
308 | 308 | ->willReturnCallback( |
309 | - function ($path) { |
|
309 | + function($path) { |
|
310 | 310 | if ($path === '/user1/files/foo') { |
311 | 311 | return true; |
312 | 312 | } |
@@ -317,7 +317,7 @@ discard block |
||
317 | 317 | $encryptAllCalls = []; |
318 | 318 | $encryptAll->expects($this->exactly(2)) |
319 | 319 | ->method('encryptFile') |
320 | - ->willReturnCallback(function (string $path) use (&$encryptAllCalls): void { |
|
320 | + ->willReturnCallback(function(string $path) use (&$encryptAllCalls): void { |
|
321 | 321 | $encryptAllCalls[] = $path; |
322 | 322 | }); |
323 | 323 |