aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2016-07-12 17:52:25 +0200
committerStas Vilchik <vilchiks@gmail.com>2016-07-14 12:01:42 +0200
commit12f55eb8322661b6d73224cfe81d44add919d0fb (patch)
tree4da0357d14d1da2eff51a0bceeedd30268538393 /server
parent15219459d8fd07f1967696dd331a10e22348f923 (diff)
downloadsonarqube-12f55eb8322661b6d73224cfe81d44add919d0fb.tar.gz
sonarqube-12f55eb8322661b6d73224cfe81d44add919d0fb.zip
SONAR-7837 Change WS api/permissions/template_users to return all permissions
Diffstat (limited to 'server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/permission/ws/TemplateUsersAction.java125
-rw-r--r--server/sonar-server/src/main/resources/org/sonar/server/permission/ws/template_users-example.json12
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/permission/ws/PermissionsWsTest.java5
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/permission/ws/TemplateUsersActionTest.java271
4 files changed, 272 insertions, 141 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/TemplateUsersAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/TemplateUsersAction.java
index 7925b465429..fb82ff7dcc0 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/TemplateUsersAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/TemplateUsersAction.java
@@ -19,25 +19,33 @@
*/
package org.sonar.server.permission.ws;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Ordering;
+import com.google.common.collect.TreeMultimap;
import java.util.List;
+import java.util.stream.Collectors;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.server.ws.WebService.Param;
-import org.sonar.api.server.ws.WebService.SelectionMode;
+import org.sonar.api.utils.Paging;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.permission.OldPermissionQuery;
+import org.sonar.db.permission.PermissionQuery;
import org.sonar.db.permission.template.PermissionTemplateDto;
-import org.sonar.db.permission.UserWithPermissionDto;
+import org.sonar.db.permission.template.PermissionTemplateUserDto;
+import org.sonar.db.user.UserDto;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.WsPermissions;
-import org.sonarqube.ws.WsPermissions.OldUser;
-import org.sonarqube.ws.WsPermissions.OldUsersWsResponse;
-
-import static java.lang.String.format;
+import org.sonarqube.ws.WsPermissions.UsersWsResponse;
+
+import static org.sonar.api.server.ws.WebService.Param.PAGE;
+import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
+import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
+import static org.sonar.db.permission.PermissionQuery.DEFAULT_PAGE_SIZE;
+import static org.sonar.db.permission.PermissionQuery.RESULTS_MAX_SIZE;
+import static org.sonar.db.permission.PermissionQuery.SEARCH_QUERY_MIN_LENGTH;
import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
-import static org.sonar.server.permission.ws.PermissionQueryParser.fromSelectionModeToMembership;
import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectPermissionParameter;
import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
@@ -61,19 +69,19 @@ public class TemplateUsersAction implements PermissionsWsAction {
WebService.NewAction action = context
.createAction("template_users")
.setSince("5.2")
- .setDescription(
- format("Lists the users that have been granted the specified permission as individual users rather than through group affiliation on the chosen template. <br />" +
- "If the query parameter '%s' is specified, the '%s' parameter is forced to '%s'.<br />" +
- "It requires administration permissions to access.<br />",
- Param.TEXT_QUERY, Param.SELECTED, SelectionMode.ALL.value()))
- .addPagingParams(100)
- .addSearchQuery("stas", "names")
- .addSelectionModeParam()
+ .setDescription("Lists the users with their permission as individual users rather than through group affiliation on the chosen template. <br />" +
+ "This service defaults to all users, but can be limited to users with a specific permission by providing the desired permission.<br>" +
+ "It requires administration permissions to access.<br />")
+ .addPagingParams(DEFAULT_PAGE_SIZE, RESULTS_MAX_SIZE)
.setInternal(true)
.setResponseExample(getClass().getResource("template_users-example.json"))
.setHandler(this);
- createProjectPermissionParameter(action);
+ action.createParam(Param.TEXT_QUERY)
+ .setDescription("Limit search to user names that contain the supplied string. Must have at least %d characters.<br/>" +
+ "When this parameter is not set, only users having at least one permission are returned.", SEARCH_QUERY_MIN_LENGTH)
+ .setExampleValue("eri");
+ createProjectPermissionParameter(action).setRequired(false);
createTemplateParameters(action);
}
@@ -85,58 +93,61 @@ public class TemplateUsersAction implements PermissionsWsAction {
WsTemplateRef templateRef = WsTemplateRef.fromRequest(wsRequest);
PermissionTemplateDto template = dependenciesFinder.getTemplate(dbSession, templateRef);
- OldPermissionQuery query = buildQuery(wsRequest, template);
- WsPermissions.OldUsersWsResponse templateUsersResponse = buildResponse(dbSession, query, template);
+ PermissionQuery query = buildQuery(wsRequest, template);
+ int total = dbClient.permissionTemplateDao().countUserLoginsByQueryAndTemplate(dbSession, query, template.getId());
+ Paging paging = Paging.forPageIndex(wsRequest.mandatoryParamAsInt(PAGE)).withPageSize(wsRequest.mandatoryParamAsInt(PAGE_SIZE)).andTotal(total);
+ List<UserDto> users = findUsers(dbSession, query, template);
+ List<PermissionTemplateUserDto> permissionTemplateUsers = dbClient.permissionTemplateDao().selectUserPermissionsByTemplateIdAndUserLogins(dbSession, template.getId(),
+ users.stream().map(UserDto::getLogin).collect(Collectors.toList()));
+ WsPermissions.UsersWsResponse templateUsersResponse = buildResponse(users, permissionTemplateUsers, paging);
writeProtobuf(templateUsersResponse, wsRequest, wsResponse);
} finally {
dbClient.closeSession(dbSession);
}
}
- private static OldPermissionQuery buildQuery(Request wsRequest, PermissionTemplateDto template) {
- String permission = validateProjectPermission(wsRequest.mandatoryParam(PARAM_PERMISSION));
-
- return OldPermissionQuery.builder()
- .template(template.getUuid())
- .permission(permission)
- .membership(fromSelectionModeToMembership(wsRequest.mandatoryParam(Param.SELECTED)))
- .pageIndex(wsRequest.mandatoryParamAsInt(Param.PAGE))
- .pageSize(wsRequest.mandatoryParamAsInt(Param.PAGE_SIZE))
- .search(wsRequest.param(Param.TEXT_QUERY))
- .build();
- }
-
- private OldUsersWsResponse buildResponse(DbSession dbSession, OldPermissionQuery query, PermissionTemplateDto template) {
- List<UserWithPermissionDto> usersWithPermission = dbClient.permissionTemplateDao().selectUsers(dbSession, query, template.getId(), query.pageOffset(), query.pageSize());
- int total = dbClient.permissionTemplateDao().countUsers(dbSession, query, template.getId());
-
- OldUsersWsResponse.Builder responseBuilder = OldUsersWsResponse.newBuilder();
- for (UserWithPermissionDto userWithPermission : usersWithPermission) {
- responseBuilder.addUsers(userDtoToUserResponse(userWithPermission));
+ private static PermissionQuery buildQuery(Request wsRequest, PermissionTemplateDto template) {
+ String textQuery = wsRequest.param(TEXT_QUERY);
+ String permission = wsRequest.param(PARAM_PERMISSION);
+ PermissionQuery.Builder query = PermissionQuery.builder()
+ .setTemplate(template.getUuid())
+ .setPermission(permission != null ? validateProjectPermission(permission) : null)
+ .setPageIndex(wsRequest.mandatoryParamAsInt(PAGE))
+ .setPageSize(wsRequest.mandatoryParamAsInt(PAGE_SIZE))
+ .setSearchQuery(textQuery);
+ if (textQuery == null) {
+ query.withPermissionOnly();
}
+ return query.build();
+ }
+ private WsPermissions.UsersWsResponse buildResponse(List<UserDto> users, List<PermissionTemplateUserDto> permissionTemplateUsers, Paging paging) {
+ Multimap<Long, String> permissionsByUserId = TreeMultimap.create();
+ permissionTemplateUsers.forEach(userPermission -> permissionsByUserId.put(userPermission.getUserId(), userPermission.getPermission()));
+
+ UsersWsResponse.Builder responseBuilder = UsersWsResponse.newBuilder();
+ users.forEach(user -> {
+ WsPermissions.User.Builder userResponse = responseBuilder.addUsersBuilder()
+ .setLogin(user.getLogin())
+ .addAllPermissions(permissionsByUserId.get(user.getId()));
+ if (user.getEmail() != null) {
+ userResponse.setEmail(user.getEmail());
+ }
+ if (user.getName() != null) {
+ userResponse.setName(user.getName());
+ }
+ });
responseBuilder.getPagingBuilder()
- .setPageIndex(query.pageIndex())
- .setPageSize(query.pageSize())
- .setTotal(total)
+ .setPageIndex(paging.pageIndex())
+ .setPageSize(paging.pageSize())
+ .setTotal(paging.total())
.build();
-
return responseBuilder.build();
}
- private static OldUser userDtoToUserResponse(UserWithPermissionDto userWithPermission) {
- OldUser.Builder userBuilder = OldUser.newBuilder();
- userBuilder.setLogin(userWithPermission.getLogin());
- String email = userWithPermission.getEmail();
- if (email != null) {
- userBuilder.setEmail(email);
- }
- String name = userWithPermission.getName();
- if (name != null) {
- userBuilder.setName(name);
- }
- userBuilder.setSelected(userWithPermission.getPermission() != null);
-
- return userBuilder.build();
+ public List<UserDto> findUsers(DbSession dbSession, PermissionQuery query, PermissionTemplateDto template) {
+ List<String> orderedLogins = dbClient.permissionTemplateDao().selectUserLoginsByQueryAndTemplate(dbSession, query, template.getId());
+ return Ordering.explicit(orderedLogins).onResultOf(UserDto::getLogin).immutableSortedCopy(dbClient.userDao().selectByLogins(dbSession, orderedLogins));
}
+
}
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/template_users-example.json b/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/template_users-example.json
index 343d96563a0..2e21d9a21f8 100644
--- a/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/template_users-example.json
+++ b/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/template_users-example.json
@@ -1,7 +1,7 @@
{
"paging": {
"pageIndex": 1,
- "pageSize": 100,
+ "pageSize": 20,
"total": 2
},
"users": [
@@ -9,14 +9,18 @@
"login": "admin",
"name": "Administrator",
"email": "admin@admin.com",
- "selected": true
+ "permissions": [
+ "codeviewer"
+ ]
},
{
"login": "george.orwell",
"name": "George Orwell",
"email": "george.orwell@1984.net",
- "selected": true
+ "permissions": [
+ "admin",
+ "codeviewer"
+ ]
}
]
}
-
diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/PermissionsWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/PermissionsWsTest.java
index 76943256ce7..2bd4726d54b 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/PermissionsWsTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/PermissionsWsTest.java
@@ -28,6 +28,7 @@ import org.sonar.server.ws.WsTester;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
public class PermissionsWsTest {
@@ -62,7 +63,7 @@ public class PermissionsWsTest {
assertThat(action.isPost()).isFalse();
assertThat(action.isInternal()).isTrue();
assertThat(action.since()).isEqualTo("5.2");
- assertThat(action.param(org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION).isRequired()).isTrue();
+ assertThat(action.param(PARAM_PERMISSION).isRequired()).isFalse();
}
@Test
@@ -73,7 +74,7 @@ public class PermissionsWsTest {
assertThat(action.isPost()).isFalse();
assertThat(action.isInternal()).isTrue();
assertThat(action.since()).isEqualTo("5.2");
- assertThat(action.param(org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION).isRequired()).isTrue();
+ assertThat(action.param(PARAM_PERMISSION).isRequired()).isTrue();
}
private WebService.Controller controller() {
diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/TemplateUsersActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/TemplateUsersActionTest.java
index 1adc1b4fcd6..724c06c0681 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/TemplateUsersActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/TemplateUsersActionTest.java
@@ -22,14 +22,12 @@ package org.sonar.server.permission.ws;
import java.io.IOException;
import java.io.InputStream;
import javax.annotation.Nullable;
-import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.System2;
-import org.sonar.api.web.UserRole;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
@@ -47,16 +45,20 @@ import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.usergroups.ws.UserGroupFinder;
import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.WsActionTester;
-import org.sonarqube.ws.WsPermissions.OldUsersWsResponse;
+import org.sonarqube.ws.WsPermissions;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.web.UserRole.ADMIN;
+import static org.sonar.api.web.UserRole.CODEVIEWER;
+import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
+import static org.sonar.api.web.UserRole.USER;
import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateUserDto;
import static org.sonar.test.JsonAssert.assertJson;
import static org.sonarqube.ws.MediaTypes.PROTOBUF;
-import static org.sonarqube.ws.WsPermissions.OldUsersWsResponse.parseFrom;
-
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
public class TemplateUsersActionTest {
@@ -66,154 +68,262 @@ public class TemplateUsersActionTest {
public UserSessionRule userSession = UserSessionRule.standalone();
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
+
ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
+
DbClient dbClient = db.getDbClient();
- final DbSession dbSession = db.getSession();
- WsActionTester ws;
+ DbSession dbSession = db.getSession();
- TemplateUsersAction underTest;
+ PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), new UserGroupFinder(dbClient), resourceTypes);
- PermissionTemplateDto template1;
- PermissionTemplateDto template2;
+ TemplateUsersAction underTest = new TemplateUsersAction(dbClient, userSession, dependenciesFinder);
+ WsActionTester ws = new WsActionTester(underTest);
- @Before
- public void setUp() {
- PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), new UserGroupFinder(dbClient), resourceTypes);
- underTest = new TemplateUsersAction(dbClient, userSession, dependenciesFinder);
- ws = new WsActionTester(underTest);
+ @Test
+ public void search_for_users_with_response_example() {
+ setSysAdminUser();
- userSession.login("login").setGlobalPermissions(ADMIN);
+ UserDto user1 = insertUser(new UserDto().setLogin("admin").setName("Administrator").setEmail("admin@admin.com"));
+ UserDto user2 = insertUser(new UserDto().setLogin("george.orwell").setName("George Orwell").setEmail("george.orwell@1984.net"));
+
+ PermissionTemplateDto template1 = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("template-uuid-1"));
+
+ addUserToTemplate(newPermissionTemplateUser(CODEVIEWER, template1.getId(), user1.getId()));
+ addUserToTemplate(newPermissionTemplateUser(CODEVIEWER, template1.getId(), user2.getId()));
+ addUserToTemplate(newPermissionTemplateUser(ADMIN, template1.getId(), user2.getId()));
+
+ commit();
+
+ String result = newRequest(null, template1.getUuid()).execute().getInput();
+ assertJson(result).isSimilarTo(getClass().getResource("template_users-example.json"));
+ }
- template1 = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("template-uuid-1"));
- template2 = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("template-uuid-2"));
+ @Test
+ public void search_for_users_by_template_name() throws IOException {
+ setSysAdminUser();
UserDto user1 = insertUser(new UserDto().setLogin("login-1").setName("name-1").setEmail("email-1"));
UserDto user2 = insertUser(new UserDto().setLogin("login-2").setName("name-2").setEmail("email-2"));
UserDto user3 = insertUser(new UserDto().setLogin("login-3").setName("name-3").setEmail("email-3"));
- addUserToTemplate(newPermissionTemplateUser(UserRole.USER, template1.getId(), user1.getId()));
- addUserToTemplate(newPermissionTemplateUser(UserRole.USER, template1.getId(), user2.getId()));
- addUserToTemplate(newPermissionTemplateUser(UserRole.ISSUE_ADMIN, template1.getId(), user1.getId()));
- addUserToTemplate(newPermissionTemplateUser(UserRole.ISSUE_ADMIN, template1.getId(), user3.getId()));
- addUserToTemplate(newPermissionTemplateUser(UserRole.USER, template2.getId(), user1.getId()));
- addUserToTemplate(newPermissionTemplateUser(UserRole.USER, template2.getId(), user2.getId()));
- addUserToTemplate(newPermissionTemplateUser(UserRole.USER, template2.getId(), user3.getId()));
- addUserToTemplate(newPermissionTemplateUser(UserRole.ISSUE_ADMIN, template2.getId(), user1.getId()));
+ PermissionTemplateDto template = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("template-uuid-1"));
+ addUserToTemplate(newPermissionTemplateUser(USER, template.getId(), user1.getId()));
+ addUserToTemplate(newPermissionTemplateUser(USER, template.getId(), user2.getId()));
+ addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template.getId(), user1.getId()));
+ addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template.getId(), user3.getId()));
+ PermissionTemplateDto anotherTemplate = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("template-uuid-2"));
+ addUserToTemplate(newPermissionTemplateUser(USER, anotherTemplate.getId(), user1.getId()));
commit();
+
+ InputStream responseStream = newRequest(null, null)
+ .setParam(PARAM_TEMPLATE_NAME, template.getName())
+ .setMediaType(PROTOBUF)
+ .execute().getInputStream();
+
+ WsPermissions.UsersWsResponse response = WsPermissions.UsersWsResponse.parseFrom(responseStream);
+ assertThat(response.getUsersList()).extracting("login").containsExactly("login-1", "login-2", "login-3");
+ assertThat(response.getUsers(0).getPermissionsList()).containsOnly("issueadmin", "user");
+ assertThat(response.getUsers(1).getPermissionsList()).containsOnly("user");
+ assertThat(response.getUsers(2).getPermissionsList()).containsOnly("issueadmin");
}
@Test
- public void search_for_users_with_response_example() {
- UserDto user1 = insertUser(new UserDto().setLogin("admin").setName("Administrator").setEmail("admin@admin.com"));
- UserDto user2 = insertUser(new UserDto().setLogin("george.orwell").setName("George Orwell").setEmail("george.orwell@1984.net"));
- addUserToTemplate(newPermissionTemplateUser(UserRole.CODEVIEWER, template1.getId(), user1.getId()));
- addUserToTemplate(newPermissionTemplateUser(UserRole.CODEVIEWER, template1.getId(), user2.getId()));
+ public void search_using_text_query() throws IOException {
+ setSysAdminUser();
+
+ UserDto user1 = insertUser(new UserDto().setLogin("login-1").setName("name-1").setEmail("email-1"));
+ UserDto user2 = insertUser(new UserDto().setLogin("login-2").setName("name-2").setEmail("email-2"));
+ UserDto user3 = insertUser(new UserDto().setLogin("login-3").setName("name-3").setEmail("email-3"));
+
+ PermissionTemplateDto template = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("template-uuid-1"));
+ addUserToTemplate(newPermissionTemplateUser(USER, template.getId(), user1.getId()));
+ addUserToTemplate(newPermissionTemplateUser(USER, template.getId(), user2.getId()));
+ addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template.getId(), user1.getId()));
+ addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template.getId(), user3.getId()));
+
+ PermissionTemplateDto anotherTemplate = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("template-uuid-2"));
+ addUserToTemplate(newPermissionTemplateUser(USER, anotherTemplate.getId(), user1.getId()));
commit();
- String result = newRequest(UserRole.CODEVIEWER, template1.getUuid()).execute().getInput();
+ InputStream responseStream = newRequest(null, null)
+ .setParam(PARAM_TEMPLATE_NAME, template.getName())
+ .setParam(WebService.Param.TEXT_QUERY, "ame-1")
+ .setMediaType(PROTOBUF)
+ .execute().getInputStream();
- assertJson(result).isSimilarTo(getClass().getResource("template_users-example.json"));
+ WsPermissions.UsersWsResponse response = WsPermissions.UsersWsResponse.parseFrom(responseStream);
+ assertThat(response.getUsersList()).extracting("login").containsOnly("login-1");
}
@Test
- public void search_for_users_by_template_name() throws IOException {
- InputStream responseStream = newRequest(UserRole.USER, null)
- .setParam(org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME, template1.getName())
- .setMediaType(PROTOBUF)
- .execute().getInputStream();
+ public void search_using_permission() throws IOException {
+ setSysAdminUser();
- OldUsersWsResponse response = parseFrom(responseStream);
+ UserDto user1 = insertUser(new UserDto().setLogin("login-1").setName("name-1").setEmail("email-1"));
+ UserDto user2 = insertUser(new UserDto().setLogin("login-2").setName("name-2").setEmail("email-2"));
+ UserDto user3 = insertUser(new UserDto().setLogin("login-3").setName("name-3").setEmail("email-3"));
+
+ PermissionTemplateDto template = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("template-uuid-1"));
+ addUserToTemplate(newPermissionTemplateUser(USER, template.getId(), user1.getId()));
+ addUserToTemplate(newPermissionTemplateUser(USER, template.getId(), user2.getId()));
+ addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template.getId(), user1.getId()));
+ addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template.getId(), user3.getId()));
+
+ PermissionTemplateDto anotherTemplate = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("template-uuid-2"));
+ addUserToTemplate(newPermissionTemplateUser(USER, anotherTemplate.getId(), user1.getId()));
+ commit();
+ InputStream responseStream = newRequest(USER, template.getUuid())
+ .setMediaType(PROTOBUF)
+ .execute().getInputStream();
+ WsPermissions.UsersWsResponse response = WsPermissions.UsersWsResponse.parseFrom(responseStream);
assertThat(response.getUsersList()).extracting("login").containsExactly("login-1", "login-2");
+ assertThat(response.getUsers(0).getPermissionsList()).containsOnly("issueadmin", "user");
+ assertThat(response.getUsers(1).getPermissionsList()).containsOnly("user");
}
@Test
- public void search_using_text_query() throws IOException {
- InputStream responseStream = newRequest(UserRole.USER, null)
- .setParam(org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME, template1.getName())
- .setParam(WebService.Param.TEXT_QUERY, "ame-1")
+ public void search_with_pagination() throws IOException {
+ setSysAdminUser();
+
+ UserDto user1 = insertUser(new UserDto().setLogin("login-1").setName("name-1").setEmail("email-1"));
+ UserDto user2 = insertUser(new UserDto().setLogin("login-2").setName("name-2").setEmail("email-2"));
+ UserDto user3 = insertUser(new UserDto().setLogin("login-3").setName("name-3").setEmail("email-3"));
+
+ PermissionTemplateDto template = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("template-uuid-1"));
+ addUserToTemplate(newPermissionTemplateUser(USER, template.getId(), user1.getId()));
+ addUserToTemplate(newPermissionTemplateUser(USER, template.getId(), user2.getId()));
+ addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template.getId(), user1.getId()));
+ addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template.getId(), user3.getId()));
+
+ PermissionTemplateDto anotherTemplate = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("template-uuid-2"));
+ addUserToTemplate(newPermissionTemplateUser(USER, anotherTemplate.getId(), user1.getId()));
+ commit();
+
+ InputStream responseStream = newRequest(USER, null)
+ .setParam(PARAM_TEMPLATE_NAME, template.getName())
+ .setParam(WebService.Param.SELECTED, "all")
+ .setParam(WebService.Param.PAGE, "2")
+ .setParam(WebService.Param.PAGE_SIZE, "1")
.setMediaType(PROTOBUF)
.execute().getInputStream();
- OldUsersWsResponse response = parseFrom(responseStream);
-
- assertThat(response.getUsersList()).extracting("login").containsOnly("login-1");
+ WsPermissions.UsersWsResponse response = WsPermissions.UsersWsResponse.parseFrom(responseStream);
+ assertThat(response.getUsersList()).extracting("login").containsOnly("login-2");
}
@Test
- public void search_using_selected() throws IOException {
- InputStream responseStream = newRequest(UserRole.USER, null)
- .setParam(org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME, template1.getName())
- .setParam(WebService.Param.SELECTED, "all")
+ public void users_are_sorted_by_name() throws IOException {
+ setSysAdminUser();
+
+ UserDto user1 = insertUser(new UserDto().setLogin("login-2").setName("name-2"));
+ UserDto user2 = insertUser(new UserDto().setLogin("login-3").setName("name-3"));
+ UserDto user3 = insertUser(new UserDto().setLogin("login-1").setName("name-1"));
+
+ PermissionTemplateDto template = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("template-uuid-1"));
+ addUserToTemplate(newPermissionTemplateUser(USER, template.getId(), user1.getId()));
+ addUserToTemplate(newPermissionTemplateUser(USER, template.getId(), user2.getId()));
+ addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template.getId(), user3.getId()));
+ commit();
+
+ InputStream responseStream = newRequest(null, null)
+ .setParam(PARAM_TEMPLATE_NAME, template.getName())
.setMediaType(PROTOBUF)
.execute().getInputStream();
- OldUsersWsResponse response = OldUsersWsResponse.parseFrom(responseStream);
-
+ WsPermissions.UsersWsResponse response = WsPermissions.UsersWsResponse.parseFrom(responseStream);
assertThat(response.getUsersList()).extracting("login").containsExactly("login-1", "login-2", "login-3");
- assertThat(response.getUsers(2).getSelected()).isFalse();
}
@Test
- public void search_with_pagination() throws IOException {
- InputStream responseStream = newRequest(UserRole.USER, null)
- .setParam(org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME, template1.getName())
- .setParam(WebService.Param.SELECTED, "all")
- .setParam(WebService.Param.PAGE, "2")
- .setParam(WebService.Param.PAGE_SIZE, "1")
+ public void empty_result_when_no_user_on_template() throws IOException {
+ setSysAdminUser();
+
+ UserDto user = insertUser(new UserDto().setLogin("login-1").setName("name-1").setEmail("email-1"));
+
+ PermissionTemplateDto template = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("template-uuid-1"));
+
+ PermissionTemplateDto anotherTemplate = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("template-uuid-2"));
+ addUserToTemplate(newPermissionTemplateUser(USER, anotherTemplate.getId(), user.getId()));
+ commit();
+
+ InputStream responseStream = newRequest(null, null)
+ .setParam(PARAM_TEMPLATE_NAME, template.getName())
.setMediaType(PROTOBUF)
.execute().getInputStream();
- OldUsersWsResponse response = parseFrom(responseStream);
-
- assertThat(response.getUsersList()).extracting("login").containsOnly("login-2");
+ WsPermissions.UsersWsResponse response = WsPermissions.UsersWsResponse.parseFrom(responseStream);
+ assertThat(response.getUsersList()).isEmpty();
}
@Test
public void fail_if_not_a_project_permission() throws IOException {
+ setSysAdminUser();
+
+ PermissionTemplateDto template = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("template-uuid-1"));
+ commit();
+
expectedException.expect(BadRequestException.class);
+ newRequest(GlobalPermissions.PROVISIONING, template.getUuid())
+ .execute();
+ }
+
+ @Test
+ public void fail_if_no_template_param() {
+ setSysAdminUser();
- newRequest(GlobalPermissions.PROVISIONING, template1.getUuid())
+ expectedException.expect(BadRequestException.class);
+ newRequest(null, null)
.execute();
}
@Test
public void fail_if_template_does_not_exist() {
- expectedException.expect(NotFoundException.class);
+ setSysAdminUser();
- newRequest(UserRole.USER, "unknown-template-uuid")
+ expectedException.expect(NotFoundException.class);
+ newRequest(null, "unknown-template-uuid")
.execute();
}
@Test
public void fail_if_template_uuid_and_name_provided() {
- expectedException.expect(BadRequestException.class);
+ setSysAdminUser();
- newRequest(UserRole.USER, template1.getUuid())
- .setParam(org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME, template1.getName())
+ PermissionTemplateDto template = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("template-uuid-1"));
+ commit();
+
+ expectedException.expect(BadRequestException.class);
+ newRequest(null, template.getUuid())
+ .setParam(PARAM_TEMPLATE_NAME, template.getName())
.execute();
}
@Test
public void fail_if_not_logged_in() {
- expectedException.expect(UnauthorizedException.class);
userSession.anonymous();
- newRequest(UserRole.USER, template1.getUuid()).execute();
+ PermissionTemplateDto template = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("template-uuid-1"));
+ commit();
+
+ expectedException.expect(UnauthorizedException.class);
+ newRequest(null, template.getUuid()).execute();
}
@Test
public void fail_if_insufficient_privileges() {
- expectedException.expect(ForbiddenException.class);
userSession.login("login");
- newRequest(UserRole.USER, template1.getUuid()).execute();
+ PermissionTemplateDto template = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("template-uuid-1"));
+ commit();
+
+ expectedException.expect(ForbiddenException.class);
+ newRequest(null, template.getUuid()).execute();
}
private UserDto insertUser(UserDto userDto) {
- UserDto user = dbClient.userDao().insert(dbSession, userDto.setActive(true));
- return user;
+ return dbClient.userDao().insert(dbSession, userDto.setActive(true));
}
private void addUserToTemplate(PermissionTemplateUserDto userRoleDto) {
@@ -231,13 +341,18 @@ public class TemplateUsersActionTest {
.setUserId(userId);
}
- private TestRequest newRequest(String permission, @Nullable String templateUuid) {
+ private TestRequest newRequest(@Nullable String permission, @Nullable String templateUuid) {
TestRequest request = ws.newRequest();
- request.setParam(org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION, permission);
+ if (permission != null) {
+ request.setParam(PARAM_PERMISSION, permission);
+ }
if (templateUuid != null) {
- request.setParam(org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID, templateUuid);
+ request.setParam(PARAM_TEMPLATE_ID, templateUuid);
}
-
return request;
}
+
+ private void setSysAdminUser() {
+ userSession.login("login").setGlobalPermissions(ADMIN);
+ }
}