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 java.io.IOException;
23 import javax.annotation.Nullable;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.experimental.categories.Category;
28 import org.junit.rules.ExpectedException;
29 import org.sonar.api.utils.System2;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.user.GroupDto;
32 import org.sonar.db.user.GroupRoleDto;
33 import org.sonar.db.user.UserDto;
34 import org.sonar.db.user.UserRoleDto;
35 import org.sonar.server.exceptions.ForbiddenException;
36 import org.sonar.server.exceptions.UnauthorizedException;
37 import org.sonar.server.i18n.I18nRule;
38 import org.sonar.server.tester.UserSessionRule;
39 import org.sonar.server.ws.WsActionTester;
40 import org.sonar.test.DbTests;
41 import org.sonarqube.ws.MediaTypes;
42 import org.sonarqube.ws.WsPermissions;
44 import static org.sonar.core.permission.GlobalPermissions.DASHBOARD_SHARING;
45 import static org.sonar.core.permission.GlobalPermissions.PREVIEW_EXECUTION;
46 import static org.sonar.core.permission.GlobalPermissions.PROVISIONING;
47 import static org.sonar.core.permission.GlobalPermissions.QUALITY_PROFILE_ADMIN;
48 import static org.sonar.core.permission.GlobalPermissions.QUALITY_GATE_ADMIN;
49 import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
50 import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
51 import static org.sonar.test.JsonAssert.assertJson;
53 @Category(DbTests.class)
54 public class SearchGlobalPermissionsActionTest {
57 public ExpectedException expectedException = ExpectedException.none();
59 public DbTester db = DbTester.create(System2.INSTANCE);
61 public UserSessionRule userSession = UserSessionRule.standalone();
63 I18nRule i18n = new I18nRule();
69 ws = new WsActionTester(new SearchGlobalPermissionsAction(db.getDbClient(), userSession, i18n));
70 userSession.login("login").setGlobalPermissions(SYSTEM_ADMIN);
74 public void search() {
75 GroupDto adminGroup = insertGroup(newGroupDto("sonar-admins", "Administrators"));
76 GroupDto userGroup = insertGroup(newGroupDto("sonar-users", "Users"));
77 insertGroupRole(newGroupRole(SCAN_EXECUTION, null));
78 insertGroupRole(newGroupRole(SCAN_EXECUTION, userGroup.getId()));
79 insertGroupRole(newGroupRole(SYSTEM_ADMIN, adminGroup.getId()));
80 insertGroupRole(newGroupRole(PROVISIONING, userGroup.getId()));
81 insertGroupRole(newGroupRole(DASHBOARD_SHARING, null));
83 UserDto user = insertUser(newUserDto("user", "user-name"));
84 UserDto adminUser = insertUser(newUserDto("admin", "admin-name"));
85 insertUserRole(newUserRoleDto(PROVISIONING, user.getId()));
86 insertUserRole(newUserRoleDto(QUALITY_PROFILE_ADMIN, user.getId()));
87 insertUserRole(newUserRoleDto(QUALITY_PROFILE_ADMIN, adminUser.getId()));
88 insertUserRole(newUserRoleDto(QUALITY_GATE_ADMIN, user.getId()));
89 insertUserRole(newUserRoleDto(QUALITY_GATE_ADMIN, adminUser.getId()));
90 insertUserRole(newUserRoleDto(PREVIEW_EXECUTION, adminUser.getId()));
91 insertUserRole(newUserRoleDto(PREVIEW_EXECUTION, user.getId()));
93 db.getSession().commit();
95 String result = ws.newRequest().execute().getInput();
97 assertJson(result).isSimilarTo(getClass().getResource("SearchGlobalPermissionsActionTest/search_global_permissions-example.json"));
101 public void protobuf_response() throws IOException {
102 WsPermissions.WsSearchGlobalPermissionsResponse wsSearchGlobalPermissionsResponse = WsPermissions.WsSearchGlobalPermissionsResponse.parseFrom(
104 .setMediaType(MediaTypes.PROTOBUF)
105 .execute().getInputStream());
106 System.out.println(wsSearchGlobalPermissionsResponse.getPermissionsList());
110 public void fail_if_insufficient_privileges() {
111 expectedException.expect(ForbiddenException.class);
112 userSession.login("login");
114 ws.newRequest().execute();
118 public void fail_if_not_logged_in() {
119 expectedException.expect(UnauthorizedException.class);
120 userSession.anonymous();
122 ws.newRequest().execute();
125 private void initI18nMessages() {
126 i18n.put("global_permissions.admin", "Administer System");
127 i18n.put("global_permissions.admin.desc", "Ability to perform all administration functions for the instance: " +
128 "global configuration and personalization of default dashboards.");
129 i18n.put("global_permissions.profileadmin", "Administer Quality Profiles");
130 i18n.put("global_permissions.profileadmin.desc", "Ability to perform any action on the quality profiles.");
131 i18n.put("global_permissions.gateadmin", "Administer Quality Gates");
132 i18n.put("global_permissions.gateadmin.desc", "Ability to perform any action on the quality gates.");
133 i18n.put("global_permissions.shareDashboard", "Share Dashboards And Filters");
134 i18n.put("global_permissions.shareDashboard.desc", "Ability to share dashboards, issue filters and measure filters.");
135 i18n.put("global_permissions.scan", "Execute Analysis");
136 i18n.put("global_permissions.scan.desc", "Ability to execute analyses, and to get all settings required to perform the analysis, " +
137 "even the secured ones like the scm account password, the jira account password, and so on.");
138 i18n.put("global_permissions.dryRunScan", "Execute Preview Analysis");
139 i18n.put("global_permissions.dryRunScan.desc", "Ability to execute preview analysis (results are not pushed to the server). " +
140 "This permission does not include the ability to access secured settings such as the scm account password, the jira account password, and so on. " +
141 "This permission is required to execute preview analysis in Eclipse or via the Issues Report plugin.");
142 i18n.put("global_permissions.provisioning", "Provision Projects");
143 i18n.put("global_permissions.provisioning.desc", "Ability to initialize project structure before first analysis.");
146 private UserDto insertUser(UserDto user) {
147 return db.getDbClient().userDao().insert(db.getSession(), user);
150 private void insertUserRole(UserRoleDto userRole) {
151 db.getDbClient().roleDao().insertUserRole(db.getSession(), userRole);
154 private GroupDto insertGroup(GroupDto groupDto) {
155 return db.getDbClient().groupDao().insert(db.getSession(), groupDto);
158 private void insertGroupRole(GroupRoleDto group) {
159 db.getDbClient().roleDao().insertGroupRole(db.getSession(), group);
162 private static UserDto newUserDto(String login, String name) {
163 return new UserDto().setLogin(login).setName(name).setActive(true);
166 private static GroupDto newGroupDto(String name, String description) {
167 return new GroupDto().setName(name).setDescription(description);
170 private static GroupRoleDto newGroupRole(String role, @Nullable Long groupId) {
171 GroupRoleDto groupRole = new GroupRoleDto().setRole(role);
172 if (groupId != null) {
173 groupRole.setGroupId(groupId);
179 private static UserRoleDto newUserRoleDto(String role, long userId) {
180 return new UserRoleDto()