SettingControllerTest.php 8.3 KB

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