added shareEditor test

This commit is contained in:
Peter Papp
2021-03-10 10:38:41 +01:00
parent 8d85aad601
commit 8a4ffd03c6
17 changed files with 233 additions and 95 deletions
@@ -0,0 +1,172 @@
<?php
namespace Tests\Feature;
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 ShareContentAccessTest 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/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/files/$share->token/public")
->assertStatus(403);
}
/**
* @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,
]);
}
}
+206
View File
@@ -0,0 +1,206 @@
<?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 Illuminate\Foundation\Testing\DatabaseMigrations;
use App\Services\SetupService;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Str;
use Storage;
use Tests\TestCase;
class ShareEditorTest extends TestCase
{
use DatabaseMigrations;
public function __construct()
{
parent::__construct();
$this->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);
}
}
+304
View File
@@ -0,0 +1,304 @@
<?php
namespace Tests\Feature;
use App\Models\File;
use App\Models\Folder;
use App\Models\Share;
use App\Models\User;
use App\Notifications\SharedSendViaEmail;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Notification;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class ShareTest extends TestCase
{
use DatabaseMigrations;
/**
* @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_for_multiple_email()
{
Notification::fake();
$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()
{
Notification::fake();
$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_sharing()
{
$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
]);
}
/**
* @test
*/
public function it_get_shared_record()
{
$share = Share::factory(Share::class)
->create([
'is_protected' => 0,
]);
$this->get("/api/shared/$share->token")
->assertStatus(200)
->assertExactJson([
'data' => [
'id' => $share->id,
'type' => 'shares',
'attributes' => [
'permission' => $share->permission,
'is_protected' => '0',
'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_deleted_shared_record()
{
$this->get("/api/shared/19ZMPNiass4ZqWwQ")
->assertNotFound();
}
/**
* @test
*/
public function it_get_shared_page()
{
$share = Share::factory(Share::class)
->create([
'type' => 'file',
'is_protected' => false,
]);
$this->get("/shared/$share->token")
->assertViewIs('index')
->assertStatus(200);
}
/**
* @test
*/
public function it_get_deleted_shared_page()
{
$this->get('/shared/19ZMPNiass4ZqWwQ')
->assertNotFound();
}
}