-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
SupportPrivateProjectInDefaultPermissionTemplateTest.java
275 lines (224 loc) · 11.9 KB
/
SupportPrivateProjectInDefaultPermissionTemplateTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v64;
import java.sql.SQLException;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.apache.commons.lang.RandomStringUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.version.v63.DefaultOrganizationUuidProviderImpl;
import static java.lang.String.valueOf;
import static org.apache.commons.lang.math.RandomUtils.nextLong;
import static org.assertj.core.api.Assertions.assertThat;
public class SupportPrivateProjectInDefaultPermissionTemplateTest {
private static final String DEFAULT_ORGANIZATION_UUID = "def org uuid";
private static final String OTHER_ORGANIZATION_UUID = "not def org uuid";
private static final String PERMISSION_USER = "user";
private static final String PERMISSION_CODEVIEWER = "codeviewer";
@Rule
public CoreDbTester db = CoreDbTester.createForSchema(SupportPrivateProjectInDefaultPermissionTemplateTest.class, "organizations_and_groups_and_permission_templates.sql");
@Rule
public ExpectedException expectedException = ExpectedException.none();
private SupportPrivateProjectInDefaultPermissionTemplate underTest = new SupportPrivateProjectInDefaultPermissionTemplate(db.database(), new DefaultOrganizationUuidProviderImpl());
@Test
public void fails_with_ISE_when_no_default_organization_is_set() throws SQLException {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Default organization uuid is missing");
underTest.execute();
}
@Test
public void fails_with_ISE_when_default_organization_does_not_exist_in_table_ORGANIZATIONS() throws SQLException {
setDefaultOrganizationProperty("blabla");
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Default organization with uuid 'blabla' does not exist in table ORGANIZATIONS");
underTest.execute();
}
@Test
public void execute_fails_with_ISE_when_default_organization_has_no_default_groupId() throws SQLException {
setupDefaultOrganization(null, "pt1", "pt2");
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("No default group id is defined for default organization (uuid=def org uuid)");
underTest.execute();
}
@Test
public void execute_fails_with_ISE_when_default_group_of_default_organization_does_not_exist() throws SQLException {
setupDefaultOrganization(112, "pT1", "pT2");
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Permission template with uuid pT1 not found");
underTest.execute();
}
@Test
public void execute_does_nothing_when_default_organization_has_default_permission_template_for_projects() throws SQLException {
int groupId = insertGroup(DEFAULT_ORGANIZATION_UUID);
setupDefaultOrganization(groupId, null, null);
underTest.execute();
}
@Test
public void execute_fails_with_ISE_when_default_organization_has_default_permission_template_for_views_but_not_for_projects() throws SQLException {
int groupId = insertGroup(DEFAULT_ORGANIZATION_UUID);
setupDefaultOrganization(groupId, null, "pt1");
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Inconsistent state for default organization (uuid=def org uuid): no project default template is defined but view default template is");
underTest.execute();
}
@Test
public void execute_fails_with_ISE_when_default_permission_template_for_projects_of_default_organization_does_not_exist() throws SQLException {
int groupId = insertGroup(DEFAULT_ORGANIZATION_UUID);
setupDefaultOrganization(groupId, "foBar2000", "pt2");
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Permission template with uuid foBar2000 not found");
underTest.execute();
}
@Test
public void execute_does_not_fail_when_default_organization_has_default_permission_template_for_view() throws SQLException {
int groupId = insertGroup(DEFAULT_ORGANIZATION_UUID);
IdAndUuid projectDefPermTemplate = insertPermissionTemplate(DEFAULT_ORGANIZATION_UUID);
setupDefaultOrganization(groupId, projectDefPermTemplate.uuid, null);
underTest.execute();
}
@Test
public void execute_adds_permission_USER_and_CODEVIEWER_to_default_group_of_default_organization_in_its_default_project_template() throws SQLException {
int groupId = insertGroup(DEFAULT_ORGANIZATION_UUID);
IdAndUuid projectDefPermTemplate = insertPermissionTemplate(DEFAULT_ORGANIZATION_UUID);
setupDefaultOrganization(groupId, projectDefPermTemplate.uuid, null);
int otherGroupId = insertGroup(OTHER_ORGANIZATION_UUID);
IdAndUuid otherProjectDefPermTemplate = insertPermissionTemplate(OTHER_ORGANIZATION_UUID);
insertOrganization(OTHER_ORGANIZATION_UUID, otherGroupId, otherProjectDefPermTemplate.uuid, null);
underTest.execute();
verifyPermissionOfGroupInTemplate(projectDefPermTemplate, groupId, PERMISSION_USER, PERMISSION_CODEVIEWER);
verifyPermissionOfGroupInTemplate(otherProjectDefPermTemplate, otherGroupId);
}
@Test
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()
throws SQLException {
int groupId = insertGroup(DEFAULT_ORGANIZATION_UUID);
IdAndUuid projectDefPermTemplate = insertPermissionTemplate(DEFAULT_ORGANIZATION_UUID);
insertGroupPermission(projectDefPermTemplate, groupId, PERMISSION_USER);
setupDefaultOrganization(groupId, projectDefPermTemplate.uuid, null);
int otherGroupId = insertGroup(OTHER_ORGANIZATION_UUID);
IdAndUuid otherProjectDefPermTemplateUuid = insertPermissionTemplate(OTHER_ORGANIZATION_UUID);
insertOrganization(OTHER_ORGANIZATION_UUID, otherGroupId, otherProjectDefPermTemplateUuid.uuid, null);
underTest.execute();
verifyPermissionOfGroupInTemplate(projectDefPermTemplate, groupId, PERMISSION_USER, PERMISSION_CODEVIEWER);
verifyPermissionOfGroupInTemplate(otherProjectDefPermTemplateUuid, otherGroupId);
}
@Test
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()
throws SQLException {
int groupId = insertGroup(DEFAULT_ORGANIZATION_UUID);
IdAndUuid projectDefPermTemplate = insertPermissionTemplate(DEFAULT_ORGANIZATION_UUID);
insertGroupPermission(projectDefPermTemplate, groupId, PERMISSION_CODEVIEWER);
setupDefaultOrganization(groupId, projectDefPermTemplate.uuid, null);
int otherGroupId = insertGroup(OTHER_ORGANIZATION_UUID);
IdAndUuid otherProjectDefPermTemplateUuid = insertPermissionTemplate(OTHER_ORGANIZATION_UUID);
insertOrganization(OTHER_ORGANIZATION_UUID, otherGroupId, otherProjectDefPermTemplateUuid.uuid, null);
underTest.execute();
verifyPermissionOfGroupInTemplate(projectDefPermTemplate, groupId, PERMISSION_USER, PERMISSION_CODEVIEWER);
verifyPermissionOfGroupInTemplate(otherProjectDefPermTemplateUuid, otherGroupId);
}
@Test
public void execute_is_reentrant()
throws SQLException {
int groupId = insertGroup(DEFAULT_ORGANIZATION_UUID);
IdAndUuid projectDefPermTemplate = insertPermissionTemplate(DEFAULT_ORGANIZATION_UUID);
setupDefaultOrganization(groupId, projectDefPermTemplate.uuid, null);
int otherGroupId = insertGroup(OTHER_ORGANIZATION_UUID);
IdAndUuid otherProjectDefPermTemplateUuid = insertPermissionTemplate(OTHER_ORGANIZATION_UUID);
insertOrganization(OTHER_ORGANIZATION_UUID, otherGroupId, otherProjectDefPermTemplateUuid.uuid, null);
underTest.execute();
underTest.execute();
verifyPermissionOfGroupInTemplate(projectDefPermTemplate, groupId, PERMISSION_USER, PERMISSION_CODEVIEWER);
verifyPermissionOfGroupInTemplate(otherProjectDefPermTemplateUuid, otherGroupId);
}
private void insertGroupPermission(IdAndUuid permissionTemplate, int groupId, String permission) {
db.executeInsert(
"PERM_TEMPLATES_GROUPS",
"GROUP_ID", groupId,
"TEMPLATE_ID", permissionTemplate.id,
"PERMISSION_REFERENCE", permission);
}
private void verifyPermissionOfGroupInTemplate(IdAndUuid permTemplate, int groupId, String... permissions) {
verifyPermissionOfGroupInTemplate(permTemplate.uuid, groupId, permissions);
}
private void verifyPermissionOfGroupInTemplate(String permTemplateUuid, int groupId, String... permissions) {
assertThat(
db.select("select permission_reference as \"permission\" from perm_templates_groups ptg inner join permission_templates pt on pt.kee='" + permTemplateUuid
+ "' where ptg.template_id=pt.id and group_id=" + groupId)
.stream()
.flatMap(row -> Stream.of((String) row.get("permission")))
.collect(MoreCollectors.toList()))
.containsOnly(permissions);
}
private void setupDefaultOrganization(@Nullable Integer defaultGroupId, @Nullable String projectPermTemplateUuid, @Nullable String viewPermTemplateUuid) {
setDefaultOrganizationProperty(DEFAULT_ORGANIZATION_UUID);
insertOrganization(DEFAULT_ORGANIZATION_UUID, defaultGroupId, projectPermTemplateUuid, viewPermTemplateUuid);
}
private void setDefaultOrganizationProperty(String defaultOrganizationUuid) {
db.executeInsert(
"INTERNAL_PROPERTIES",
"KEE", "organization.default",
"IS_EMPTY", "false",
"TEXT_VALUE", defaultOrganizationUuid);
}
private void insertOrganization(String uuid, @Nullable Integer defaultGroupId, @Nullable String projectPermTemplateUuid, @Nullable String viewPermTemplateUuid) {
db.executeInsert("ORGANIZATIONS",
"UUID", uuid,
"KEE", uuid,
"NAME", uuid,
"GUARDED", false,
"default_group_id", defaultGroupId == null ? null : valueOf(defaultGroupId),
"default_perm_template_project", projectPermTemplateUuid,
"default_perm_template_view", viewPermTemplateUuid,
"CREATED_AT", nextLong(),
"UPDATED_AT", nextLong());
}
private int insertGroup(String organizationUuid) {
String name = "name" + RandomStringUtils.random(20);
db.executeInsert(
"GROUPS",
"ORGANIZATION_UUID", organizationUuid,
"NAME", name);
return ((Long) db.selectFirst("select id as \"ID\" from groups where name='" + name + "'").get("ID")).intValue();
}
private IdAndUuid insertPermissionTemplate(String organizationUuid) {
String random = RandomStringUtils.random(20);
String uuid = "ptUuid" + random;
db.executeInsert(
"PERMISSION_TEMPLATES",
"ORGANIZATION_UUID", organizationUuid,
"NAME", "name" + random,
"KEE", uuid);
return new IdAndUuid(
((Long) db.selectFirst("select id as \"ID\" from permission_templates where kee='" + uuid + "'").get("ID")).intValue(),
uuid);
}
private static final class IdAndUuid {
private final int id;
private final String uuid;
private IdAndUuid(int id, String uuid) {
this.id = id;
this.uuid = uuid;
}
}
}