Skip to content

Commit

Permalink
Test downloading a private file
Browse files Browse the repository at this point in the history
  • Loading branch information
unclexo committed Dec 27, 2022
1 parent fe489a5 commit 15eb9df
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
24 changes: 24 additions & 0 deletions app/Http/Controllers/MediaUploaderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,28 @@ public function resize()

return ['path' => $path];
}

/**
* Not this method is coupled with download() method
* because it needs a file for downloading
*
* @return array
*/
public function uploadPrivate()
{
$privateFilename = 'private-file.pdf';

$path = \request()->file('file')->storeAs('private', $privateFilename, 'local');

return ['path' => $path];
}

public function download($filename)
{
// Retrieve the private file. Based on user subscription or role, for example.

$privateFilepath = Storage::disk('local')->path('private/private-file.pdf');

return response()->download($privateFilepath, $filename);
}
}
4 changes: 4 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@
Route::post('/upload/multiple-files', 'uploadMultipleFiles')->name('upload.multiple');

Route::post('/upload/resize', 'resize')->name('upload.resize');

Route::post('/upload/private', 'uploadPrivate')->name('upload.private');

Route::get('/upload/download/{filename}', 'download')->name('upload.download');
});
});
21 changes: 21 additions & 0 deletions tests/Feature/UploadModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,25 @@ public function it_can_resize_uploaded_image()
$this->assertEquals(300, $image->width());
$this->assertEquals(200, $image->height());
}

/** @test */
public function it_can_download_a_private_file()
{
$this->actingAs(User::factory()->create());

Storage::fake('local');

$response1 = $this->post(route('upload.private'), [
'file' => $file = UploadedFile::fake()->create('private-file.pdf', 1024, 'application/pdf'),
]);

Storage::disk('local')->assertExists($response1->json('path'));

// This name will determine the filename that is seen by the user downloading the file
$filename = 'download-private-file.pdf';

$response2 = $this->get(route('upload.download', $filename));
$response2->assertStatus(200);
$response2->assertDownload($filename);
}
}

0 comments on commit 15eb9df

Please sign in to comment.