IconControllerTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Tests\Api\v1\Controllers;
  3. use App\Api\v1\Controllers\IconController;
  4. use App\Models\TwoFAccount;
  5. use App\Models\User;
  6. use Illuminate\Http\UploadedFile;
  7. use PHPUnit\Framework\Attributes\CoversClass;
  8. use PHPUnit\Framework\Attributes\Test;
  9. use Tests\FeatureTestCase;
  10. /**
  11. * IconController test class
  12. */
  13. #[CoversClass(IconController::class)]
  14. class IconControllerTest extends FeatureTestCase
  15. {
  16. /**
  17. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  18. */
  19. protected $user;
  20. public function setUp() : void
  21. {
  22. parent::setUp();
  23. $this->user = User::factory()->create();
  24. }
  25. #[Test]
  26. public function test_upload_icon_returns_filename()
  27. {
  28. $file = UploadedFile::fake()->image('testIcon.jpg');
  29. $response = $this->actingAs($this->user, 'api-guard')
  30. ->json('POST', '/api/v1/icons', [
  31. 'icon' => $file,
  32. ])
  33. ->assertCreated()
  34. ->assertJsonStructure([
  35. 'filename',
  36. ]);
  37. }
  38. #[Test]
  39. public function test_upload_with_invalid_data_returns_validation_error()
  40. {
  41. $response = $this->actingAs($this->user, 'api-guard')
  42. ->json('POST', '/api/v1/icons', [
  43. 'icon' => null,
  44. ])
  45. ->assertStatus(422);
  46. }
  47. #[Test]
  48. public function test_fetch_logo_returns_filename()
  49. {
  50. $response = $this->actingAs($this->user, 'api-guard')
  51. ->json('POST', '/api/v1/icons/default', [
  52. 'service' => 'dropbox',
  53. ])
  54. ->assertStatus(201)
  55. ->assertJsonStructure([
  56. 'filename',
  57. ]);
  58. }
  59. #[Test]
  60. public function test_fetch_unknown_logo_returns_nothing()
  61. {
  62. $response = $this->actingAs($this->user, 'api-guard')
  63. ->json('POST', '/api/v1/icons/default', [
  64. 'service' => 'unknown_company',
  65. ])
  66. ->assertNoContent();
  67. }
  68. #[Test]
  69. public function test_delete_icon_returns_success()
  70. {
  71. $response = $this->actingAs($this->user, 'api-guard')
  72. ->json('DELETE', '/api/v1/icons/testIcon.jpg')
  73. ->assertNoContent(204);
  74. }
  75. #[Test]
  76. public function test_delete_invalid_icon_returns_success()
  77. {
  78. $response = $this->actingAs($this->user, 'api-guard')
  79. ->json('DELETE', '/api/v1/icons/null')
  80. ->assertNoContent(204);
  81. }
  82. #[Test]
  83. public function test_delete_icon_of_another_user_is_forbidden()
  84. {
  85. $anotherUser = User::factory()->create();
  86. TwoFAccount::factory()->for($anotherUser)->create([
  87. 'icon' => 'testIcon.jpg',
  88. ]);
  89. $response = $this->actingAs($this->user, 'api-guard')
  90. ->json('DELETE', '/api/v1/icons/testIcon.jpg')
  91. ->assertForbidden()
  92. ->assertJsonStructure([
  93. 'message',
  94. ]);
  95. }
  96. }