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.

AddActionIT.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.favorite.ws;
  21. import java.util.List;
  22. import javax.annotation.Nullable;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.sonar.api.server.ws.WebService;
  26. import org.sonar.api.web.UserRole;
  27. import org.sonar.db.DbClient;
  28. import org.sonar.db.DbSession;
  29. import org.sonar.db.DbTester;
  30. import org.sonar.db.component.ComponentDto;
  31. import org.sonar.db.component.ProjectData;
  32. import org.sonar.db.project.ProjectDto;
  33. import org.sonar.db.property.PropertyDto;
  34. import org.sonar.db.property.PropertyQuery;
  35. import org.sonar.db.user.UserDto;
  36. import org.sonar.server.exceptions.ForbiddenException;
  37. import org.sonar.server.exceptions.NotFoundException;
  38. import org.sonar.server.exceptions.UnauthorizedException;
  39. import org.sonar.server.favorite.FavoriteUpdater;
  40. import org.sonar.server.tester.UserSessionRule;
  41. import org.sonar.server.ws.TestRequest;
  42. import org.sonar.server.ws.TestResponse;
  43. import org.sonar.server.ws.WsActionTester;
  44. import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
  45. import static java.util.Optional.ofNullable;
  46. import static org.assertj.core.api.Assertions.assertThat;
  47. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  48. import static org.sonar.api.web.UserRole.USER;
  49. import static org.sonar.db.component.ComponentTesting.newFileDto;
  50. import static org.sonar.server.favorite.ws.FavoritesWsParameters.PARAM_COMPONENT;
  51. public class AddActionIT {
  52. @Rule
  53. public UserSessionRule userSession = UserSessionRule.standalone();
  54. @Rule
  55. public DbTester db = DbTester.create();
  56. private final DbClient dbClient = db.getDbClient();
  57. private final DbSession dbSession = db.getSession();
  58. private final FavoriteUpdater favoriteUpdater = new FavoriteUpdater(dbClient);
  59. private final WsActionTester ws = new WsActionTester(new AddAction(userSession, dbClient, favoriteUpdater));
  60. @Test
  61. public void add_a_project() {
  62. ProjectDto project = db.components().insertPrivateProject().getProjectDto();
  63. UserDto user = db.users().insertUser();
  64. userSession.logIn(user).addProjectPermission(USER, project);
  65. TestResponse result = call(project.getKey());
  66. assertThat(result.getStatus()).isEqualTo(HTTP_NO_CONTENT);
  67. List<PropertyDto> favorites = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder()
  68. .setUserUuid(user.getUuid())
  69. .setKey("favourite")
  70. .build(), dbSession);
  71. assertThat(favorites).hasSize(1);
  72. PropertyDto favorite = favorites.get(0);
  73. assertThat(favorite)
  74. .extracting(PropertyDto::getEntityUuid, PropertyDto::getUserUuid, PropertyDto::getKey)
  75. .containsOnly(project.getUuid(), user.getUuid(), "favourite");
  76. }
  77. @Test
  78. public void fail_when_no_browse_permission_on_the_project() {
  79. ProjectDto project = db.components().insertPrivateProject().getProjectDto();
  80. userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
  81. assertThatThrownBy(() -> call(project.getKey()))
  82. .isInstanceOf(ForbiddenException.class);
  83. }
  84. @Test
  85. public void fail_when_component_is_not_found() {
  86. userSession.logIn();
  87. assertThatThrownBy(() -> call("P42"))
  88. .isInstanceOf(NotFoundException.class);
  89. }
  90. @Test
  91. public void fail_when_user_is_not_authenticated() {
  92. ProjectDto project = db.components().insertPrivateProject().getProjectDto();
  93. assertThatThrownBy(() -> call(project.getKey()))
  94. .isInstanceOf(UnauthorizedException.class);
  95. }
  96. @Test
  97. public void fail_on_file() {
  98. ProjectData project = db.components().insertPrivateProject();
  99. ComponentDto file = db.components().insertComponent(newFileDto(project.getMainBranchComponent()));
  100. UserDto user = db.users().insertUser();
  101. userSession.logIn(user).addProjectPermission(USER, project.getProjectDto());
  102. assertThatThrownBy(() -> call(file.getKey()))
  103. .isInstanceOf(NotFoundException.class)
  104. .hasMessage("Entity with key '" + file.getKey() + "' not found");
  105. }
  106. @Test
  107. public void definition() {
  108. WebService.Action definition = ws.getDef();
  109. assertThat(definition.key()).isEqualTo("add");
  110. assertThat(definition.isPost()).isTrue();
  111. WebService.Param param = definition.param("component");
  112. assertThat(param).isNotNull();
  113. assertThat(param.isRequired()).isTrue();
  114. }
  115. private TestResponse call(@Nullable String componentKey) {
  116. TestRequest request = ws.newRequest();
  117. ofNullable(componentKey).ifPresent(c -> request.setParam(PARAM_COMPONENT, c));
  118. return request.execute();
  119. }
  120. }