IconControllerTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /**
  17. *
  18. */
  19. public function setUp() : void
  20. {
  21. parent::setUp();
  22. $this->user = User::factory()->create();
  23. }
  24. /**
  25. * @test
  26. */
  27. public function test_upload_icon_returns_filename()
  28. {
  29. $file = UploadedFile::fake()->image('testIcon.jpg');
  30. $response = $this->actingAs($this->user, 'api-guard')
  31. ->json('POST', '/api/v1/icons', [
  32. 'icon' => $file,
  33. ])
  34. ->assertCreated()
  35. ->assertJsonStructure([
  36. 'filename',
  37. ]);
  38. }
  39. /**
  40. * @test
  41. */
  42. public function test_upload_with_invalid_data_returns_validation_error()
  43. {
  44. $response = $this->actingAs($this->user, 'api-guard')
  45. ->json('POST', '/api/v1/icons', [
  46. 'icon' => null,
  47. ])
  48. ->assertStatus(422);
  49. }
  50. /**
  51. * @test
  52. */
  53. public function test_fetch_logo_returns_filename()
  54. {
  55. $response = $this->actingAs($this->user, 'api-guard')
  56. ->json('POST', '/api/v1/icons/default', [
  57. 'service' => 'twitter',
  58. ])
  59. ->assertStatus(201)
  60. ->assertJsonStructure([
  61. 'filename',
  62. ]);
  63. }
  64. /**
  65. * @test
  66. */
  67. public function test_fetch_unknown_logo_returns_nothing()
  68. {
  69. $response = $this->actingAs($this->user, 'api-guard')
  70. ->json('POST', '/api/v1/icons/default', [
  71. 'service' => 'unknown_company',
  72. ])
  73. ->assertNoContent();
  74. }
  75. /**
  76. * @test
  77. */
  78. public function test_delete_icon_returns_success()
  79. {
  80. $response = $this->actingAs($this->user, 'api-guard')
  81. ->json('DELETE', '/api/v1/icons/testIcon.jpg')
  82. ->assertNoContent(204);
  83. }
  84. /**
  85. * @test
  86. */
  87. public function test_delete_invalid_icon_returns_success()
  88. {
  89. $response = $this->actingAs($this->user, 'api-guard')
  90. ->json('DELETE', '/api/v1/icons/null')
  91. ->assertNoContent(204);
  92. }
  93. /**
  94. * @test
  95. */
  96. public function test_delete_icon_of_another_user_is_forbidden()
  97. {
  98. $anotherUser = User::factory()->create();
  99. TwoFAccount::factory()->for($anotherUser)->create([
  100. 'icon' => 'testIcon.jpg',
  101. ]);
  102. $response = $this->actingAs($this->user, 'api-guard')
  103. ->json('DELETE', '/api/v1/icons/testIcon.jpg')
  104. ->assertForbidden()
  105. ->assertJsonStructure([
  106. 'message',
  107. ]);
  108. }
  109. }