You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SetSettingActionTest.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.user.ws;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.sonar.api.server.ws.WebService;
  24. import org.sonar.api.utils.System2;
  25. import org.sonar.db.DbTester;
  26. import org.sonar.db.user.UserDto;
  27. import org.sonar.db.user.UserPropertyDto;
  28. import org.sonar.server.exceptions.UnauthorizedException;
  29. import org.sonar.server.tester.UserSessionRule;
  30. import org.sonar.server.ws.WsActionTester;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  33. import static org.assertj.core.api.Assertions.tuple;
  34. public class SetSettingActionTest {
  35. @Rule
  36. public UserSessionRule userSession = UserSessionRule.standalone();
  37. @Rule
  38. public DbTester db = DbTester.create(System2.INSTANCE);
  39. private WsActionTester ws = new WsActionTester(new SetSettingAction(db.getDbClient(), userSession));
  40. @Test
  41. public void set_new_setting() {
  42. UserDto user = db.users().insertUser();
  43. userSession.logIn(user);
  44. ws.newRequest()
  45. .setParam("key", "notifications.optOut")
  46. .setParam("value", "true")
  47. .execute();
  48. assertThat(db.getDbClient().userPropertiesDao().selectByUser(db.getSession(), user))
  49. .extracting(UserPropertyDto::getKey, UserPropertyDto::getValue)
  50. .containsExactlyInAnyOrder(tuple("notifications.optOut", "true"));
  51. }
  52. @Test
  53. public void update_existing_setting() {
  54. UserDto user = db.users().insertUser();
  55. db.users().insertUserSetting(user, userSetting -> userSetting
  56. .setKey("notifications.optOut")
  57. .setValue("false"));
  58. userSession.logIn(user);
  59. ws.newRequest()
  60. .setParam("key", "notifications.optOut")
  61. .setParam("value", "true")
  62. .execute();
  63. assertThat(db.getDbClient().userPropertiesDao().selectByUser(db.getSession(), user))
  64. .extracting(UserPropertyDto::getKey, UserPropertyDto::getValue)
  65. .containsExactlyInAnyOrder(tuple("notifications.optOut", "true"));
  66. }
  67. @Test
  68. public void keep_existing_setting_when_setting_new_one() {
  69. UserDto user = db.users().insertUser();
  70. db.users().insertUserSetting(user, userSetting -> userSetting
  71. .setKey("notifications.readDate")
  72. .setValue("1234"));
  73. userSession.logIn(user);
  74. ws.newRequest()
  75. .setParam("key", "notifications.optOut")
  76. .setParam("value", "true")
  77. .execute();
  78. assertThat(db.getDbClient().userPropertiesDao().selectByUser(db.getSession(), user))
  79. .extracting(UserPropertyDto::getKey, UserPropertyDto::getValue)
  80. .containsExactlyInAnyOrder(
  81. tuple("notifications.readDate", "1234"),
  82. tuple("notifications.optOut", "true"));
  83. }
  84. @Test
  85. public void fail_when_not_authenticated() {
  86. assertThatThrownBy(() -> {
  87. ws.newRequest()
  88. .setParam("key", "notifications.optOut")
  89. .setParam("value", "true")
  90. .execute();
  91. })
  92. .isInstanceOf(UnauthorizedException.class);
  93. }
  94. @Test
  95. public void definition() {
  96. WebService.Action definition = ws.getDef();
  97. assertThat(definition.key()).isEqualTo("set_setting");
  98. assertThat(definition.isPost()).isTrue();
  99. assertThat(definition.isInternal()).isTrue();
  100. assertThat(definition.since()).isEqualTo("7.6");
  101. assertThat(definition.params())
  102. .extracting(WebService.Param::key, WebService.Param::isRequired, WebService.Param::maximumLength)
  103. .containsOnly(
  104. tuple("key", true, 100),
  105. tuple("value", true, 4000));
  106. assertThat(definition.param("key").possibleValues()).containsExactlyInAnyOrder(
  107. "tutorials.jenkins.skipBitbucketPreReqs",
  108. "notifications.optOut");
  109. }
  110. }