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.

UpdateDefaultVisibilityActionTest.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.project.ws;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.junit.rules.ExpectedException;
  24. import org.sonar.api.server.ws.Change;
  25. import org.sonar.api.server.ws.WebService;
  26. import org.sonar.db.DbTester;
  27. import org.sonar.server.exceptions.ForbiddenException;
  28. import org.sonar.server.organization.DefaultOrganizationProvider;
  29. import org.sonar.server.organization.DefaultOrganizationProviderImpl;
  30. import org.sonar.server.tester.UserSessionRule;
  31. import org.sonar.server.ws.TestRequest;
  32. import org.sonar.server.ws.WsActionTester;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. import static org.assertj.core.api.Assertions.tuple;
  35. import static org.sonar.server.project.ws.UpdateDefaultVisibilityAction.ACTION;
  36. import static org.sonar.server.project.ws.UpdateDefaultVisibilityAction.PARAM_PROJECT_VISIBILITY;
  37. public class UpdateDefaultVisibilityActionTest {
  38. @Rule
  39. public DbTester dbTester = DbTester.create();
  40. @Rule
  41. public UserSessionRule userSession = UserSessionRule.standalone();
  42. @Rule
  43. public ExpectedException expectedException = ExpectedException.none();
  44. private DefaultOrganizationProvider defaultOrganizationProvider = new DefaultOrganizationProviderImpl(dbTester.getDbClient());
  45. private UpdateDefaultVisibilityAction underTest = new UpdateDefaultVisibilityAction(userSession, dbTester.getDbClient(), defaultOrganizationProvider);
  46. private WsActionTester wsTester = new WsActionTester(underTest);
  47. @Test
  48. public void change_project_visibility_to_private() {
  49. userSession.logIn().setSystemAdministrator();
  50. wsTester.newRequest()
  51. .setParam(PARAM_PROJECT_VISIBILITY, "private")
  52. .execute();
  53. assertThat(dbTester.getDbClient().organizationDao().getNewProjectPrivate(dbTester.getSession(), dbTester.getDefaultOrganization())).isTrue();
  54. }
  55. @Test
  56. public void change_project_visibility_to_public() {
  57. dbTester.organizations().setNewProjectPrivate(dbTester.getDefaultOrganization(), true);
  58. userSession.logIn().setSystemAdministrator();
  59. wsTester.newRequest()
  60. .setParam(PARAM_PROJECT_VISIBILITY, "public")
  61. .execute();
  62. assertThat(dbTester.organizations().getNewProjectPrivate(dbTester.getDefaultOrganization())).isFalse();
  63. }
  64. @Test
  65. public void fail_if_not_loggued_as_system_administrator() {
  66. userSession.logIn();
  67. TestRequest request = wsTester.newRequest()
  68. .setParam(PARAM_PROJECT_VISIBILITY, "private");
  69. expectedException.expect(ForbiddenException.class);
  70. request.execute();
  71. }
  72. @Test
  73. public void verify_define() {
  74. WebService.Action action = wsTester.getDef();
  75. assertThat(action.key()).isEqualTo(ACTION);
  76. assertThat(action.isPost()).isTrue();
  77. assertThat(action.description()).isNotEmpty();
  78. assertThat(action.isInternal()).isTrue();
  79. assertThat(action.since()).isEqualTo("6.4");
  80. assertThat(action.handler()).isEqualTo(underTest);
  81. assertThat(action.changelog())
  82. .extracting(Change::getVersion, Change::getDescription)
  83. .contains(tuple("7.3", "This WS used to be located at /api/organizations/update_project_visibility"));
  84. WebService.Param projectVisibility = action.param(PARAM_PROJECT_VISIBILITY);
  85. assertThat(projectVisibility.isRequired()).isTrue();
  86. assertThat(projectVisibility.possibleValues()).containsExactlyInAnyOrder("private", "public");
  87. assertThat(projectVisibility.description()).isEqualTo("Default visibility for projects");
  88. }
  89. }