UserControllerTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace Tests\Api\v1\Controllers\Auth;
  3. use App\Models\User;
  4. use Tests\FeatureTestCase;
  5. /**
  6. * @covers \App\Api\v1\Controllers\UserController
  7. * @covers \App\Api\v1\Resources\UserResource
  8. */
  9. class UserControllerTest extends FeatureTestCase
  10. {
  11. /**
  12. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  13. */
  14. protected $user;
  15. private const PREFERENCE_JSON_STRUCTURE = [
  16. 'key',
  17. 'value',
  18. ];
  19. /**
  20. * @test
  21. */
  22. public function setUp() : void
  23. {
  24. parent::setUp();
  25. $this->user = User::factory()->create();
  26. }
  27. /**
  28. * @test
  29. */
  30. public function test_show_existing_user_when_authenticated_returns_success()
  31. {
  32. $response = $this->actingAs($this->user, 'api-guard')
  33. ->json('GET', '/api/v1/user')
  34. ->assertOk()
  35. ->assertExactJson([
  36. 'name' => $this->user->name,
  37. 'id' => $this->user->id,
  38. 'email' => $this->user->email,
  39. 'is_admin' => $this->user->is_admin,
  40. ]);
  41. }
  42. /**
  43. * @test
  44. */
  45. public function test_allPreferences_returns_consistent_json_structure()
  46. {
  47. $response = $this->actingAs($this->user, 'api-guard')
  48. ->json('GET', '/api/v1/user/preferences')
  49. ->assertOk()
  50. ->assertJsonStructure([
  51. '*' => self::PREFERENCE_JSON_STRUCTURE,
  52. ]);
  53. }
  54. /**
  55. * @test
  56. */
  57. public function test_allPreferences_returns_preferences_with_default_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. /**
  70. * @test
  71. */
  72. public function test_allPreferences_returns_preferences_with_user_values()
  73. {
  74. $userPrefs = [
  75. 'showTokenAsDot' => true,
  76. 'closeOtpOnCopy' => true,
  77. 'copyOtpOnDisplay' => true,
  78. 'useBasicQrcodeReader' => true,
  79. 'displayMode' => 'grid',
  80. 'showAccountsIcons' => false,
  81. 'kickUserAfter' => 5,
  82. 'activeGroup' => 1,
  83. 'rememberActiveGroup' => false,
  84. 'defaultGroup' => 1,
  85. 'defaultCaptureMode' => 'advancedForm',
  86. 'useDirectCapture' => true,
  87. 'useWebauthnAsDefault' => true,
  88. 'useWebauthnOnly' => true,
  89. 'getOfficialIcons' => false,
  90. 'theme' => 'dark',
  91. 'formatPassword' => false,
  92. 'formatPasswordBy' => 1,
  93. 'lang' => 'fr',
  94. ];
  95. $this->user['preferences->showTokenAsDot'] = $userPrefs['showTokenAsDot'];
  96. $this->user['preferences->closeOtpOnCopy'] = $userPrefs['closeOtpOnCopy'];
  97. $this->user['preferences->copyOtpOnDisplay'] = $userPrefs['copyOtpOnDisplay'];
  98. $this->user['preferences->useBasicQrcodeReader'] = $userPrefs['useBasicQrcodeReader'];
  99. $this->user['preferences->displayMode'] = $userPrefs['displayMode'];
  100. $this->user['preferences->showAccountsIcons'] = $userPrefs['showAccountsIcons'];
  101. $this->user['preferences->kickUserAfter'] = $userPrefs['kickUserAfter'];
  102. $this->user['preferences->activeGroup'] = $userPrefs['activeGroup'];
  103. $this->user['preferences->rememberActiveGroup'] = $userPrefs['rememberActiveGroup'];
  104. $this->user['preferences->defaultGroup'] = $userPrefs['defaultGroup'];
  105. $this->user['preferences->defaultCaptureMode'] = $userPrefs['defaultCaptureMode'];
  106. $this->user['preferences->useDirectCapture'] = $userPrefs['useDirectCapture'];
  107. $this->user['preferences->useWebauthnAsDefault'] = $userPrefs['useWebauthnAsDefault'];
  108. $this->user['preferences->useWebauthnOnly'] = $userPrefs['useWebauthnOnly'];
  109. $this->user['preferences->getOfficialIcons'] = $userPrefs['getOfficialIcons'];
  110. $this->user['preferences->theme'] = $userPrefs['theme'];
  111. $this->user['preferences->formatPassword'] = $userPrefs['formatPassword'];
  112. $this->user['preferences->formatPasswordBy'] = $userPrefs['formatPasswordBy'];
  113. $this->user['preferences->lang'] = $userPrefs['lang'];
  114. $this->user->save();
  115. $response = $this->actingAs($this->user, 'api-guard')
  116. ->json('GET', '/api/v1/user/preferences')
  117. ->assertJsonCount(count(config('2fauth.preferences')), $key = null);
  118. foreach ($userPrefs as $pref => $value) {
  119. $response->assertJsonFragment([
  120. 'key' => $pref,
  121. 'value' => $value,
  122. ]);
  123. }
  124. }
  125. /**
  126. * @test
  127. */
  128. public function test_showPreference_returns_preference_with_default_value()
  129. {
  130. /**
  131. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  132. */
  133. $this->user = User::factory()->create();
  134. $response = $this->actingAs($this->user, 'api-guard')
  135. ->json('GET', '/api/v1/user/preferences/showTokenAsDot')
  136. ->assertOk()
  137. ->assertExactJson([
  138. 'key' => 'showTokenAsDot',
  139. 'value' => config('2fauth.preferences.showTokenAsDot'),
  140. ]);
  141. }
  142. /**
  143. * @test
  144. */
  145. public function test_showPreference_returns_preference_with_custom_value()
  146. {
  147. $showTokenAsDot = ! config('2fauth.preferences.showTokenAsDot');
  148. $this->user['preferences->showTokenAsDot'] = $showTokenAsDot;
  149. $this->user->save();
  150. $response = $this->actingAs($this->user, 'api-guard')
  151. ->json('GET', '/api/v1/user/preferences/showTokenAsDot')
  152. ->assertJsonFragment([
  153. 'key' => 'showTokenAsDot',
  154. 'value' => $showTokenAsDot,
  155. ]);
  156. }
  157. /**
  158. * @test
  159. */
  160. public function test_showPreference_for_missing_preference_returns_not_found()
  161. {
  162. $response = $this->actingAs($this->user, 'api-guard')
  163. ->json('GET', '/api/v1/user/preferences/unknown')
  164. ->assertNotFound();
  165. }
  166. /**
  167. * @test
  168. */
  169. public function test_setPreference_returns_updated_preference()
  170. {
  171. /**
  172. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  173. */
  174. $this->user = User::factory()->create();
  175. $showTokenAsDot = ! config('2fauth.preferences.showTokenAsDot');
  176. $response = $this->actingAs($this->user, 'api-guard')
  177. ->json('PUT', '/api/v1/user/preferences/showTokenAsDot', [
  178. 'key' => 'showTokenAsDot',
  179. 'value' => $showTokenAsDot,
  180. ])
  181. ->assertCreated()
  182. ->assertExactJson([
  183. 'key' => 'showTokenAsDot',
  184. 'value' => $showTokenAsDot,
  185. ]);
  186. }
  187. /**
  188. * @test
  189. */
  190. public function test_setPreference_for_missing_preference_returns_not_found()
  191. {
  192. $response = $this->actingAs($this->user, 'api-guard')
  193. ->json('PUT', '/api/v1/user/preferences/unknown', [
  194. 'key' => 'showTokenAsDot',
  195. 'value' => true,
  196. ])
  197. ->assertNotFound();
  198. }
  199. /**
  200. * @test
  201. */
  202. public function test_setPreference_with_invalid_data_returns_validation_error()
  203. {
  204. $response = $this->actingAs($this->user, 'api-guard')
  205. ->json('PUT', '/api/v1/user/preferences/showTokenAsDot', [
  206. 'key' => 'showTokenAsDot',
  207. 'value' => null,
  208. ])
  209. ->assertStatus(422);
  210. }
  211. }