Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DeleteTemplateActionTest.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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.permission.ws.template;
  21. import java.util.Arrays;
  22. import javax.annotation.Nullable;
  23. import org.junit.Before;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.junit.rules.ExpectedException;
  27. import org.sonar.api.resources.Qualifiers;
  28. import org.sonar.api.utils.internal.AlwaysIncreasingSystem2;
  29. import org.sonar.api.web.UserRole;
  30. import org.sonar.db.DbClient;
  31. import org.sonar.db.DbTester;
  32. import org.sonar.db.component.ResourceTypesRule;
  33. import org.sonar.db.organization.OrganizationDto;
  34. import org.sonar.db.permission.template.PermissionTemplateDto;
  35. import org.sonar.db.permission.template.PermissionTemplateTesting;
  36. import org.sonar.db.user.GroupDto;
  37. import org.sonar.db.user.GroupTesting;
  38. import org.sonar.db.user.UserDto;
  39. import org.sonar.db.user.UserTesting;
  40. import org.sonar.server.component.ComponentFinder;
  41. import org.sonar.server.exceptions.BadRequestException;
  42. import org.sonar.server.exceptions.ForbiddenException;
  43. import org.sonar.server.exceptions.NotFoundException;
  44. import org.sonar.server.exceptions.UnauthorizedException;
  45. import org.sonar.server.organization.TestDefaultOrganizationProvider;
  46. import org.sonar.server.permission.ws.PermissionWsSupport;
  47. import org.sonar.server.tester.UserSessionRule;
  48. import org.sonar.server.usergroups.DefaultGroupFinder;
  49. import org.sonar.server.usergroups.ws.GroupWsSupport;
  50. import org.sonar.server.ws.TestRequest;
  51. import org.sonar.server.ws.TestResponse;
  52. import org.sonar.server.ws.WsActionTester;
  53. import static org.assertj.core.api.Assertions.assertThat;
  54. import static org.assertj.core.api.Assertions.fail;
  55. import static org.sonar.db.permission.OrganizationPermission.ADMINISTER;
  56. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION;
  57. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
  58. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
  59. public class DeleteTemplateActionTest {
  60. @Rule
  61. public DbTester db = DbTester.create(new AlwaysIncreasingSystem2());
  62. @Rule
  63. public ExpectedException expectedException = ExpectedException.none();
  64. private UserSessionRule userSession = UserSessionRule.standalone();
  65. private DbClient dbClient = db.getDbClient();
  66. private final ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT);
  67. private final ResourceTypesRule resourceTypesWithViews = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW);
  68. private DefaultTemplatesResolver defaultTemplatesResolver = new DefaultTemplatesResolverImpl(resourceTypes);
  69. private DefaultTemplatesResolver defaultTemplatesResolverWithViews = new DefaultTemplatesResolverImpl(resourceTypesWithViews);
  70. private WsActionTester underTestWithoutViews;
  71. private WsActionTester underTestWithViews;
  72. @Before
  73. public void setUp() throws Exception {
  74. GroupWsSupport groupWsSupport = new GroupWsSupport(dbClient, TestDefaultOrganizationProvider.from(db), new DefaultGroupFinder(db.getDbClient()));
  75. this.underTestWithoutViews = new WsActionTester(new DeleteTemplateAction(dbClient, userSession,
  76. new PermissionWsSupport(dbClient, new ComponentFinder(dbClient, resourceTypes), groupWsSupport), defaultTemplatesResolver));
  77. this.underTestWithViews = new WsActionTester(new DeleteTemplateAction(dbClient, userSession,
  78. new PermissionWsSupport(dbClient, new ComponentFinder(dbClient, resourceTypes), groupWsSupport), defaultTemplatesResolverWithViews));
  79. }
  80. @Test
  81. public void delete_template_in_db() throws Exception {
  82. runOnAllUnderTests((underTest) -> {
  83. OrganizationDto organization = db.organizations().insert();
  84. PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
  85. db.organizations().setDefaultTemplates(
  86. db.permissionTemplates().insertTemplate(organization),
  87. null, db.permissionTemplates().insertTemplate(organization)
  88. );
  89. loginAsAdmin(organization);
  90. TestResponse result = newRequestByUuid(underTest, template.getUuid());
  91. assertThat(result.getInput()).isEmpty();
  92. assertTemplateDoesNotExist(template);
  93. });
  94. }
  95. @Test
  96. public void delete_template_by_name_case_insensitive() throws Exception {
  97. runOnAllUnderTests((underTest) -> {
  98. OrganizationDto organization = db.organizations().insert();
  99. db.organizations().setDefaultTemplates(
  100. db.permissionTemplates().insertTemplate(organization),
  101. db.permissionTemplates().insertTemplate(organization), db.permissionTemplates().insertTemplate(organization)
  102. );
  103. PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
  104. loginAsAdmin(organization);
  105. newRequestByName(underTest, organization, template);
  106. assertTemplateDoesNotExist(template);
  107. });
  108. }
  109. @Test
  110. public void delete_template_by_name_returns_empty_when_no_organization_is_provided_and_templates_does_not_belong_to_default_organization() throws Exception {
  111. OrganizationDto organization = db.organizations().insert();
  112. db.organizations().setDefaultTemplates(
  113. db.permissionTemplates().insertTemplate(organization),
  114. db.permissionTemplates().insertTemplate(organization), db.permissionTemplates().insertTemplate(organization)
  115. );
  116. PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
  117. loginAsAdmin(organization);
  118. runOnAllUnderTests((underTest) -> {
  119. try {
  120. newRequestByName(underTest, null, template);
  121. fail("NotFoundException should have been raised");
  122. } catch (NotFoundException e) {
  123. assertThat(e).hasMessage(
  124. "Permission template with name '" + template.getName() + "' is not found (case insensitive) in organization with key '" + db.getDefaultOrganization().getKey() + "'");
  125. }
  126. });
  127. }
  128. @Test
  129. public void delete_template_by_name_returns_empty_when_wrong_organization_is_provided() throws Exception {
  130. OrganizationDto organization = db.organizations().insert();
  131. db.organizations().setDefaultTemplates(
  132. db.permissionTemplates().insertTemplate(organization),
  133. db.permissionTemplates().insertTemplate(organization), db.permissionTemplates().insertTemplate(organization)
  134. );
  135. PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
  136. OrganizationDto otherOrganization = db.organizations().insert();
  137. loginAsAdmin(organization);
  138. runOnAllUnderTests((underTest) -> {
  139. try {
  140. newRequestByName(underTest, otherOrganization, template);
  141. fail("NotFoundException should have been raised");
  142. } catch (NotFoundException e) {
  143. assertThat(e)
  144. .hasMessage("Permission template with name '" + template.getName() + "' is not found (case insensitive) in organization with key '" + otherOrganization.getKey() + "'");
  145. }
  146. });
  147. }
  148. @Test
  149. public void fail_if_uuid_is_not_known_without_views() {
  150. userSession.logIn();
  151. expectedException.expect(NotFoundException.class);
  152. newRequestByUuid(underTestWithoutViews, "unknown-template-uuid");
  153. }
  154. @Test
  155. public void fail_if_uuid_is_not_known_with_views() {
  156. userSession.logIn();
  157. expectedException.expect(NotFoundException.class);
  158. newRequestByUuid(underTestWithViews, "unknown-template-uuid");
  159. }
  160. @Test
  161. public void fail_to_delete_by_uuid_if_template_is_default_template_for_project_without_views() throws Exception {
  162. fail_to_delete_by_uuid_if_template_is_default_template_for_project(this.underTestWithoutViews);
  163. }
  164. @Test
  165. public void fail_to_delete_by_uuid_if_template_is_default_template_for_project_with_views() throws Exception {
  166. fail_to_delete_by_uuid_if_template_is_default_template_for_project(this.underTestWithViews);
  167. }
  168. private void fail_to_delete_by_uuid_if_template_is_default_template_for_project(WsActionTester underTest) {
  169. OrganizationDto organization = db.organizations().insert();
  170. PermissionTemplateDto projectTemplate = insertTemplateAndAssociatedPermissions(organization);
  171. db.organizations().setDefaultTemplates(projectTemplate,
  172. null, db.permissionTemplates().insertTemplate(organization));
  173. loginAsAdmin(organization);
  174. expectedException.expect(BadRequestException.class);
  175. expectedException.expectMessage("It is not possible to delete the default permission template for projects");
  176. newRequestByUuid(underTest, projectTemplate.getUuid());
  177. }
  178. @Test
  179. public void fail_to_delete_by_name_if_template_is_default_template_for_project_without_views() throws Exception {
  180. fail_to_delete_by_name_if_template_is_default_template_for_project(this.underTestWithoutViews);
  181. }
  182. @Test
  183. public void fail_to_delete_by_name_if_template_is_default_template_for_project_with_views() throws Exception {
  184. fail_to_delete_by_name_if_template_is_default_template_for_project(this.underTestWithViews);
  185. }
  186. private void fail_to_delete_by_name_if_template_is_default_template_for_project(WsActionTester underTest) {
  187. OrganizationDto organization = db.organizations().insert();
  188. PermissionTemplateDto projectTemplate = insertTemplateAndAssociatedPermissions(organization);
  189. db.organizations().setDefaultTemplates(projectTemplate, null, db.permissionTemplates().insertTemplate(organization));
  190. loginAsAdmin(organization);
  191. expectedException.expect(BadRequestException.class);
  192. expectedException.expectMessage("It is not possible to delete the default permission template for projects");
  193. newRequestByName(underTest, organization.getKey(), projectTemplate.getName());
  194. }
  195. @Test
  196. public void fail_to_delete_by_uuid_if_template_is_default_template_for_portfolios_with_views() {
  197. OrganizationDto organization = db.organizations().insert();
  198. PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
  199. db.organizations().setDefaultTemplates(db.permissionTemplates().insertTemplate(organization), null, template);
  200. loginAsAdmin(organization);
  201. expectedException.expect(BadRequestException.class);
  202. expectedException.expectMessage("It is not possible to delete the default permission template for portfolios");
  203. newRequestByUuid(this.underTestWithViews, template.getUuid());
  204. }
  205. @Test
  206. public void fail_to_delete_by_uuid_if_template_is_default_template_for_applications_with_views() {
  207. OrganizationDto organization = db.organizations().insert();
  208. PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
  209. db.organizations().setDefaultTemplates(db.permissionTemplates().insertTemplate(organization), template, null);
  210. loginAsAdmin(organization);
  211. expectedException.expect(BadRequestException.class);
  212. expectedException.expectMessage("It is not possible to delete the default permission template for applications");
  213. newRequestByUuid(this.underTestWithViews, template.getUuid());
  214. }
  215. @Test
  216. public void default_template_for_views_can_be_deleted_by_uuid_if_views_is_not_installed_and_default_template_for_views_is_reset() {
  217. OrganizationDto organization = db.organizations().insert();
  218. PermissionTemplateDto projectTemplate = db.permissionTemplates().insertTemplate(organization);
  219. PermissionTemplateDto viewTemplate = insertTemplateAndAssociatedPermissions(organization);
  220. db.organizations().setDefaultTemplates(projectTemplate, null, viewTemplate);
  221. loginAsAdmin(organization);
  222. newRequestByUuid(this.underTestWithoutViews, viewTemplate.getUuid());
  223. assertTemplateDoesNotExist(viewTemplate);
  224. assertThat(db.getDbClient().organizationDao().getDefaultTemplates(db.getSession(), organization.getUuid())
  225. .get().getApplicationsUuid())
  226. .isNull();
  227. }
  228. @Test
  229. public void fail_to_delete_by_uuid_if_not_logged_in_without_views() {
  230. expectedException.expect(UnauthorizedException.class);
  231. newRequestByUuid(underTestWithoutViews, "uuid");
  232. }
  233. @Test
  234. public void fail_to_delete_by_uuid_if_not_logged_in_with_views() {
  235. expectedException.expect(UnauthorizedException.class);
  236. newRequestByUuid(underTestWithViews, "uuid");
  237. }
  238. @Test
  239. public void fail_to_delete_by_name_if_not_logged_in_without_views() {
  240. expectedException.expect(UnauthorizedException.class);
  241. newRequestByName(underTestWithoutViews, "whatever", "name");
  242. }
  243. @Test
  244. public void fail_to_delete_by_name_if_not_logged_in_with_views() {
  245. expectedException.expect(UnauthorizedException.class);
  246. newRequestByName(underTestWithViews, "whatever", "name");
  247. }
  248. @Test
  249. public void fail_to_delete_by_uuid_if_not_admin_without_views() {
  250. OrganizationDto organization = db.organizations().insert();
  251. PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
  252. userSession.logIn();
  253. expectedException.expect(ForbiddenException.class);
  254. newRequestByUuid(underTestWithoutViews, template.getUuid());
  255. }
  256. @Test
  257. public void fail_to_delete_by_uuid_if_not_admin_with_views() {
  258. OrganizationDto organization = db.organizations().insert();
  259. PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
  260. userSession.logIn();
  261. expectedException.expect(ForbiddenException.class);
  262. newRequestByUuid(underTestWithViews, template.getUuid());
  263. }
  264. @Test
  265. public void fail_to_delete_by_name_if_not_admin_without_views() {
  266. OrganizationDto organization = db.organizations().insert();
  267. PermissionTemplateDto template = db.permissionTemplates().insertTemplate(organization);
  268. userSession.logIn();
  269. expectedException.expect(ForbiddenException.class);
  270. newRequestByName(underTestWithoutViews, organization.getKey(), template.getName());
  271. }
  272. @Test
  273. public void fail_to_delete_by_name_if_not_admin_with_views() throws Exception {
  274. OrganizationDto organization = db.organizations().insert();
  275. PermissionTemplateDto template = db.permissionTemplates().insertTemplate(PermissionTemplateTesting.newPermissionTemplateDto()
  276. .setOrganizationUuid(organization.getUuid())
  277. .setName("the name"));
  278. userSession.logIn();
  279. expectedException.expect(ForbiddenException.class);
  280. newRequestByName(underTestWithViews, organization, template);
  281. }
  282. @Test
  283. public void fail_if_neither_uuid_nor_name_is_provided_without_views() {
  284. userSession.logIn();
  285. expectedException.expect(BadRequestException.class);
  286. newRequestByUuid(underTestWithoutViews, null);
  287. }
  288. @Test
  289. public void fail_if_neither_uuid_nor_name_is_provided_with_views() {
  290. userSession.logIn();
  291. expectedException.expect(BadRequestException.class);
  292. newRequestByUuid(underTestWithViews, null);
  293. }
  294. @Test
  295. public void fail_if_both_uuid_and_name_are_provided_without_views() {
  296. userSession.logIn();
  297. expectedException.expect(BadRequestException.class);
  298. underTestWithoutViews.newRequest().setMethod("POST")
  299. .setParam(PARAM_TEMPLATE_ID, "uuid")
  300. .setParam(PARAM_TEMPLATE_NAME, "name")
  301. .execute();
  302. }
  303. @Test
  304. public void fail_if_both_uuid_and_name_are_provided_with_views() {
  305. userSession.logIn();
  306. expectedException.expect(BadRequestException.class);
  307. underTestWithViews.newRequest().setMethod("POST")
  308. .setParam(PARAM_TEMPLATE_ID, "uuid")
  309. .setParam(PARAM_TEMPLATE_NAME, "name")
  310. .execute();
  311. }
  312. // @Test
  313. // public void delete_perm_tpl_characteristic_when_delete_template() throws Exception {
  314. // db.getDbClient().permissionTemplateCharacteristicDao().insert(db.getSession(), new PermissionTemplateCharacteristicDto()
  315. // .setPermission(UserRole.USER)
  316. // .setTemplateId(template.getId())
  317. // .setWithProjectCreator(true)
  318. // .setCreatedAt(new Date().getTime())
  319. // .setUpdatedAt(new Date().getTime()));
  320. // db.commit();
  321. //
  322. // newRequest(template.getUuid());
  323. //
  324. // assertThat(db.getDbClient().permissionTemplateCharacteristicDao().selectByTemplateIds(db.getSession(),
  325. // asList(template.getId()))).isEmpty();
  326. // }
  327. private UserSessionRule loginAsAdmin(OrganizationDto organization) {
  328. return userSession.logIn().addPermission(ADMINISTER, organization);
  329. }
  330. private void runOnAllUnderTests(ConsumerWithException<WsActionTester> consumer) throws Exception {
  331. for (WsActionTester underTest : Arrays.asList(underTestWithoutViews, underTestWithViews)) {
  332. consumer.accept(underTest);
  333. }
  334. }
  335. private interface ConsumerWithException<T> {
  336. void accept(T e) throws Exception;
  337. }
  338. private PermissionTemplateDto insertTemplateAndAssociatedPermissions(OrganizationDto organization) {
  339. PermissionTemplateDto dto = db.permissionTemplates().insertTemplate(organization);
  340. UserDto user = db.getDbClient().userDao().insert(db.getSession(), UserTesting.newUserDto().setActive(true));
  341. GroupDto group = db.getDbClient().groupDao().insert(db.getSession(), GroupTesting.newGroupDto());
  342. db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), dto.getId(), user.getId(), UserRole.ADMIN);
  343. db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), dto.getId(), group.getId(), UserRole.CODEVIEWER);
  344. db.commit();
  345. return dto;
  346. }
  347. private TestResponse newRequestByUuid(WsActionTester actionTester, @Nullable String id) {
  348. TestRequest request = actionTester.newRequest().setMethod("POST");
  349. if (id != null) {
  350. request.setParam(PARAM_TEMPLATE_ID, id);
  351. }
  352. return request.execute();
  353. }
  354. private TestResponse newRequestByName(WsActionTester actionTester, @Nullable OrganizationDto organizationDto, @Nullable PermissionTemplateDto permissionTemplateDto)
  355. throws Exception {
  356. return newRequestByName(
  357. actionTester,
  358. organizationDto == null ? null : organizationDto.getKey(),
  359. permissionTemplateDto == null ? null : permissionTemplateDto.getName());
  360. }
  361. private TestResponse newRequestByName(WsActionTester actionTester, @Nullable String organizationKey, @Nullable String name) {
  362. TestRequest request = actionTester.newRequest().setMethod("POST");
  363. if (organizationKey != null) {
  364. request.setParam(PARAM_ORGANIZATION, organizationKey);
  365. }
  366. if (name != null) {
  367. request.setParam(PARAM_TEMPLATE_NAME, name);
  368. }
  369. return request.execute();
  370. }
  371. private void assertTemplateDoesNotExist(PermissionTemplateDto template) {
  372. assertThat(db.getDbClient().permissionTemplateDao().selectByUuid(db.getSession(), template.getUuid())).isNull();
  373. }
  374. }