mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-18 00:02:15 +00:00
- public sharing refactored part 1
This commit is contained in:
@@ -8,7 +8,7 @@ use App\Services\SetupService;
|
||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PrivateShareContentAccessTest extends TestCase
|
||||
class PrivateFilesAccessTest extends TestCase
|
||||
{
|
||||
use DatabaseMigrations;
|
||||
|
||||
106
tests/Feature/Share/PrivateVisitorTest.php
Normal file
106
tests/Feature/Share/PrivateVisitorTest.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?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\Foundation\Testing\DatabaseMigrations;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PrivateVisitorTest extends TestCase
|
||||
{
|
||||
use DatabaseMigrations;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->setup = app()->make(SetupService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function authenticated_visitor_get_folder_content()
|
||||
{
|
||||
$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' => true,
|
||||
'permission' => 'editor',
|
||||
]);
|
||||
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'parent_id' => $root->id,
|
||||
'name' => 'Documents',
|
||||
"user_scope" => "master",
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$file = File::factory(File::class)
|
||||
->create([
|
||||
'folder_id' => $root->id,
|
||||
'name' => 'Document',
|
||||
'basename' => 'document.pdf',
|
||||
"mimetype" => "application/pdf",
|
||||
"user_scope" => "master",
|
||||
"type" => "file",
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$this->withUnencryptedCookie('share_session', json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
]))
|
||||
->get("/api/browse/folders/$root->id/private/$share->token")
|
||||
->assertStatus(200)
|
||||
->assertExactJson([
|
||||
[
|
||||
"id" => $folder->id,
|
||||
"user_id" => $user->id,
|
||||
"parent_id" => $root->id,
|
||||
"name" => "Documents",
|
||||
"color" => null,
|
||||
"emoji" => null,
|
||||
"user_scope" => "master",
|
||||
"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,
|
||||
"user_scope" => "master",
|
||||
"deleted_at" => null,
|
||||
"created_at" => $file->created_at,
|
||||
"updated_at" => $file->updated_at->toJson(),
|
||||
"file_url" => "http://localhost/file/document.pdf/private/$share->token",
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
254
tests/Feature/Share/PublicFilesAccessTest.php
Normal file
254
tests/Feature/Share/PublicFilesAccessTest.php
Normal file
@@ -0,0 +1,254 @@
|
||||
<?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\Foundation\Testing\DatabaseMigrations;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Cookie;
|
||||
use Illuminate\Support\Str;
|
||||
use Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PublicFilesAccessTest extends TestCase
|
||||
{
|
||||
use DatabaseMigrations;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->setup = app()->make(SetupService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_public_file_record_and_download_file_within()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
collect(['private', 'public'])
|
||||
->each(function ($permission) {
|
||||
|
||||
$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' => 'fake-file.pdf',
|
||||
]);
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $file->id,
|
||||
'user_id' => $user->id,
|
||||
'type' => 'file',
|
||||
'is_protected' => $permission === 'private',
|
||||
'password' => \Hash::make('secret'),
|
||||
]);
|
||||
|
||||
if ($permission === 'private') {
|
||||
|
||||
$cookie = ['share_session' => json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
])];
|
||||
|
||||
$this->disableCookieEncryption();
|
||||
$this->defaultCookies = $cookie;
|
||||
|
||||
$this->get("/api/browse/file/$share->token/private")
|
||||
->assertStatus(200)
|
||||
->assertJsonFragment([
|
||||
'basename' => $document->name
|
||||
]);
|
||||
|
||||
$this->get("/file/$document->name/private/$share->token")
|
||||
->assertStatus(200);
|
||||
}
|
||||
|
||||
if ($permission === 'public') {
|
||||
|
||||
$this->get("/api/browse/file/$share->token/public")
|
||||
->assertStatus(200)
|
||||
->assertJsonFragment([
|
||||
'basename' => $document->name
|
||||
]);
|
||||
|
||||
// Get shared file
|
||||
$this->get("/file/$document->name/public/$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/public")
|
||||
->assertStatus(403);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_shared_image()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$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' => false,
|
||||
]);
|
||||
|
||||
$this->get("/shared/$share->token")
|
||||
->assertStatus(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_public_thumbnail()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
collect(['private', 'public'])
|
||||
->each(function ($permission) {
|
||||
|
||||
$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' => $permission === 'private',
|
||||
'password' => \Hash::make('secret'),
|
||||
]);
|
||||
|
||||
// Get thumbnail file
|
||||
if ($permission === 'private') {
|
||||
$this->withCookie('share_session', json_encode([
|
||||
'token' => $share->token,
|
||||
'authenticated' => true,
|
||||
]))
|
||||
->get("/thumbnail/$thumbnail->name/private/$share->token")
|
||||
->assertStatus(200);
|
||||
}
|
||||
|
||||
if ($permission === 'public') {
|
||||
$this->get("/thumbnail/$thumbnail->name/public/$share->token")
|
||||
->assertStatus(200);
|
||||
}
|
||||
|
||||
$this->assertDatabaseMissing('traffic', [
|
||||
'user_id' => $user->id,
|
||||
'download' => null,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_download_publicly_zipped_files()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'user_id' => $user->id,
|
||||
'type' => 'folder',
|
||||
'is_protected' => false,
|
||||
]);
|
||||
|
||||
$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);
|
||||
|
||||
$this->get("/zip/$zip->id/public/$share->token")
|
||||
->assertStatus(200);
|
||||
|
||||
$this->assertDatabaseMissing('traffic', [
|
||||
'user_id' => $user->id,
|
||||
'download' => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -1,211 +0,0 @@
|
||||
<?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\Foundation\Testing\DatabaseMigrations;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Str;
|
||||
use Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PublicShareContentAccessTest extends TestCase
|
||||
{
|
||||
use DatabaseMigrations;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->setup = app()->make(SetupService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_public_file_record_and_download_file_within()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$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' => 'fake-file.pdf',
|
||||
]);
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $file->id,
|
||||
'user_id' => $user->id,
|
||||
'type' => 'file',
|
||||
'is_protected' => false,
|
||||
]);
|
||||
|
||||
// Get share record
|
||||
$this->get("/api/browse/files/$share->token/public")
|
||||
->assertStatus(200)
|
||||
->assertJsonFragment([
|
||||
'basename' => $document->name
|
||||
]);
|
||||
|
||||
// Get shared file
|
||||
$this->get("/file/$document->name/public/$share->token")
|
||||
->assertStatus(200);
|
||||
|
||||
$this->assertDatabaseHas('traffic', [
|
||||
'user_id' => $user->id,
|
||||
'download' => '1024000',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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/files/$share->token/public")
|
||||
->assertStatus(403);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_shared_image()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$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' => false,
|
||||
]);
|
||||
|
||||
$this->get("/shared/$share->token")
|
||||
->assertStatus(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_download_public_thumbnail()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$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' => false,
|
||||
]);
|
||||
|
||||
// Get thumbnail file
|
||||
$this->get("/thumbnail/$thumbnail->name/public/$share->token")
|
||||
->assertStatus(200);
|
||||
|
||||
$this->assertDatabaseMissing('traffic', [
|
||||
'user_id' => $user->id,
|
||||
'download' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_download_publicly_zipped_files()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'user_id' => $user->id,
|
||||
'type' => 'folder',
|
||||
'is_protected' => false,
|
||||
]);
|
||||
|
||||
$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);
|
||||
|
||||
$this->get("/zip/$zip->id/public/$share->token")
|
||||
->assertStatus(200);
|
||||
|
||||
$this->assertDatabaseMissing('traffic', [
|
||||
'user_id' => $user->id,
|
||||
'download' => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ use Laravel\Sanctum\Sanctum;
|
||||
use Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ShareEditorTest extends TestCase
|
||||
class PrivateVisitorTest extends TestCase
|
||||
{
|
||||
use DatabaseMigrations;
|
||||
|
||||
@@ -775,7 +775,7 @@ class ShareEditorTest extends TestCase
|
||||
]);
|
||||
|
||||
|
||||
$this->getJson("/api/browse/files/$share->token/public")
|
||||
$this->getJson("/api/browse/file/$share->token/public")
|
||||
->assertStatus(200)
|
||||
->assertJsonFragment([
|
||||
'name' => 'Document'
|
||||
Reference in New Issue
Block a user