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

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Http\Controllers\Traits\FileTrait;
  4. use App\Image\ImageProcessor;
  5. use App\Models\Collection;
  6. use App\Models\File;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. use Illuminate\Foundation\Testing\WithFaker;
  9. use Illuminate\Http\UploadedFile;
  10. use Illuminate\Support\Facades\Storage;
  11. use Tests\Bootstrap;
  12. use Tests\TestCase;
  13. use Illuminate\Support\Str;
  14. use Tests\Feature\Traits\FileTraits;
  15. class FileStoreTest extends Bootstrap
  16. {
  17. use FileTraits;
  18. public function test_tmp_and_model_id_forbidden()
  19. {
  20. $data = [
  21. "file" => UploadedFile::fake()->image('test.png'),
  22. "alts" => ['1', '2', '3'],
  23. "description" => 'lfjdsklfslfsdlfasdfsfhgsfgsdf',
  24. "public" => 1
  25. ];
  26. $collection = Collection::factory()->createQuietly([
  27. 'tmp_support' => false
  28. ]);
  29. $response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
  30. $response->assertForbidden();
  31. }
  32. public function test_tmp_false_and_collection_is_full_forbidden()
  33. {
  34. $randomCount = rand(2, 10);
  35. $collection = Collection::factory()->createQuietly([
  36. 'tmp_support' => false,
  37. 'count' => $randomCount
  38. ]);
  39. for ($i = $randomCount; $i > 0; $i--) {
  40. $uuid = app()->uuid;
  41. $this->one(File::class, [
  42. 'uuid' => $uuid,
  43. 'user_id' => 1,
  44. 'collection_id' => $collection->id,
  45. 'server_path' => '/' . date('y') . '/' . date('m') . '/' . $uuid . '.' . $collection->ext,
  46. ]);
  47. }
  48. $data = [
  49. "file" => UploadedFile::fake()->image('test.png'),
  50. "alts" => ['1', '2', '3'],
  51. "description" => 'lfjdsklfslfsdlfasdfsfhgsfgsdf',
  52. "public" => 1
  53. ];
  54. $response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
  55. $response->assertForbidden();
  56. }
  57. public function test_file_is_not_isset_forbidden()
  58. {
  59. $collection = Collection::factory()->createQuietly([
  60. 'tmp_support' => true
  61. ]);
  62. $data = [
  63. "file" => app()->uuid,
  64. "alts" => ['1', '2', '3'],
  65. "description" => 'lfjdsklfslfsdlfasdfsfhgsfgsdf',
  66. "public" => 1
  67. ];
  68. $response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
  69. $response->assertForbidden();
  70. }
  71. public function test_user_can_not_access_not_ownered_files()
  72. {
  73. $collection = Collection::factory()->createQuietly();
  74. $file = File::factory()->createQuietly([
  75. 'user_id' => auth()->id() + 1234,
  76. 'collection_id' => $collection->id
  77. ]);
  78. $data = [
  79. "file" => $file->uuid,
  80. "public" => 1
  81. ];
  82. $response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
  83. $response->assertForbidden();
  84. }
  85. /**
  86. * @dataProvider storeValidationTestProvider
  87. */
  88. public function test_store_dynamic_validation_unprocessable($collectionFields, $dataFields)
  89. {
  90. $collection = Collection::factory()->createQuietly($collectionFields);
  91. $response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $dataFields);
  92. $response->assertUnprocessable();
  93. }
  94. public function test_store_static_validation_unprocessable()
  95. {
  96. $collection = Collection::factory()->createQuietly([
  97. 'min_file_size' => 0
  98. ]);
  99. $data = [
  100. "file" => UploadedFile::fake()->image('lol.png'),
  101. "description" => Str::random(1000)
  102. ];
  103. $this->modelWithPolicy('collections', ['permission:collections.store'])
  104. ->loginAsAdmin()
  105. ->postJson(route("api.files.store", ['collection_name' => $collection->name]), $data)
  106. ->assertUnprocessable();
  107. }
  108. /**
  109. * @dataProvider storeValidationTestProvider
  110. */
  111. public function test_store_dynamic_validation_stored_file_unprocessable($collectionFields)
  112. {
  113. $collectionFields['withImage'] = true;
  114. $file = $this->one(File::class, dependencyAttributes: $collectionFields);
  115. $collection = Collection::find($file->collection_id);
  116. $data = [
  117. "file" => $file->uuid,
  118. ];
  119. $response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
  120. $response->assertUnprocessable();
  121. }
  122. public function test_user_can_store_sent_file_count_multiple_without_image_processor_ok()
  123. {
  124. $collectionFields['withImage'] = true;
  125. $file = $this->one(File::class, dependencyAttributes: $collectionFields);
  126. $collection = Collection::find($file->collection_id);
  127. $data = [
  128. "file" => UploadedFile::fake()->image('test.png'),
  129. ];
  130. $response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
  131. $response->assertCreated();
  132. }
  133. public function test_user_can_store_sent_file_uuid_count_multiple_without_image_processor_ok()
  134. {
  135. $collectionFields['withImage'] = true;
  136. $file = $this->one(File::class, dependencyAttributes: $collectionFields);
  137. $collection = Collection::find($file->collection_id);
  138. $data = [
  139. "file" => $file->uuid,
  140. "public" => 1
  141. ];
  142. $response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
  143. $response->assertCreated();
  144. }
  145. public function test_user_can_store_sent_file_count_one_without_image_processor_ok()
  146. {
  147. $collection = Collection::factory()->createQuietly([
  148. 'count' => 1
  149. ]);
  150. $data = [
  151. "file" => UploadedFile::fake()->image('lol.png'),
  152. "public" => 1
  153. ];
  154. $response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name, 'model_id' => 100]), $data);
  155. $response->assertCreated();
  156. }
  157. public function test_user_can_store_sent_file_uuid_count_one_without_image_processor_ok()
  158. {
  159. $collectionFields['withImage'] = true;
  160. $collectionFields['count'] = 1;
  161. $file = $this->one(File::class, dependencyAttributes: $collectionFields);
  162. $collection = Collection::find($file->collection_id);
  163. $data = [
  164. "file" => $file->uuid,
  165. "public" => 1
  166. ];
  167. $response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name, 'model_id' => 100]), $data);
  168. $response->assertCreated();
  169. }
  170. }