mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-18 08:12:15 +00:00
test folder refactoring
This commit is contained in:
229
tests/Domain/Sharing/UserShareTest.php
Normal file
229
tests/Domain/Sharing/UserShareTest.php
Normal file
@@ -0,0 +1,229 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Share;
|
||||
|
||||
use App\Models\File;
|
||||
use App\Models\Folder;
|
||||
use App\Models\User;
|
||||
use App\Notifications\SharedSendViaEmail;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class UserShareTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_share_single_file_without_password()
|
||||
{
|
||||
$file = File::factory(File::class)
|
||||
->create();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->postJson("/api/share/$file->id", [
|
||||
'isPassword' => false,
|
||||
'permission' => 'editor',
|
||||
'type' => 'file',
|
||||
])->assertStatus(201)->assertJsonFragment([
|
||||
'item_id' => $file->id,
|
||||
'type' => 'file',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('shares', [
|
||||
'user_id' => $user->id,
|
||||
'item_id' => $file->id,
|
||||
'type' => 'file',
|
||||
'is_protected' => false,
|
||||
'password' => null,
|
||||
'expire_in' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_share_folder_without_password()
|
||||
{
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->postJson("/api/share/$folder->id", [
|
||||
'isPassword' => false,
|
||||
'permission' => 'editor',
|
||||
'type' => 'folder',
|
||||
])->assertStatus(201)->assertJsonFragment([
|
||||
'item_id' => $folder->id,
|
||||
'type' => 'folder',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('shares', [
|
||||
'user_id' => $user->id,
|
||||
'item_id' => $folder->id,
|
||||
'type' => 'folder',
|
||||
'is_protected' => false,
|
||||
'password' => null,
|
||||
'expire_in' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_share_folder_with_password()
|
||||
{
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->postJson("/api/share/$folder->id", [
|
||||
'isPassword' => true,
|
||||
'password' => 'secret',
|
||||
'permission' => 'editor',
|
||||
'type' => 'folder',
|
||||
])
|
||||
->assertStatus(201)
|
||||
->assertJsonFragment([
|
||||
'item_id' => $folder->id,
|
||||
'type' => 'folder',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('shares', [
|
||||
'user_id' => $user->id,
|
||||
'item_id' => $folder->id,
|
||||
'type' => 'folder',
|
||||
'is_protected' => true,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseMissing('shares', [
|
||||
'item_id' => $folder->id,
|
||||
'password' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_share_folder_with_expiration_time()
|
||||
{
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->postJson("/api/share/$folder->id", [
|
||||
'isPassword' => false,
|
||||
'permission' => 'editor',
|
||||
'type' => 'folder',
|
||||
'expiration' => 12,
|
||||
])
|
||||
->assertStatus(201)
|
||||
->assertJsonFragment([
|
||||
'item_id' => $folder->id,
|
||||
'expire_in' => 12,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_share_folder_and_send_link_for_multiple_email()
|
||||
{
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->postJson("/api/share/$folder->id", [
|
||||
'isPassword' => false,
|
||||
'permission' => 'editor',
|
||||
'type' => 'folder',
|
||||
'emails' => [
|
||||
'john@doe.com',
|
||||
'jane@doe.com',
|
||||
],
|
||||
])->assertStatus(201);
|
||||
|
||||
Notification::assertTimesSent(2, SharedSendViaEmail::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_send_existing_shared_folder_for_multiple_email_once_again()
|
||||
{
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->postJson("/api/share/$folder->id", [
|
||||
'isPassword' => false,
|
||||
'permission' => 'editor',
|
||||
'type' => 'folder',
|
||||
])->assertStatus(201);
|
||||
|
||||
$this->postJson("/api/share/$folder->id/email", [
|
||||
'emails' => [
|
||||
'john@doe.com',
|
||||
'jane@doe.com',
|
||||
],
|
||||
])->assertStatus(204);
|
||||
|
||||
Notification::assertTimesSent(2, SharedSendViaEmail::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* TODO: pridat test na zmazanie zip
|
||||
*/
|
||||
public function it_revoke_single_share_record()
|
||||
{
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->postJson("/api/share/$folder->id", [
|
||||
'isPassword' => false,
|
||||
'permission' => 'editor',
|
||||
'type' => 'folder',
|
||||
])->assertStatus(201);
|
||||
|
||||
$this->deleteJson("/api/share/revoke", [
|
||||
'tokens' => [
|
||||
$folder->shared->token
|
||||
],
|
||||
])->assertStatus(204);
|
||||
|
||||
$this->assertDatabaseMissing('shares', [
|
||||
'item_id' => $folder->id
|
||||
]);
|
||||
}
|
||||
}
|
||||
273
tests/Domain/Sharing/VisitorAccessToItemsTest.php
Normal file
273
tests/Domain/Sharing/VisitorAccessToItemsTest.php
Normal file
@@ -0,0 +1,273 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Share;
|
||||
|
||||
use App\Models\File;
|
||||
use App\Models\Share;
|
||||
use App\Models\User;
|
||||
use App\Models\Zip;
|
||||
use App\Services\SetupService;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Str;
|
||||
use Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class VisitorAccessToItemsTest extends TestCase
|
||||
{
|
||||
private $setup;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->setup = app()->make(SetupService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_download_file()
|
||||
{
|
||||
$this->setup->create_directories();
|
||||
|
||||
collect([true, false])
|
||||
->each(function ($is_protected) {
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
$document = UploadedFile::fake()
|
||||
->create(Str::random() . '-fake-file.pdf', 1000, 'application/pdf');
|
||||
|
||||
Storage::putFileAs("files/$user->id", $document, $document->name);
|
||||
|
||||
$file = File::factory(File::class)
|
||||
->create([
|
||||
'filesize' => $document->getSize(),
|
||||
'user_id' => $user->id,
|
||||
'basename' => $document->name,
|
||||
'name' => $document->name,
|
||||
]);
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $file->id,
|
||||
'user_id' => $user->id,
|
||||
'type' => 'file',
|
||||
'is_protected' => $is_protected,
|
||||
]);
|
||||
|
||||
if ($is_protected) {
|
||||
|
||||
$cookie = ['share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])];
|
||||
|
||||
$this->withCookies($cookie)
|
||||
->get("/file/$document->name/$share->token")
|
||||
->assertStatus(200);
|
||||
}
|
||||
|
||||
if (!$is_protected) {
|
||||
|
||||
// Get shared file
|
||||
$this->get("/file/$document->name/$share->token")
|
||||
->assertStatus(200);
|
||||
}
|
||||
|
||||
$this->assertDatabaseHas('traffic', [
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @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/browse/file/$share->token")
|
||||
->assertStatus(403);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_shared_image()
|
||||
{
|
||||
$this->setup->create_directories();
|
||||
|
||||
collect([true, false])
|
||||
->each(function ($is_protected) {
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
$thumbnail = UploadedFile::fake()
|
||||
->image(Str::random() . '-fake-image.jpg');
|
||||
|
||||
Storage::putFileAs("files/$user->id", $thumbnail, $thumbnail->name);
|
||||
|
||||
$file = File::factory(File::class)
|
||||
->create([
|
||||
'user_id' => $user->id,
|
||||
'thumbnail' => $thumbnail->name,
|
||||
'basename' => $thumbnail->name,
|
||||
'name' => 'fake-thumbnail.jpg',
|
||||
'type' => 'image',
|
||||
'mimetype' => 'jpg',
|
||||
]);
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $file->id,
|
||||
'user_id' => $user->id,
|
||||
'type' => 'file',
|
||||
'is_protected' => $is_protected,
|
||||
]);
|
||||
|
||||
if ($is_protected) {
|
||||
|
||||
$cookie = [
|
||||
'share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])
|
||||
];
|
||||
|
||||
$this->withCookies($cookie)
|
||||
->get("/share/$share->token")
|
||||
->assertStatus(200);
|
||||
}
|
||||
|
||||
if (!$is_protected) {
|
||||
$this->get("/share/$share->token")
|
||||
->assertStatus(200);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_public_thumbnail()
|
||||
{
|
||||
$this->setup->create_directories();
|
||||
|
||||
collect([true, false])
|
||||
->each(function ($is_protected) {
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
$thumbnail = UploadedFile::fake()
|
||||
->image(Str::random() . '-fake-thumbnail.jpg');
|
||||
|
||||
Storage::putFileAs("files/$user->id", $thumbnail, $thumbnail->name);
|
||||
|
||||
$file = File::factory(File::class)
|
||||
->create([
|
||||
'user_id' => $user->id,
|
||||
'thumbnail' => $thumbnail->name,
|
||||
'name' => 'fake-thumbnail.jpg',
|
||||
]);
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $file->id,
|
||||
'user_id' => $user->id,
|
||||
'type' => 'file',
|
||||
'is_protected' => $is_protected,
|
||||
]);
|
||||
|
||||
// Get thumbnail file
|
||||
if ($is_protected) {
|
||||
|
||||
$cookie = [
|
||||
'share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])
|
||||
];
|
||||
|
||||
$this->withCookies($cookie)
|
||||
->get("/thumbnail/$thumbnail->name/$share->token")
|
||||
->assertStatus(200);
|
||||
}
|
||||
|
||||
if (!$is_protected) {
|
||||
$this->get("/thumbnail/$thumbnail->name/$share->token")
|
||||
->assertStatus(200);
|
||||
}
|
||||
|
||||
$this->assertDatabaseMissing('traffic', [
|
||||
'user_id' => $user->id,
|
||||
'download' => null,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_download_publicly_zipped_files()
|
||||
{
|
||||
$this->setup->create_directories();
|
||||
|
||||
collect([true, false])
|
||||
->each(function ($is_protected) {
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'user_id' => $user->id,
|
||||
'type' => 'folder',
|
||||
'is_protected' => $is_protected,
|
||||
]);
|
||||
|
||||
$zip = Zip::factory(Zip::class)->create([
|
||||
'basename' => 'EHWKcuvKzA4Gv29v-archive.zip',
|
||||
'user_id' => $user->id,
|
||||
'shared_token' => $share->token,
|
||||
]);
|
||||
|
||||
$file = UploadedFile::fake()
|
||||
->create($zip->basename, 1000, 'application/zip');
|
||||
|
||||
Storage::putFileAs("zip", $file, $file->name);
|
||||
|
||||
if ($is_protected) {
|
||||
|
||||
$cookie = [
|
||||
'share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])
|
||||
];
|
||||
|
||||
$this->withCookies($cookie)
|
||||
->get("/zip/$zip->id/$share->token")
|
||||
->assertStatus(200);
|
||||
}
|
||||
|
||||
if (!$is_protected) {
|
||||
$this->get("/zip/$zip->id/$share->token")
|
||||
->assertStatus(200);
|
||||
}
|
||||
|
||||
$this->assertDatabaseMissing('traffic', [
|
||||
'user_id' => $user->id,
|
||||
'download' => null,
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
787
tests/Domain/Sharing/VisitorBrowseTest.php
Normal file
787
tests/Domain/Sharing/VisitorBrowseTest.php
Normal file
@@ -0,0 +1,787 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Share;
|
||||
|
||||
use App\Models\File;
|
||||
use App\Models\Folder;
|
||||
use App\Models\Share;
|
||||
use App\Models\User;
|
||||
use App\Models\Zip;
|
||||
use App\Services\SetupService;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Str;
|
||||
use Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class VisitorBrowseTest extends TestCase
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->setup = app()->make(SetupService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_share_record()
|
||||
{
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'is_protected' => 0,
|
||||
]);
|
||||
|
||||
$this->get("/api/browse/share/$share->token")
|
||||
->assertStatus(200)
|
||||
->assertExactJson([
|
||||
'data' => [
|
||||
'id' => $share->id,
|
||||
'type' => 'shares',
|
||||
'attributes' => [
|
||||
'permission' => $share->permission,
|
||||
'is_protected' => false,
|
||||
'item_id' => $share->item_id,
|
||||
'expire_in' => $share->expire_in,
|
||||
'token' => $share->token,
|
||||
'link' => $share->link,
|
||||
'type' => $share->type,
|
||||
'created_at' => $share->created_at->toJson(),
|
||||
'updated_at' => $share->updated_at->toJson(),
|
||||
],
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_share_page()
|
||||
{
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'type' => 'folder',
|
||||
'is_protected' => false,
|
||||
]);
|
||||
|
||||
$this->get("/share/$share->token")
|
||||
->assertViewIs('index')
|
||||
->assertStatus(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_try_to_get_deleted_share_record()
|
||||
{
|
||||
$this->get("/api/browse/share/19ZMPNiass4ZqWwQ")
|
||||
->assertNotFound();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_try_to_get_deleted_share_page()
|
||||
{
|
||||
$this->get('/share/19ZMPNiass4ZqWwQ')
|
||||
->assertNotFound(404);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_authenticate_protected_file_with_correct_password()
|
||||
{
|
||||
$file = File::factory(File::class)
|
||||
->create();
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $file->id,
|
||||
'user_id' => $file->user_id,
|
||||
'type' => 'file',
|
||||
'is_protected' => true,
|
||||
'password' => bcrypt('secret'),
|
||||
]);
|
||||
|
||||
$this->postJson("/api/browse/authenticate/$share->token", [
|
||||
'password' => 'secret'
|
||||
])
|
||||
->assertStatus(200)
|
||||
->assertCookie('share_session', json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
]), false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_authenticate_protected_file_with_incorrect_password()
|
||||
{
|
||||
$file = File::factory(File::class)
|
||||
->create();
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $file->id,
|
||||
'user_id' => $file->user_id,
|
||||
'type' => 'file',
|
||||
'is_protected' => true,
|
||||
'password' => bcrypt('secret'),
|
||||
]);
|
||||
|
||||
$this->postJson("/api/browse/authenticate/$share->token", [
|
||||
'password' => 'bad-password'
|
||||
])
|
||||
->assertStatus(401)
|
||||
->assertCookieMissing('share_session');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function visitor_zip_shared_multiple_files()
|
||||
{
|
||||
$this->setup->create_directories();
|
||||
|
||||
// check private or public share record
|
||||
collect([true, false])
|
||||
->each(function ($is_protected) {
|
||||
|
||||
$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' => $is_protected,
|
||||
]);
|
||||
|
||||
// Check shared item protected by password
|
||||
if ($is_protected) {
|
||||
|
||||
$cookie = ['share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])];
|
||||
|
||||
$this
|
||||
->withUnencryptedCookies($cookie)
|
||||
->post("/api/zip/files/$share->token", [
|
||||
'items' => File::all()->pluck('id')
|
||||
])->assertStatus(201);
|
||||
}
|
||||
|
||||
// Check public shared item
|
||||
if (!$is_protected) {
|
||||
$this->postJson("/api/zip/files/$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 visitor_try_zip_not_shared_file_with_already_shared_multiple_files()
|
||||
{
|
||||
// check private or public share record
|
||||
collect([true, false])
|
||||
->each(function ($is_protected) {
|
||||
|
||||
$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' => $is_protected,
|
||||
]);
|
||||
|
||||
// Check shared item protected by password
|
||||
if ($is_protected) {
|
||||
|
||||
$cookie = ['share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])];
|
||||
|
||||
$this
|
||||
->withUnencryptedCookies($cookie)
|
||||
->post("/api/zip/files/$share->token", [
|
||||
'items' => File::all()->pluck('id')
|
||||
])->assertStatus(403);
|
||||
}
|
||||
|
||||
// Check public shared item
|
||||
if (!$is_protected) {
|
||||
$this->postJson("/api/zip/files/$share->token", [
|
||||
'items' => File::all()->pluck('id')
|
||||
])->assertStatus(403);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function visitor_zip_shared_folder()
|
||||
{
|
||||
$this->setup->create_directories();
|
||||
|
||||
// check private or public share record
|
||||
collect([true, false])
|
||||
->each(function ($is_protected) {
|
||||
|
||||
$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' => $is_protected,
|
||||
]);
|
||||
|
||||
// Check shared item protected by password
|
||||
if ($is_protected) {
|
||||
|
||||
$cookie = ['share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])];
|
||||
|
||||
$this
|
||||
->withUnencryptedCookies($cookie)
|
||||
->get("/api/zip/folder/$children->id/$share->token")
|
||||
->assertStatus(201);
|
||||
}
|
||||
|
||||
// Check public shared item
|
||||
if (!$is_protected) {
|
||||
$this->getJson("/api/zip/folder/$children->id/$share->token")
|
||||
->assertStatus(201);
|
||||
}
|
||||
|
||||
$this->assertDatabaseHas('zips', [
|
||||
'user_id' => $user->id,
|
||||
'shared_token' => $share->token,
|
||||
]);
|
||||
|
||||
Zip::all()
|
||||
->each(function ($zip) {
|
||||
Storage::assertExists("zip/$zip->basename");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function visitor_try_zip_not_shared_folder()
|
||||
{
|
||||
$this->setup->create_directories();
|
||||
|
||||
// check private or public share record
|
||||
collect([true, false])
|
||||
->each(function ($is_protected) {
|
||||
|
||||
$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' => $is_protected,
|
||||
]);
|
||||
|
||||
// Check shared item protected by password
|
||||
if ($is_protected) {
|
||||
|
||||
$cookie = ['share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])];
|
||||
|
||||
$this
|
||||
->withUnencryptedCookies($cookie)
|
||||
->get("/api/zip/folder/$folder->id/$share->token")
|
||||
->assertStatus(403);
|
||||
}
|
||||
|
||||
// Check public shared item
|
||||
if (!$is_protected) {
|
||||
$this->getJson("/api/zip/folder/$folder->id/$share->token")
|
||||
->assertStatus(403);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function visitor_get_folder_content()
|
||||
{
|
||||
// check private or public share record
|
||||
collect([true, false])
|
||||
->each(function ($is_protected) {
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
$root = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'name' => 'root',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $root->id,
|
||||
'user_id' => $user->id,
|
||||
'type' => 'folder',
|
||||
'is_protected' => $is_protected,
|
||||
'permission' => 'editor',
|
||||
]);
|
||||
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'parent_id' => $root->id,
|
||||
'name' => 'Documents',
|
||||
"author" => "user",
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$file = File::factory(File::class)
|
||||
->create([
|
||||
'folder_id' => $root->id,
|
||||
'name' => 'Document',
|
||||
'basename' => 'document.pdf',
|
||||
"mimetype" => "application/pdf",
|
||||
"author" => "user",
|
||||
"type" => "file",
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$json = [
|
||||
[
|
||||
"id" => $folder->id,
|
||||
"user_id" => $user->id,
|
||||
"parent_id" => $root->id,
|
||||
"name" => "Documents",
|
||||
"color" => null,
|
||||
"emoji" => null,
|
||||
"author" => "user",
|
||||
"deleted_at" => null,
|
||||
"created_at" => $folder->created_at,
|
||||
"updated_at" => $folder->updated_at->toJson(),
|
||||
"items" => 0,
|
||||
"trashed_items" => 0,
|
||||
"type" => "folder",
|
||||
],
|
||||
[
|
||||
"id" => $file->id,
|
||||
"user_id" => $user->id,
|
||||
"folder_id" => $root->id,
|
||||
"thumbnail" => null,
|
||||
"name" => "Document",
|
||||
"basename" => "document.pdf",
|
||||
"mimetype" => "application/pdf",
|
||||
"filesize" => $file->filesize,
|
||||
"type" => "file",
|
||||
"metadata" => null,
|
||||
"author" => "user",
|
||||
"deleted_at" => null,
|
||||
"created_at" => $file->created_at,
|
||||
"updated_at" => $file->updated_at->toJson(),
|
||||
"file_url" => "http://localhost/file/document.pdf/$share->token",
|
||||
]
|
||||
];
|
||||
|
||||
// Check shared item protected by password
|
||||
if ($is_protected) {
|
||||
|
||||
$cookie = ['share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])];
|
||||
|
||||
$this
|
||||
->withUnencryptedCookies($cookie)
|
||||
->get("/api/browse/folders/$root->id/$share->token")
|
||||
->assertStatus(200)
|
||||
->assertExactJson($json);
|
||||
}
|
||||
|
||||
// Check public shared item
|
||||
if (!$is_protected) {
|
||||
$this->getJson("/api/browse/folders/$root->id/$share->token")
|
||||
->assertStatus(200)
|
||||
->assertExactJson($json);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function visitor_get_navigator_tree()
|
||||
{
|
||||
// check private or public share record
|
||||
collect([true, false])
|
||||
->each(function ($is_protected) {
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
$folder_level_1 = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'name' => 'level 1',
|
||||
'author' => 'user',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $folder_level_1->id,
|
||||
'user_id' => $user->id,
|
||||
'type' => 'folder',
|
||||
'permission' => 'editor',
|
||||
'is_protected' => $is_protected,
|
||||
'password' => bcrypt('secret'),
|
||||
]);
|
||||
|
||||
$folder_level_2 = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'name' => 'level 2',
|
||||
'parent_id' => $folder_level_1->id,
|
||||
'author' => 'user',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$folder_level_3 = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'name' => 'level 3',
|
||||
'parent_id' => $folder_level_2->id,
|
||||
'author' => 'user',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$folder_level_2_sibling = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'name' => 'level 2 Sibling',
|
||||
'parent_id' => $folder_level_1->id,
|
||||
'author' => 'user',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$tree = [
|
||||
[
|
||||
'id' => $share->item_id,
|
||||
"name" => "Home",
|
||||
"location" => "public",
|
||||
"folders" => [
|
||||
[
|
||||
"id" => $folder_level_2->id,
|
||||
"parent_id" => $folder_level_1->id,
|
||||
"name" => "level 2",
|
||||
"items" => 1,
|
||||
"trashed_items" => 1,
|
||||
"type" => "folder",
|
||||
"folders" => [
|
||||
[
|
||||
"id" => $folder_level_3->id,
|
||||
"parent_id" => $folder_level_2->id,
|
||||
"name" => "level 3",
|
||||
"items" => 0,
|
||||
"trashed_items" => 0,
|
||||
"type" => "folder",
|
||||
"folders" => [],
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
"id" => $folder_level_2_sibling->id,
|
||||
"parent_id" => $folder_level_1->id,
|
||||
"name" => "level 2 Sibling",
|
||||
"items" => 0,
|
||||
"trashed_items" => 0,
|
||||
"type" => "folder",
|
||||
"folders" => []
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
// Check shared item protected by password
|
||||
if ($is_protected) {
|
||||
|
||||
$cookie = ['share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])];
|
||||
|
||||
$this
|
||||
->withUnencryptedCookies($cookie)
|
||||
->get("/api/browse/navigation/$share->token")
|
||||
->assertStatus(200)
|
||||
->assertExactJson($tree);
|
||||
}
|
||||
|
||||
// Check public shared item
|
||||
if (!$is_protected) {
|
||||
|
||||
$this->getJson("/api/browse/navigation/$share->token")
|
||||
->assertStatus(200)
|
||||
->assertExactJson($tree);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function visitor_search_file()
|
||||
{
|
||||
// check private or public share record
|
||||
collect([true, false])
|
||||
->each(function ($is_protected) {
|
||||
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create();
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $folder->id,
|
||||
'user_id' => $folder->user_id,
|
||||
'type' => 'folder',
|
||||
'permission' => 'editor',
|
||||
'is_protected' => $is_protected,
|
||||
'password' => bcrypt('secret'),
|
||||
]);
|
||||
|
||||
$file = File::factory(File::class)
|
||||
->create([
|
||||
'name' => 'Document',
|
||||
'folder_id' => $folder->id,
|
||||
'user_id' => $folder->user_id,
|
||||
]);
|
||||
|
||||
// Check shared item protected by password
|
||||
if ($is_protected) {
|
||||
|
||||
$cookie = ['share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])];
|
||||
|
||||
$this->withUnencryptedCookies($cookie)
|
||||
->get("/api/browse/search/$share->token?query=doc")
|
||||
->assertStatus(200)
|
||||
->assertJsonFragment([
|
||||
'id' => $file->id
|
||||
]);
|
||||
}
|
||||
|
||||
// Check public shared item
|
||||
if (!$is_protected) {
|
||||
|
||||
$this->getJson("/api/browse/search/$share->token?query=doc")
|
||||
->assertStatus(200)
|
||||
->assertJsonFragment([
|
||||
'id' => $file->id
|
||||
]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function visitor_try_search_not_shared_user_file()
|
||||
{
|
||||
// check private or public share record
|
||||
collect([true, false])
|
||||
->each(function ($is_protected) {
|
||||
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create();
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $folder->id,
|
||||
'user_id' => $folder->user_id,
|
||||
'type' => 'folder',
|
||||
'permission' => 'editor',
|
||||
'is_protected' => $is_protected,
|
||||
'password' => bcrypt('secret'),
|
||||
]);
|
||||
|
||||
File::factory(File::class)
|
||||
->create([
|
||||
'name' => 'Document',
|
||||
'user_id' => $folder->user_id,
|
||||
]);
|
||||
|
||||
// Check shared item protected by password
|
||||
if ($is_protected) {
|
||||
|
||||
$cookie = ['share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])];
|
||||
|
||||
$this->withUnencryptedCookies($cookie)
|
||||
->get("/api/browse/search/$share->token?query=doc")
|
||||
->assertStatus(200)
|
||||
->assertJsonFragment([]);
|
||||
}
|
||||
|
||||
// Check public shared item
|
||||
if (!$is_protected) {
|
||||
|
||||
$this->getJson("/api/browse/search/$share->token?query=doc")
|
||||
->assertStatus(200)
|
||||
->assertJsonFragment([]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function visitor_get_file_detail()
|
||||
{
|
||||
// check private or public share record
|
||||
collect([true, false])
|
||||
->each(function ($is_protected) {
|
||||
|
||||
$file = File::factory(File::class)
|
||||
->create([
|
||||
'name' => 'Document',
|
||||
]);
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $file->id,
|
||||
'user_id' => $file->user_id,
|
||||
'type' => 'file',
|
||||
'permission' => 'editor',
|
||||
'is_protected' => $is_protected,
|
||||
'password' => bcrypt('secret'),
|
||||
]);
|
||||
|
||||
// Check shared item protected by password
|
||||
if ($is_protected) {
|
||||
|
||||
$cookie = ['share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])];
|
||||
|
||||
$this->withUnencryptedCookies($cookie)
|
||||
->get("/api/browse/file/$share->token")
|
||||
->assertStatus(200)
|
||||
->assertJsonFragment([
|
||||
'name' => 'Document'
|
||||
]);
|
||||
}
|
||||
|
||||
// Check public shared item
|
||||
if (!$is_protected) {
|
||||
$this->getJson("/api/browse/file/$share->token")
|
||||
->assertStatus(200)
|
||||
->assertJsonFragment([
|
||||
'name' => 'Document'
|
||||
]);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
535
tests/Domain/Sharing/VisitorManipulatingTest.php
Normal file
535
tests/Domain/Sharing/VisitorManipulatingTest.php
Normal file
@@ -0,0 +1,535 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Share;
|
||||
|
||||
use App\Models\File;
|
||||
use App\Models\Folder;
|
||||
use App\Models\Share;
|
||||
use App\Models\User;
|
||||
use App\Services\SetupService;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class VisitorManipulatingTest extends TestCase
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->setup = app()->make(SetupService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function editor_rename_shared_file()
|
||||
{
|
||||
// check private or public share record
|
||||
collect([true, false])
|
||||
->each(function ($is_protected) {
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'user_id' => $user->id
|
||||
]);
|
||||
|
||||
$file = File::factory(File::class)
|
||||
->create([
|
||||
'folder_id' => $folder->id
|
||||
]);
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $folder->id,
|
||||
'user_id' => $user->id,
|
||||
'type' => 'folder',
|
||||
'is_protected' => $is_protected,
|
||||
'permission' => 'editor',
|
||||
]);
|
||||
|
||||
// Check shared item protected by password
|
||||
if ($is_protected) {
|
||||
|
||||
$cookie = ['share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])];
|
||||
|
||||
$this
|
||||
->withUnencryptedCookies($cookie)
|
||||
->patch("/api/editor/rename/{$file->id}/$share->token", [
|
||||
'name' => 'Renamed Item',
|
||||
'type' => 'file',
|
||||
])
|
||||
->assertStatus(201)
|
||||
->assertJsonFragment([
|
||||
'name' => 'Renamed Item',
|
||||
]);
|
||||
}
|
||||
|
||||
// Check public shared item
|
||||
if (!$is_protected) {
|
||||
$this->patchJson("/api/editor/rename/{$file->id}/$share->token", [
|
||||
'name' => 'Renamed Item',
|
||||
'type' => 'file',
|
||||
])
|
||||
->assertStatus(201)
|
||||
->assertJsonFragment([
|
||||
'name' => 'Renamed Item',
|
||||
]);
|
||||
}
|
||||
|
||||
$this->assertDatabaseHas('files', [
|
||||
'name' => 'Renamed Item',
|
||||
'id' => $file->id,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function editor_rename_shared_folder()
|
||||
{
|
||||
// check private or public share record
|
||||
collect([true, false])
|
||||
->each(function ($is_protected) {
|
||||
|
||||
$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
|
||||
]);
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $root->id,
|
||||
'user_id' => $user->id,
|
||||
'type' => 'folder',
|
||||
'is_protected' => $is_protected,
|
||||
'permission' => 'editor',
|
||||
]);
|
||||
|
||||
// Check shared item protected by password
|
||||
if ($is_protected) {
|
||||
|
||||
$cookie = ['share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])];
|
||||
|
||||
$this
|
||||
->withUnencryptedCookies($cookie)
|
||||
->patch("/api/editor/rename/{$children->id}/$share->token", [
|
||||
'name' => 'Renamed Folder',
|
||||
'type' => 'folder',
|
||||
])
|
||||
->assertStatus(201)
|
||||
->assertJsonFragment([
|
||||
'name' => 'Renamed Folder',
|
||||
]);
|
||||
}
|
||||
|
||||
// Check public shared item
|
||||
if (!$is_protected) {
|
||||
|
||||
$this->patchJson("/api/editor/rename/{$children->id}/$share->token", [
|
||||
'name' => 'Renamed Folder',
|
||||
'type' => 'folder',
|
||||
])
|
||||
->assertStatus(201)
|
||||
->assertJsonFragment([
|
||||
'name' => 'Renamed Folder',
|
||||
]);
|
||||
}
|
||||
|
||||
$this->assertDatabaseHas('folders', [
|
||||
'name' => 'Renamed Folder',
|
||||
'id' => $children->id
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function editor_create_new_folder_in_shared_folder()
|
||||
{
|
||||
// check private or public share record
|
||||
collect([true, false])
|
||||
->each(function ($is_protected) {
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $folder->id,
|
||||
'user_id' => $user->id,
|
||||
'type' => 'folder',
|
||||
'is_protected' => $is_protected,
|
||||
'permission' => 'editor',
|
||||
]);
|
||||
|
||||
// Check shared item protected by password
|
||||
if ($is_protected) {
|
||||
|
||||
$cookie = ['share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])];
|
||||
|
||||
$this
|
||||
->withUnencryptedCookies($cookie)
|
||||
->post("/api/editor/create-folder/$share->token", [
|
||||
'name' => 'Awesome New Folder',
|
||||
'parent_id' => $folder->id,
|
||||
])
|
||||
->assertStatus(201)
|
||||
->assertJsonFragment([
|
||||
'name' => 'Awesome New Folder',
|
||||
]);
|
||||
}
|
||||
|
||||
// Check public shared item
|
||||
if (!$is_protected) {
|
||||
|
||||
$this->postJson("/api/editor/create-folder/$share->token", [
|
||||
'name' => 'Awesome New Folder',
|
||||
'parent_id' => $folder->id,
|
||||
])
|
||||
->assertStatus(201)
|
||||
->assertJsonFragment([
|
||||
'name' => 'Awesome New Folder',
|
||||
]);
|
||||
}
|
||||
|
||||
$this->assertDatabaseHas('folders', [
|
||||
'name' => 'Awesome New Folder',
|
||||
'parent_id' => $folder->id,
|
||||
'author' => 'visitor',
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function editor_delete_multiple_files_in_shared_folder()
|
||||
{
|
||||
// check private or public share record
|
||||
collect([true, false])
|
||||
->each(function ($is_protected) {
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $folder->id,
|
||||
'user_id' => $user->id,
|
||||
'type' => 'folder',
|
||||
'is_protected' => $is_protected,
|
||||
'permission' => 'editor',
|
||||
]);
|
||||
|
||||
$files = File::factory(File::class)
|
||||
->count(2)
|
||||
->create([
|
||||
'folder_id' => $folder->id
|
||||
]);
|
||||
|
||||
$payload = [
|
||||
'items' => [
|
||||
[
|
||||
'id' => $files[0]->id,
|
||||
'type' => 'file',
|
||||
'force_delete' => false,
|
||||
],
|
||||
[
|
||||
'id' => $files[1]->id,
|
||||
'type' => 'file',
|
||||
'force_delete' => false,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
// Check shared item protected by password
|
||||
if ($is_protected) {
|
||||
|
||||
$cookie = ['share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])];
|
||||
|
||||
$this
|
||||
->withUnencryptedCookies($cookie)
|
||||
->post("/api/editor/remove/$share->token", $payload)
|
||||
->assertStatus(204);
|
||||
}
|
||||
|
||||
// Check public shared item
|
||||
if (!$is_protected) {
|
||||
|
||||
$this->postJson("/api/editor/remove/$share->token", $payload)
|
||||
->assertStatus(204);
|
||||
}
|
||||
|
||||
$files
|
||||
->each(function ($file) {
|
||||
$this->assertSoftDeleted('files', [
|
||||
'id' => $file->id,
|
||||
]);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function editor_upload_file_into_shared_folder()
|
||||
{
|
||||
$this->setup->create_directories();
|
||||
|
||||
// check private or public share record
|
||||
collect([true, false])
|
||||
->each(function ($is_protected) {
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'user_id' => $user->id,
|
||||
'author' => 'user',
|
||||
]);
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $folder->id,
|
||||
'user_id' => $user->id,
|
||||
'type' => 'folder',
|
||||
'is_protected' => $is_protected,
|
||||
'permission' => 'editor',
|
||||
]);
|
||||
|
||||
$file = UploadedFile::fake()
|
||||
->create('fake-file.pdf', 1000, 'application/pdf');
|
||||
|
||||
// Check shared item protected by password
|
||||
if ($is_protected) {
|
||||
|
||||
$cookie = ['share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])];
|
||||
|
||||
$this
|
||||
->withUnencryptedCookies($cookie)
|
||||
->post("/api/editor/upload/$share->token", [
|
||||
'filename' => $file->name,
|
||||
'file' => $file,
|
||||
'folder_id' => $folder->id,
|
||||
'is_last' => true,
|
||||
])->assertStatus(201);
|
||||
}
|
||||
|
||||
// Check public shared item
|
||||
if (!$is_protected) {
|
||||
|
||||
$this->postJson("/api/editor/upload/$share->token", [
|
||||
'filename' => $file->name,
|
||||
'file' => $file,
|
||||
'folder_id' => $folder->id,
|
||||
'is_last' => true,
|
||||
])->assertStatus(201);
|
||||
}
|
||||
|
||||
$this->assertDatabaseHas('traffic', [
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('files', [
|
||||
'author' => 'visitor',
|
||||
]);
|
||||
|
||||
Storage::disk('local')
|
||||
->assertExists(
|
||||
"files/$user->id/fake-file.pdf"
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function editor_move_file_to_another_folder()
|
||||
{
|
||||
// check private or public share record
|
||||
collect([true, false])
|
||||
->each(function ($is_protected) {
|
||||
|
||||
$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,
|
||||
]);
|
||||
|
||||
$file = File::factory(File::class)
|
||||
->create([
|
||||
'user_id' => $user->id,
|
||||
'folder_id' => $root->id
|
||||
]);
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $root->id,
|
||||
'user_id' => $user->id,
|
||||
'type' => 'folder',
|
||||
'is_protected' => $is_protected,
|
||||
'permission' => 'editor',
|
||||
]);
|
||||
|
||||
$payload = [
|
||||
'to_id' => $children->id,
|
||||
'items' => [
|
||||
[
|
||||
'type' => 'file',
|
||||
'id' => $file->id,
|
||||
]
|
||||
],
|
||||
];
|
||||
|
||||
// Check shared item protected by password
|
||||
if ($is_protected) {
|
||||
|
||||
$cookie = ['share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])];
|
||||
|
||||
$this
|
||||
->withUnencryptedCookies($cookie)
|
||||
->post("/api/editor/move/$share->token", $payload)
|
||||
->assertStatus(204);
|
||||
}
|
||||
|
||||
// Check public shared item
|
||||
if (!$is_protected) {
|
||||
|
||||
$this->postJson("/api/editor/move/$share->token", $payload)
|
||||
->assertStatus(204);
|
||||
}
|
||||
|
||||
$this->assertDatabaseHas('files', [
|
||||
'id' => $file->id,
|
||||
'folder_id' => $children->id,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function editor_move_folder_to_another_folder()
|
||||
{
|
||||
// check private or public share record
|
||||
collect([true, false])
|
||||
->each(function ($is_protected) {
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
$root = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'user_id' => $user->id
|
||||
]);
|
||||
|
||||
$brother = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'user_id' => $user->id,
|
||||
'parent_id' => $root->id,
|
||||
]);
|
||||
|
||||
$sister = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'user_id' => $user->id,
|
||||
'parent_id' => $root->id,
|
||||
]);
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $root->id,
|
||||
'user_id' => $user->id,
|
||||
'type' => 'folder',
|
||||
'is_protected' => $is_protected,
|
||||
'permission' => 'editor',
|
||||
]);
|
||||
|
||||
$payload = [
|
||||
'to_id' => $brother->id,
|
||||
'items' => [
|
||||
[
|
||||
'type' => 'folder',
|
||||
'id' => $sister->id,
|
||||
]
|
||||
],
|
||||
];
|
||||
|
||||
// Check shared item protected by password
|
||||
if ($is_protected) {
|
||||
|
||||
$cookie = ['share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])];
|
||||
|
||||
$this
|
||||
->withUnencryptedCookies($cookie)
|
||||
->post("/api/editor/move/$share->token", $payload)
|
||||
->assertStatus(204);
|
||||
}
|
||||
|
||||
// Check public shared item
|
||||
if (!$is_protected) {
|
||||
|
||||
$this->postJson("/api/editor/move/$share->token", $payload)
|
||||
->assertStatus(204);
|
||||
}
|
||||
|
||||
$this->assertDatabaseHas('folders', [
|
||||
'id' => $sister->id,
|
||||
'parent_id' => $brother->id,
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user