-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
AddGroupToTemplateActionTest.java
218 lines (169 loc) · 7.63 KB
/
AddGroupToTemplateActionTest.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
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact 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.permission.ws.template;
import java.util.List;
import javax.annotation.Nullable;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.web.UserRole;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.permission.PermissionQuery;
import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.user.GroupDto;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.permission.ws.BasePermissionWsTest;
import org.sonar.server.ws.WsTester;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.security.DefaultGroups.ANYONE;
import static org.sonar.api.web.UserRole.ADMIN;
import static org.sonar.api.web.UserRole.CODEVIEWER;
import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.CONTROLLER;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_ID;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_NAME;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
public class AddGroupToTemplateActionTest extends BasePermissionWsTest<AddGroupToTemplateAction> {
private PermissionTemplateDto template;
private GroupDto group;
@Override
protected AddGroupToTemplateAction buildWsAction() {
return new AddGroupToTemplateAction(db.getDbClient(), newPermissionWsSupport(), userSession);
}
@Before
public void setUp() {
template = insertTemplate();
group = db.users().insertGroup(defaultOrganizationProvider.getDto(), "group-name");
}
@Test
public void add_group_to_template() throws Exception {
loginAsAdminOnDefaultOrganization();
newRequest(group.getName(), template.getUuid(), CODEVIEWER);
assertThat(getGroupNamesInTemplateAndPermission(template.getId(), CODEVIEWER)).containsExactly(group.getName());
}
@Test
public void add_group_to_template_by_name() throws Exception {
loginAsAdminOnDefaultOrganization();
newRequest()
.setParam(PARAM_GROUP_NAME, group.getName())
.setParam(PARAM_PERMISSION, CODEVIEWER)
.setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
.execute();
assertThat(getGroupNamesInTemplateAndPermission(template.getId(), CODEVIEWER)).containsExactly(group.getName());
}
@Test
public void add_with_group_id() throws Exception {
loginAsAdminOnDefaultOrganization();
newRequest()
.setParam(PARAM_TEMPLATE_ID, template.getUuid())
.setParam(PARAM_PERMISSION, CODEVIEWER)
.setParam(PARAM_GROUP_ID, String.valueOf(group.getId()))
.execute();
assertThat(getGroupNamesInTemplateAndPermission(template.getId(), CODEVIEWER)).containsExactly(group.getName());
}
@Test
public void does_not_add_a_group_twice() throws Exception {
loginAsAdminOnDefaultOrganization();
newRequest(group.getName(), template.getUuid(), ISSUE_ADMIN);
newRequest(group.getName(), template.getUuid(), ISSUE_ADMIN);
assertThat(getGroupNamesInTemplateAndPermission(template.getId(), ISSUE_ADMIN)).containsExactly(group.getName());
}
@Test
public void add_anyone_group_to_template() throws Exception {
loginAsAdminOnDefaultOrganization();
newRequest(ANYONE, template.getUuid(), CODEVIEWER);
assertThat(getGroupNamesInTemplateAndPermission(template.getId(), CODEVIEWER)).containsExactly(ANYONE);
}
@Test
public void fail_if_add_anyone_group_to_admin_permission() throws Exception {
loginAsAdminOnDefaultOrganization();
expectedException.expect(BadRequestException.class);
expectedException.expectMessage(String.format("It is not possible to add the '%s' permission to the group 'Anyone'", UserRole.ADMIN));
newRequest(ANYONE, template.getUuid(), ADMIN);
}
@Test
public void fail_if_not_a_project_permission() throws Exception {
loginAsAdminOnDefaultOrganization();
expectedException.expect(IllegalArgumentException.class);
newRequest(group.getName(), template.getUuid(), GlobalPermissions.PROVISIONING);
}
@Test
public void fail_if_not_admin_of_default_organization() throws Exception {
userSession.login();
expectedException.expect(ForbiddenException.class);
newRequest(group.getName(), template.getUuid(), CODEVIEWER);
}
@Test
public void fail_if_group_params_missing() throws Exception {
loginAsAdminOnDefaultOrganization();
expectedException.expect(BadRequestException.class);
newRequest(null, template.getUuid(), CODEVIEWER);
}
@Test
public void fail_if_permission_missing() throws Exception {
loginAsAdminOnDefaultOrganization();
expectedException.expect(IllegalArgumentException.class);
newRequest(group.getName(), template.getUuid(), null);
}
@Test
public void fail_if_template_uuid_and_name_missing() throws Exception {
loginAsAdminOnDefaultOrganization();
expectedException.expect(BadRequestException.class);
newRequest(group.getName(), null, CODEVIEWER);
}
@Test
public void fail_if_group_does_not_exist() throws Exception {
loginAsAdminOnDefaultOrganization();
expectedException.expect(NotFoundException.class);
expectedException.expectMessage("No group with name 'unknown-group-name'");
newRequest("unknown-group-name", template.getUuid(), CODEVIEWER);
}
@Test
public void fail_if_template_key_does_not_exist() throws Exception {
loginAsAdminOnDefaultOrganization();
expectedException.expect(NotFoundException.class);
expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
newRequest(group.getName(), "unknown-key", CODEVIEWER);
}
private void newRequest(@Nullable String groupName, @Nullable String templateKey, @Nullable String permission) throws Exception {
WsTester.TestRequest request = newRequest();
if (groupName != null) {
request.setParam(PARAM_GROUP_NAME, groupName);
}
if (templateKey != null) {
request.setParam(PARAM_TEMPLATE_ID, templateKey);
}
if (permission != null) {
request.setParam(PARAM_PERMISSION, permission);
}
request.execute();
}
private List<String> getGroupNamesInTemplateAndPermission(long templateId, String permission) {
PermissionQuery query = PermissionQuery.builder().setPermission(permission).build();
return db.getDbClient().permissionTemplateDao()
.selectGroupNamesByQueryAndTemplate(db.getSession(), query, templateId);
}
private WsTester.TestRequest newRequest() {
return wsTester.newPostRequest(CONTROLLER, "add_group_to_template");
}
}