SettingControllerTest.php 8.4 KB

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