Skip to content

Commit 0b84bc4

Browse files
committed
Merge Filepond endpoint tests
2 parents 534a247 + 9ca500d commit 0b84bc4

4 files changed

Lines changed: 135 additions & 3 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ composer.phar
33
composer.lock
44
.DS_Store
55
.idea
6+
.codex
67
.phpunit.result.cache
78
.phpunit.cache

‎src/Http/Controllers/FilepondController.php‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,12 @@ private function persistFileIfDone($disk, $basePath, $length, $finalFilePath)
200200
*/
201201
public function delete(Request $request)
202202
{
203-
$filePath = $this->filepond->getPathFromServerId($request->getContent());
203+
try {
204+
$filePath = $this->filepond->getPathFromServerId($request->getContent());
205+
} catch (DecryptException $e) {
206+
abort(400, 'Invalid encryption for id');
207+
}
208+
204209
$folderPath = dirname($filePath);
205210
if (Storage::disk(config('filepond.temporary_files_disk', 'local'))->deleteDirectory($folderPath)) {
206211
return Response::make('', 200, [
@@ -213,4 +218,3 @@ public function delete(Request $request)
213218
]);
214219
}
215220
}
216-

‎tests/Feature/SingleFileUploadTest.php‎

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
class SingleFileUploadTest extends TestCase
1111
{
12-
/** @test */
1312
public function test_normal_file_upload()
1413
{
1514
$tmpPath = config('filepond.temporary_files_path', 'filepond');
@@ -33,4 +32,94 @@ public function test_normal_file_upload()
3332

3433
Storage::disk($diskName)->assertExists($pathFromServerId);
3534
}
35+
36+
public function test_it_initializes_a_chunked_upload()
37+
{
38+
$tmpPath = config('filepond.temporary_files_path', 'filepond');
39+
$diskName = config('filepond.temporary_files_disk', 'local');
40+
41+
Storage::fake($diskName);
42+
43+
$response = $this->postJson('/filepond/api/process', [], [
44+
'Upload-Name' => 'chunked.txt',
45+
]);
46+
47+
$response->assertStatus(200);
48+
49+
/** @var Filepond $filepond */
50+
$filepond = app(Filepond::class);
51+
$pathFromServerId = $filepond->getPathFromServerId($response->content());
52+
53+
$this->assertStringStartsWith($tmpPath . DIRECTORY_SEPARATOR . 'chunked-', $pathFromServerId);
54+
$this->assertStringEndsWith('.txt', $pathFromServerId);
55+
Storage::disk($diskName)->assertExists($pathFromServerId);
56+
}
57+
58+
public function test_it_persists_a_chunked_upload_when_all_chunks_have_arrived()
59+
{
60+
$diskName = config('filepond.temporary_files_disk', 'local');
61+
62+
Storage::fake($diskName);
63+
64+
$initializeResponse = $this->postJson('/filepond/api/process', [], [
65+
'Upload-Name' => 'chunked.txt',
66+
]);
67+
68+
$initializeResponse->assertStatus(200);
69+
$serverId = $initializeResponse->content();
70+
71+
$response = $this->call('PATCH', '/filepond/api', [
72+
'patch' => $serverId,
73+
], [], [], [
74+
'HTTP_UPLOAD_OFFSET' => '0',
75+
'HTTP_UPLOAD_LENGTH' => '11',
76+
], 'hello world');
77+
78+
$response->assertStatus(204);
79+
80+
/** @var Filepond $filepond */
81+
$filepond = app(Filepond::class);
82+
$pathFromServerId = $filepond->getPathFromServerId($serverId);
83+
84+
Storage::disk($diskName)->assertExists($pathFromServerId);
85+
$this->assertSame('hello world', Storage::disk($diskName)->get($pathFromServerId));
86+
Storage::disk($diskName)->assertMissing(config('filepond.chunks_path') . DIRECTORY_SEPARATOR . basename($pathFromServerId));
87+
}
88+
89+
public function test_it_rejects_chunk_uploads_with_invalid_encrypted_ids()
90+
{
91+
$response = $this->call('PATCH', '/filepond/api', [
92+
'patch' => 'not-a-valid-server-id',
93+
], [], [], [
94+
'HTTP_UPLOAD_OFFSET' => '0',
95+
'HTTP_UPLOAD_LENGTH' => '11',
96+
], 'hello world');
97+
98+
$response->assertStatus(400);
99+
}
100+
101+
public function test_it_deletes_an_uploaded_file_folder_from_its_server_id()
102+
{
103+
$diskName = config('filepond.temporary_files_disk', 'local');
104+
$path = config('filepond.temporary_files_path', 'filepond') . DIRECTORY_SEPARATOR . 'delete-me' . DIRECTORY_SEPARATOR . 'test.txt';
105+
106+
Storage::fake($diskName);
107+
Storage::disk($diskName)->put($path, 'delete me');
108+
109+
/** @var Filepond $filepond */
110+
$filepond = app(Filepond::class);
111+
$serverId = $filepond->getServerIdFromPath($path);
112+
113+
$response = $this->call('DELETE', '/filepond/api/process', [], [], [], [], $serverId);
114+
115+
$response->assertStatus(200);
116+
Storage::disk($diskName)->assertMissing($path);
117+
}
118+
119+
public function test_it_rejects_delete_requests_with_invalid_encrypted_ids()
120+
{
121+
$response = $this->call('DELETE', '/filepond/api/process', [], [], [], [], 'not-a-valid-server-id');
122+
123+
$response->assertStatus(400);
124+
}
36125
}

‎tests/Unit/FilepondTest.php‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Sopamo\LaravelFilepond\Tests\Unit;
4+
5+
use Sopamo\LaravelFilepond\Exceptions\InvalidPathException;
6+
use Sopamo\LaravelFilepond\Filepond;
7+
use Sopamo\LaravelFilepond\Tests\TestCase;
8+
9+
class FilepondTest extends TestCase
10+
{
11+
public function test_it_converts_paths_to_server_ids_and_back()
12+
{
13+
$filepond = app(Filepond::class);
14+
$path = 'filepond/uploads/test.txt';
15+
16+
$serverId = $filepond->getServerIdFromPath($path);
17+
18+
$this->assertNotSame($path, $serverId);
19+
$this->assertSame($path, $filepond->getPathFromServerId($serverId));
20+
}
21+
22+
public function test_it_rejects_empty_server_ids()
23+
{
24+
$this->expectException(InvalidPathException::class);
25+
26+
app(Filepond::class)->getPathFromServerId('');
27+
}
28+
29+
public function test_it_rejects_paths_outside_the_temporary_directory()
30+
{
31+
$filepond = app(Filepond::class);
32+
$serverId = $filepond->getServerIdFromPath('other/path/test.txt');
33+
34+
$this->expectException(InvalidPathException::class);
35+
36+
$filepond->getPathFromServerId($serverId);
37+
}
38+
}

0 commit comments

Comments
 (0)