]> source.dussan.org Git - sonarqube.git/blob
8aa93341310011b6430b8da3c8bf62d8bd04ddf5
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.permission.ws.template;
21
22 import java.util.List;
23 import javax.annotation.Nullable;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.api.web.UserRole;
28 import org.sonar.core.util.SequenceUuidFactory;
29 import org.sonar.db.DbTester;
30 import org.sonar.db.component.ComponentDto;
31 import org.sonar.db.component.ResourceTypesRule;
32 import org.sonar.db.permission.PermissionQuery;
33 import org.sonar.db.permission.template.PermissionTemplateDto;
34 import org.sonar.db.user.GroupDto;
35 import org.sonar.db.user.UserDto;
36 import org.sonar.server.es.TestProjectIndexers;
37 import org.sonar.server.exceptions.BadRequestException;
38 import org.sonar.server.exceptions.ForbiddenException;
39 import org.sonar.server.exceptions.NotFoundException;
40 import org.sonar.server.permission.DefaultTemplatesResolver;
41 import org.sonar.server.permission.DefaultTemplatesResolverImpl;
42 import org.sonar.server.permission.PermissionTemplateService;
43 import org.sonar.server.permission.ws.BasePermissionWsTest;
44 import org.sonar.server.ws.TestRequest;
45
46 import static org.assertj.core.api.Assertions.assertThat;
47 import static org.assertj.core.api.Assertions.assertThatThrownBy;
48 import static org.sonar.api.resources.Qualifiers.APP;
49 import static org.sonar.api.resources.Qualifiers.PROJECT;
50 import static org.sonar.api.resources.Qualifiers.VIEW;
51 import static org.sonar.api.web.UserRole.CODEVIEWER;
52 import static org.sonar.db.permission.GlobalPermission.SCAN;
53 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID;
54 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY;
55 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
56 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
57
58 public class ApplyTemplateActionTest extends BasePermissionWsTest<ApplyTemplateAction> {
59
60   @Rule
61   public DbTester dbTester = DbTester.create();
62
63   private UserDto user1;
64   private UserDto user2;
65   private GroupDto group1;
66   private GroupDto group2;
67   private ComponentDto project;
68   private PermissionTemplateDto template1;
69   private PermissionTemplateDto template2;
70
71   private ResourceTypesRule resourceTypesRule = new ResourceTypesRule().setRootQualifiers(PROJECT, VIEW, APP);
72   private DefaultTemplatesResolver defaultTemplatesResolver = new DefaultTemplatesResolverImpl(dbTester.getDbClient(), resourceTypesRule);
73   private PermissionTemplateService permissionTemplateService = new PermissionTemplateService(db.getDbClient(),
74     new TestProjectIndexers(), userSession, defaultTemplatesResolver, new SequenceUuidFactory());
75
76   @Override
77   protected ApplyTemplateAction buildWsAction() {
78     return new ApplyTemplateAction(db.getDbClient(), userSession, permissionTemplateService, newPermissionWsSupport());
79   }
80
81   @Before
82   public void setUp() {
83     user1 = db.users().insertUser();
84     user2 = db.users().insertUser();
85     group1 = db.users().insertGroup();
86     group2 = db.users().insertGroup();
87
88     // template 1
89     template1 = db.permissionTemplates().insertTemplate();
90     addUserToTemplate(user1, template1, UserRole.CODEVIEWER);
91     addUserToTemplate(user2, template1, UserRole.ISSUE_ADMIN);
92     addGroupToTemplate(group1, template1, UserRole.ADMIN);
93     addGroupToTemplate(group2, template1, UserRole.USER);
94     // template 2
95     template2 = db.permissionTemplates().insertTemplate();
96     addUserToTemplate(user1, template2, UserRole.USER);
97     addUserToTemplate(user2, template2, UserRole.USER);
98     addGroupToTemplate(group1, template2, UserRole.USER);
99     addGroupToTemplate(group2, template2, UserRole.USER);
100
101     project = db.components().insertPrivateProject();
102     db.users().insertProjectPermissionOnUser(user1, UserRole.ADMIN, project);
103     db.users().insertProjectPermissionOnUser(user2, UserRole.ADMIN, project);
104     db.users().insertProjectPermissionOnGroup(group1, UserRole.ADMIN, project);
105     db.users().insertProjectPermissionOnGroup(group2, UserRole.ADMIN, project);
106   }
107
108   @Test
109   public void apply_template_with_project_uuid() {
110     loginAsAdmin();
111
112     newRequest(template1.getUuid(), project.uuid(), null);
113
114     assertTemplate1AppliedToProject();
115   }
116
117   @Test
118   public void apply_template_with_project_uuid_by_template_name() {
119     loginAsAdmin();
120
121     newRequest()
122       .setParam(PARAM_TEMPLATE_NAME, template1.getName().toUpperCase())
123       .setParam(PARAM_PROJECT_ID, project.uuid())
124       .execute();
125
126     assertTemplate1AppliedToProject();
127   }
128
129   @Test
130   public void apply_template_with_project_key() {
131     loginAsAdmin();
132
133     newRequest(template1.getUuid(), null, project.getDbKey());
134
135     assertTemplate1AppliedToProject();
136   }
137
138   @Test
139   public void fail_when_unknown_template() {
140     loginAsAdmin();
141
142     assertThatThrownBy(() -> {
143       newRequest("unknown-template-uuid", project.uuid(), null);
144     })
145       .isInstanceOf(NotFoundException.class)
146       .hasMessage("Permission template with id 'unknown-template-uuid' is not found");
147   }
148
149   @Test
150   public void fail_when_unknown_project_uuid() {
151     loginAsAdmin();
152
153     assertThatThrownBy(() -> {
154       newRequest(template1.getUuid(), "unknown-project-uuid", null);
155     })
156       .isInstanceOf(NotFoundException.class)
157       .hasMessage("Project id 'unknown-project-uuid' not found");
158   }
159
160   @Test
161   public void fail_when_unknown_project_key() {
162     loginAsAdmin();
163
164     assertThatThrownBy(() -> {
165       newRequest(template1.getUuid(), null, "unknown-project-key");
166     })
167       .isInstanceOf(NotFoundException.class)
168       .hasMessage("Project key 'unknown-project-key' not found");
169   }
170
171   @Test
172   public void fail_when_template_is_not_provided() {
173     loginAsAdmin();
174
175     assertThatThrownBy(() -> {
176       newRequest(null, project.uuid(), null);
177     })
178       .isInstanceOf(BadRequestException.class);
179   }
180
181   @Test
182   public void fail_when_project_uuid_and_key_not_provided() {
183     loginAsAdmin();
184
185     assertThatThrownBy(() -> {
186       newRequest(template1.getUuid(), null, null);
187     })
188       .isInstanceOf(BadRequestException.class)
189       .hasMessage("Project id or project key can be provided, not both.");
190   }
191
192   @Test
193   public void fail_when_not_admin() {
194     userSession.logIn().addPermission(SCAN);
195
196     assertThatThrownBy(() -> {
197       newRequest(template1.getUuid(), project.uuid(), null);
198     })
199       .isInstanceOf(ForbiddenException.class);
200   }
201
202   private void assertTemplate1AppliedToProject() {
203     assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).containsExactly(group1.getName());
204     assertThat(selectProjectPermissionGroups(project, UserRole.USER)).containsExactly(group2.getName());
205     assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
206     assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).containsExactly(user1.getUuid());
207     assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).containsExactly(user2.getUuid());
208   }
209
210   private void newRequest(@Nullable String templateUuid, @Nullable String projectUuid, @Nullable String projectKey) {
211     TestRequest request = newRequest();
212     if (templateUuid != null) {
213       request.setParam(PARAM_TEMPLATE_ID, templateUuid);
214     }
215     if (projectUuid != null) {
216       request.setParam(PARAM_PROJECT_ID, projectUuid);
217     }
218     if (projectKey != null) {
219       request.setParam(PARAM_PROJECT_KEY, projectKey);
220     }
221     request.execute();
222   }
223
224   private void addUserToTemplate(UserDto user, PermissionTemplateDto permissionTemplate, String permission) {
225     db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), permissionTemplate.getUuid(), user.getUuid(),
226       permission, permissionTemplate.getName(), user.getLogin());
227     db.commit();
228   }
229
230   private void addGroupToTemplate(GroupDto group, PermissionTemplateDto permissionTemplate, String permission) {
231     db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), permissionTemplate.getUuid(), group.getUuid(),
232       permission, permissionTemplate.getName(), group.getName());
233     db.commit();
234   }
235
236   private List<String> selectProjectPermissionGroups(ComponentDto project, String permission) {
237     PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponent(project).build();
238     return db.getDbClient().groupPermissionDao().selectGroupNamesByQuery(db.getSession(), query);
239   }
240
241   private List<String> selectProjectPermissionUsers(ComponentDto project, String permission) {
242     PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponent(project).build();
243     return db.getDbClient().userPermissionDao().selectUserUuidsByQuery(db.getSession(), query);
244   }
245 }