]> source.dussan.org Git - sonarqube.git/blob
daac2f09ec5a8c096018baec3a323d681df88fdd
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.v64;
21
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;
32
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;
36
37 public class SupportPrivateProjectInDefaultPermissionTemplateTest {
38
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";
43
44   @Rule
45   public CoreDbTester db = CoreDbTester.createForSchema(SupportPrivateProjectInDefaultPermissionTemplateTest.class, "organizations_and_groups_and_permission_templates.sql");
46   @Rule
47   public ExpectedException expectedException = ExpectedException.none();
48
49   private SupportPrivateProjectInDefaultPermissionTemplate underTest = new SupportPrivateProjectInDefaultPermissionTemplate(db.database(), new DefaultOrganizationUuidProviderImpl());
50
51   @Test
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");
55
56     underTest.execute();
57   }
58
59   @Test
60   public void fails_with_ISE_when_default_organization_does_not_exist_in_table_ORGANIZATIONS() throws SQLException {
61     setDefaultOrganizationProperty("blabla");
62
63     expectedException.expect(IllegalStateException.class);
64     expectedException.expectMessage("Default organization with uuid 'blabla' does not exist in table ORGANIZATIONS");
65
66     underTest.execute();
67   }
68
69   @Test
70   public void execute_fails_with_ISE_when_default_organization_has_no_default_groupId() throws SQLException {
71     setupDefaultOrganization(null, "pt1", "pt2");
72
73     expectedException.expect(IllegalStateException.class);
74     expectedException.expectMessage("No default group id is defined for default organization (uuid=def org uuid)");
75
76     underTest.execute();
77   }
78
79   @Test
80   public void execute_fails_with_ISE_when_default_group_of_default_organization_does_not_exist() throws SQLException {
81     setupDefaultOrganization(112, "pT1", "pT2");
82
83     expectedException.expect(IllegalStateException.class);
84     expectedException.expectMessage("Permission template with uuid pT1 not found");
85
86     underTest.execute();
87   }
88
89   @Test
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);
93
94     underTest.execute();
95   }
96
97   @Test
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");
101
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");
104
105     underTest.execute();
106   }
107
108   @Test
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");
112
113     expectedException.expect(IllegalStateException.class);
114     expectedException.expectMessage("Permission template with uuid foBar2000 not found");
115
116     underTest.execute();
117   }
118
119   @Test
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);
124
125     underTest.execute();
126   }
127
128   @Test
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);
136
137     underTest.execute();
138
139     verifyPermissionOfGroupInTemplate(projectDefPermTemplate, groupId, PERMISSION_USER, PERMISSION_CODEVIEWER);
140     verifyPermissionOfGroupInTemplate(otherProjectDefPermTemplate, otherGroupId);
141   }
142
143   @Test
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);
153
154     underTest.execute();
155
156     verifyPermissionOfGroupInTemplate(projectDefPermTemplate, groupId, PERMISSION_USER, PERMISSION_CODEVIEWER);
157     verifyPermissionOfGroupInTemplate(otherProjectDefPermTemplateUuid, otherGroupId);
158   }
159
160   @Test
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);
170
171     underTest.execute();
172
173     verifyPermissionOfGroupInTemplate(projectDefPermTemplate, groupId, PERMISSION_USER, PERMISSION_CODEVIEWER);
174     verifyPermissionOfGroupInTemplate(otherProjectDefPermTemplateUuid, otherGroupId);
175   }
176
177   @Test
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);
186
187     underTest.execute();
188
189     underTest.execute();
190
191     verifyPermissionOfGroupInTemplate(projectDefPermTemplate, groupId, PERMISSION_USER, PERMISSION_CODEVIEWER);
192     verifyPermissionOfGroupInTemplate(otherProjectDefPermTemplateUuid, otherGroupId);
193   }
194
195   private void insertGroupPermission(IdAndUuid permissionTemplate, int groupId, String permission) {
196     db.executeInsert(
197       "PERM_TEMPLATES_GROUPS",
198       "GROUP_ID", groupId,
199       "TEMPLATE_ID", permissionTemplate.id,
200       "PERMISSION_REFERENCE", permission);
201   }
202
203   private void verifyPermissionOfGroupInTemplate(IdAndUuid permTemplate, int groupId, String... permissions) {
204     verifyPermissionOfGroupInTemplate(permTemplate.uuid, groupId, permissions);
205   }
206
207   private void verifyPermissionOfGroupInTemplate(String permTemplateUuid, int groupId, String... permissions) {
208     assertThat(
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)
211         .stream()
212         .flatMap(row -> Stream.of((String) row.get("permission")))
213         .collect(MoreCollectors.toList()))
214           .containsOnly(permissions);
215   }
216
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);
220   }
221
222   private void setDefaultOrganizationProperty(String defaultOrganizationUuid) {
223     db.executeInsert(
224       "INTERNAL_PROPERTIES",
225       "KEE", "organization.default",
226       "IS_EMPTY", "false",
227       "TEXT_VALUE", defaultOrganizationUuid);
228   }
229
230   private void insertOrganization(String uuid, @Nullable Integer defaultGroupId, @Nullable String projectPermTemplateUuid, @Nullable String viewPermTemplateUuid) {
231     db.executeInsert("ORGANIZATIONS",
232       "UUID", uuid,
233       "KEE", uuid,
234       "NAME", uuid,
235       "GUARDED", false,
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());
241   }
242
243   private int insertGroup(String organizationUuid) {
244     String name = "name" + RandomStringUtils.random(20);
245     db.executeInsert(
246       "GROUPS",
247       "ORGANIZATION_UUID", organizationUuid,
248       "NAME", name);
249
250     return ((Long) db.selectFirst("select id as \"ID\" from groups where name='" + name + "'").get("ID")).intValue();
251   }
252
253   private IdAndUuid insertPermissionTemplate(String organizationUuid) {
254     String random = RandomStringUtils.randomAlphanumeric(20);
255     String uuid = "ptUuid" + random;
256     db.executeInsert(
257       "PERMISSION_TEMPLATES",
258       "ORGANIZATION_UUID", organizationUuid,
259       "NAME", "name" + random,
260       "KEE", uuid);
261     return new IdAndUuid(
262       ((Long) db.selectFirst("select id as \"ID\" from permission_templates where kee='" + uuid + "'").get("ID")).intValue(),
263       uuid);
264   }
265
266   private static final class IdAndUuid {
267     private final int id;
268     private final String uuid;
269
270     private IdAndUuid(int id, String uuid) {
271       this.id = id;
272       this.uuid = uuid;
273     }
274   }
275 }