From 8a4ffd03c68ad281ce6240435f4723594ed48bce Mon Sep 17 00:00:00 2001 From: Peter Papp Date: Wed, 10 Mar 2021 10:38:41 +0100 Subject: [PATCH] added shareEditor test --- .../FileFunctions/EditItemsController.php | 3 +- routes/api.php | 2 +- tests/Feature/{ => Accounts}/AdminTest.php | 0 tests/Feature/{ => Accounts}/AuthTest.php | 0 .../{ => Accounts}/UserAccountTest.php | 0 .../{ => External}/SubscriptionTest.php | 0 .../Feature/{ => FileManager}/BrowseTest.php | 0 .../{ => FileManager}/ContentAccessTest.php | 0 tests/Feature/{ => FileManager}/FileTest.php | 0 .../Feature/{ => FileManager}/FolderTest.php | 0 tests/Feature/{ => FileManager}/TrashTest.php | 0 .../Feature/{ => Setup}/SetupServiceTest.php | 0 tests/Feature/{ => Setup}/SetupWizardTest.php | 0 .../{ => Share}/ShareContentAccessTest.php | 117 +++------- tests/Feature/Share/ShareEditorTest.php | 206 ++++++++++++++++++ tests/Feature/{ => Share}/ShareTest.php | 0 tests/Feature/{ => System}/SchedulerTest.php | 0 17 files changed, 233 insertions(+), 95 deletions(-) rename tests/Feature/{ => Accounts}/AdminTest.php (100%) rename tests/Feature/{ => Accounts}/AuthTest.php (100%) rename tests/Feature/{ => Accounts}/UserAccountTest.php (100%) rename tests/Feature/{ => External}/SubscriptionTest.php (100%) rename tests/Feature/{ => FileManager}/BrowseTest.php (100%) rename tests/Feature/{ => FileManager}/ContentAccessTest.php (100%) rename tests/Feature/{ => FileManager}/FileTest.php (100%) rename tests/Feature/{ => FileManager}/FolderTest.php (100%) rename tests/Feature/{ => FileManager}/TrashTest.php (100%) rename tests/Feature/{ => Setup}/SetupServiceTest.php (100%) rename tests/Feature/{ => Setup}/SetupWizardTest.php (100%) rename tests/Feature/{ => Share}/ShareContentAccessTest.php (62%) create mode 100644 tests/Feature/Share/ShareEditorTest.php rename tests/Feature/{ => Share}/ShareTest.php (100%) rename tests/Feature/{ => System}/SchedulerTest.php (100%) diff --git a/app/Http/Controllers/FileFunctions/EditItemsController.php b/app/Http/Controllers/FileFunctions/EditItemsController.php index 7f9848e5..097fd3e9 100644 --- a/app/Http/Controllers/FileFunctions/EditItemsController.php +++ b/app/Http/Controllers/FileFunctions/EditItemsController.php @@ -388,7 +388,6 @@ class EditItemsController extends Controller $folder = Folder::whereUserId($shared->user_id) ->where('id', $id); - if (! $folder->exists()) { abort(404, 'Requested folder doesn\'t exists.'); } @@ -402,7 +401,7 @@ class EditItemsController extends Controller 'token' => $shared->token, ]), 'name' => $zip->basename, - ], 200); + ], 201); } /** diff --git a/routes/api.php b/routes/api.php index 9fb9b983..95af0489 100644 --- a/routes/api.php +++ b/routes/api.php @@ -13,7 +13,7 @@ use App\Http\Controllers\Sharing\FileSharingController; // Edit Functions Route::patch('/rename-item/{unique_id}/public/{token}', [EditItemsController::class, 'guest_rename_item']); -Route::get('/zip-folder/{unique_id}/public/{token}', [EditItemsController::class, 'guest_zip_folder']); +Route::get('/zip-folder/{id}/public/{token}', [EditItemsController::class, 'guest_zip_folder']); Route::post('/create-folder/public/{token}', [EditItemsController::class, 'guest_create_folder']); Route::post('/remove-item/public/{token}', [EditItemsController::class, 'guest_delete_item']); Route::post('/zip/public/{token}', [EditItemsController::class, 'guest_zip_multiple_files']); diff --git a/tests/Feature/AdminTest.php b/tests/Feature/Accounts/AdminTest.php similarity index 100% rename from tests/Feature/AdminTest.php rename to tests/Feature/Accounts/AdminTest.php diff --git a/tests/Feature/AuthTest.php b/tests/Feature/Accounts/AuthTest.php similarity index 100% rename from tests/Feature/AuthTest.php rename to tests/Feature/Accounts/AuthTest.php diff --git a/tests/Feature/UserAccountTest.php b/tests/Feature/Accounts/UserAccountTest.php similarity index 100% rename from tests/Feature/UserAccountTest.php rename to tests/Feature/Accounts/UserAccountTest.php diff --git a/tests/Feature/SubscriptionTest.php b/tests/Feature/External/SubscriptionTest.php similarity index 100% rename from tests/Feature/SubscriptionTest.php rename to tests/Feature/External/SubscriptionTest.php diff --git a/tests/Feature/BrowseTest.php b/tests/Feature/FileManager/BrowseTest.php similarity index 100% rename from tests/Feature/BrowseTest.php rename to tests/Feature/FileManager/BrowseTest.php diff --git a/tests/Feature/ContentAccessTest.php b/tests/Feature/FileManager/ContentAccessTest.php similarity index 100% rename from tests/Feature/ContentAccessTest.php rename to tests/Feature/FileManager/ContentAccessTest.php diff --git a/tests/Feature/FileTest.php b/tests/Feature/FileManager/FileTest.php similarity index 100% rename from tests/Feature/FileTest.php rename to tests/Feature/FileManager/FileTest.php diff --git a/tests/Feature/FolderTest.php b/tests/Feature/FileManager/FolderTest.php similarity index 100% rename from tests/Feature/FolderTest.php rename to tests/Feature/FileManager/FolderTest.php diff --git a/tests/Feature/TrashTest.php b/tests/Feature/FileManager/TrashTest.php similarity index 100% rename from tests/Feature/TrashTest.php rename to tests/Feature/FileManager/TrashTest.php diff --git a/tests/Feature/SetupServiceTest.php b/tests/Feature/Setup/SetupServiceTest.php similarity index 100% rename from tests/Feature/SetupServiceTest.php rename to tests/Feature/Setup/SetupServiceTest.php diff --git a/tests/Feature/SetupWizardTest.php b/tests/Feature/Setup/SetupWizardTest.php similarity index 100% rename from tests/Feature/SetupWizardTest.php rename to tests/Feature/Setup/SetupWizardTest.php diff --git a/tests/Feature/ShareContentAccessTest.php b/tests/Feature/Share/ShareContentAccessTest.php similarity index 62% rename from tests/Feature/ShareContentAccessTest.php rename to tests/Feature/Share/ShareContentAccessTest.php index 72362477..541dfe17 100644 --- a/tests/Feature/ShareContentAccessTest.php +++ b/tests/Feature/Share/ShareContentAccessTest.php @@ -3,17 +3,13 @@ namespace Tests\Feature; use App\Models\File; -use App\Models\Folder; use App\Models\Share; -use App\Models\Traffic; use App\Models\User; use App\Models\Zip; use App\Services\SetupService; -use Carbon\Carbon; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Http\UploadedFile; use Illuminate\Support\Str; -use Laravel\Sanctum\Sanctum; use Storage; use Tests\TestCase; @@ -30,7 +26,7 @@ class ShareContentAccessTest extends TestCase /** * @test */ - public function it_get_public_file_record_and_download_them() + public function it_get_public_file_record_and_download_file_within() { Storage::fake('local'); @@ -80,7 +76,23 @@ class ShareContentAccessTest extends TestCase /** * @test */ - public function it_get_public_thumbnail() + public function it_try_to_get_protected_file_record() + { + $share = Share::factory(Share::class) + ->create([ + 'type' => 'file', + 'is_protected' => true, + ]); + + // Get share record + $this->get("/api/files/$share->token/public") + ->assertStatus(403); + } + + /** + * @test + */ + public function it_download_public_thumbnail() { Storage::fake('local'); @@ -109,7 +121,7 @@ class ShareContentAccessTest extends TestCase 'is_protected' => false, ]); - // Get shared file + // Get thumbnail file $this->get("/thumbnail/$thumbnail->name/public/$share->token") ->assertStatus(200); @@ -122,23 +134,7 @@ class ShareContentAccessTest extends TestCase /** * @test */ - public function it_try_to_get_protected_file_record() - { - $share = Share::factory(Share::class) - ->create([ - 'type' => 'file', - 'is_protected' => true, - ]); - - // Get share record - $this->get("/api/files/$share->token/public") - ->assertStatus(403); - } - - /** - * @test - */ - public function it_zip_shared_multiple_files_and_download_it() + public function it_download_publicly_zipped_files() { Storage::fake('local'); @@ -147,49 +143,23 @@ class ShareContentAccessTest extends TestCase $user = User::factory(User::class) ->create(); - $folder = Folder::factory(Folder::class) - ->create([ - 'user_id' => $user->id - ]); - - collect([0, 1]) - ->each(function ($index) use ($folder, $user) { - - $file = UploadedFile::fake() - ->create(Str::random() . "-fake-file-$index.pdf", 1000, 'application/pdf'); - - Storage::putFileAs("files/$user->id", $file, $file->name); - - File::factory(File::class) - ->create([ - 'filesize' => $file->getSize(), - 'folder_id' => $folder->id, - 'user_id' => $user->id, - 'basename' => $file->name, - 'name' => "fake-file-$index.pdf", - ]); - }); - $share = Share::factory(Share::class) ->create([ - 'item_id' => $folder->id, 'user_id' => $user->id, 'type' => 'folder', 'is_protected' => false, ]); - $this->postJson("/api/zip/public/$share->token", [ - 'items' => File::all()->pluck('id') - ])->assertStatus(201); - - $this->assertDatabaseHas('zips', [ + $zip = Zip::factory(Zip::class)->create([ + 'basename' => 'EHWKcuvKzA4Gv29v-archive.zip', 'user_id' => $user->id, 'shared_token' => $share->token, ]); - $zip = Zip::first(); + $file = UploadedFile::fake() + ->create($zip->basename, 1000, 'application/zip'); - Storage::assertExists("zip/$zip->basename"); + Storage::putFileAs("zip", $file, $file->name); $this->get("/zip/$zip->id/public/$share->token") ->assertStatus(200); @@ -199,41 +169,4 @@ class ShareContentAccessTest extends TestCase 'download' => null, ]); } - - /** - * @test - */ - public function it_try_zip_non_shared_file_with_shared_multiple_files_and_download_it() - { - $user = User::factory(User::class) - ->create(); - - $folder = Folder::factory(Folder::class) - ->create([ - 'user_id' => $user->id - ]); - - File::factory(File::class) - ->create([ - 'folder_id' => $folder->id, - 'user_id' => $user->id, - ]); - - File::factory(File::class) - ->create([ - 'user_id' => $user->id, - ]); - - $share = Share::factory(Share::class) - ->create([ - 'item_id' => $folder->id, - 'user_id' => $user->id, - 'type' => 'folder', - 'is_protected' => false, - ]); - - $this->postJson("/api/zip/public/$share->token", [ - 'items' => File::all()->pluck('id') - ])->assertStatus(403); - } } diff --git a/tests/Feature/Share/ShareEditorTest.php b/tests/Feature/Share/ShareEditorTest.php new file mode 100644 index 00000000..d635523c --- /dev/null +++ b/tests/Feature/Share/ShareEditorTest.php @@ -0,0 +1,206 @@ +setup = app()->make(SetupService::class); + } + + /** + * @test + */ + public function it_zip_shared_multiple_files() + { + Storage::fake('local'); + + $this->setup->create_directories(); + + $user = User::factory(User::class) + ->create(); + + $folder = Folder::factory(Folder::class) + ->create([ + 'user_id' => $user->id + ]); + + collect([0, 1]) + ->each(function ($index) use ($folder, $user) { + + $file = UploadedFile::fake() + ->create(Str::random() . "-fake-file-$index.pdf", 1000, 'application/pdf'); + + Storage::putFileAs("files/$user->id", $file, $file->name); + + File::factory(File::class) + ->create([ + 'filesize' => $file->getSize(), + 'folder_id' => $folder->id, + 'user_id' => $user->id, + 'basename' => $file->name, + 'name' => "fake-file-$index.pdf", + ]); + }); + + $share = Share::factory(Share::class) + ->create([ + 'item_id' => $folder->id, + 'user_id' => $user->id, + 'type' => 'folder', + 'is_protected' => false, + ]); + + $this->postJson("/api/zip/public/$share->token", [ + 'items' => File::all()->pluck('id') + ])->assertStatus(201); + + $this->assertDatabaseHas('zips', [ + 'user_id' => $user->id, + 'shared_token' => $share->token, + ]); + + Storage::assertExists("zip/" . Zip::first()->basename); + } + + /** + * @test + */ + public function it_try_zip_non_shared_file_with_already_shared_multiple_files() + { + $user = User::factory(User::class) + ->create(); + + $folder = Folder::factory(Folder::class) + ->create([ + 'user_id' => $user->id + ]); + + File::factory(File::class) + ->create([ + 'folder_id' => $folder->id, + 'user_id' => $user->id, + ]); + + File::factory(File::class) + ->create([ + 'user_id' => $user->id, + ]); + + $share = Share::factory(Share::class) + ->create([ + 'item_id' => $folder->id, + 'user_id' => $user->id, + 'type' => 'folder', + 'is_protected' => false, + ]); + + $this->postJson("/api/zip/public/$share->token", [ + 'items' => File::all()->pluck('id') + ])->assertStatus(403); + } + + /** + * @test + */ + public function it_zip_shared_folder() + { + Storage::fake('local'); + + $this->setup->create_directories(); + + $user = User::factory(User::class) + ->create(); + + $root = Folder::factory(Folder::class) + ->create([ + 'user_id' => $user->id + ]); + + $children = Folder::factory(Folder::class) + ->create([ + 'user_id' => $user->id, + 'parent_id' => $root->id + ]); + + collect([0, 1]) + ->each(function ($index) use ($children, $user) { + + $file = UploadedFile::fake() + ->create(Str::random() . "-fake-file-$index.pdf", 1000, 'application/pdf'); + + Storage::putFileAs("files/$user->id", $file, $file->name); + + File::factory(File::class) + ->create([ + 'filesize' => $file->getSize(), + 'folder_id' => $children->id, + 'user_id' => $user->id, + 'basename' => $file->name, + 'name' => "fake-file-$index.pdf", + ]); + }); + + $share = Share::factory(Share::class) + ->create([ + 'item_id' => $children->id, + 'user_id' => $user->id, + 'type' => 'folder', + 'is_protected' => false, + ]); + + $this->getJson("/api/zip-folder/$children->id/public/$share->token") + ->assertStatus(201); + + $this->assertDatabaseHas('zips', [ + 'user_id' => $user->id, + 'shared_token' => $share->token, + ]); + + Storage::assertExists("zip/" . Zip::first()->basename); + } + + /** + * @test + */ + public function it_try_zip_non_shared_folder() + { + Storage::fake('local'); + + $this->setup->create_directories(); + + $user = User::factory(User::class) + ->create(); + + $folder = Folder::factory(Folder::class) + ->create([ + 'user_id' => $user->id + ]); + + $share = Share::factory(Share::class) + ->create([ + 'user_id' => $user->id, + 'type' => 'folder', + 'is_protected' => false, + ]); + + $this->getJson("/api/zip-folder/$folder->id/public/$share->token") + ->assertStatus(403); + } +} diff --git a/tests/Feature/ShareTest.php b/tests/Feature/Share/ShareTest.php similarity index 100% rename from tests/Feature/ShareTest.php rename to tests/Feature/Share/ShareTest.php diff --git a/tests/Feature/SchedulerTest.php b/tests/Feature/System/SchedulerTest.php similarity index 100% rename from tests/Feature/SchedulerTest.php rename to tests/Feature/System/SchedulerTest.php