]> source.dussan.org Git - sonarqube.git/blob
1391b9981931e27012aebe52b361a2baeb95765e
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2014 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
6  * SonarQube 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  * SonarQube 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
21 package org.sonar.server.permission.ws.template;
22
23 import com.google.common.base.Function;
24 import java.util.List;
25 import javax.annotation.Nonnull;
26 import javax.annotation.Nullable;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.experimental.categories.Category;
31 import org.junit.rules.ExpectedException;
32 import org.sonar.api.resources.Qualifiers;
33 import org.sonar.api.utils.System2;
34 import org.sonar.core.permission.GlobalPermissions;
35 import org.sonar.db.DbClient;
36 import org.sonar.db.DbSession;
37 import org.sonar.db.DbTester;
38 import org.sonar.db.component.ResourceTypesRule;
39 import org.sonar.db.permission.PermissionQuery;
40 import org.sonar.db.permission.PermissionTemplateDto;
41 import org.sonar.db.permission.UserWithPermissionDto;
42 import org.sonar.db.user.UserDto;
43 import org.sonar.server.component.ComponentFinder;
44 import org.sonar.server.exceptions.BadRequestException;
45 import org.sonar.server.exceptions.ForbiddenException;
46 import org.sonar.server.exceptions.NotFoundException;
47 import org.sonar.server.exceptions.UnauthorizedException;
48 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
49 import org.sonar.server.permission.ws.PermissionsWsParameters;
50 import org.sonar.server.tester.UserSessionRule;
51 import org.sonar.server.usergroups.ws.UserGroupFinder;
52 import org.sonar.server.ws.TestRequest;
53 import org.sonar.server.ws.WsActionTester;
54 import org.sonar.test.DbTests;
55
56 import static com.google.common.collect.FluentIterable.from;
57 import static org.assertj.core.api.Assertions.assertThat;
58 import static org.sonar.api.web.UserRole.CODEVIEWER;
59 import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
60 import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
61 import static org.sonar.db.user.GroupMembershipQuery.IN;
62 import static org.sonar.db.user.UserTesting.newUserDto;
63 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_PERMISSION;
64 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
65 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_USER_LOGIN;
66
67 @Category(DbTests.class)
68 public class AddUserToTemplateActionTest {
69
70   private static final String USER_LOGIN = "user-login";
71   @Rule
72   public DbTester db = DbTester.create(System2.INSTANCE);
73   @Rule
74   public ExpectedException expectedException = ExpectedException.none();
75   @Rule
76   public UserSessionRule userSession = UserSessionRule.standalone();
77   ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
78
79   WsActionTester ws;
80   DbClient dbClient;
81   DbSession dbSession;
82   UserDto user;
83   PermissionTemplateDto permissionTemplate;
84
85   @Before
86   public void setUp() {
87     dbClient = db.getDbClient();
88     dbSession = db.getSession();
89     userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
90
91     PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), new UserGroupFinder(dbClient), resourceTypes);
92     ws = new WsActionTester(new AddUserToTemplateAction(dbClient, dependenciesFinder, userSession));
93
94     user = insertUser(newUserDto().setLogin(USER_LOGIN));
95     permissionTemplate = insertPermissionTemplate(newPermissionTemplateDto());
96     commit();
97   }
98
99   @Test
100   public void add_user_to_template() {
101     newRequest(USER_LOGIN, permissionTemplate.getUuid(), CODEVIEWER);
102
103     assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), CODEVIEWER)).containsExactly(USER_LOGIN);
104   }
105
106   @Test
107   public void add_user_to_template_by_name() {
108     ws.newRequest()
109       .setParam(PARAM_USER_LOGIN, USER_LOGIN)
110       .setParam(PARAM_PERMISSION, CODEVIEWER)
111       .setParam(PARAM_TEMPLATE_NAME, permissionTemplate.getName().toUpperCase())
112       .execute();
113     commit();
114
115     assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), CODEVIEWER)).containsExactly(USER_LOGIN);
116   }
117
118   @Test
119   public void does_not_add_a_user_twice() {
120     newRequest(USER_LOGIN, permissionTemplate.getUuid(), ISSUE_ADMIN);
121     newRequest(USER_LOGIN, permissionTemplate.getUuid(), ISSUE_ADMIN);
122
123     assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), ISSUE_ADMIN)).containsExactly(USER_LOGIN);
124   }
125
126   @Test
127   public void fail_if_not_a_project_permission() {
128     expectedException.expect(BadRequestException.class);
129
130     newRequest(USER_LOGIN, permissionTemplate.getUuid(), GlobalPermissions.PREVIEW_EXECUTION);
131   }
132
133   @Test
134   public void fail_if_insufficient_privileges() {
135     expectedException.expect(ForbiddenException.class);
136     userSession.setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
137
138     newRequest(USER_LOGIN, permissionTemplate.getUuid(), CODEVIEWER);
139   }
140
141   @Test
142   public void fail_if_not_logged_in() {
143     expectedException.expect(UnauthorizedException.class);
144     userSession.anonymous();
145
146     newRequest(USER_LOGIN, permissionTemplate.getUuid(), CODEVIEWER);
147   }
148
149   @Test
150   public void fail_if_user_missing() {
151     expectedException.expect(IllegalArgumentException.class);
152
153     newRequest(null, permissionTemplate.getUuid(), CODEVIEWER);
154   }
155
156   @Test
157   public void fail_if_permission_missing() {
158     expectedException.expect(IllegalArgumentException.class);
159
160     newRequest(USER_LOGIN, permissionTemplate.getUuid(), null);
161   }
162
163   @Test
164   public void fail_if_template_uuid_and_name_are_missing() {
165     expectedException.expect(BadRequestException.class);
166
167     newRequest(USER_LOGIN, null, CODEVIEWER);
168   }
169
170   @Test
171   public void fail_if_user_does_not_exist() {
172     expectedException.expect(NotFoundException.class);
173     expectedException.expectMessage("User with login 'unknown-login' is not found");
174
175     newRequest("unknown-login", permissionTemplate.getUuid(), CODEVIEWER);
176   }
177
178   @Test
179   public void fail_if_template_key_does_not_exist() {
180     expectedException.expect(NotFoundException.class);
181     expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
182
183     newRequest(USER_LOGIN, "unknown-key", CODEVIEWER);
184   }
185
186   private void newRequest(@Nullable String userLogin, @Nullable String templateKey, @Nullable String permission) {
187     TestRequest request = ws.newRequest();
188     if (userLogin != null) {
189       request.setParam(PARAM_USER_LOGIN, userLogin);
190     }
191     if (templateKey != null) {
192       request.setParam(PermissionsWsParameters.PARAM_TEMPLATE_UUID, templateKey);
193     }
194     if (permission != null) {
195       request.setParam(PermissionsWsParameters.PARAM_PERMISSION, permission);
196     }
197
198     request.execute();
199   }
200
201   private void commit() {
202     dbSession.commit();
203   }
204
205   private UserDto insertUser(UserDto userDto) {
206     return dbClient.userDao().insert(dbSession, userDto.setActive(true));
207   }
208
209   private PermissionTemplateDto insertPermissionTemplate(PermissionTemplateDto permissionTemplate) {
210     return dbClient.permissionTemplateDao().insert(dbSession, permissionTemplate);
211   }
212
213   private List<String> getLoginsInTemplateAndPermission(long templateId, String permission) {
214     PermissionQuery permissionQuery = PermissionQuery.builder().permission(permission).membership(IN).build();
215     return from(dbClient.permissionTemplateDao()
216       .selectUsers(dbSession, permissionQuery, templateId, 0, Integer.MAX_VALUE))
217       .transform(UserWithPermissionToUserLogin.INSTANCE)
218       .toList();
219   }
220
221   private enum UserWithPermissionToUserLogin implements Function<UserWithPermissionDto, String> {
222     INSTANCE;
223
224     @Override
225     public String apply(@Nonnull UserWithPermissionDto userWithPermission) {
226       return userWithPermission.getLogin();
227     }
228
229   }
230 }