UserControllerTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Tests\Feature\Http\Auth;
  3. use App\Models\User;
  4. use App\Facades\Settings;
  5. use Tests\FeatureTestCase;
  6. use Illuminate\Support\Facades\Config;
  7. class UserControllerTest extends FeatureTestCase
  8. {
  9. /**
  10. * @var \App\Models\User
  11. */
  12. protected $user;
  13. private const NEW_USERNAME = 'Jane DOE';
  14. private const NEW_EMAIL = 'janedoe@example.org';
  15. private const PASSWORD = 'password';
  16. /**
  17. * @test
  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_update_user_returns_success()
  28. {
  29. $response = $this->actingAs($this->user, 'web-guard')
  30. ->json('PUT', '/user', [
  31. 'name' => self::NEW_USERNAME,
  32. 'email' => self::NEW_EMAIL,
  33. 'password' => self::PASSWORD,
  34. ])
  35. ->assertOk()
  36. ->assertExactJson([
  37. 'name' => self::NEW_USERNAME,
  38. 'id' => $this->user->id,
  39. 'email' => self::NEW_EMAIL,
  40. ]);
  41. }
  42. /**
  43. * @test
  44. */
  45. public function test_update_user_in_demo_mode_returns_unchanged_user()
  46. {
  47. Settings::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. Settings::set('isDemoApp', true);
  105. $response = $this->actingAs($this->user, 'web-guard')
  106. ->json('DELETE', '/user', [
  107. 'password' => self::PASSWORD,
  108. ])
  109. ->assertUnauthorized()
  110. ->assertJsonStructure([
  111. 'message'
  112. ]);
  113. }
  114. /**
  115. * @test
  116. */
  117. public function test_delete_user_passing_wrong_password_returns_bad_request()
  118. {
  119. $response = $this->actingAs($this->user, 'web-guard')
  120. ->json('DELETE', '/user', [
  121. 'password' => 'wrongPassword',
  122. ])
  123. ->assertStatus(400);
  124. }
  125. }