SettingControllerTest.php 8.3 KB

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