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.test.JsonAssert.assertJson;
45 public class SearchGlobalPermissionsActionTest extends BasePermissionWsTest<SearchGlobalPermissionsAction> {
47 private I18nRule i18n = new I18nRule();
50 protected SearchGlobalPermissionsAction buildWsAction() {
51 return new SearchGlobalPermissionsAction(db.getDbClient(), userSession, i18n, newPermissionWsSupport());
60 public void search_in_organization() throws Exception {
61 OrganizationDto org = db.organizations().insert();
63 GroupDto adminGroup = db.users().insertGroup(newGroup(org, "sonar-admins", "Administrators"));
64 GroupDto userGroup = db.users().insertGroup(newGroup(org, "sonar-users", "Users"));
65 db.users().insertPermissionOnAnyone(org, SCAN_EXECUTION);
66 db.users().insertPermissionOnGroup(userGroup, SCAN_EXECUTION);
67 db.users().insertPermissionOnGroup(userGroup, PROVISIONING);
68 db.users().insertPermissionOnGroup(adminGroup, SYSTEM_ADMIN);
69 UserDto user = db.users().insertUser(newUserDto("user", "user-name"));
70 UserDto adminUser = db.users().insertUser(newUserDto("admin", "admin-name"));
71 db.users().insertPermissionOnUser(org, user, PROVISIONING);
72 db.users().insertPermissionOnUser(org, user, QUALITY_PROFILE_ADMIN);
73 db.users().insertPermissionOnUser(org, adminUser, QUALITY_PROFILE_ADMIN);
74 db.users().insertPermissionOnUser(org, user, QUALITY_GATE_ADMIN);
75 db.users().insertPermissionOnUser(org, adminUser, QUALITY_GATE_ADMIN);
77 // to be excluded, permission on another organization (the default one)
78 db.users().insertPermissionOnUser(db.getDefaultOrganization(), adminUser, QUALITY_GATE_ADMIN);
80 String result = newRequest()
81 .setParam("organization", org.getKey())
85 assertJson(result).isSimilarTo(getClass().getResource("search_global_permissions-example.json"));
89 public void search_in_default_organization_by_default() throws Exception {
90 OrganizationDto org = db.organizations().insert();
91 loginAsAdmin(org, db.getDefaultOrganization());
93 UserDto user = db.users().insertUser();
94 db.users().insertPermissionOnUser(db.getDefaultOrganization(), user, SCAN_EXECUTION);
96 // to be ignored, by default organization is used when searching for permissions
97 db.users().insertPermissionOnUser(org, user, QUALITY_GATE_ADMIN);
99 WsPermissions.WsSearchGlobalPermissionsResponse result = WsPermissions.WsSearchGlobalPermissionsResponse.parseFrom(
101 .setMediaType(MediaTypes.PROTOBUF)
105 assertThat(result.getPermissionsCount()).isEqualTo(GlobalPermissions.ALL.size());
106 for (WsPermissions.Permission permission : result.getPermissionsList()) {
107 if (permission.getKey().equals(SCAN_EXECUTION)) {
108 assertThat(permission.getUsersCount()).isEqualTo(1);
110 assertThat(permission.getUsersCount()).isEqualTo(0);
116 public void supports_protobuf_response() throws Exception {
117 loginAsAdminOnDefaultOrganization();
119 WsPermissions.WsSearchGlobalPermissionsResponse result = WsPermissions.WsSearchGlobalPermissionsResponse.parseFrom(
121 .setMediaType(MediaTypes.PROTOBUF)
125 assertThat(result).isNotNull();
129 public void fail_if_not_admin_of_default_organization() throws Exception {
132 expectedException.expect(ForbiddenException.class);
139 public void fail_if_not_admin_of_specified_organization() throws Exception {
140 OrganizationDto org = db.organizations().insert();
141 loginAsAdminOnDefaultOrganization();
143 expectedException.expect(ForbiddenException.class);
146 .setParam("organization", org.getKey())
151 public void fail_if_not_logged_in() throws Exception {
152 userSession.anonymous();
154 expectedException.expect(UnauthorizedException.class);
156 newRequest().execute();
160 public void fail_if_organization_does_not_exist() throws Exception {
161 expectedException.expect(NotFoundException.class);
164 .setParam("organization", "does_not_exist")
168 private void initI18nMessages() {
169 i18n.put("global_permissions.admin", "Administer System");
170 i18n.put("global_permissions.admin.desc", "Ability to perform all administration functions for the instance: " +
171 "global configuration and personalization of default dashboards.");
172 i18n.put("global_permissions.profileadmin", "Administer Quality Profiles");
173 i18n.put("global_permissions.profileadmin.desc", "Ability to perform any action on the quality profiles.");
174 i18n.put("global_permissions.gateadmin", "Administer Quality Gates");
175 i18n.put("global_permissions.gateadmin.desc", "Ability to perform any action on the quality gates.");
176 i18n.put("global_permissions.scan", "Execute Analysis");
177 i18n.put("global_permissions.scan.desc", "Ability to execute analyses, and to get all settings required to perform the analysis, " +
178 "even the secured ones like the scm account password, the jira account password, and so on.");
179 i18n.put("global_permissions.provisioning", "Create Projects");
180 i18n.put("global_permissions.provisioning.desc", "Ability to initialize project structure before first analysis.");
183 private static UserDto newUserDto(String login, String name) {
184 return UserTesting.newUserDto().setLogin(login).setName(name).setActive(true);
187 private static GroupDto newGroup(OrganizationDto org, String name, String description) {
188 return GroupTesting.newGroupDto().setName(name).setDescription(description).setOrganizationUuid(org.getUuid());