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 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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.notification.ws;
  21. import javax.annotation.Nullable;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.sonar.api.notifications.Notification;
  25. import org.sonar.db.DbClient;
  26. import org.sonar.db.DbTester;
  27. import org.sonar.db.component.ComponentDto;
  28. import org.sonar.db.component.ProjectData;
  29. import org.sonar.db.project.ProjectDto;
  30. import org.sonar.db.user.UserDto;
  31. import org.sonar.server.component.TestComponentFinder;
  32. import org.sonar.server.exceptions.BadRequestException;
  33. import org.sonar.server.exceptions.ForbiddenException;
  34. import org.sonar.server.exceptions.NotFoundException;
  35. import org.sonar.server.exceptions.UnauthorizedException;
  36. import org.sonar.server.notification.NotificationChannel;
  37. import org.sonar.server.notification.NotificationDispatcherMetadata;
  38. import org.sonar.server.tester.UserSessionRule;
  39. import org.sonar.server.ws.TestRequest;
  40. import org.sonar.server.ws.TestResponse;
  41. import org.sonar.server.ws.WsActionTester;
  42. import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
  43. import static java.util.Arrays.asList;
  44. import static java.util.Collections.singletonList;
  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.mockito.Mockito.mock;
  49. import static org.mockito.Mockito.when;
  50. import static org.sonar.api.web.UserRole.USER;
  51. import static org.sonar.db.component.ComponentTesting.newPortfolio;
  52. import static org.sonar.server.notification.ws.NotificationsWsParameters.PARAM_CHANNEL;
  53. import static org.sonar.server.notification.ws.NotificationsWsParameters.PARAM_LOGIN;
  54. import static org.sonar.server.notification.ws.NotificationsWsParameters.PARAM_PROJECT;
  55. import static org.sonar.server.notification.ws.NotificationsWsParameters.PARAM_TYPE;
  56. public class AddActionIT {
  57. private static final String NOTIF_MY_NEW_ISSUES = "Dispatcher1";
  58. private static final String NOTIF_NEW_ISSUES = "Dispatcher2";
  59. private static final String NOTIF_NEW_QUALITY_GATE_STATUS = "Dispatcher3";
  60. @Rule
  61. public UserSessionRule userSession = UserSessionRule.standalone();
  62. @Rule
  63. public DbTester db = DbTester.create();
  64. private final DbClient dbClient = db.getDbClient();
  65. private final NotificationChannel emailChannel = new FakeNotificationChannel("EmailChannel");
  66. private final NotificationChannel twitterChannel = new FakeNotificationChannel("TwitterChannel");
  67. // default channel, based on class simple name
  68. private final NotificationChannel defaultChannel = new FakeNotificationChannel("EmailNotificationChannel");
  69. private final Dispatchers dispatchers = mock(Dispatchers.class);
  70. private final WsActionTester ws = new WsActionTester(new AddAction(new NotificationCenter(
  71. new NotificationDispatcherMetadata[] {},
  72. new NotificationChannel[] {emailChannel, twitterChannel, defaultChannel}),
  73. new NotificationUpdater(dbClient), dispatchers, dbClient, TestComponentFinder.from(db), userSession));
  74. @Test
  75. public void add_to_email_channel_by_default() {
  76. UserDto user = db.users().insertUser();
  77. userSession.logIn(user);
  78. when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  79. call(NOTIF_MY_NEW_ISSUES, null, null, null);
  80. db.notifications().assertExists(defaultChannel.getKey(), NOTIF_MY_NEW_ISSUES, userSession.getUuid(), null);
  81. }
  82. @Test
  83. public void add_to_a_specific_channel() {
  84. UserDto user = db.users().insertUser();
  85. userSession.logIn(user);
  86. when(dispatchers.getGlobalDispatchers()).thenReturn(asList(NOTIF_MY_NEW_ISSUES, NOTIF_NEW_QUALITY_GATE_STATUS));
  87. call(NOTIF_NEW_QUALITY_GATE_STATUS, twitterChannel.getKey(), null, null);
  88. db.notifications().assertExists(twitterChannel.getKey(), NOTIF_NEW_QUALITY_GATE_STATUS, userSession.getUuid(), null);
  89. }
  90. @Test
  91. public void add_notification_on_private_with_USER_permission() {
  92. UserDto user = db.users().insertUser();
  93. userSession.logIn(user);
  94. ProjectDto project = db.components().insertPrivateProject().getProjectDto();
  95. userSession.addProjectPermission(USER, project);
  96. when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  97. when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  98. call(NOTIF_MY_NEW_ISSUES, null, project.getKey(), null);
  99. db.notifications().assertExists(defaultChannel.getKey(), NOTIF_MY_NEW_ISSUES, userSession.getUuid(), project);
  100. }
  101. @Test
  102. public void add_notification_on_public_project() {
  103. UserDto user = db.users().insertUser();
  104. userSession.logIn(user);
  105. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  106. userSession.registerProjects(project);
  107. when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  108. when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  109. call(NOTIF_MY_NEW_ISSUES, null, project.getKey(), null);
  110. db.notifications().assertExists(defaultChannel.getKey(), NOTIF_MY_NEW_ISSUES, userSession.getUuid(), project);
  111. }
  112. @Test
  113. public void add_a_global_notification_when_a_project_one_exists() {
  114. UserDto user = db.users().insertUser();
  115. userSession.logIn(user);
  116. when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  117. when(dispatchers.getProjectDispatchers()).thenReturn(asList(NOTIF_MY_NEW_ISSUES));
  118. ProjectDto project = db.components().insertPrivateProject().getProjectDto();
  119. userSession.addProjectPermission(USER, project);
  120. call(NOTIF_MY_NEW_ISSUES, null, project.getKey(), null);
  121. call(NOTIF_MY_NEW_ISSUES, null, null, null);
  122. db.notifications().assertExists(defaultChannel.getKey(), NOTIF_MY_NEW_ISSUES, userSession.getUuid(), project);
  123. db.notifications().assertExists(defaultChannel.getKey(), NOTIF_MY_NEW_ISSUES, userSession.getUuid(), null);
  124. }
  125. @Test
  126. public void add_a_notification_on_private_project_when_a_global_one_exists() {
  127. UserDto user = db.users().insertUser();
  128. userSession.logIn(user);
  129. when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  130. when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  131. ProjectDto project = db.components().insertPrivateProject().getProjectDto();
  132. call(NOTIF_MY_NEW_ISSUES, null, null, null);
  133. userSession.addProjectPermission(USER, project);
  134. call(NOTIF_MY_NEW_ISSUES, null, project.getKey(), null);
  135. db.notifications().assertExists(defaultChannel.getKey(), NOTIF_MY_NEW_ISSUES, userSession.getUuid(), project);
  136. db.notifications().assertExists(defaultChannel.getKey(), NOTIF_MY_NEW_ISSUES, userSession.getUuid(), null);
  137. }
  138. @Test
  139. public void add_a_notification_on_public_project_when_a_global_one_exists() {
  140. UserDto user = db.users().insertUser();
  141. userSession.logIn(user);
  142. when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  143. when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  144. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  145. userSession.registerProjects(project);
  146. call(NOTIF_MY_NEW_ISSUES, null, null, null);
  147. call(NOTIF_MY_NEW_ISSUES, null, project.getKey(), null);
  148. db.notifications().assertExists(defaultChannel.getKey(), NOTIF_MY_NEW_ISSUES, userSession.getUuid(), project);
  149. db.notifications().assertExists(defaultChannel.getKey(), NOTIF_MY_NEW_ISSUES, userSession.getUuid(), null);
  150. }
  151. @Test
  152. public void http_no_content() {
  153. UserDto user = db.users().insertUser();
  154. userSession.logIn(user);
  155. when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  156. TestResponse result = call(NOTIF_MY_NEW_ISSUES, null, null, null);
  157. assertThat(result.getStatus()).isEqualTo(HTTP_NO_CONTENT);
  158. }
  159. @Test
  160. public void add_a_notification_to_a_user_as_system_administrator() {
  161. UserDto user = db.users().insertUser();
  162. userSession.logIn(user).setSystemAdministrator();
  163. when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  164. call(NOTIF_MY_NEW_ISSUES, null, null, user.getLogin());
  165. db.notifications().assertExists(defaultChannel.getKey(), NOTIF_MY_NEW_ISSUES, user.getUuid(), null);
  166. }
  167. @Test
  168. public void fail_if_login_is_provided_and_unknown() {
  169. UserDto user = db.users().insertUser();
  170. userSession.logIn(user).setSystemAdministrator();
  171. when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  172. assertThatThrownBy(() -> call(NOTIF_MY_NEW_ISSUES, null, null, "LOGIN 404"))
  173. .isInstanceOf(NotFoundException.class)
  174. .hasMessageContaining("User 'LOGIN 404' not found");
  175. }
  176. @Test
  177. public void fail_if_login_provided_and_not_system_administrator() {
  178. UserDto user = db.users().insertUser();
  179. userSession.logIn(user).setNonSystemAdministrator();
  180. when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  181. String login = user.getLogin();
  182. assertThatThrownBy(() -> call(NOTIF_MY_NEW_ISSUES, null, null, login))
  183. .isInstanceOf(ForbiddenException.class);
  184. }
  185. @Test
  186. public void fail_if_project_provided_and_not_project_user() {
  187. ProjectDto project = db.components().insertPrivateProject().getProjectDto();
  188. UserDto user = db.users().insertUser();
  189. userSession.logIn(user);
  190. when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  191. String projectKey = project.getKey();
  192. assertThatThrownBy(() -> call(NOTIF_MY_NEW_ISSUES, null, projectKey, null))
  193. .isInstanceOf(ForbiddenException.class);
  194. }
  195. @Test
  196. public void fail_when_notification_already_exists() {
  197. UserDto user = db.users().insertUser();
  198. userSession.logIn(user);
  199. when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  200. call(NOTIF_MY_NEW_ISSUES, null, null, null);
  201. assertThatThrownBy(() -> call(NOTIF_MY_NEW_ISSUES, null, null, null))
  202. .isInstanceOf(IllegalArgumentException.class)
  203. .hasMessageContaining("Notification already added");
  204. }
  205. @Test
  206. public void fail_when_unknown_channel() {
  207. assertThatThrownBy(() -> call(NOTIF_MY_NEW_ISSUES, "Channel42", null, null))
  208. .isInstanceOf(IllegalArgumentException.class);
  209. }
  210. @Test
  211. public void fail_when_unknown_global_dispatcher() {
  212. UserDto user = db.users().insertUser();
  213. userSession.logIn(user);
  214. when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  215. assertThatThrownBy(() -> call("Dispatcher42", null, null, null))
  216. .isInstanceOf(BadRequestException.class)
  217. .hasMessageContaining("Value of parameter 'type' (Dispatcher42) must be one of: [Dispatcher1]");
  218. }
  219. @Test
  220. public void fail_when_unknown_project_dispatcher_on_private_project() {
  221. ProjectData project = db.components().insertPrivateProject();
  222. userSession.addProjectPermission(USER, project.getProjectDto());
  223. when(dispatchers.getGlobalDispatchers()).thenReturn(asList(NOTIF_MY_NEW_ISSUES, NOTIF_NEW_ISSUES));
  224. when(dispatchers.getProjectDispatchers()).thenReturn(asList(NOTIF_MY_NEW_ISSUES, NOTIF_NEW_ISSUES));
  225. assertThatThrownBy(() -> call("Dispatcher42", null, project.projectKey(), null))
  226. .isInstanceOf(BadRequestException.class)
  227. .hasMessageContaining("Value of parameter 'type' (Dispatcher42) must be one of: [Dispatcher1, Dispatcher2]");
  228. }
  229. @Test
  230. public void fail_when_unknown_project_dispatcher_on_public_project() {
  231. UserDto user = db.users().insertUser();
  232. userSession.logIn(user);
  233. ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
  234. when(dispatchers.getGlobalDispatchers()).thenReturn(asList(NOTIF_MY_NEW_ISSUES, NOTIF_NEW_ISSUES));
  235. when(dispatchers.getProjectDispatchers()).thenReturn(asList(NOTIF_MY_NEW_ISSUES, NOTIF_NEW_ISSUES));
  236. String projectKey = project.getKey();
  237. assertThatThrownBy(() -> call("Dispatcher42", null, projectKey, null))
  238. .isInstanceOf(BadRequestException.class)
  239. .hasMessageContaining("Value of parameter 'type' (Dispatcher42) must be one of: [Dispatcher1, Dispatcher2]");
  240. }
  241. @Test
  242. public void fail_when_no_dispatcher() {
  243. TestRequest request = ws.newRequest();
  244. assertThatThrownBy(request::execute)
  245. .isInstanceOf(IllegalArgumentException.class);
  246. }
  247. @Test
  248. public void fail_when_project_is_unknown() {
  249. UserDto user = db.users().insertUser();
  250. userSession.logIn(user);
  251. when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  252. when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  253. assertThatThrownBy(() -> call(NOTIF_MY_NEW_ISSUES, null, "Project-42", null))
  254. .isInstanceOf(NotFoundException.class);
  255. }
  256. @Test
  257. public void fail_when_component_is_not_a_project() {
  258. UserDto user = db.users().insertUser();
  259. userSession.logIn(user);
  260. db.components().insertPortfolioAndSnapshot(newPortfolio().setKey("VIEW_1"));
  261. when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  262. when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  263. assertThatThrownBy(() -> call(NOTIF_MY_NEW_ISSUES, null, "VIEW_1", null))
  264. .isInstanceOf(NotFoundException.class)
  265. .hasMessageContaining("Project 'VIEW_1' not found");
  266. }
  267. @Test
  268. public void fail_when_not_authenticated() {
  269. userSession.anonymous();
  270. when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  271. assertThatThrownBy(() -> call(NOTIF_MY_NEW_ISSUES, null, null, null))
  272. .isInstanceOf(UnauthorizedException.class);
  273. }
  274. @Test
  275. public void fail_when_user_does_not_have_USER_permission_on_private_project() {
  276. ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
  277. userSession.logIn().setNonSystemAdministrator();
  278. when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  279. when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
  280. String key = project.getKey();
  281. String login = userSession.getLogin();
  282. assertThatThrownBy(() -> call(NOTIF_MY_NEW_ISSUES, null, key, login))
  283. .isInstanceOf(ForbiddenException.class);
  284. }
  285. private TestResponse call(String type, @Nullable String channel, @Nullable String project, @Nullable String login) {
  286. TestRequest request = ws.newRequest();
  287. request.setParam(PARAM_TYPE, type);
  288. ofNullable(channel).ifPresent(channel1 -> request.setParam(PARAM_CHANNEL, channel1));
  289. ofNullable(project).ifPresent(project1 -> request.setParam(PARAM_PROJECT, project1));
  290. ofNullable(login).ifPresent(login1 -> request.setParam(PARAM_LOGIN, login1));
  291. return request.execute();
  292. }
  293. private static class FakeNotificationChannel extends NotificationChannel {
  294. private final String key;
  295. private FakeNotificationChannel(String key) {
  296. this.key = key;
  297. }
  298. @Override
  299. public String getKey() {
  300. return this.key;
  301. }
  302. @Override
  303. public boolean deliver(Notification notification, String username) {
  304. // do nothing
  305. return true;
  306. }
  307. }
  308. }