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.

AddApplicationCreatorAndPortfolioCreatorToSonarAdministratorTest.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.platform.db.migration.version.v74;
  21. import java.sql.SQLException;
  22. import java.util.Date;
  23. import java.util.stream.Collectors;
  24. import javax.annotation.Nullable;
  25. import org.assertj.core.groups.Tuple;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.sonar.api.impl.config.MapSettings;
  29. import org.sonar.core.util.UuidFactoryFast;
  30. import org.sonar.db.CoreDbTester;
  31. import org.sonar.server.platform.db.migration.version.v63.DefaultOrganizationUuidProvider;
  32. import org.sonar.server.platform.db.migration.version.v63.DefaultOrganizationUuidProviderImpl;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. import static org.assertj.core.api.Assertions.tuple;
  35. public class AddApplicationCreatorAndPortfolioCreatorToSonarAdministratorTest {
  36. @Rule
  37. public CoreDbTester db = CoreDbTester.createForSchema(AddApplicationCreatorAndPortfolioCreatorToSonarAdministratorTest.class,
  38. "group_roles_and_internal_properties.sql");
  39. private UuidFactoryFast uuidFactoryFast = UuidFactoryFast.getInstance();
  40. private MapSettings settings = new MapSettings();
  41. private DefaultOrganizationUuidProvider defaultOrganizationUuidProvider = new DefaultOrganizationUuidProviderImpl();
  42. private AddApplicationCreatorAndPortfolioCreatorToSonarAdministrator underTest = new AddApplicationCreatorAndPortfolioCreatorToSonarAdministrator(db.database(), settings.asConfig(),
  43. defaultOrganizationUuidProvider);
  44. @Test
  45. public void is_reentrant() throws SQLException {
  46. String orgUuid = uuidFactoryFast.create();
  47. insertDefaultOrganizationUuid(orgUuid);
  48. insertGroup(orgUuid, "sonar-administrators");
  49. Long adminGroupId = getGroupId("sonar-administrators");
  50. underTest.execute();
  51. underTest.execute();
  52. assertGroupRoles(
  53. tuple(orgUuid, adminGroupId, null, "applicationcreator"),
  54. tuple(orgUuid, adminGroupId, null, "portfoliocreator"));
  55. }
  56. @Test
  57. public void create_missing_permissions() throws SQLException {
  58. String orgUuid = uuidFactoryFast.create();
  59. insertDefaultOrganizationUuid(orgUuid);
  60. insertGroup(orgUuid, "sonar-administrators");
  61. Long adminGroupId = getGroupId("sonar-administrators");
  62. underTest.execute();
  63. assertGroupRoles(
  64. tuple(orgUuid, adminGroupId, null, "applicationcreator"),
  65. tuple(orgUuid, adminGroupId, null, "portfoliocreator"));
  66. }
  67. @Test
  68. public void has_no_effect_if_group_does_not_exist() throws SQLException {
  69. String orgUuid = uuidFactoryFast.create();
  70. insertDefaultOrganizationUuid(orgUuid);
  71. insertGroup(orgUuid, "sonar");
  72. underTest.execute();
  73. assertGroupRoles();
  74. }
  75. @Test
  76. public void has_no_effect_if_roles_are_already_present() throws SQLException {
  77. String orgUuid = uuidFactoryFast.create();
  78. insertDefaultOrganizationUuid(orgUuid);
  79. insertGroup(orgUuid, "sonar-administrators");
  80. Long adminGroupId = getGroupId("sonar-administrators");
  81. insertGroupRole(orgUuid, adminGroupId, null, "applicationcreator");
  82. insertGroupRole(orgUuid, adminGroupId, null, "portfoliocreator");
  83. underTest.execute();
  84. assertGroupRoles(
  85. tuple(orgUuid, adminGroupId, null, "applicationcreator"),
  86. tuple(orgUuid, adminGroupId, null, "portfoliocreator"));
  87. }
  88. @Test
  89. public void has_no_effect_on_SonarCloud() throws SQLException {
  90. settings.setProperty("sonar.sonarcloud.enabled", true);
  91. underTest.execute();
  92. assertGroupRoles();
  93. }
  94. private void insertDefaultOrganizationUuid(String uuid) {
  95. db.executeInsert("INTERNAL_PROPERTIES",
  96. "KEE", "organization.default",
  97. "IS_EMPTY", false,
  98. "TEXT_VALUE", uuid,
  99. "CREATED_AT", System.currentTimeMillis());
  100. }
  101. private void insertGroup(String organizationUuid, String name) {
  102. db.executeInsert("GROUPS",
  103. "ORGANIZATION_UUID", organizationUuid,
  104. "NAME", name,
  105. "CREATED_AT", new Date(),
  106. "UPDATED_AT", new Date());
  107. }
  108. private void insertGroupRole(String organizationUuid, @Nullable Long groupId, @Nullable Integer resourceId, String role) {
  109. db.executeInsert("GROUP_ROLES",
  110. "ORGANIZATION_UUID", organizationUuid,
  111. "GROUP_ID", groupId,
  112. "RESOURCE_ID", resourceId,
  113. "ROLE", role);
  114. }
  115. private Long getGroupId(String groupName) {
  116. return (Long) db.selectFirst("SELECT id FROM groups WHERE name = '" + groupName + "'").get("ID");
  117. }
  118. private void assertGroupRoles(Tuple... expectedTuples) {
  119. assertThat(db.select("SELECT organization_uuid, group_id, resource_id, role FROM group_roles")
  120. .stream()
  121. .map(row -> new Tuple(row.get("ORGANIZATION_UUID"), row.get("GROUP_ID"), row.get("RESOURCE_ID"), row.get("ROLE")))
  122. .collect(Collectors.toList()))
  123. .containsExactlyInAnyOrder(expectedTuples);
  124. }
  125. }