3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.platform.db.migration.version.v64;
22 import java.sql.SQLException;
23 import java.util.stream.Stream;
24 import javax.annotation.Nullable;
25 import org.apache.commons.lang.RandomStringUtils;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.sonar.core.util.stream.MoreCollectors;
30 import org.sonar.db.CoreDbTester;
31 import org.sonar.server.platform.db.migration.version.v63.DefaultOrganizationUuidProviderImpl;
33 import static java.lang.String.valueOf;
34 import static org.apache.commons.lang.math.RandomUtils.nextLong;
35 import static org.assertj.core.api.Assertions.assertThat;
37 public class SupportPrivateProjectInDefaultPermissionTemplateTest {
39 private static final String DEFAULT_ORGANIZATION_UUID = "def org uuid";
40 private static final String OTHER_ORGANIZATION_UUID = "not def org uuid";
41 private static final String PERMISSION_USER = "user";
42 private static final String PERMISSION_CODEVIEWER = "codeviewer";
45 public CoreDbTester db = CoreDbTester.createForSchema(SupportPrivateProjectInDefaultPermissionTemplateTest.class, "organizations_and_groups_and_permission_templates.sql");
47 public ExpectedException expectedException = ExpectedException.none();
49 private SupportPrivateProjectInDefaultPermissionTemplate underTest = new SupportPrivateProjectInDefaultPermissionTemplate(db.database(), new DefaultOrganizationUuidProviderImpl());
52 public void fails_with_ISE_when_no_default_organization_is_set() throws SQLException {
53 expectedException.expect(IllegalStateException.class);
54 expectedException.expectMessage("Default organization uuid is missing");
60 public void fails_with_ISE_when_default_organization_does_not_exist_in_table_ORGANIZATIONS() throws SQLException {
61 setDefaultOrganizationProperty("blabla");
63 expectedException.expect(IllegalStateException.class);
64 expectedException.expectMessage("Default organization with uuid 'blabla' does not exist in table ORGANIZATIONS");
70 public void execute_fails_with_ISE_when_default_organization_has_no_default_groupId() throws SQLException {
71 setupDefaultOrganization(null, "pt1", "pt2");
73 expectedException.expect(IllegalStateException.class);
74 expectedException.expectMessage("No default group id is defined for default organization (uuid=def org uuid)");
80 public void execute_fails_with_ISE_when_default_group_of_default_organization_does_not_exist() throws SQLException {
81 setupDefaultOrganization(112, "pT1", "pT2");
83 expectedException.expect(IllegalStateException.class);
84 expectedException.expectMessage("Permission template with uuid pT1 not found");
90 public void execute_does_nothing_when_default_organization_has_default_permission_template_for_projects() throws SQLException {
91 int groupId = insertGroup(DEFAULT_ORGANIZATION_UUID);
92 setupDefaultOrganization(groupId, null, null);
98 public void execute_fails_with_ISE_when_default_organization_has_default_permission_template_for_views_but_not_for_projects() throws SQLException {
99 int groupId = insertGroup(DEFAULT_ORGANIZATION_UUID);
100 setupDefaultOrganization(groupId, null, "pt1");
102 expectedException.expect(IllegalStateException.class);
103 expectedException.expectMessage("Inconsistent state for default organization (uuid=def org uuid): no project default template is defined but view default template is");
109 public void execute_fails_with_ISE_when_default_permission_template_for_projects_of_default_organization_does_not_exist() throws SQLException {
110 int groupId = insertGroup(DEFAULT_ORGANIZATION_UUID);
111 setupDefaultOrganization(groupId, "foBar2000", "pt2");
113 expectedException.expect(IllegalStateException.class);
114 expectedException.expectMessage("Permission template with uuid foBar2000 not found");
120 public void execute_does_not_fail_when_default_organization_has_default_permission_template_for_view() throws SQLException {
121 int groupId = insertGroup(DEFAULT_ORGANIZATION_UUID);
122 IdAndUuid projectDefPermTemplate = insertPermissionTemplate(DEFAULT_ORGANIZATION_UUID);
123 setupDefaultOrganization(groupId, projectDefPermTemplate.uuid, null);
129 public void execute_adds_permission_USER_and_CODEVIEWER_to_default_group_of_default_organization_in_its_default_project_template() throws SQLException {
130 int groupId = insertGroup(DEFAULT_ORGANIZATION_UUID);
131 IdAndUuid projectDefPermTemplate = insertPermissionTemplate(DEFAULT_ORGANIZATION_UUID);
132 setupDefaultOrganization(groupId, projectDefPermTemplate.uuid, null);
133 int otherGroupId = insertGroup(OTHER_ORGANIZATION_UUID);
134 IdAndUuid otherProjectDefPermTemplate = insertPermissionTemplate(OTHER_ORGANIZATION_UUID);
135 insertOrganization(OTHER_ORGANIZATION_UUID, otherGroupId, otherProjectDefPermTemplate.uuid, null);
139 verifyPermissionOfGroupInTemplate(projectDefPermTemplate, groupId, PERMISSION_USER, PERMISSION_CODEVIEWER);
140 verifyPermissionOfGroupInTemplate(otherProjectDefPermTemplate, otherGroupId);
144 public void execute_does_not_fail_if_default_group_already_has_permission_USER_and_adds_only_CODEVIEWER_to_default_group_of_default_organization_in_its_default_project_template()
145 throws SQLException {
146 int groupId = insertGroup(DEFAULT_ORGANIZATION_UUID);
147 IdAndUuid projectDefPermTemplate = insertPermissionTemplate(DEFAULT_ORGANIZATION_UUID);
148 insertGroupPermission(projectDefPermTemplate, groupId, PERMISSION_USER);
149 setupDefaultOrganization(groupId, projectDefPermTemplate.uuid, null);
150 int otherGroupId = insertGroup(OTHER_ORGANIZATION_UUID);
151 IdAndUuid otherProjectDefPermTemplateUuid = insertPermissionTemplate(OTHER_ORGANIZATION_UUID);
152 insertOrganization(OTHER_ORGANIZATION_UUID, otherGroupId, otherProjectDefPermTemplateUuid.uuid, null);
156 verifyPermissionOfGroupInTemplate(projectDefPermTemplate, groupId, PERMISSION_USER, PERMISSION_CODEVIEWER);
157 verifyPermissionOfGroupInTemplate(otherProjectDefPermTemplateUuid, otherGroupId);
161 public void execute_does_not_fail_if_default_group_already_has_permission_CODEVIEWER_and_adds_only_USER_to_default_group_of_default_organization_in_its_default_project_template()
162 throws SQLException {
163 int groupId = insertGroup(DEFAULT_ORGANIZATION_UUID);
164 IdAndUuid projectDefPermTemplate = insertPermissionTemplate(DEFAULT_ORGANIZATION_UUID);
165 insertGroupPermission(projectDefPermTemplate, groupId, PERMISSION_CODEVIEWER);
166 setupDefaultOrganization(groupId, projectDefPermTemplate.uuid, null);
167 int otherGroupId = insertGroup(OTHER_ORGANIZATION_UUID);
168 IdAndUuid otherProjectDefPermTemplateUuid = insertPermissionTemplate(OTHER_ORGANIZATION_UUID);
169 insertOrganization(OTHER_ORGANIZATION_UUID, otherGroupId, otherProjectDefPermTemplateUuid.uuid, null);
173 verifyPermissionOfGroupInTemplate(projectDefPermTemplate, groupId, PERMISSION_USER, PERMISSION_CODEVIEWER);
174 verifyPermissionOfGroupInTemplate(otherProjectDefPermTemplateUuid, otherGroupId);
178 public void execute_is_reentrant()
179 throws SQLException {
180 int groupId = insertGroup(DEFAULT_ORGANIZATION_UUID);
181 IdAndUuid projectDefPermTemplate = insertPermissionTemplate(DEFAULT_ORGANIZATION_UUID);
182 setupDefaultOrganization(groupId, projectDefPermTemplate.uuid, null);
183 int otherGroupId = insertGroup(OTHER_ORGANIZATION_UUID);
184 IdAndUuid otherProjectDefPermTemplateUuid = insertPermissionTemplate(OTHER_ORGANIZATION_UUID);
185 insertOrganization(OTHER_ORGANIZATION_UUID, otherGroupId, otherProjectDefPermTemplateUuid.uuid, null);
191 verifyPermissionOfGroupInTemplate(projectDefPermTemplate, groupId, PERMISSION_USER, PERMISSION_CODEVIEWER);
192 verifyPermissionOfGroupInTemplate(otherProjectDefPermTemplateUuid, otherGroupId);
195 private void insertGroupPermission(IdAndUuid permissionTemplate, int groupId, String permission) {
197 "PERM_TEMPLATES_GROUPS",
199 "TEMPLATE_ID", permissionTemplate.id,
200 "PERMISSION_REFERENCE", permission);
203 private void verifyPermissionOfGroupInTemplate(IdAndUuid permTemplate, int groupId, String... permissions) {
204 verifyPermissionOfGroupInTemplate(permTemplate.uuid, groupId, permissions);
207 private void verifyPermissionOfGroupInTemplate(String permTemplateUuid, int groupId, String... permissions) {
209 db.select("select permission_reference as \"permission\" from perm_templates_groups ptg inner join permission_templates pt on pt.kee='" + permTemplateUuid
210 + "' where ptg.template_id=pt.id and group_id=" + groupId)
212 .flatMap(row -> Stream.of((String) row.get("permission")))
213 .collect(MoreCollectors.toList()))
214 .containsOnly(permissions);
217 private void setupDefaultOrganization(@Nullable Integer defaultGroupId, @Nullable String projectPermTemplateUuid, @Nullable String viewPermTemplateUuid) {
218 setDefaultOrganizationProperty(DEFAULT_ORGANIZATION_UUID);
219 insertOrganization(DEFAULT_ORGANIZATION_UUID, defaultGroupId, projectPermTemplateUuid, viewPermTemplateUuid);
222 private void setDefaultOrganizationProperty(String defaultOrganizationUuid) {
224 "INTERNAL_PROPERTIES",
225 "KEE", "organization.default",
227 "TEXT_VALUE", defaultOrganizationUuid);
230 private void insertOrganization(String uuid, @Nullable Integer defaultGroupId, @Nullable String projectPermTemplateUuid, @Nullable String viewPermTemplateUuid) {
231 db.executeInsert("ORGANIZATIONS",
236 "default_group_id", defaultGroupId == null ? null : valueOf(defaultGroupId),
237 "default_perm_template_project", projectPermTemplateUuid,
238 "default_perm_template_view", viewPermTemplateUuid,
239 "CREATED_AT", nextLong(),
240 "UPDATED_AT", nextLong());
243 private int insertGroup(String organizationUuid) {
244 String name = "name" + RandomStringUtils.random(20);
247 "ORGANIZATION_UUID", organizationUuid,
250 return ((Long) db.selectFirst("select id as \"ID\" from groups where name='" + name + "'").get("ID")).intValue();
253 private IdAndUuid insertPermissionTemplate(String organizationUuid) {
254 String random = RandomStringUtils.randomAscii(20);
255 String uuid = "ptUuid" + random;
257 "PERMISSION_TEMPLATES",
258 "ORGANIZATION_UUID", organizationUuid,
259 "NAME", "name" + random,
261 return new IdAndUuid(
262 ((Long) db.selectFirst("select id as \"ID\" from permission_templates where kee='" + uuid + "'").get("ID")).intValue(),
266 private static final class IdAndUuid {
267 private final int id;
268 private final String uuid;
270 private IdAndUuid(int id, String uuid) {