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.
 
 
 
 

206 lines
6.8 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();
$file = File::factory()->createQuietly([
'user_id' => auth()->id() + 1234,
'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([
'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)
{
$collectionFields['withImage'] = true;
$file = $this->one(File::class, dependencyAttributes: $collectionFields);
$collection = Collection::find($file->collection_id);
$data = [
"file" => $file->uuid,
];
$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()
{
$collectionFields['withImage'] = true;
$file = $this->one(File::class, dependencyAttributes: $collectionFields);
$collection = Collection::find($file->collection_id);
$data = [
"file" => UploadedFile::fake()->image('test.png'),
];
$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()
{
$collectionFields['withImage'] = true;
$file = $this->one(File::class, dependencyAttributes: $collectionFields);
$collection = Collection::find($file->collection_id);
$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([
'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()
{
$collectionFields['withImage'] = true;
$collectionFields['count'] = 1;
$file = $this->one(File::class, dependencyAttributes: $collectionFields);
$collection = Collection::find($file->collection_id);
$data = [
"file" => $file->uuid,
"public" => 1
];
$response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name, 'model_id' => 100]), $data);
$response->assertCreated();
}
}