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.

271 lines
9.3 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()->create([
  27. 'tmp_support' => false
  28. ]);
  29. $response = $this->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' => auth()->id(),
  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->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()->create([
  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->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. $uuid = app()->uuid;
  75. $file = File::factory()->createQuietly([
  76. 'uuid' => $uuid,
  77. 'user_id' => auth()->id() + 1234,
  78. 'server_path' => '/' . date('y') . '/' . date('m') . '/',
  79. 'collection_id' => $collection->id
  80. ]);
  81. $data = [
  82. "file" => $file->uuid,
  83. "public" => 1
  84. ];
  85. $response = $this->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
  86. $response->assertForbidden();
  87. }
  88. /**
  89. * @dataProvider storeValidationTestProvider
  90. */
  91. public function test_store_dynamic_validation_unprocessable($collectionFields, $dataFields)
  92. {
  93. $collection = Collection::factory()->create($collectionFields);
  94. $response = $this->postJson(route('api.files.store', ['collection_name' => $collection->name]), $dataFields);
  95. $response->assertUnprocessable();
  96. }
  97. // /**
  98. // * @testWith
  99. // * ['email:gt']
  100. // * ['email:gt']
  101. // * ['email:gt']
  102. // * ['email:gt']
  103. // * ['email:gt']
  104. // */
  105. // public function test_store_static_validation_unprocessable($key)
  106. // {
  107. // File::factory()->smash($key);
  108. // }
  109. /**
  110. * @dataProvider storeValidationTestProvider
  111. */
  112. public function test_store_dynamic_validation_stored_file_unprocessable($collectionFields)
  113. {
  114. $collection = Collection::factory()->createQuietly($collectionFields);
  115. $uuid = app()->uuid;
  116. $file = File::factory()->createQuietly([
  117. 'uuid' => $uuid,
  118. 'server_path' => '/' . date('y') . '/' . date('m') . '/',
  119. 'user_id' => auth()->id(),
  120. 'collection_id' => $collection->id
  121. ]);
  122. $imageProcessor = new ImageProcessor;
  123. $imageProcessor->createFakeImage(storage_path('stub') . '/image.png', Storage::disk($collection->disk)->path($file->server_path . $uuid . '.' . $collection->ext));
  124. $data = [
  125. "file" => $file->uuid,
  126. "public" => 1
  127. ];
  128. $response = $this->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
  129. $response->assertUnprocessable();
  130. }
  131. public function test_user_can_store_sent_file_count_multiple_without_image_processor_ok()
  132. {
  133. $collection = Collection::factory()->createQuietly([
  134. 'alt_required' => false,
  135. 'description_required' => false,
  136. 'tmp_support' => true,
  137. 'max_width' => 2000,
  138. 'max_height' => 2000,
  139. 'min_width' => 1,
  140. 'min_height' => 1,
  141. 'min_file_size' => 0
  142. ]);
  143. $uuid = app()->uuid;
  144. $this->one(File::class, [
  145. 'uuid' => $uuid,
  146. 'user_id' => auth()->id(),
  147. 'collection_id' => $collection->id,
  148. 'server_path' => '/' . date('y') . '/' . date('m') . '/',
  149. ]);
  150. $data = [
  151. "file" => UploadedFile::fake()->image('test.png'),
  152. "public" => 1
  153. ];
  154. $response = $this->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
  155. $response->assertCreated();
  156. }
  157. public function test_user_can_store_sent_file_uuid_count_multiple_without_image_processor_ok()
  158. {
  159. $collection = Collection::factory()->createQuietly([
  160. 'alt_required' => false,
  161. 'description_required' => false,
  162. 'tmp_support' => true,
  163. 'max_width' => 2000,
  164. 'max_height' => 2000,
  165. 'min_width' => 1,
  166. 'min_height' => 1,
  167. 'min_file_size' => 0
  168. ]);
  169. $uuid = app()->uuid;
  170. $file = File::factory()->createQuietly([
  171. 'uuid' => $uuid,
  172. 'server_path' => '/' . date('y') . '/' . date('m') . '/',
  173. 'user_id' => auth()->id(),
  174. 'collection_id' => $collection->id
  175. ]);
  176. $imageProcessor = new ImageProcessor;
  177. $imageProcessor->createFakeImage(storage_path('stub') . '/image.png', Storage::disk($collection->disk)->path($file->server_path . $uuid . '.' . $collection->ext));
  178. $data = [
  179. "file" => $file->uuid,
  180. "public" => 1
  181. ];
  182. $response = $this->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
  183. $response->assertCreated();
  184. }
  185. public function test_user_can_store_sent_file_count_one_without_image_processor_ok()
  186. {
  187. $collection = Collection::factory()->createQuietly([
  188. 'alt_required' => false,
  189. 'description_required' => false,
  190. 'tmp_support' => true,
  191. 'max_width' => 2000,
  192. 'max_height' => 2000,
  193. 'min_width' => 1,
  194. 'min_height' => 1,
  195. 'min_file_size' => 0
  196. ]);
  197. $uuid = app()->uuid;
  198. $file = File::factory()->createQuietly([
  199. 'uuid' => $uuid,
  200. 'server_path' => '/' . date('y') . '/' . date('m') . '/',
  201. 'user_id' => auth()->id(),
  202. 'collection_id' => $collection->id
  203. ]);
  204. $imageProcessor = new ImageProcessor;
  205. $imageProcessor->createFakeImage(storage_path('stub') . '/image.png', Storage::disk($collection->disk)->path($file->server_path . $uuid . '.' . $collection->ext));
  206. $data = [
  207. "file" => $file->uuid,
  208. "public" => 1
  209. ];
  210. $response = $this->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
  211. $response->assertCreated();
  212. }
  213. public function test_user_can_store_sent_file_uuid_count_one_without_image_processor_ok()
  214. {
  215. $collection = Collection::factory()->createQuietly([
  216. 'alt_required' => false,
  217. 'description_required' => false,
  218. 'tmp_support' => true,
  219. 'max_width' => 2000,
  220. 'max_height' => 2000,
  221. 'min_width' => 1,
  222. 'min_height' => 1,
  223. 'min_file_size' => 0
  224. ]);
  225. $uuid = app()->uuid;
  226. $file = File::factory()->createQuietly([
  227. 'uuid' => $uuid,
  228. 'server_path' => '/' . date('y') . '/' . date('m') . '/',
  229. 'user_id' => auth()->id(),
  230. 'collection_id' => $collection->id
  231. ]);
  232. $imageProcessor = new ImageProcessor;
  233. $imageProcessor->createFakeImage(storage_path('stub') . '/image.png', Storage::disk($collection->disk)->path($file->server_path . $uuid . '.' . $collection->ext));
  234. $data = [
  235. "file" => $file->uuid,
  236. "public" => 1
  237. ];
  238. $response = $this->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
  239. $response->assertCreated();
  240. }
  241. }