UserControllerTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace Tests\Feature\Http\Auth;
  3. use App\Facades\Settings;
  4. use App\Models\Group;
  5. use App\Models\TwoFAccount;
  6. use App\Models\User;
  7. use Illuminate\Support\Facades\Config;
  8. use Tests\FeatureTestCase;
  9. /**
  10. * @covers \App\Http\Controllers\Auth\UserController
  11. * @covers \App\Http\Middleware\RejectIfDemoMode
  12. */
  13. class UserControllerTest extends FeatureTestCase
  14. {
  15. /**
  16. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  17. */
  18. protected $user;
  19. private const NEW_USERNAME = 'Jane DOE';
  20. private const NEW_EMAIL = 'janedoe@example.org';
  21. private const PASSWORD = 'password';
  22. /**
  23. * @test
  24. */
  25. public function setUp() : void
  26. {
  27. parent::setUp();
  28. $this->user = User::factory()->create();
  29. }
  30. /**
  31. * @test
  32. */
  33. public function test_update_user_returns_success()
  34. {
  35. $response = $this->actingAs($this->user, 'web-guard')
  36. ->json('PUT', '/user', [
  37. 'name' => self::NEW_USERNAME,
  38. 'email' => self::NEW_EMAIL,
  39. 'password' => self::PASSWORD,
  40. ])
  41. ->assertOk()
  42. ->assertExactJson([
  43. 'name' => self::NEW_USERNAME,
  44. 'id' => $this->user->id,
  45. 'email' => self::NEW_EMAIL,
  46. 'is_admin' => false,
  47. ]);
  48. $this->assertDatabaseHas('users', [
  49. 'name' => self::NEW_USERNAME,
  50. 'id' => $this->user->id,
  51. 'email' => self::NEW_EMAIL,
  52. 'is_admin' => false,
  53. ]);
  54. }
  55. /**
  56. * @test
  57. */
  58. public function test_update_user_in_demo_mode_returns_unchanged_user()
  59. {
  60. Settings::set('isDemoApp', true);
  61. $response = $this->actingAs($this->user, 'web-guard')
  62. ->json('PUT', '/user', [
  63. 'name' => self::NEW_USERNAME,
  64. 'email' => self::NEW_EMAIL,
  65. 'password' => self::PASSWORD,
  66. ])
  67. ->assertOk()
  68. ->assertExactJson([
  69. 'name' => $this->user->name,
  70. 'id' => $this->user->id,
  71. 'email' => $this->user->email,
  72. 'is_admin' => $this->user->is_admin,
  73. ]);
  74. $this->assertDatabaseHas('users', [
  75. 'name' => $this->user->name,
  76. 'id' => $this->user->id,
  77. 'email' => $this->user->email,
  78. 'is_admin' => $this->user->is_admin,
  79. ]);
  80. }
  81. /**
  82. * @test
  83. */
  84. public function test_update_user_passing_wrong_password_returns_bad_request()
  85. {
  86. $response = $this->actingAs($this->user, 'web-guard')
  87. ->json('PUT', '/user', [
  88. 'name' => self::NEW_USERNAME,
  89. 'email' => self::NEW_EMAIL,
  90. 'password' => 'wrongPassword',
  91. ])
  92. ->assertStatus(400);
  93. }
  94. /**
  95. * @test
  96. */
  97. public function test_update_user_with_invalid_data_returns_validation_error()
  98. {
  99. $response = $this->actingAs($this->user, 'web-guard')
  100. ->json('PUT', '/user', [
  101. 'name' => '',
  102. 'email' => '',
  103. 'password' => self::PASSWORD,
  104. ])
  105. ->assertStatus(422);
  106. }
  107. /**
  108. * @test
  109. */
  110. public function test_delete_user_returns_success()
  111. {
  112. TwoFAccount::factory()->for($this->user)->create();
  113. Group::factory()->for($this->user)->create();
  114. $admin = User::factory()->administrator()->create();
  115. $this->assertDatabaseCount('users', 2);
  116. $this->actingAs($this->user, 'web-guard')
  117. ->json('DELETE', '/user', [
  118. 'password' => self::PASSWORD,
  119. ])
  120. ->assertNoContent();
  121. $this->assertDatabaseMissing('users', [
  122. 'id' => $this->user->id,
  123. ]);
  124. $this->assertDatabaseHas('users', [
  125. 'id' => $admin->id,
  126. ]);
  127. $this->assertDatabaseCount('users', 1);
  128. $this->assertDatabaseMissing('twofaccounts', [
  129. 'user_id' => $this->user->id,
  130. ]);
  131. $this->assertDatabaseMissing('groups', [
  132. 'user_id' => $this->user->id,
  133. ]);
  134. $this->assertDatabaseMissing('webauthn_credentials', [
  135. 'authenticatable_id' => $this->user->id,
  136. ]);
  137. $this->assertDatabaseMissing('webauthn_recoveries', [
  138. 'email' => $this->user->email,
  139. ]);
  140. $this->assertDatabaseMissing('oauth_access_tokens', [
  141. 'user_id' => $this->user->id,
  142. ]);
  143. $this->assertDatabaseMissing('password_resets', [
  144. 'email' => $this->user->email,
  145. ]);
  146. }
  147. /**
  148. * @test
  149. */
  150. public function test_delete_user_in_demo_mode_returns_unauthorized()
  151. {
  152. Config::set('2fauth.config.isDemoApp', true);
  153. Settings::set('isDemoApp', true);
  154. $response = $this->actingAs($this->user, 'web-guard')
  155. ->json('DELETE', '/user', [
  156. 'password' => self::PASSWORD,
  157. ])
  158. ->assertUnauthorized()
  159. ->assertJsonStructure([
  160. 'message',
  161. ]);
  162. $this->assertDatabaseHas('users', [
  163. 'id' => $this->user->id,
  164. ]);
  165. }
  166. /**
  167. * @test
  168. */
  169. public function test_delete_user_passing_wrong_password_returns_bad_request()
  170. {
  171. $response = $this->actingAs($this->user, 'web-guard')
  172. ->json('DELETE', '/user', [
  173. 'password' => 'wrongPassword',
  174. ])
  175. ->assertStatus(400);
  176. $this->assertDatabaseHas('users', [
  177. 'id' => $this->user->id,
  178. ]);
  179. }
  180. /**
  181. * @test
  182. */
  183. public function test_delete_the_only_admin_returns_bad_request()
  184. {
  185. /**
  186. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  187. */
  188. $admin = User::factory()->administrator()->create();
  189. $this->assertDatabaseCount('users', 2);
  190. $this->assertEquals(1, User::admins()->count());
  191. $response = $this->actingAs($admin, 'web-guard')
  192. ->json('DELETE', '/user', [
  193. 'password' => self::PASSWORD,
  194. ])
  195. ->assertStatus(400);
  196. $this->assertDatabaseHas('users', [
  197. 'id' => $admin->id,
  198. ]);
  199. }
  200. }