IconControllerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Tests\Api\v1\Controllers;
  3. use App\Api\v1\Controllers\IconController;
  4. use App\Facades\IconStore;
  5. use App\Models\TwoFAccount;
  6. use App\Models\User;
  7. use App\Services\LogoService;
  8. use Illuminate\Http\Testing\FileFactory;
  9. use Illuminate\Http\UploadedFile;
  10. use Illuminate\Support\Facades\Http;
  11. use Illuminate\Support\Facades\Storage;
  12. use PHPUnit\Framework\Attributes\CoversClass;
  13. use PHPUnit\Framework\Attributes\Test;
  14. use Tests\Data\HttpRequestTestData;
  15. use Tests\Data\OtpTestData;
  16. use Tests\FeatureTestCase;
  17. /**
  18. * IconController test class
  19. */
  20. #[CoversClass(IconController::class)]
  21. class IconControllerTest extends FeatureTestCase
  22. {
  23. /**
  24. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  25. */
  26. protected $user;
  27. public function setUp() : void
  28. {
  29. parent::setUp();
  30. Storage::fake('icons');
  31. Storage::fake('logos');
  32. Http::preventStrayRequests();
  33. Http::fake([
  34. LogoService::TFA_IMG_URL . '*' => Http::response(HttpRequestTestData::SVG_LOGO_BODY, 200),
  35. LogoService::TFA_URL => Http::response(HttpRequestTestData::TFA_JSON_BODY, 200),
  36. ]);
  37. Http::fake([
  38. OtpTestData::EXTERNAL_IMAGE_URL_DECODED => Http::response((new FileFactory)->image('file.png', 10, 10)->tempFile, 200),
  39. ]);
  40. $this->user = User::factory()->create();
  41. }
  42. #[Test]
  43. public function test_upload_icon_returns_filename_using_the_iconStore()
  44. {
  45. $iconName = 'testIcon.jpg';
  46. $file = UploadedFile::fake()->image($iconName);
  47. $response = $this->actingAs($this->user, 'api-guard')
  48. ->json('POST', '/api/v1/icons', [
  49. 'icon' => $file,
  50. ])
  51. ->assertCreated()
  52. ->assertJsonStructure([
  53. 'filename',
  54. ]);
  55. }
  56. #[Test]
  57. public function test_upload_icon_stores_it_to_database()
  58. {
  59. $file = UploadedFile::fake()->image('testIcon.jpg');
  60. $response = $this->actingAs($this->user, 'api-guard')
  61. ->json('POST', '/api/v1/icons', [
  62. 'icon' => $file,
  63. ]);
  64. }
  65. #[Test]
  66. public function test_upload_with_invalid_data_returns_validation_error()
  67. {
  68. $response = $this->actingAs($this->user, 'api-guard')
  69. ->json('POST', '/api/v1/icons', [
  70. 'icon' => null,
  71. ])
  72. ->assertStatus(422);
  73. }
  74. #[Test]
  75. public function test_fetch_logo_returns_filename()
  76. {
  77. $response = $this->actingAs($this->user, 'api-guard')
  78. ->json('POST', '/api/v1/icons/default', [
  79. 'service' => 'service',
  80. ])
  81. ->assertStatus(201)
  82. ->assertJsonStructure([
  83. 'filename',
  84. ]);
  85. }
  86. #[Test]
  87. public function test_fetch_unknown_logo_returns_nothing()
  88. {
  89. $response = $this->actingAs($this->user, 'api-guard')
  90. ->json('POST', '/api/v1/icons/default', [
  91. 'service' => 'unknown_company',
  92. ])
  93. ->assertNoContent();
  94. }
  95. #[Test]
  96. public function test_delete_icon_returns_success_using_the_iconStore()
  97. {
  98. IconStore::spy();
  99. $iconName = 'testIcon.jpg';
  100. $response = $this->actingAs($this->user, 'api-guard')
  101. ->json('DELETE', '/api/v1/icons/' . $iconName)
  102. ->assertNoContent(204);
  103. IconStore::shouldHaveReceived('delete')->once()->with($iconName);
  104. }
  105. #[Test]
  106. public function test_delete_invalid_icon_returns_success()
  107. {
  108. $response = $this->actingAs($this->user, 'api-guard')
  109. ->json('DELETE', '/api/v1/icons/null')
  110. ->assertNoContent(204);
  111. }
  112. #[Test]
  113. public function test_delete_icon_of_another_user_is_forbidden()
  114. {
  115. $anotherUser = User::factory()->create();
  116. TwoFAccount::factory()->for($anotherUser)->create([
  117. 'icon' => 'testIcon.jpg',
  118. ]);
  119. $response = $this->actingAs($this->user, 'api-guard')
  120. ->json('DELETE', '/api/v1/icons/testIcon.jpg')
  121. ->assertForbidden()
  122. ->assertJsonStructure([
  123. 'message',
  124. ]);
  125. }
  126. }