]> source.dussan.org Git - sonarqube.git/blob
aee00390bff658207fc96fc1744a65cefe435a58
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.Date;
23 import javax.annotation.Nullable;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.sonar.api.resources.Qualifiers;
27 import org.sonar.api.web.UserRole;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.component.ResourceTypesRule;
31 import org.sonar.db.organization.OrganizationDto;
32 import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
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.exceptions.UnauthorizedException;
37 import org.sonar.server.i18n.I18nRule;
38 import org.sonar.server.permission.ws.BasePermissionWsTest;
39 import org.sonar.server.ws.TestRequest;
40 import org.sonar.server.ws.WsActionTester;
41 import org.sonarqube.ws.MediaTypes;
42 import org.sonarqube.ws.WsPermissions;
43
44 import static org.assertj.core.api.Assertions.assertThat;
45 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
46 import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
47 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
48 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_02;
49 import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
50 import static org.sonar.test.JsonAssert.assertJson;
51
52 public class SearchTemplatesActionTest extends BasePermissionWsTest<SearchTemplatesAction> {
53
54   private I18nRule i18n = new I18nRule();
55   private DbClient dbClient = db.getDbClient();
56   private DbSession dbSession = db.getSession();
57   private ResourceTypesRule resourceTypesWithViews = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW);
58   private ResourceTypesRule resourceTypesWithoutViews = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT);
59
60   private WsActionTester underTestWithoutViews;
61
62   @Override
63   protected SearchTemplatesAction buildWsAction() {
64     DefaultTemplatesResolver defaultTemplatesResolverWithViews = new DefaultTemplatesResolverImpl(resourceTypesWithViews);
65     SearchTemplatesDataLoader dataLoaderWithViews = new SearchTemplatesDataLoader(dbClient, defaultTemplatesResolverWithViews);
66     SearchTemplatesAction searchTemplatesAction = new SearchTemplatesAction(dbClient, userSession, i18n, newPermissionWsSupport(), dataLoaderWithViews);
67     return searchTemplatesAction;
68   }
69
70   @Before
71   public void setUp() {
72     DefaultTemplatesResolver defaultTemplatesResolverWithViews = new DefaultTemplatesResolverImpl(resourceTypesWithoutViews);
73     SearchTemplatesDataLoader dataLoaderWithViews = new SearchTemplatesDataLoader(dbClient, defaultTemplatesResolverWithViews);
74     underTestWithoutViews = new WsActionTester(new SearchTemplatesAction(dbClient, userSession, i18n, newPermissionWsSupport(), dataLoaderWithViews));
75     i18n.setProjectPermissions();
76     userSession.login().addOrganizationPermission(db.getDefaultOrganization().getUuid(), SYSTEM_ADMIN);
77   }
78
79   @Test
80   public void search_project_permissions() {
81     OrganizationDto organization = db.getDefaultOrganization();
82     PermissionTemplateDto projectTemplate = insertProjectTemplate(organization);
83     PermissionTemplateDto viewsTemplate = insertViewsTemplate(organization);
84
85     UserDto user1 = db.users().insertUser();
86     UserDto user2 = db.users().insertUser();
87     UserDto user3 = db.users().insertUser();
88
89     GroupDto group1 = db.users().insertGroup(organization);
90     GroupDto group2 = db.users().insertGroup(organization);
91     GroupDto group3 = db.users().insertGroup(organization);
92
93     addUserToTemplate(projectTemplate.getId(), user1.getId(), UserRole.ISSUE_ADMIN);
94     addUserToTemplate(projectTemplate.getId(), user2.getId(), UserRole.ISSUE_ADMIN);
95     addUserToTemplate(projectTemplate.getId(), user3.getId(), UserRole.ISSUE_ADMIN);
96     addUserToTemplate(projectTemplate.getId(), user1.getId(), UserRole.CODEVIEWER);
97     addGroupToTemplate(projectTemplate.getId(), group1.getId(), UserRole.ADMIN);
98     addPermissionTemplateWithProjectCreator(projectTemplate.getId(), UserRole.ADMIN);
99
100     addUserToTemplate(viewsTemplate.getId(), user1.getId(), UserRole.USER);
101     addUserToTemplate(viewsTemplate.getId(), user2.getId(), UserRole.USER);
102     addGroupToTemplate(viewsTemplate.getId(), group1.getId(), UserRole.ISSUE_ADMIN);
103     addGroupToTemplate(viewsTemplate.getId(), group2.getId(), UserRole.ISSUE_ADMIN);
104     addGroupToTemplate(viewsTemplate.getId(), group3.getId(), UserRole.ISSUE_ADMIN);
105
106     db.organizations().setDefaultTemplates(organization, projectTemplate.getUuid(), viewsTemplate.getUuid());
107
108     String result = newRequest().execute().getInput();
109
110     assertJson(result)
111       .withStrictArrayOrder()
112       .isSimilarTo(getClass().getResource("search_templates-example.json"));
113   }
114
115   @Test
116   public void empty_result_with_views() {
117     db.organizations().setDefaultTemplates(db.getDefaultOrganization(), "AU-Tpxb--iU5OvuD2FLy", "AU-TpxcA-iU5OvuD2FLz");
118     String result = newRequest(wsTester).execute().getInput();
119
120     assertJson(result)
121       .withStrictArrayOrder()
122       .ignoreFields("permissions")
123       .isSimilarTo("{" +
124         "  \"permissionTemplates\": []," +
125         "  \"defaultTemplates\": [" +
126         "    {" +
127         "      \"templateId\": \"AU-Tpxb--iU5OvuD2FLy\"," +
128         "      \"qualifier\": \"TRK\"" +
129         "    }," +
130         "    {" +
131         "      \"templateId\": \"AU-TpxcA-iU5OvuD2FLz\"," +
132         "      \"qualifier\": \"VW\"" +
133         "    }" +
134         "  ]" +
135         "}");
136   }
137
138   @Test
139   public void empty_result_without_views() {
140     db.organizations().setDefaultTemplates(db.getDefaultOrganization(), "AU-Tpxb--iU5OvuD2FLy", "AU-TpxcA-iU5OvuD2FLz");
141     String result = newRequest(underTestWithoutViews).execute().getInput();
142
143     assertJson(result)
144       .withStrictArrayOrder()
145       .ignoreFields("permissions")
146       .isSimilarTo("{" +
147         "  \"permissionTemplates\": []," +
148         "  \"defaultTemplates\": [" +
149         "    {" +
150         "      \"templateId\": \"AU-Tpxb--iU5OvuD2FLy\"," +
151         "      \"qualifier\": \"TRK\"" +
152         "    }" +
153         "  ]" +
154         "}");
155   }
156
157   @Test
158   public void search_by_name_in_default_organization() {
159     db.organizations().setDefaultTemplates(db.getDefaultOrganization(), "foo", null);
160     insertProjectTemplate(db.getDefaultOrganization());
161     insertViewsTemplate(db.getDefaultOrganization());
162
163     String result = newRequest(wsTester)
164       .setParam(TEXT_QUERY, "views")
165       .execute()
166       .getInput();
167
168     assertThat(result).contains("Default template for Views")
169       .doesNotContain("projects")
170       .doesNotContain("developers");
171   }
172
173   @Test
174   public void search_in_organization() throws Exception {
175     OrganizationDto org = db.organizations().insert();
176     db.organizations().setDefaultTemplates(org, "foo", null);
177     PermissionTemplateDto templateInOrg = insertProjectTemplate(org);
178     insertProjectTemplate(db.getDefaultOrganization());
179     db.commit();
180     userSession.addOrganizationPermission(org.getUuid(), SYSTEM_ADMIN);
181
182     WsPermissions.SearchTemplatesWsResponse result = WsPermissions.SearchTemplatesWsResponse.parseFrom(
183       newRequest(underTestWithoutViews)
184         .setParam("organization", org.getKey())
185         .setMediaType(MediaTypes.PROTOBUF)
186         .execute()
187         .getInputStream());
188
189     assertThat(result.getPermissionTemplatesCount()).isEqualTo(1);
190     assertThat(result.getPermissionTemplates(0).getId()).isEqualTo(templateInOrg.getUuid());
191   }
192
193   @Test
194   public void fail_if_not_logged_in() {
195     expectedException.expect(UnauthorizedException.class);
196     userSession.anonymous();
197
198     newRequest().execute();
199   }
200
201   @Test
202   public void display_all_project_permissions() {
203     db.organizations().setDefaultTemplates(db.getDefaultOrganization(), "foo", "bar");
204
205     String result = newRequest().execute().getInput();
206
207     assertJson(result)
208       .withStrictArrayOrder()
209       .ignoreFields("defaultTemplates", "permissionTemplates")
210       .isSimilarTo(
211         "{" +
212           "  \"permissions\": [" +
213           "    {" +
214           "      \"key\": \"admin\"," +
215           "      \"name\": \"Administer\"," +
216           "      \"description\": \"Ability to access project settings and perform administration tasks. (Users will also need \\\"Browse\\\" permission)\"" +
217           "    }," +
218           "    {" +
219           "      \"key\": \"codeviewer\"," +
220           "      \"name\": \"See Source Code\"," +
221           "      \"description\": \"Ability to view the project\\u0027s source code. (Users will also need \\\"Browse\\\" permission)\"" +
222           "    }," +
223           "    {" +
224           "      \"key\": \"issueadmin\"," +
225           "      \"name\": \"Administer Issues\"," +
226           "      \"description\": \"Grants the permission to perform advanced editing on issues: marking an issue False Positive / Won\\u0027t Fix or changing an Issue\\u0027s severity. (Users will also need \\\"Browse\\\" permission)\""
227           +
228           "    }," +
229           "    {" +
230           "      \"key\": \"scan\"," +
231           "      \"name\": \"Execute Analysis\"," +
232           "      \"description\": \"Ability to execute analyses, and to get all settings required to perform the analysis, even the secured ones like the scm account password, the jira account password, and so on.\""
233           +
234           "    }," +
235           "    {" +
236           "      \"key\": \"user\"," +
237           "      \"name\": \"Browse\"," +
238           "      \"description\": \"Ability to access a project, browse its measures, and create/edit issues for it.\"" +
239           "    }" +
240           "  ]" +
241           "}");
242   }
243
244   private PermissionTemplateDto insertProjectTemplate(OrganizationDto org) {
245     return insertTemplate(newPermissionTemplateDto()
246       .setOrganizationUuid(org.getUuid())
247       .setUuid(UUID_EXAMPLE_01)
248       .setName("Default template for Projects")
249       .setDescription("Template for new projects")
250       .setKeyPattern(null)
251       .setCreatedAt(new Date(1_000_000_000_000L))
252       .setUpdatedAt(new Date(1_000_000_000_000L)));
253   }
254
255   private PermissionTemplateDto insertViewsTemplate(OrganizationDto organization) {
256     return insertTemplate(newPermissionTemplateDto()
257       .setOrganizationUuid(organization.getUuid())
258       .setUuid(UUID_EXAMPLE_02)
259       .setName("Default template for Views")
260       .setDescription("Template for new views")
261       .setKeyPattern(".*sonar.views.*")
262       .setCreatedAt(new Date(1_000_000_000_000L))
263       .setUpdatedAt(new Date(1_100_000_000_000L)));
264   }
265
266   private PermissionTemplateDto insertTemplate(PermissionTemplateDto template) {
267     PermissionTemplateDto insert = dbClient.permissionTemplateDao().insert(db.getSession(), template);
268     db.getSession().commit();
269     return insert;
270   }
271
272   private void addGroupToTemplate(long templateId, @Nullable Long groupId, String permission) {
273     dbClient.permissionTemplateDao().insertGroupPermission(db.getSession(), templateId, groupId, permission);
274     db.getSession().commit();
275   }
276
277   private void addUserToTemplate(long templateId, long userId, String permission) {
278     dbClient.permissionTemplateDao().insertUserPermission(db.getSession(), templateId, userId, permission);
279     db.getSession().commit();
280   }
281
282   private void addPermissionTemplateWithProjectCreator(long templateId, String permission) {
283     dbClient.permissionTemplateCharacteristicDao().insert(dbSession, new PermissionTemplateCharacteristicDto()
284       .setWithProjectCreator(true)
285       .setTemplateId(templateId)
286       .setPermission(permission)
287       .setCreatedAt(1_000_000_000L)
288       .setUpdatedAt(2_000_000_000L));
289     db.commit();
290   }
291
292   private TestRequest newRequest(WsActionTester underTest) {
293     return underTest.newRequest().setMethod("POST");
294   }
295 }