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

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