3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.permission.ws;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.sonar.core.permission.GlobalPermissions;
25 import org.sonar.db.organization.OrganizationDto;
26 import org.sonar.db.user.GroupDto;
27 import org.sonar.db.user.GroupTesting;
28 import org.sonar.db.user.UserDto;
29 import org.sonar.db.user.UserTesting;
30 import org.sonar.server.exceptions.ForbiddenException;
31 import org.sonar.server.exceptions.NotFoundException;
32 import org.sonar.server.exceptions.UnauthorizedException;
33 import org.sonar.server.i18n.I18nRule;
34 import org.sonarqube.ws.MediaTypes;
35 import org.sonarqube.ws.WsPermissions;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.sonar.core.permission.GlobalPermissions.PROVISIONING;
39 import static org.sonar.core.permission.GlobalPermissions.QUALITY_GATE_ADMIN;
40 import static org.sonar.core.permission.GlobalPermissions.QUALITY_PROFILE_ADMIN;
41 import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
42 import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
43 import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto;
44 import static org.sonar.server.permission.ws.SearchGlobalPermissionsAction.ACTION;
45 import static org.sonar.test.JsonAssert.assertJson;
46 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.CONTROLLER;
48 public class SearchGlobalPermissionsActionTest extends BasePermissionWsTest<SearchGlobalPermissionsAction> {
50 private I18nRule i18n = new I18nRule();
53 protected SearchGlobalPermissionsAction buildWsAction() {
54 return new SearchGlobalPermissionsAction(db.getDbClient(), userSession, i18n, newPermissionWsSupport());
63 public void search_in_organization() throws Exception {
64 OrganizationDto org = db.organizations().insert(newOrganizationDto());
66 GroupDto adminGroup = db.users().insertGroup(newGroup(org, "sonar-admins", "Administrators"));
67 GroupDto userGroup = db.users().insertGroup(newGroup(org, "sonar-users", "Users"));
68 db.users().insertPermissionOnAnyone(org, SCAN_EXECUTION);
69 db.users().insertPermissionOnGroup(userGroup, SCAN_EXECUTION);
70 db.users().insertPermissionOnGroup(userGroup, PROVISIONING);
71 db.users().insertPermissionOnGroup(adminGroup, SYSTEM_ADMIN);
72 UserDto user = db.users().insertUser(newUserDto("user", "user-name"));
73 UserDto adminUser = db.users().insertUser(newUserDto("admin", "admin-name"));
74 db.users().insertPermissionOnUser(org, user, PROVISIONING);
75 db.users().insertPermissionOnUser(org, user, QUALITY_PROFILE_ADMIN);
76 db.users().insertPermissionOnUser(org, adminUser, QUALITY_PROFILE_ADMIN);
77 db.users().insertPermissionOnUser(org, user, QUALITY_GATE_ADMIN);
78 db.users().insertPermissionOnUser(org, adminUser, QUALITY_GATE_ADMIN);
80 // to be excluded, permission on another organization (the default one)
81 db.users().insertPermissionOnUser(db.getDefaultOrganization(), adminUser, QUALITY_GATE_ADMIN);
83 String result = wsTester.newPostRequest(CONTROLLER, ACTION)
84 .setParam("organization", org.getKey())
88 assertJson(result).isSimilarTo(getClass().getResource("search_global_permissions-example.json"));
92 public void search_in_default_organization_by_default() throws Exception {
93 OrganizationDto org = db.organizations().insert(newOrganizationDto());
94 loginAsAdmin(org, db.getDefaultOrganization());
96 UserDto user = db.users().insertUser();
97 db.users().insertPermissionOnUser(db.getDefaultOrganization(), user, SCAN_EXECUTION);
99 // to be ignored, by default organization is used when searching for permissions
100 db.users().insertPermissionOnUser(org, user, QUALITY_GATE_ADMIN);
102 WsPermissions.WsSearchGlobalPermissionsResponse result = WsPermissions.WsSearchGlobalPermissionsResponse.parseFrom(
103 wsTester.newPostRequest(CONTROLLER, ACTION)
104 .setMediaType(MediaTypes.PROTOBUF)
108 assertThat(result.getPermissionsCount()).isEqualTo(GlobalPermissions.ALL.size());
109 for (WsPermissions.Permission permission : result.getPermissionsList()) {
110 if (permission.getKey().equals(SCAN_EXECUTION)) {
111 assertThat(permission.getUsersCount()).isEqualTo(1);
113 assertThat(permission.getUsersCount()).isEqualTo(0);
119 public void supports_protobuf_response() throws Exception {
120 loginAsAdminOnDefaultOrganization();
122 WsPermissions.WsSearchGlobalPermissionsResponse result = WsPermissions.WsSearchGlobalPermissionsResponse.parseFrom(
123 wsTester.newPostRequest(CONTROLLER, ACTION)
124 .setMediaType(MediaTypes.PROTOBUF)
128 assertThat(result).isNotNull();
132 public void fail_if_not_admin_of_default_organization() throws Exception {
135 expectedException.expect(ForbiddenException.class);
137 wsTester.newPostRequest(CONTROLLER, ACTION)
142 public void fail_if_not_admin_of_specified_organization() throws Exception {
143 OrganizationDto org = db.organizations().insert(newOrganizationDto());
144 loginAsAdminOnDefaultOrganization();
146 expectedException.expect(ForbiddenException.class);
148 wsTester.newPostRequest(CONTROLLER, ACTION)
149 .setParam("organization", org.getKey())
154 public void fail_if_not_logged_in() throws Exception {
155 userSession.anonymous();
157 expectedException.expect(UnauthorizedException.class);
159 wsTester.newPostRequest(CONTROLLER, ACTION).execute();
163 public void fail_if_organization_does_not_exist() throws Exception {
164 expectedException.expect(NotFoundException.class);
166 wsTester.newPostRequest(CONTROLLER, ACTION)
167 .setParam("organization", "does_not_exist")
171 private void initI18nMessages() {
172 i18n.put("global_permissions.admin", "Administer System");
173 i18n.put("global_permissions.admin.desc", "Ability to perform all administration functions for the instance: " +
174 "global configuration and personalization of default dashboards.");
175 i18n.put("global_permissions.profileadmin", "Administer Quality Profiles");
176 i18n.put("global_permissions.profileadmin.desc", "Ability to perform any action on the quality profiles.");
177 i18n.put("global_permissions.gateadmin", "Administer Quality Gates");
178 i18n.put("global_permissions.gateadmin.desc", "Ability to perform any action on the quality gates.");
179 i18n.put("global_permissions.scan", "Execute Analysis");
180 i18n.put("global_permissions.scan.desc", "Ability to execute analyses, and to get all settings required to perform the analysis, " +
181 "even the secured ones like the scm account password, the jira account password, and so on.");
182 i18n.put("global_permissions.provisioning", "Create Projects");
183 i18n.put("global_permissions.provisioning.desc", "Ability to initialize project structure before first analysis.");
186 private static UserDto newUserDto(String login, String name) {
187 return UserTesting.newUserDto().setLogin(login).setName(name).setActive(true);
190 private static GroupDto newGroup(OrganizationDto org, String name, String description) {
191 return GroupTesting.newGroupDto().setName(name).setDescription(description).setOrganizationUuid(org.getUuid());