-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathApplicationTest.php
More file actions
206 lines (176 loc) · 7.43 KB
/
Copy pathApplicationTest.php
File metadata and controls
206 lines (176 loc) · 7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Files_Sharing\Tests;
use OCA\Files_Sharing\AppInfo\Application;
use OCA\Files_Sharing\Listener\BeforeDirectFileDownloadListener;
use OCA\Files_Sharing\Listener\BeforeZipCreatedListener;
use OCA\Files_Sharing\SharedStorage;
use OCP\Files\Events\BeforeDirectFileDownloadEvent;
use OCP\Files\Events\BeforeZipCreatedEvent;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Storage\IStorage;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Share\IAttributes;
use OCP\Share\IShare;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class ApplicationTest extends TestCase {
private Application $application;
private IUserSession&MockObject $userSession;
private IRootFolder&MockObject $rootFolder;
protected function setUp(): void {
parent::setUp();
$this->application = new Application([]);
$this->userSession = $this->createMock(IUserSession::class);
$this->rootFolder = $this->createMock(IRootFolder::class);
}
public static function providesDataForCanGet(): array {
return [
'normal file (sender)' => [
'/bar.txt', IStorage::class, false, null, true
],
'shared file (receiver) with attribute secure-view-enabled set false' => [
'/share-bar.txt', SharedStorage::class, true, true, true
],
'shared file (receiver) with attribute secure-view-enabled set true' => [
'/secure-share-bar.txt', SharedStorage::class, true, false, false
],
];
}
#[DataProvider(methodName: 'providesDataForCanGet')]
public function testCheckDirectCanBeDownloaded(
string $path,
string $storageClass,
bool $instanceOfSharedStorage,
?bool $storageShareDownloadPermission,
bool $canDownloadDirectly,
): void {
$fileStorage = $this->createMock($storageClass);
$fileStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn($instanceOfSharedStorage);
if ($storageShareDownloadPermission !== null) {
$fileShareAttributes = $this->createMock(IAttributes::class);
$fileShareAttributes->method('getAttribute')->with('permissions', 'download')->willReturn($storageShareDownloadPermission);
$fileShare = $this->createMock(IShare::class);
$fileShare->method('getAttributes')->willReturn($fileShareAttributes);
$fileStorage->method('getShare')->willReturn($fileShare);
}
$file = $this->createMock(File::class);
$file->method('getStorage')->willReturn($fileStorage);
$userFolder = $this->createMock(Folder::class);
$userFolder->method('get')->willReturn($file);
$user = $this->createMock(IUser::class);
$user->method('getUID')->willReturn('test');
$this->userSession->method('getUser')->willReturn($user);
$this->userSession->method('isLoggedIn')->willReturn(true);
$this->rootFolder->method('getUserFolder')->willReturn($userFolder);
// Simulate direct download of file
$event = new BeforeDirectFileDownloadEvent($path);
$listener = new BeforeDirectFileDownloadListener(
$this->userSession,
$this->rootFolder
);
$listener->handle($event);
$this->assertEquals($canDownloadDirectly, $event->isSuccessful());
}
public static function providesDataForCanZip(): array {
return [
'can download zipped 2 non-shared files inside non-shared folder' => [
'/folder', ['bar1.txt', 'bar2.txt'], 'nonSharedStorage', ['nonSharedStorage','nonSharedStorage'], true
],
'can download zipped non-shared folder' => [
'/', ['folder'], 'nonSharedStorage', ['nonSharedStorage','nonSharedStorage'], true
],
'cannot download zipped 1 non-shared file and 1 secure-shared inside non-shared folder' => [
'/folder', ['secured-bar1.txt', 'bar2.txt'], 'nonSharedStorage', ['nonSharedStorage','secureSharedStorage'], false,
],
'cannot download zipped secure-shared folder' => [
'/', ['secured-folder'], 'secureSharedStorage', [], false,
],
];
}
#[DataProvider(methodName: 'providesDataForCanZip')]
public function testCheckZipCanBeDownloaded(
string $dir,
array $files,
string $folderStorage,
array $directoryListing,
bool $downloadSuccessful,
): void {
// Mock: Normal file/folder storage
$nonSharedStorage = $this->createMock(IStorage::class);
$nonSharedStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(false);
// Mock: Secure-view file/folder shared storage
$secureReceiverFileShareAttributes = $this->createMock(IAttributes::class);
$secureReceiverFileShareAttributes->method('getAttribute')->with('permissions', 'download')->willReturn(false);
$secureReceiverFileShare = $this->createMock(IShare::class);
$secureReceiverFileShare->method('getAttributes')->willReturn($secureReceiverFileShareAttributes);
$secureSharedStorage = $this->createMock(SharedStorage::class);
$secureSharedStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true);
$secureSharedStorage->method('getShare')->willReturn($secureReceiverFileShare);
$folder = $this->createMock(Folder::class);
if ($folderStorage === 'nonSharedStorage') {
$folder->method('getStorage')->willReturn($nonSharedStorage);
} elseif ($folderStorage === 'secureSharedStorage') {
$folder->method('getStorage')->willReturn($secureSharedStorage);
} else {
throw new \Exception('Unknown storage ' . $folderStorage);
}
if (count($directoryListing) > 0) {
$directoryListing = array_map(
function (string $fileStorage) use ($nonSharedStorage, $secureSharedStorage) {
$file = $this->createMock(File::class);
if ($fileStorage === 'nonSharedStorage') {
$file->method('getStorage')->willReturn($nonSharedStorage);
} elseif ($fileStorage === 'secureSharedStorage') {
$file->method('getStorage')->willReturn($secureSharedStorage);
} else {
throw new \Exception('Unknown storage ' . $fileStorage);
}
return $file;
},
$directoryListing
);
$folder->method('getDirectoryListing')->willReturn($directoryListing);
}
$rootFolder = $this->createMock(Folder::class);
$rootFolder->method('getStorage')->willReturn($nonSharedStorage);
$rootFolder->method('getDirectoryListing')->willReturn([$folder]);
$userFolder = $this->createMock(Folder::class);
$userFolder->method('get')->willReturn($rootFolder);
$user = $this->createMock(IUser::class);
$user->method('getUID')->willReturn('test');
$this->userSession->method('getUser')->willReturn($user);
$this->userSession->method('isLoggedIn')->willReturn(true);
$this->rootFolder->method('getUserFolder')->with('test')->willReturn($userFolder);
// Simulate zip download of folder folder
$event = new BeforeZipCreatedEvent($dir, $files);
$listener = new BeforeZipCreatedListener(
$this->userSession,
$this->rootFolder
);
$listener->handle($event);
$this->assertEquals($downloadSuccessful, $event->isSuccessful());
$this->assertEquals($downloadSuccessful, $event->getErrorMessage() === null);
}
public function testCheckFileUserNotFound(): void {
$this->userSession->method('isLoggedIn')->willReturn(false);
// Simulate zip download of folder folder
$event = new BeforeZipCreatedEvent('/test', ['test.txt']);
$listener = new BeforeZipCreatedListener(
$this->userSession,
$this->rootFolder
);
$listener->handle($event);
// It should run as this would restrict e.g. share links otherwise
$this->assertTrue($event->isSuccessful());
$this->assertEquals(null, $event->getErrorMessage());
}
}