UserControllerTest.php 3.7 KB

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