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.

UpdateBitbucketActionIT.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.almsettings.ws;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.sonar.api.config.internal.Encryption;
  24. import org.sonar.api.server.ws.WebService;
  25. import org.sonar.db.DbTester;
  26. import org.sonar.db.alm.setting.AlmSettingDto;
  27. import org.sonar.db.user.UserDto;
  28. import org.sonar.server.almsettings.MultipleAlmFeature;
  29. import org.sonar.server.component.ComponentFinder;
  30. import org.sonar.server.exceptions.ForbiddenException;
  31. import org.sonar.server.exceptions.NotFoundException;
  32. import org.sonar.server.tester.UserSessionRule;
  33. import org.sonar.server.ws.TestRequest;
  34. import org.sonar.server.ws.WsActionTester;
  35. import static java.lang.String.format;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  38. import static org.assertj.core.groups.Tuple.tuple;
  39. import static org.mockito.Mockito.mock;
  40. public class UpdateBitbucketActionIT {
  41. @Rule
  42. public UserSessionRule userSession = UserSessionRule.standalone();
  43. @Rule
  44. public DbTester db = DbTester.create();
  45. private final Encryption encryption = mock(Encryption.class);
  46. private WsActionTester ws = new WsActionTester(new UpdateBitbucketAction(db.getDbClient(), userSession,
  47. new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null),
  48. mock(MultipleAlmFeature.class))));
  49. @Test
  50. public void update() {
  51. UserDto user = db.users().insertUser();
  52. userSession.logIn(user).setSystemAdministrator();
  53. AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
  54. ws.newRequest()
  55. .setParam("key", almSettingDto.getKey())
  56. .setParam("url", "https://bitbucket.enterprise-unicorn.com")
  57. .setParam("personalAccessToken", "10987654321")
  58. .execute();
  59. assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
  60. .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, s -> s.getDecryptedPersonalAccessToken(encryption))
  61. .containsOnly(tuple(almSettingDto.getKey(), "https://bitbucket.enterprise-unicorn.com", "10987654321"));
  62. }
  63. @Test
  64. public void update_with_new_key() {
  65. UserDto user = db.users().insertUser();
  66. userSession.logIn(user).setSystemAdministrator();
  67. AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
  68. ws.newRequest()
  69. .setParam("key", almSettingDto.getKey())
  70. .setParam("newKey", "Bitbucket Server - Infra Team")
  71. .setParam("url", "https://bitbucket.enterprise-unicorn.com")
  72. .setParam("personalAccessToken", "0123456789")
  73. .execute();
  74. assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
  75. .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, s -> s.getDecryptedPersonalAccessToken(encryption))
  76. .containsOnly(tuple("Bitbucket Server - Infra Team", "https://bitbucket.enterprise-unicorn.com", "0123456789"));
  77. }
  78. @Test
  79. public void fail_when_url_updated_without_pat() {
  80. UserDto user = db.users().insertUser();
  81. userSession.logIn(user).setSystemAdministrator();
  82. AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
  83. TestRequest request = ws.newRequest()
  84. .setParam("key", almSettingDto.getKey())
  85. .setParam("url", "https://bitbucket.enterprise-unicorn.com");
  86. assertThatThrownBy(() -> request.execute())
  87. .isInstanceOf(IllegalArgumentException.class)
  88. .hasMessage("Please provide the Personal Access Token to update the URL.");
  89. }
  90. @Test
  91. public void update_with_url_change_needs_path() {
  92. UserDto user = db.users().insertUser();
  93. userSession.logIn(user).setSystemAdministrator();
  94. AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
  95. ws.newRequest()
  96. .setParam("key", almSettingDto.getKey())
  97. .setParam("url", "https://bitbucket.enterprise-unicorn.com")
  98. .setParam("personalAccessToken", "0123456789")
  99. .execute();
  100. assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
  101. .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, s -> s.getDecryptedPersonalAccessToken(encryption))
  102. .containsOnly(tuple(almSettingDto.getKey(), "https://bitbucket.enterprise-unicorn.com", "0123456789"));
  103. }
  104. @Test
  105. public void fail_when_key_does_not_match_existing_alm_setting() {
  106. UserDto user = db.users().insertUser();
  107. userSession.logIn(user).setSystemAdministrator();
  108. assertThatThrownBy(() -> ws.newRequest()
  109. .setParam("key", "unknown")
  110. .setParam("url", "https://bitbucket.enterprise-unicorn.com")
  111. .setParam("personalAccessToken", "0123456789")
  112. .execute())
  113. .isInstanceOf(NotFoundException.class)
  114. .hasMessageContaining("DevOps Platform setting with key 'unknown' cannot be found");
  115. }
  116. @Test
  117. public void fail_when_new_key_matches_existing_alm_setting() {
  118. UserDto user = db.users().insertUser();
  119. userSession.logIn(user).setSystemAdministrator();
  120. AlmSettingDto almSetting1 = db.almSettings().insertBitbucketAlmSetting();
  121. AlmSettingDto almSetting2 = db.almSettings().insertBitbucketAlmSetting();
  122. assertThatThrownBy(() -> ws.newRequest()
  123. .setParam("key", almSetting1.getKey())
  124. .setParam("newKey", almSetting2.getKey())
  125. .setParam("url", "https://bitbucket.enterprise-unicorn.com")
  126. .setParam("personalAccessToken", "0123456789")
  127. .execute())
  128. .isInstanceOf(IllegalArgumentException.class)
  129. .hasMessageContaining(format("An DevOps Platform setting with key '%s' already exists", almSetting2.getKey()));
  130. }
  131. @Test
  132. public void fail_when_missing_administer_system_permission() {
  133. UserDto user = db.users().insertUser();
  134. userSession.logIn(user);
  135. AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
  136. assertThatThrownBy(() -> ws.newRequest()
  137. .setParam("key", almSettingDto.getKey())
  138. .setParam("newKey", "Bitbucket Server - Infra Team")
  139. .setParam("url", "https://bitbucket.enterprise-unicorn.com")
  140. .setParam("personalAccessToken", "0123456789")
  141. .execute())
  142. .isInstanceOf(ForbiddenException.class);
  143. }
  144. @Test
  145. public void definition() {
  146. WebService.Action def = ws.getDef();
  147. assertThat(def.since()).isEqualTo("8.1");
  148. assertThat(def.isPost()).isTrue();
  149. assertThat(def.params())
  150. .extracting(WebService.Param::key, WebService.Param::isRequired)
  151. .containsExactlyInAnyOrder(tuple("key", true), tuple("newKey", false), tuple("url", true), tuple("personalAccessToken", false));
  152. }
  153. }