-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathInstallModuleCommandTest.php
More file actions
374 lines (313 loc) · 18.2 KB
/
InstallModuleCommandTest.php
File metadata and controls
374 lines (313 loc) · 18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
namespace Tests\Commands;
use GuzzleHttp\Psr7\Response;
use Illuminate\Support\Facades\File;
use Tests\TestCase;
use ZipArchive;
class InstallModuleCommandTest extends TestCase
{
public function test_local_module_install_with_active_theme()
{
$this->usingThemeFolder(function () {
$zip = $this->getModuleZipPath();
$expectedInstallPath = theme_path('modules/test-module');
$this->artisan('bookstack:install-module', ['location' => $zip])
->expectsOutput("\nThis will install a module from: {$zip}\n\nModules can contain code which would have the ability to do anything on the BookStack host server.\nYou should only install modules from trusted sources.")
->expectsConfirmation('Are you sure you want to install this module?', 'yes')
->expectsOutput('Module "Test Module" (v1.0.0) successfully installed!')
->expectsOutput("Install location: {$expectedInstallPath}")
->assertExitCode(0);
$this->assertDirectoryExists($expectedInstallPath);
$this->assertFileExists($expectedInstallPath . '/bookstack-module.json');
});
}
public function test_remote_module_install_with_active_theme()
{
$this->usingThemeFolder(function () {
$zip = $this->getModuleZipPath();
$http = $this->mockHttpClient([
new Response(200, ['Content-Length' => filesize($zip)], file_get_contents($zip))
]);
$expectedInstallPath = theme_path('modules/test-module');
$this->artisan('bookstack:install-module', ['location' => 'https://example.com/test-module.zip'])
->expectsOutput("\nThis will download a module from: example.com\n\nModules can contain code which would have the ability to do anything on the BookStack host server.\nYou should only install modules from trusted sources.")
->expectsConfirmation('Are you sure you trust this source?', 'yes')
->expectsOutput('Module "Test Module" (v1.0.0) successfully installed!')
->expectsOutput("Install location: {$expectedInstallPath}")
->assertExitCode(0);
$this->assertEquals(1, $http->requestCount());
$request = $http->requestAt(0);
$this->assertEquals('/test-module.zip', $request->getUri()->getPath());
$this->assertDirectoryExists($expectedInstallPath);
$this->assertFileExists($expectedInstallPath . '/bookstack-module.json');
});
}
public function test_remote_http_module_warns_and_prompts_users()
{
$this->usingThemeFolder(function () {
$zip = $this->getModuleZipPath();
$http = $this->mockHttpClient([
new Response(200, ['Content-Length' => filesize($zip)], file_get_contents($zip))
]);
$expectedInstallPath = theme_path('modules/test-module');
$this->artisan('bookstack:install-module', ['location' => 'http://example.com/test-module.zip'])
->expectsOutput("\nThis will download a module from: example.com\n\nModules can contain code which would have the ability to do anything on the BookStack host server.\nYou should only install modules from trusted sources.")
->expectsConfirmation('Are you sure you trust this source?', 'yes')
->expectsOutput("You are downloading a module from an insecure HTTP source.\nWe recommend only using HTTPS sources to avoid various security risks.")
->expectsConfirmation('Are you sure you want to continue without HTTPS?', 'yes')
->expectsOutput('Module "Test Module" (v1.0.0) successfully installed!')
->expectsOutput("Install location: {$expectedInstallPath}")
->assertExitCode(0);
$request = $http->requestAt(0);
$this->assertEquals('/test-module.zip', $request->getUri()->getPath());
});
}
public function test_remote_module_install_follows_redirects()
{
$this->usingThemeFolder(function () {
$zip = $this->getModuleZipPath();
$http = $this->mockHttpClient([
new Response(302, ['Location' => 'https://example.com/a-test-module.zip']),
new Response(200, ['Content-Length' => filesize($zip)], file_get_contents($zip))
]);
$this->artisan('bookstack:install-module', ['location' => 'https://example.com/test-module.zip'])
->expectsConfirmation('Are you sure you trust this source?', 'yes')
->assertExitCode(0);
$this->assertEquals(2, $http->requestCount());
$this->assertEquals('/test-module.zip', $http->requestAt(0)->getUri()->getPath());
$this->assertEquals('/a-test-module.zip', $http->requestAt(1)->getUri()->getPath());
});
}
public function test_remote_module_install_prompts_on_following_redirects_to_different_origin()
{
$this->usingThemeFolder(function () {
$zip = $this->getModuleZipPath();
$http = $this->mockHttpClient([
new Response(302, ['Location' => 'http://example.com/a-test-module.zip']),
new Response(301, ['Location' => 'https://a.example.com:8080/a-test-module.zip']),
new Response(200, ['Content-Length' => filesize($zip)], file_get_contents($zip))
]);
$this->artisan('bookstack:install-module', ['location' => 'https://example.com/test-module.zip'])
->expectsConfirmation('Are you sure you trust this source?', 'yes')
->expectsOutput('The download URL is redirecting to a different site: http://example.com')
->expectsConfirmation('Do you trust downloading the module from this site?', 'yes')
->expectsOutput('The download URL is redirecting to a different site: https://a.example.com:8080')
->expectsConfirmation('Do you trust downloading the module from this site?', 'yes')
->assertExitCode(0);
$this->assertEquals(3, $http->requestCount());
$this->assertEquals('https', $http->requestAt(0)->getUri()->getScheme());
$this->assertEquals('http', $http->requestAt(1)->getUri()->getScheme());
$this->assertEquals('a.example.com', $http->requestAt(2)->getUri()->getHost());
});
}
public function test_remote_module_install_redirect_origin_prompt_rejection()
{
$this->usingThemeFolder(function () {
$http = $this->mockHttpClient([
new Response(302, ['Location' => 'http://example.com/a-test-module.zip']),
new Response(301, ['Location' => 'https://a.example.com:8080/a-test-module.zip']),
]);
$this->artisan('bookstack:install-module', ['location' => 'https://example.com/test-module.zip'])
->expectsConfirmation('Are you sure you trust this source?', 'yes')
->expectsOutput('The download URL is redirecting to a different site: http://example.com')
->expectsConfirmation('Do you trust downloading the module from this site?', 'no')
->assertExitCode(1);
$this->assertEquals(1, $http->requestCount());
$this->assertEquals('https', $http->requestAt(0)->getUri()->getScheme());
});
}
public function test_remote_module_install_has_redirect_limit()
{
$this->usingThemeFolder(function () {
$http = $this->mockHttpClient([
new Response(302, ['Location' => 'https://example.com/a-test-module.zip']),
new Response(302, ['Location' => 'https://example.com/b-test-module.zip']),
new Response(302, ['Location' => 'https://example.com/c-test-module.zip']),
new Response(302, ['Location' => 'https://example.com/d-test-module.zip']),
]);
$this->artisan('bookstack:install-module', ['location' => 'https://example.com/test-module.zip'])
->expectsConfirmation('Are you sure you trust this source?', 'yes')
->expectsOutput('ERROR: Failed to download module from https://example.com/test-module.zip')
->assertExitCode(1);
$this->assertEquals(4, $http->requestCount());
$this->assertEquals('/c-test-module.zip', $http->requestAt(3)->getUri()->getPath());
});
}
public function test_remote_module_install_download_failures_are_announced_to_user()
{
$this->usingThemeFolder(function () {
$http = $this->mockHttpClient([
new Response(404),
]);
$this->artisan('bookstack:install-module', ['location' => 'https://example.com/test-module.zip'])
->expectsConfirmation('Are you sure you trust this source?', 'yes')
->expectsOutput('ERROR: Failed to download module from https://example.com/test-module.zip')
->expectsOutput('Download failed with status code 404')
->assertExitCode(1);
$this->assertEquals(1, $http->requestCount());
});
}
public function test_run_with_invalid_path_exits_early()
{
$this->artisan('bookstack:install-module', ['location' => '/not-found.zip'])
->expectsOutput('ERROR: Module file not found at /not-found.zip')
->assertExitCode(1);
}
public function test_run_with_invalid_zip_has_early_exit()
{
$zip = $this->getModuleZipPath();
file_put_contents($zip, 'invalid zip');
$this->artisan('bookstack:install-module', ['location' => $zip])
->expectsConfirmation('Are you sure you want to install this module?', 'yes')
->expectsOutput("ERROR: Cannot open ZIP file at {$zip}")
->assertExitCode(1);
}
public function test_run_with_large_zip_has_early_exit()
{
$zip = $this->getModuleZipPath(null, [
'large-file.txt' => str_repeat('a', 1024 * 1024 * 51)
]);
$this->artisan('bookstack:install-module', ['location' => $zip])
->expectsConfirmation('Are you sure you want to install this module?', 'yes')
->expectsOutput("ERROR: Module ZIP file contents are too large. Maximum size is 50MB")
->assertExitCode(1);
}
public function test_run_with_invalid_module_data_has_early_exit()
{
$zip = $this->getModuleZipPath([
'name' => 'Invalid Module',
'description' => 'A module with invalid data',
'version' => 'dog',
]);
$this->artisan('bookstack:install-module', ['location' => $zip])
->expectsConfirmation('Are you sure you want to install this module?', 'yes')
->expectsOutput("ERROR: Failed to read module metadata with error: Module in folder \"_temp\" has an invalid 'version' format. Expected semantic version format like '1.0.0' or 'v1.0.0'")
->assertExitCode(1);
}
public function test_module_zip_when_files_in_nested_directory()
{
$this->usingThemeFolder(function ($themeFolder) {
$zip = new ZipArchive();
$zipFile = tempnam(sys_get_temp_dir(), 'bs-test-module');
$zip->open($zipFile, ZipArchive::CREATE);
$zip->addEmptyDir('mod');
$zip->addFromString('mod/bookstack-module.json', json_encode($metadata ?? [
'name' => 'Test Module',
'description' => 'A test module for BookStack',
'version' => '1.0.0',
]));
$zip->addFromString('mod/functions.php', '<?php $a = "cat";');
$zip->addEmptyDir('mod/a');
$zip->addFromString('mod/a/cat.txt', 'Meow');
$zip->close();
$this->artisan('bookstack:install-module', ['location' => $zipFile])
->expectsConfirmation('Are you sure you want to install this module?', 'yes')
->assertExitCode(0);
$modulePath = glob(theme_path('modules/*'), GLOB_ONLYDIR)[0];
$this->assertFileExists($modulePath . '/a/cat.txt');
$contents = file_get_contents($modulePath . '/a/cat.txt');
$this->assertEquals('Meow', $contents);
});
}
public function test_local_module_install_without_active_theme_can_setup_theme_folder()
{
$zip = $this->getModuleZipPath();
$expectedThemePath = base_path('themes/custom');
File::deleteDirectory($expectedThemePath);
$this->artisan('bookstack:install-module', ['location' => $zip])
->expectsConfirmation('Are you sure you want to install this module?', 'yes')
->expectsConfirmation('No active theme folder found, would you like to create one?', 'yes')
->expectsOutput("Created theme folder at {$expectedThemePath}")
->expectsOutput("You will need to set APP_THEME=custom in your BookStack env configuration to enable this theme!")
->expectsOutput('Module "Test Module" (v1.0.0) successfully installed!')
->assertExitCode(0);
$this->assertDirectoryExists($expectedThemePath . '/modules/test-module');
File::deleteDirectory($expectedThemePath);
}
public function test_local_module_install_with_active_theme_and_conflicting_modules_file_causes_early_exit()
{
$this->usingThemeFolder(function () {
$zip = $this->getModuleZipPath();
File::put(theme_path('modules'), '{}');
$this->artisan('bookstack:install-module', ['location' => $zip])
->expectsConfirmation('Are you sure you want to install this module?', 'yes')
->expectsOutput("ERROR: Cannot create a modules folder, file already exists at " . theme_path('modules'))
->assertExitCode(1);
});
}
public function test_single_existing_module_with_same_name_replace()
{
$this->usingThemeFolder(function () {
$original = $this->createModuleFolderInCurrentTheme(['name' => 'Test Module', 'description' => 'cat', 'version' => '1.0.0']);
$new = $this->getModuleZipPath(['name' => 'Test Module', 'description' => '', 'version' => '2.0.0']);
$this->artisan('bookstack:install-module', ['location' => $new])
->expectsConfirmation('Are you sure you want to install this module?', 'yes')
->expectsOutput('The following modules already exist with the same name:')
->expectsOutput('Test Module (test-module:v1.0.0) - cat')
->expectsChoice('What would you like to do?', 'Replace existing module', ['Cancel module install', 'Add alongside existing module', 'Replace existing module'])
->expectsOutput("Replacing existing module in test-module folder")
->assertExitCode(0);
$this->assertFileExists($original . '/bookstack-module.json');
$metadata = json_decode(file_get_contents($original . '/bookstack-module.json'), true);
$this->assertEquals('2.0.0', $metadata['version']);
});
}
public function test_single_existing_module_with_same_name_cancel()
{
$this->usingThemeFolder(function () {
$original = $this->createModuleFolderInCurrentTheme(['name' => 'Test Module', 'description' => 'cat', 'version' => '1.0.0']);
$new = $this->getModuleZipPath(['name' => 'Test Module', 'description' => '', 'version' => '2.0.0']);
$this->artisan('bookstack:install-module', ['location' => $new])
->expectsConfirmation('Are you sure you want to install this module?', 'yes')
->expectsOutput('The following modules already exist with the same name:')
->expectsOutput('Test Module (test-module:v1.0.0) - cat')
->expectsChoice('What would you like to do?', 'Cancel module install', ['Cancel module install', 'Add alongside existing module', 'Replace existing module'])
->assertExitCode(1);
$this->assertFileExists($original . '/bookstack-module.json');
$metadata = json_decode(file_get_contents($original . '/bookstack-module.json'), true);
$this->assertEquals('1.0.0', $metadata['version']);
});
}
public function test_single_existing_module_with_same_name_add()
{
$this->usingThemeFolder(function () {
$original = $this->createModuleFolderInCurrentTheme(['name' => 'Test Module', 'description' => 'cat', 'version' => '1.0.0']);
$new = $this->getModuleZipPath(['name' => 'Test Module', 'description' => '', 'version' => '2.0.0']);
$this->artisan('bookstack:install-module', ['location' => $new])
->expectsConfirmation('Are you sure you want to install this module?', 'yes')
->expectsOutput('The following modules already exist with the same name:')
->expectsOutput('Test Module (test-module:v1.0.0) - cat')
->expectsChoice('What would you like to do?', 'Add alongside existing module', ['Cancel module install', 'Add alongside existing module', 'Replace existing module'])
->assertExitCode(0);
$dirs = File::directories(theme_path('modules/'));
$this->assertCount(2, $dirs);
});
}
protected function createModuleFolderInCurrentTheme(array|null $metadata = null, array $extraFiles = []): string
{
$original = $this->getModuleZipPath($metadata, $extraFiles);
$targetPath = theme_path('modules/test-module');
mkdir($targetPath, 0777, true);
$originalZip = new ZipArchive();
$originalZip->open($original);
$originalZip->extractTo($targetPath);
$originalZip->close();
return $targetPath;
}
protected function getModuleZipPath(array|null $metadata = null, array $extraFiles = []): string
{
$zip = new ZipArchive();
$tmpFile = tempnam(sys_get_temp_dir(), 'bs-test-module');
$zip->open($tmpFile, ZipArchive::CREATE);
$zip->addFromString('bookstack-module.json', json_encode($metadata ?? [
'name' => 'Test Module',
'description' => 'A test module for BookStack',
'version' => '1.0.0',
]));
foreach ($extraFiles as $path => $contents) {
$zip->addFromString($path, $contents);
}
$zip->close();
return $tmpFile;
}
}