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