UserControllerTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. namespace Tests\Feature\Http\Auth;
  3. use App\Http\Controllers\Auth\UserController;
  4. use App\Http\Middleware\RejectIfDemoMode;
  5. use App\Http\Requests\UserUpdateRequest;
  6. use App\Models\User;
  7. use App\Observers\UserObserver;
  8. use App\Policies\UserPolicy;
  9. use Illuminate\Support\Facades\Config;
  10. use PHPUnit\Framework\Attributes\CoversClass;
  11. use Tests\FeatureTestCase;
  12. /**
  13. * UserControllerTest test class
  14. */
  15. #[CoversClass(UserController::class)]
  16. #[CoversClass(UserObserver::class)]
  17. #[CoversClass(UserPolicy::class)]
  18. #[CoversClass(RejectIfDemoMode::class)]
  19. #[CoversClass(UserUpdateRequest::class)]
  20. class UserControllerTest extends FeatureTestCase
  21. {
  22. /**
  23. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  24. */
  25. protected $user;
  26. private const NEW_USERNAME = 'Jane DOE';
  27. private const NEW_EMAIL = 'janedoe@example.org';
  28. private const PASSWORD = 'password';
  29. /**
  30. * @test
  31. */
  32. public function setUp() : void
  33. {
  34. parent::setUp();
  35. $this->user = User::factory()->create();
  36. }
  37. /**
  38. * @test
  39. */
  40. public function test_update_user_returns_success()
  41. {
  42. $response = $this->actingAs($this->user, 'web-guard')
  43. ->json('PUT', '/user', [
  44. 'name' => self::NEW_USERNAME,
  45. 'email' => self::NEW_EMAIL,
  46. 'password' => self::PASSWORD,
  47. ])
  48. ->assertOk()
  49. ->assertJsonFragment([
  50. 'name' => self::NEW_USERNAME,
  51. 'id' => $this->user->id,
  52. 'email' => self::NEW_EMAIL,
  53. 'is_admin' => false,
  54. ])
  55. ->assertJsonStructure([
  56. 'preferences',
  57. ]);
  58. $this->assertDatabaseHas('users', [
  59. 'name' => self::NEW_USERNAME,
  60. 'id' => $this->user->id,
  61. 'email' => self::NEW_EMAIL,
  62. 'is_admin' => false,
  63. ]);
  64. }
  65. /**
  66. * @test
  67. */
  68. public function test_update_user_without_changing_email_returns_success()
  69. {
  70. $response = $this->actingAs($this->user, 'web-guard')
  71. ->json('PUT', '/user', [
  72. 'name' => self::NEW_USERNAME,
  73. 'email' => $this->user->email,
  74. 'password' => self::PASSWORD,
  75. ])
  76. ->assertOk()
  77. ->assertJsonFragment([
  78. 'name' => self::NEW_USERNAME,
  79. 'id' => $this->user->id,
  80. 'email' => $this->user->email,
  81. 'is_admin' => false,
  82. ]);
  83. $this->assertDatabaseHas('users', [
  84. 'name' => self::NEW_USERNAME,
  85. 'id' => $this->user->id,
  86. 'email' => $this->user->email,
  87. 'is_admin' => false,
  88. ]);
  89. }
  90. /**
  91. * @test
  92. */
  93. public function test_update_user_without_changing_name_returns_success()
  94. {
  95. $response = $this->actingAs($this->user, 'web-guard')
  96. ->json('PUT', '/user', [
  97. 'name' => $this->user->name,
  98. 'email' => self::NEW_EMAIL,
  99. 'password' => self::PASSWORD,
  100. ])
  101. ->assertOk()
  102. ->assertJsonFragment([
  103. 'name' => $this->user->name,
  104. 'id' => $this->user->id,
  105. 'email' => self::NEW_EMAIL,
  106. 'is_admin' => false,
  107. ]);
  108. $this->assertDatabaseHas('users', [
  109. 'name' => $this->user->name,
  110. 'id' => $this->user->id,
  111. 'email' => self::NEW_EMAIL,
  112. 'is_admin' => false,
  113. ]);
  114. }
  115. /**
  116. * @test
  117. */
  118. public function test_update_user_with_uppercased_email_returns_success()
  119. {
  120. $response = $this->actingAs($this->user, 'web-guard')
  121. ->json('PUT', '/user', [
  122. 'name' => self::NEW_USERNAME,
  123. 'email' => strtoupper(self::NEW_EMAIL),
  124. 'password' => self::PASSWORD,
  125. ])
  126. ->assertOk()
  127. ->assertJsonFragment([
  128. 'name' => self::NEW_USERNAME,
  129. 'id' => $this->user->id,
  130. 'email' => self::NEW_EMAIL,
  131. 'is_admin' => false,
  132. ]);
  133. $this->assertDatabaseHas('users', [
  134. 'name' => self::NEW_USERNAME,
  135. 'id' => $this->user->id,
  136. 'email' => self::NEW_EMAIL,
  137. 'is_admin' => false,
  138. ]);
  139. }
  140. /**
  141. * @test
  142. */
  143. public function test_update_user_in_demo_mode_returns_unchanged_user()
  144. {
  145. Config::set('2fauth.config.isDemoApp', true);
  146. $name = $this->user->name;
  147. $email = $this->user->email;
  148. $response = $this->actingAs($this->user, 'web-guard')
  149. ->json('PUT', '/user', [
  150. 'name' => self::NEW_USERNAME,
  151. 'email' => self::NEW_EMAIL,
  152. 'password' => self::PASSWORD,
  153. ])
  154. ->assertOk()
  155. ->assertJsonFragment([
  156. 'name' => $name,
  157. 'id' => $this->user->id,
  158. 'email' => $email,
  159. 'is_admin' => $this->user->is_admin,
  160. ]);
  161. $this->assertDatabaseHas('users', [
  162. 'name' => $name,
  163. 'id' => $this->user->id,
  164. 'email' => $email,
  165. ]);
  166. }
  167. /**
  168. * @test
  169. */
  170. public function test_update_user_passing_wrong_password_returns_bad_request()
  171. {
  172. $response = $this->actingAs($this->user, 'web-guard')
  173. ->json('PUT', '/user', [
  174. 'name' => self::NEW_USERNAME,
  175. 'email' => self::NEW_EMAIL,
  176. 'password' => 'wrongPassword',
  177. ])
  178. ->assertStatus(400);
  179. }
  180. /**
  181. * @test
  182. */
  183. public function test_update_user_with_invalid_data_returns_validation_error()
  184. {
  185. $response = $this->actingAs($this->user, 'web-guard')
  186. ->json('PUT', '/user', [
  187. 'name' => '',
  188. 'email' => '',
  189. 'password' => self::PASSWORD,
  190. ])
  191. ->assertStatus(422);
  192. }
  193. /**
  194. * @test
  195. */
  196. public function test_delete_user_returns_success()
  197. {
  198. $this->actingAs($this->user, 'web-guard')
  199. ->json('DELETE', '/user', [
  200. 'password' => self::PASSWORD,
  201. ])
  202. ->assertNoContent();
  203. }
  204. /**
  205. * @test
  206. */
  207. public function test_delete_user_in_demo_mode_returns_unauthorized()
  208. {
  209. Config::set('2fauth.config.isDemoApp', true);
  210. $response = $this->actingAs($this->user, 'web-guard')
  211. ->json('DELETE', '/user', [
  212. 'password' => self::PASSWORD,
  213. ])
  214. ->assertUnauthorized()
  215. ->assertJsonStructure([
  216. 'message',
  217. ]);
  218. $this->assertDatabaseHas('users', [
  219. 'id' => $this->user->id,
  220. ]);
  221. }
  222. /**
  223. * @test
  224. */
  225. public function test_delete_user_passing_wrong_password_returns_bad_request()
  226. {
  227. $response = $this->actingAs($this->user, 'web-guard')
  228. ->json('DELETE', '/user', [
  229. 'password' => 'wrongPassword',
  230. ])
  231. ->assertStatus(400);
  232. $this->assertDatabaseHas('users', [
  233. 'id' => $this->user->id,
  234. ]);
  235. }
  236. /**
  237. * @test
  238. */
  239. public function test_delete_the_only_admin_returns_bad_request()
  240. {
  241. /**
  242. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  243. */
  244. $admin = User::factory()->administrator()->create();
  245. $this->assertDatabaseCount('users', 2);
  246. $this->assertEquals(1, User::admins()->count());
  247. $response = $this->actingAs($admin, 'web-guard')
  248. ->json('DELETE', '/user', [
  249. 'password' => self::PASSWORD,
  250. ])
  251. ->assertStatus(400);
  252. $this->assertDatabaseHas('users', [
  253. 'id' => $admin->id,
  254. ]);
  255. }
  256. }