UserControllerTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace Tests\Api\v1\Controllers\Auth;
  3. use App\Api\v1\Controllers\UserController;
  4. use App\Api\v1\Resources\UserResource;
  5. use App\Models\User;
  6. use PHPUnit\Framework\Attributes\CoversClass;
  7. use Tests\FeatureTestCase;
  8. /**
  9. * UserControllerTest test class
  10. */
  11. #[CoversClass(UserController::class)]
  12. #[CoversClass(UserResource::class)]
  13. class UserControllerTest extends FeatureTestCase
  14. {
  15. /**
  16. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  17. */
  18. protected $user;
  19. private const PREFERENCE_JSON_STRUCTURE = [
  20. 'key',
  21. 'value',
  22. ];
  23. /**
  24. * @test
  25. */
  26. public function setUp() : void
  27. {
  28. parent::setUp();
  29. $this->user = User::factory()->create();
  30. }
  31. /**
  32. * @test
  33. */
  34. public function test_show_existing_user_when_authenticated_returns_success()
  35. {
  36. $response = $this->actingAs($this->user, 'api-guard')
  37. ->json('GET', '/api/v1/user')
  38. ->assertOk()
  39. ->assertJsonFragment([
  40. 'name' => $this->user->name,
  41. 'id' => $this->user->id,
  42. 'email' => $this->user->email,
  43. 'is_admin' => $this->user->is_admin,
  44. ])
  45. ->assertJsonStructure([
  46. 'preferences',
  47. ]);
  48. }
  49. /**
  50. * @test
  51. */
  52. public function test_allPreferences_returns_consistent_json_structure()
  53. {
  54. $response = $this->actingAs($this->user, 'api-guard')
  55. ->json('GET', '/api/v1/user/preferences')
  56. ->assertOk()
  57. ->assertJsonStructure([
  58. '*' => self::PREFERENCE_JSON_STRUCTURE,
  59. ]);
  60. }
  61. /**
  62. * @test
  63. */
  64. public function test_allPreferences_returns_preferences_with_default_values()
  65. {
  66. $response = $this->actingAs($this->user, 'api-guard')
  67. ->json('GET', '/api/v1/user/preferences')
  68. ->assertJsonCount(count(config('2fauth.preferences')), $key = null);
  69. foreach (config('2fauth.preferences') as $pref => $value) {
  70. $response->assertJsonFragment([
  71. 'key' => $pref,
  72. 'value' => $value,
  73. ]);
  74. }
  75. }
  76. /**
  77. * @test
  78. */
  79. public function test_allPreferences_returns_preferences_with_user_values()
  80. {
  81. $userPrefs = [];
  82. foreach (config('2fauth.preferences') as $pref => $value) {
  83. if (is_numeric($value)) {
  84. $userPrefs[$pref] = $value + 1;
  85. } elseif (is_string($value)) {
  86. $userPrefs[$pref] = $value . '_';
  87. } elseif (is_bool($value)) {
  88. $userPrefs[$pref] = ! $value;
  89. }
  90. $this->user['preferences->' . $pref] = $userPrefs[$pref];
  91. }
  92. $this->user->save();
  93. $response = $this->actingAs($this->user, 'api-guard')
  94. ->json('GET', '/api/v1/user/preferences')
  95. ->assertJsonCount(count(config('2fauth.preferences')), $key = null);
  96. foreach ($userPrefs as $pref => $value) {
  97. $response->assertJsonFragment([
  98. 'key' => $pref,
  99. 'value' => $value,
  100. ]);
  101. }
  102. }
  103. /**
  104. * @test
  105. */
  106. public function test_showPreference_returns_preference_with_default_value()
  107. {
  108. /**
  109. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  110. */
  111. $this->user = User::factory()->create();
  112. $response = $this->actingAs($this->user, 'api-guard')
  113. ->json('GET', '/api/v1/user/preferences/showOtpAsDot')
  114. ->assertOk()
  115. ->assertExactJson([
  116. 'key' => 'showOtpAsDot',
  117. 'value' => config('2fauth.preferences.showOtpAsDot'),
  118. ]);
  119. }
  120. /**
  121. * @test
  122. */
  123. public function test_showPreference_returns_preference_with_custom_value()
  124. {
  125. $showOtpAsDot = ! config('2fauth.preferences.showOtpAsDot');
  126. $this->user['preferences->showOtpAsDot'] = $showOtpAsDot;
  127. $this->user->save();
  128. $response = $this->actingAs($this->user, 'api-guard')
  129. ->json('GET', '/api/v1/user/preferences/showOtpAsDot')
  130. ->assertJsonFragment([
  131. 'key' => 'showOtpAsDot',
  132. 'value' => $showOtpAsDot,
  133. ]);
  134. }
  135. /**
  136. * @test
  137. */
  138. public function test_showPreference_for_missing_preference_returns_not_found()
  139. {
  140. $response = $this->actingAs($this->user, 'api-guard')
  141. ->json('GET', '/api/v1/user/preferences/unknown')
  142. ->assertNotFound();
  143. }
  144. /**
  145. * @test
  146. */
  147. public function test_setPreference_returns_updated_preference()
  148. {
  149. /**
  150. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  151. */
  152. $this->user = User::factory()->create();
  153. $showOtpAsDot = ! config('2fauth.preferences.showOtpAsDot');
  154. $response = $this->actingAs($this->user, 'api-guard')
  155. ->json('PUT', '/api/v1/user/preferences/showOtpAsDot', [
  156. 'key' => 'showOtpAsDot',
  157. 'value' => $showOtpAsDot,
  158. ])
  159. ->assertCreated()
  160. ->assertExactJson([
  161. 'key' => 'showOtpAsDot',
  162. 'value' => $showOtpAsDot,
  163. ]);
  164. }
  165. /**
  166. * @test
  167. */
  168. public function test_setPreference_for_missing_preference_returns_not_found()
  169. {
  170. $response = $this->actingAs($this->user, 'api-guard')
  171. ->json('PUT', '/api/v1/user/preferences/unknown', [
  172. 'key' => 'showOtpAsDot',
  173. 'value' => true,
  174. ])
  175. ->assertNotFound();
  176. }
  177. /**
  178. * @test
  179. */
  180. public function test_setPreference_with_invalid_data_returns_validation_error()
  181. {
  182. $response = $this->actingAs($this->user, 'api-guard')
  183. ->json('PUT', '/api/v1/user/preferences/showOtpAsDot', [
  184. 'key' => 'showOtpAsDot',
  185. 'value' => null,
  186. ])
  187. ->assertStatus(422);
  188. }
  189. }