IconControllerTest.php 2.9 KB

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