UserControllerTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Tests\Api\v1\Controllers\Auth;
  3. use App\Models\User;
  4. use Tests\FeatureTestCase;
  5. class UserControllerTest extends FeatureTestCase
  6. {
  7. /**
  8. * @var \App\Models\User
  9. */
  10. protected $user;
  11. private const NEW_USERNAME = 'Jane DOE';
  12. private const NEW_EMAIL = 'janedoe@example.org';
  13. private const PASSWORD = 'password';
  14. /**
  15. * @test
  16. */
  17. public function setUp(): void
  18. {
  19. parent::setUp();
  20. $this->user = User::factory()->create();
  21. }
  22. /**
  23. * @test
  24. */
  25. public function test_show_existing_user_when_authenticated_returns_success()
  26. {
  27. $response = $this->actingAs($this->user, 'api')
  28. ->json('GET', '/api/v1/user')
  29. ->assertOk()
  30. ->assertExactJson([
  31. 'name' => $this->user->name,
  32. 'email' => $this->user->email,
  33. ]);
  34. }
  35. /**
  36. * @test
  37. */
  38. public function test_show_existing_user_when_anonymous_returns_success()
  39. {
  40. $response = $this->json('GET', '/api/v1/user/name')
  41. ->assertOk()
  42. ->assertExactJson([
  43. 'name' => $this->user->name,
  44. ]);
  45. }
  46. /**
  47. * @test
  48. */
  49. public function test_show_missing_user_returns_success_with_null_name()
  50. {
  51. User::destroy($this->user->id);
  52. $response = $this->actingAs($this->user, 'api')
  53. ->json('GET', '/api/v1/user')
  54. ->assertOk()
  55. ->assertExactJson([
  56. 'name' => null,
  57. ]);
  58. }
  59. /**
  60. * @test
  61. */
  62. public function test_update_user_returns_success()
  63. {
  64. $response = $this->actingAs($this->user, 'api')
  65. ->json('PUT', '/api/v1/user', [
  66. 'name' => self::NEW_USERNAME,
  67. 'email' => self::NEW_EMAIL,
  68. 'password' => self::PASSWORD,
  69. ])
  70. ->assertOk()
  71. ->assertExactJson([
  72. 'name' => self::NEW_USERNAME,
  73. 'email' => self::NEW_EMAIL,
  74. ]);
  75. }
  76. /**
  77. * @test
  78. */
  79. public function test_update_user_in_demo_mode_returns_unchanged_user()
  80. {
  81. $settingService = resolve('App\Services\SettingService');
  82. $settingService->set('isDemoApp', true);
  83. $response = $this->actingAs($this->user, 'api')
  84. ->json('PUT', '/api/v1/user', [
  85. 'name' => self::NEW_USERNAME,
  86. 'email' => self::NEW_EMAIL,
  87. 'password' => self::PASSWORD,
  88. ])
  89. ->assertOk()
  90. ->assertExactJson([
  91. 'name' => $this->user->name,
  92. 'email' => $this->user->email,
  93. ]);
  94. }
  95. /**
  96. * @test
  97. */
  98. public function test_update_user_passing_wrong_password_returns_bad_request()
  99. {
  100. $response = $this->actingAs($this->user, 'api')
  101. ->json('PUT', '/api/v1/user', [
  102. 'name' => self::NEW_USERNAME,
  103. 'email' => self::NEW_EMAIL,
  104. 'password' => 'wrongPassword',
  105. ])
  106. ->assertStatus(400);
  107. }
  108. /**
  109. * @test
  110. */
  111. public function test_update_user_with_invalid_data_returns_validation_error()
  112. {
  113. $response = $this->actingAs($this->user, 'api')
  114. ->json('PUT', '/api/v1/user', [
  115. 'name' => '',
  116. 'email' => '',
  117. 'password' => self::PASSWORD,
  118. ])
  119. ->assertStatus(422);
  120. }
  121. }