You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

278 lines
9.5 KiB

<?php
namespace Tests\Feature;
use App\Http\Controllers\Traits\FileTrait;
use App\Image\ImageProcessor;
use App\Models\Collection;
use App\Models\File;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\Bootstrap;
use Tests\TestCase;
use Illuminate\Support\Str;
use Tests\Feature\Traits\FileTraits;
class FileStoreTest extends Bootstrap
{
use FileTraits;
public function test_tmp_and_model_id_forbidden()
{
$data = [
"file" => UploadedFile::fake()->image('test.png'),
"alts" => ['1', '2', '3'],
"description" => 'lfjdsklfslfsdlfasdfsfhgsfgsdf',
"public" => 1
];
$collection = Collection::factory()->createQuietly([
'tmp_support' => false
]);
$response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
$response->assertForbidden();
}
public function test_tmp_false_and_collection_is_full_forbidden()
{
$randomCount = rand(2, 10);
$collection = Collection::factory()->createQuietly([
'tmp_support' => false,
'count' => $randomCount
]);
for ($i = $randomCount; $i > 0; $i--) {
$uuid = app()->uuid;
$this->one(File::class, [
'uuid' => $uuid,
'user_id' => 1,
'collection_id' => $collection->id,
'server_path' => '/' . date('y') . '/' . date('m') . '/' . $uuid . '.' . $collection->ext,
]);
}
$data = [
"file" => UploadedFile::fake()->image('test.png'),
"alts" => ['1', '2', '3'],
"description" => 'lfjdsklfslfsdlfasdfsfhgsfgsdf',
"public" => 1
];
$response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
$response->assertForbidden();
}
public function test_file_is_not_isset_forbidden()
{
$collection = Collection::factory()->createQuietly([
'tmp_support' => true
]);
$data = [
"file" => app()->uuid,
"alts" => ['1', '2', '3'],
"description" => 'lfjdsklfslfsdlfasdfsfhgsfgsdf',
"public" => 1
];
$response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
$response->assertForbidden();
}
public function test_user_can_not_access_not_ownered_files()
{
$collection = Collection::factory()->createQuietly();
$uuid = app()->uuid;
$file = File::factory()->createQuietly([
'uuid' => $uuid,
'user_id' => auth()->id() + 1234,
'server_path' => '/' . date('y') . '/' . date('m') . '/',
'collection_id' => $collection->id
]);
$data = [
"file" => $file->uuid,
"public" => 1
];
$response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
$response->assertForbidden();
}
/**
* @dataProvider storeValidationTestProvider
*/
public function test_store_dynamic_validation_unprocessable($collectionFields, $dataFields)
{
$collection = Collection::factory()->createQuietly($collectionFields);
$response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $dataFields);
$response->assertUnprocessable();
}
public function test_store_static_validation_unprocessable()
{
$collection = Collection::factory()->createQuietly([
'alt_required' => false,
'description_required' => true,
'tmp_support' => true,
'max_width' => 2000,
'max_height' => 2000,
'min_width' => 1,
'min_height' => 1,
'min_file_size' => 0
]);
$data = [
"file" => UploadedFile::fake()->image('lol.png'),
"description" => Str::random(1000)
];
$this->modelWithPolicy('collections', ['permission:collections.store'])
->loginAsAdmin()
->postJson(route("api.files.store", ['collection_name' => $collection->name]), $data)
->assertUnprocessable();
}
/**
* @dataProvider storeValidationTestProvider
*/
public function test_store_dynamic_validation_stored_file_unprocessable($collectionFields)
{
$collection = Collection::factory()->createQuietly($collectionFields);
$uuid = app()->uuid;
$file = File::factory()->createQuietly([
'uuid' => $uuid,
'server_path' => '/' . date('y') . '/' . date('m') . '/',
'user_id' => auth()->id(),
'collection_id' => $collection->id
]);
$imageProcessor = new ImageProcessor;
$imageProcessor->createFakeImage(storage_path('stub') . '/image.png', Storage::disk($collection->disk)->path($file->server_path . $uuid . '.' . $collection->ext));
$data = [
"file" => $file->uuid,
"public" => 1
];
$response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
$response->assertUnprocessable();
}
public function test_user_can_store_sent_file_count_multiple_without_image_processor_ok()
{
$collection = Collection::factory()->createQuietly([
'alt_required' => false,
'description_required' => false,
'tmp_support' => true,
'max_width' => 2000,
'max_height' => 2000,
'min_width' => 1,
'min_height' => 1,
'min_file_size' => 0
]);
$uuid = app()->uuid;
$this->one(File::class, [
'uuid' => $uuid,
'user_id' => auth()->id(),
'collection_id' => $collection->id,
'server_path' => '/' . date('y') . '/' . date('m') . '/',
]);
$data = [
"file" => UploadedFile::fake()->image('test.png'),
"public" => 1
];
$response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
$response->assertCreated();
}
public function test_user_can_store_sent_file_uuid_count_multiple_without_image_processor_ok()
{
$collection = Collection::factory()->createQuietly([
'alt_required' => false,
'description_required' => false,
'tmp_support' => true,
'max_width' => 2000,
'max_height' => 2000,
'min_width' => 1,
'min_height' => 1,
'min_file_size' => 0
]);
$uuid = app()->uuid;
$file = File::factory()->createQuietly([
'uuid' => $uuid,
'server_path' => '/' . date('y') . '/' . date('m') . '/',
'user_id' => auth()->id(),
'collection_id' => $collection->id
]);
$imageProcessor = new ImageProcessor;
$imageProcessor->createFakeImage(storage_path('stub') . '/image.png', Storage::disk($collection->disk)->path($file->server_path . $uuid . '.' . $collection->ext));
$data = [
"file" => $file->uuid,
"public" => 1
];
$response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
$response->assertCreated();
}
public function test_user_can_store_sent_file_count_one_without_image_processor_ok()
{
$collection = Collection::factory()->createQuietly([
'alt_required' => false,
'description_required' => false,
'tmp_support' => false,
'max_width' => 2000,
'max_height' => 2000,
'min_width' => 1,
'min_height' => 1,
'min_file_size' => 0,
'count' => 1
]);
$data = [
"file" => UploadedFile::fake()->image('lol.png'),
"public" => 1
];
$response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name, 'model_id' => 100]), $data);
$response->assertCreated();
}
public function test_user_can_store_sent_file_uuid_count_one_without_image_processor_ok()
{
$collection = Collection::factory()->createQuietly([
'alt_required' => false,
'description_required' => false,
'tmp_support' => false,
'max_width' => 2000,
'max_height' => 2000,
'min_width' => 1,
'min_height' => 1,
'min_file_size' => 0,
'count' => 1
]);
$uuid = app()->uuid;
$file = File::factory()->createQuietly([
'uuid' => $uuid,
'server_path' => '/' . date('y') . '/' . date('m') . '/',
'user_id' => auth()->id(),
'collection_id' => $collection->id
]);
$imageProcessor = new ImageProcessor;
$imageProcessor->createFakeImage(storage_path('stub') . '/image.png', Storage::disk($collection->disk)->path($file->server_path . $uuid . '.' . $collection->ext));
$data = [
"file" => $file->uuid,
"public" => 1
];
$response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name, 'model_id' => 100]), $data);
$response->assertCreated();
}
}