UserControllerTest.php 3.7 KB

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