SettingControllerTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace Tests\Api\v1\Controllers;
  3. use App\User;
  4. use App\Group;
  5. use Tests\FeatureTestCase;
  6. use App\TwoFAccount;
  7. class SettingControllerTest extends FeatureTestCase
  8. {
  9. /**
  10. * @var \App\User
  11. */
  12. protected $user;
  13. private const SETTING_JSON_STRUCTURE = [
  14. 'key',
  15. 'value'
  16. ];
  17. private const TWOFAUTH_NATIVE_SETTING = 'showTokenAsDot';
  18. private const TWOFAUTH_NATIVE_SETTING_DEFAULT_VALUE = false;
  19. private const TWOFAUTH_NATIVE_SETTING_CHANGED_VALUE = true;
  20. private const USER_DEFINED_SETTING = 'mySetting';
  21. private const USER_DEFINED_SETTING_VALUE = 'mySetting';
  22. private const USER_DEFINED_SETTING_CHANGED_VALUE = 'mySetting';
  23. /**
  24. * @test
  25. */
  26. public function setUp(): void
  27. {
  28. parent::setUp();
  29. $this->user = factory(User::class)->create();
  30. }
  31. /**
  32. * @test
  33. */
  34. public function test_index_returns_setting_collection()
  35. {
  36. $response = $this->actingAs($this->user, 'api')
  37. ->json('GET', '/api/v1/settings')
  38. ->assertOk()
  39. ->assertJsonStructure([
  40. '*' => self::SETTING_JSON_STRUCTURE
  41. ]);
  42. }
  43. /**
  44. * @test
  45. */
  46. public function test_show_native_unchanged_setting_returns_consistent_value()
  47. {
  48. $response = $this->actingAs($this->user, 'api')
  49. ->json('GET', '/api/v1/settings/' . self::TWOFAUTH_NATIVE_SETTING)
  50. ->assertOk()
  51. ->assertExactJson([
  52. 'key' => self::TWOFAUTH_NATIVE_SETTING,
  53. 'value' => self::TWOFAUTH_NATIVE_SETTING_DEFAULT_VALUE,
  54. ]);
  55. }
  56. /**
  57. * @test
  58. */
  59. public function test_show_native_changed_setting_returns_consistent_value()
  60. {
  61. $settingService = resolve('App\Services\SettingServiceInterface');
  62. $settingService->set(self::TWOFAUTH_NATIVE_SETTING, self::TWOFAUTH_NATIVE_SETTING_CHANGED_VALUE);
  63. $response = $this->actingAs($this->user, 'api')
  64. ->json('GET', '/api/v1/settings/' . self::TWOFAUTH_NATIVE_SETTING)
  65. ->assertOk()
  66. ->assertExactJson([
  67. 'key' => self::TWOFAUTH_NATIVE_SETTING,
  68. 'value' => self::TWOFAUTH_NATIVE_SETTING_CHANGED_VALUE,
  69. ]);
  70. }
  71. /**
  72. * @test
  73. */
  74. public function test_show_custom_user_setting_returns_consistent_value()
  75. {
  76. $settingService = resolve('App\Services\SettingServiceInterface');
  77. $settingService->set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE);
  78. $response = $this->actingAs($this->user, 'api')
  79. ->json('GET', '/api/v1/settings/' . self::USER_DEFINED_SETTING)
  80. ->assertOk()
  81. ->assertExactJson([
  82. 'key' => self::USER_DEFINED_SETTING,
  83. 'value' => self::USER_DEFINED_SETTING_VALUE,
  84. ]);
  85. }
  86. /**
  87. * @test
  88. */
  89. public function test_show_missing_setting_returns_not_found()
  90. {
  91. $response = $this->actingAs($this->user, 'api')
  92. ->json('GET', '/api/v1/settings/missing')
  93. ->assertNotFound();
  94. }
  95. /**
  96. * @test
  97. */
  98. public function test_store_custom_user_setting_returns_success()
  99. {
  100. $response = $this->actingAs($this->user, 'api')
  101. ->json('POST', '/api/v1/settings', [
  102. 'key' => self::USER_DEFINED_SETTING,
  103. 'value' => self::USER_DEFINED_SETTING_VALUE,
  104. ])
  105. ->assertCreated()
  106. ->assertExactJson([
  107. 'key' => self::USER_DEFINED_SETTING,
  108. 'value' => self::USER_DEFINED_SETTING_VALUE,
  109. ]);
  110. }
  111. /**
  112. * @test
  113. */
  114. public function test_store_invalid_custom_user_setting_returns_validation_error()
  115. {
  116. $response = $this->actingAs($this->user, 'api')
  117. ->json('POST', '/api/v1/settings', [
  118. 'key' => null,
  119. 'value' => null,
  120. ])
  121. ->assertStatus(422);
  122. }
  123. /**
  124. * @test
  125. */
  126. public function test_store_existing_custom_user_setting_returns_validation_error()
  127. {
  128. $settingService = resolve('App\Services\SettingServiceInterface');
  129. $settingService->set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE);
  130. $response = $this->actingAs($this->user, 'api')
  131. ->json('POST', '/api/v1/settings', [
  132. 'key' => self::USER_DEFINED_SETTING,
  133. 'value' => self::USER_DEFINED_SETTING_VALUE,
  134. ])
  135. ->assertStatus(422);
  136. }
  137. /**
  138. * @test
  139. */
  140. public function test_update_unchanged_native_setting_returns_updated_setting()
  141. {
  142. $response = $this->actingAs($this->user, 'api')
  143. ->json('PUT', '/api/v1/settings/' . self::TWOFAUTH_NATIVE_SETTING, [
  144. 'key' => self::TWOFAUTH_NATIVE_SETTING,
  145. 'value' => self::TWOFAUTH_NATIVE_SETTING_CHANGED_VALUE,
  146. ])
  147. ->assertOk()
  148. ->assertExactJson([
  149. 'key' => self::TWOFAUTH_NATIVE_SETTING,
  150. 'value' => self::TWOFAUTH_NATIVE_SETTING_CHANGED_VALUE,
  151. ]);
  152. }
  153. /**
  154. * @test
  155. */
  156. public function test_update_custom_user_setting_returns_updated_setting()
  157. {
  158. $settingService = resolve('App\Services\SettingServiceInterface');
  159. $settingService->set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE);
  160. $response = $this->actingAs($this->user, 'api')
  161. ->json('PUT', '/api/v1/settings/' . self::USER_DEFINED_SETTING, [
  162. 'key' => self::USER_DEFINED_SETTING,
  163. 'value' => self::USER_DEFINED_SETTING_CHANGED_VALUE,
  164. ])
  165. ->assertOk()
  166. ->assertExactJson([
  167. 'key' => self::USER_DEFINED_SETTING,
  168. 'value' => self::USER_DEFINED_SETTING_CHANGED_VALUE,
  169. ]);
  170. }
  171. /**
  172. * @test
  173. */
  174. public function test_update_missing_user_setting_returns_created_setting()
  175. {
  176. $response = $this->actingAs($this->user, 'api')
  177. ->json('PUT', '/api/v1/settings/' . self::USER_DEFINED_SETTING, [
  178. 'key' => self::USER_DEFINED_SETTING,
  179. 'value' => self::USER_DEFINED_SETTING_CHANGED_VALUE,
  180. ])
  181. ->assertOk()
  182. ->assertExactJson([
  183. 'key' => self::USER_DEFINED_SETTING,
  184. 'value' => self::USER_DEFINED_SETTING_CHANGED_VALUE,
  185. ]);
  186. }
  187. /**
  188. * @test
  189. */
  190. public function test_destroy_user_setting_returns_success()
  191. {
  192. $settingService = resolve('App\Services\SettingServiceInterface');
  193. $settingService->set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE);
  194. $response = $this->actingAs($this->user, 'api')
  195. ->json('DELETE', '/api/v1/settings/' . self::USER_DEFINED_SETTING)
  196. ->assertNoContent();
  197. }
  198. /**
  199. * @test
  200. */
  201. public function test_destroy_native_setting_returns_bad_request()
  202. {
  203. $response = $this->actingAs($this->user, 'api')
  204. ->json('DELETE', '/api/v1/settings/' . self::TWOFAUTH_NATIVE_SETTING)
  205. ->assertStatus(400)
  206. ->assertJsonStructure([
  207. 'message',
  208. 'reason',
  209. ]);
  210. }
  211. /**
  212. * @test
  213. */
  214. public function test_destroy_missing_user_setting_returns_not_found()
  215. {
  216. $response = $this->actingAs($this->user, 'api')
  217. ->json('DELETE', '/api/v1/settings/' . self::USER_DEFINED_SETTING)
  218. ->assertNotFound();
  219. }
  220. }