UserControllerTest.php 7.0 KB

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