From: Teryk Bellahsene Date: Fri, 2 Oct 2015 14:36:45 +0000 (+0200) Subject: WS of permission domain are compatible with Views and Developer Cockpit X-Git-Tag: 5.2-RC1~101 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d2a19c82b2ee6b8a45e54195e29050baaa54c022;p=sonarqube.git WS of permission domain are compatible with Views and Developer Cockpit --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentFinder.java b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentFinder.java index 19c594d2289..2dbe0d3611d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentFinder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentFinder.java @@ -21,15 +21,20 @@ package org.sonar.server.component; import com.google.common.base.Optional; +import java.util.Set; import javax.annotation.Nullable; import org.sonar.api.resources.Qualifiers; +import org.sonar.api.resources.ResourceTypes; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; -import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.NotFoundException; import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.collect.FluentIterable.from; +import static java.lang.String.format; +import static org.sonar.server.component.ResourceTypeFunctions.RESOURCE_TYPE_TO_QUALIFIER; +import static org.sonar.server.ws.WsUtils.checkRequest; public class ComponentFinder { @@ -49,21 +54,29 @@ public class ComponentFinder { } public ComponentDto getByKey(DbSession dbSession, String key) { - return getIfPresentOrFail(dbClient.componentDao().selectByKey(dbSession, key), String.format("Component key '%s' not found", key)); + return getIfPresentOrFail(dbClient.componentDao().selectByKey(dbSession, key), format("Component key '%s' not found", key)); } public ComponentDto getByUuid(DbSession dbSession, String uuid) { - return getIfPresentOrFail(dbClient.componentDao().selectByUuid(dbSession, uuid), String.format("Component id '%s' not found", uuid)); + return getIfPresentOrFail(dbClient.componentDao().selectByUuid(dbSession, uuid), format("Component id '%s' not found", uuid)); } - public ComponentDto getProjectByUuidOrKey(DbSession dbSession, @Nullable String projectUuid, @Nullable String projectKey) { + /** + * A project can be: + * + */ + public ComponentDto getRootComponentOrModuleByUuidOrKey(DbSession dbSession, @Nullable String projectUuid, @Nullable String projectKey, ResourceTypes resourceTypes) { ComponentDto project; if (projectUuid != null) { - project = getIfPresentOrFail(dbClient.componentDao().selectByUuid(dbSession, projectUuid), String.format("Project id '%s' not found", projectUuid)); + project = getIfPresentOrFail(dbClient.componentDao().selectByUuid(dbSession, projectUuid), format("Project id '%s' not found", projectUuid)); } else { - project = getIfPresentOrFail(dbClient.componentDao().selectByKey(dbSession, projectKey), String.format("Project key '%s' not found", projectKey)); + project = getIfPresentOrFail(dbClient.componentDao().selectByKey(dbSession, projectKey), format("Project key '%s' not found", projectKey)); } - checkIsProjectOrModule(project); + checkIsProjectOrModule(project, resourceTypes); return project; } @@ -75,9 +88,13 @@ public class ComponentFinder { return component.get(); } - private static void checkIsProjectOrModule(ComponentDto component) { - if (!Qualifiers.PROJECT.equals(component.qualifier()) && !Qualifiers.MODULE.equals(component.qualifier())) { - throw new BadRequestException(String.format("Component '%s' (id: %s) must be a project or a module.", component.key(), component.uuid())); - } + private void checkIsProjectOrModule(ComponentDto component, ResourceTypes resourceTypes) { + Set rootQualifiers = from(resourceTypes.getRoots()) + .transform(RESOURCE_TYPE_TO_QUALIFIER) + .toSet(); + String qualifier = component.qualifier(); + + checkRequest(rootQualifiers.contains(qualifier) || Qualifiers.MODULE.equals(qualifier), + format("Component '%s' (id: %s) must be a project or a module.", component.key(), component.uuid())); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionDependenciesFinder.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionDependenciesFinder.java index 9838e66f733..dbcc8f6ec96 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionDependenciesFinder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionDependenciesFinder.java @@ -22,6 +22,7 @@ package org.sonar.server.permission.ws; import com.google.common.base.Optional; import javax.annotation.CheckForNull; +import org.sonar.api.resources.ResourceTypes; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; @@ -38,10 +39,12 @@ import static org.sonar.server.ws.WsUtils.checkFound; public class PermissionDependenciesFinder { private final DbClient dbClient; private final ComponentFinder componentFinder; + private final ResourceTypes resourceTypes; - public PermissionDependenciesFinder(DbClient dbClient, ComponentFinder componentFinder) { + public PermissionDependenciesFinder(DbClient dbClient, ComponentFinder componentFinder, ResourceTypes resourceTypes) { this.dbClient = dbClient; this.componentFinder = componentFinder; + this.resourceTypes = resourceTypes; } /** @@ -53,11 +56,11 @@ public class PermissionDependenciesFinder { } WsProjectRef wsProjectRef = request.project().get(); - return Optional.of(componentFinder.getProjectByUuidOrKey(dbSession, wsProjectRef.uuid(), wsProjectRef.key())); + return Optional.of(componentFinder.getRootComponentOrModuleByUuidOrKey(dbSession, wsProjectRef.uuid(), wsProjectRef.key(), resourceTypes)); } - public ComponentDto getProject(DbSession dbSession, WsProjectRef projectRef) { - return componentFinder.getProjectByUuidOrKey(dbSession, projectRef.uuid(), projectRef.key()); + public ComponentDto getRootComponentOrModule(DbSession dbSession, WsProjectRef projectRef) { + return componentFinder.getRootComponentOrModuleByUuidOrKey(dbSession, projectRef.uuid(), projectRef.key(), resourceTypes); } public String getGroupName(DbSession dbSession, PermissionRequest request) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchProjectPermissionsDataLoader.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchProjectPermissionsDataLoader.java index bbdca2994dd..0e91bce2eb5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchProjectPermissionsDataLoader.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchProjectPermissionsDataLoader.java @@ -92,7 +92,7 @@ public class SearchProjectPermissionsDataLoader { Optional project = WsProjectRef.optionalFromRequest(wsRequest); if (project.isPresent()) { - return singletonList(finder.getProject(dbSession, project.get())); + return singletonList(finder.getRootComponentOrModule(dbSession, project.get())); } return dbClient.componentDao().selectComponents(dbSession, rootQualifiers, paging.offset(), paging.pageSize(), query); diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/ApplyTemplateAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/ApplyTemplateAction.java index ab97c359cc8..11111c34762 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/ApplyTemplateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/ApplyTemplateAction.java @@ -68,7 +68,7 @@ public class ApplyTemplateAction implements PermissionsWsAction { DbSession dbSession = dbClient.openSession(false); try { PermissionTemplateDto template = finder.getTemplate(dbSession, WsTemplateRef.fromRequest(wsRequest)); - ComponentDto project = finder.getProject(dbSession, WsProjectRef.fromRequest(wsRequest)); + ComponentDto project = finder.getRootComponentOrModule(dbSession, WsProjectRef.fromRequest(wsRequest)); ApplyPermissionTemplateQuery query = ApplyPermissionTemplateQuery.create( template.getUuid(), diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddGroupActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddGroupActionTest.java index 2544e3dc145..3c3779189d8 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddGroupActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddGroupActionTest.java @@ -26,12 +26,14 @@ import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.rules.ExpectedException; import org.mockito.ArgumentCaptor; +import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; import org.sonar.db.DbClient; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentTesting; +import org.sonar.db.component.ResourceTypesRule; import org.sonar.db.user.GroupDto; import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.BadRequestException; @@ -48,6 +50,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN; import static org.sonar.db.component.ComponentTesting.newProjectDto; +import static org.sonar.db.component.ComponentTesting.newView; import static org.sonar.server.permission.ws.AddGroupAction.ACTION; import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_GROUP_ID; import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_GROUP_NAME; @@ -64,6 +67,7 @@ public class AddGroupActionTest { public DbTester db = DbTester.create(System2.INSTANCE); @Rule public ExpectedException expectedException = ExpectedException.none(); + ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV"); private PermissionUpdater permissionUpdater; private DbClient dbClient; private ArgumentCaptor permissionChangeCaptor; @@ -75,7 +79,7 @@ public class AddGroupActionTest { dbClient = db.getDbClient(); ComponentFinder componentFinder = new ComponentFinder(dbClient); ws = new WsTester(new PermissionsWs( - new AddGroupAction(dbClient, new PermissionChangeBuilder(new PermissionDependenciesFinder(dbClient, componentFinder)), permissionUpdater))); + new AddGroupAction(dbClient, new PermissionChangeBuilder(new PermissionDependenciesFinder(dbClient, componentFinder, resourceTypes)), permissionUpdater))); userSession.login("admin").setGlobalPermissions(SYSTEM_ADMIN); } @@ -146,6 +150,24 @@ public class AddGroupActionTest { assertThat(permissionChange.groupName()).isEqualTo("sonar-administrators"); } + @Test + public void add_with_view_uuid() throws Exception { + insertGroup("sonar-administrators"); + insertComponent(newView("view-uuid").setKey("view-key")); + commit(); + + newRequest() + .setParam(PARAM_GROUP_NAME, "sonar-administrators") + .setParam(PARAM_PROJECT_ID, "view-uuid") + .setParam(PARAM_PERMISSION, SYSTEM_ADMIN) + .execute(); + + verify(permissionUpdater).addPermission(permissionChangeCaptor.capture()); + PermissionChange permissionChange = permissionChangeCaptor.getValue(); + assertThat(permissionChange.componentKey()).isEqualTo("view-key"); + assertThat(permissionChange.groupName()).isEqualTo("sonar-administrators"); + } + @Test public void fail_if_project_uuid_not_found() throws Exception { expectedException.expect(NotFoundException.class); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddUserActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddUserActionTest.java index 7b6e00e49f1..48e3b6d7bc8 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddUserActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddUserActionTest.java @@ -26,12 +26,14 @@ import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.rules.ExpectedException; import org.mockito.ArgumentCaptor; +import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ResourceTypesRule; import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.NotFoundException; @@ -48,25 +50,29 @@ import static org.mockito.Mockito.verify; import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN; import static org.sonar.db.component.ComponentTesting.newFileDto; import static org.sonar.db.component.ComponentTesting.newProjectDto; +import static org.sonar.db.component.ComponentTesting.newView; import static org.sonar.server.permission.ws.AddUserAction.ACTION; +import static org.sonar.server.permission.ws.PermissionsWs.ENDPOINT; import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PERMISSION; -import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PROJECT_KEY; import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PROJECT_ID; +import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PROJECT_KEY; import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_USER_LOGIN; -import static org.sonar.server.permission.ws.PermissionsWs.ENDPOINT; @Category(DbTests.class) public class AddUserActionTest { - UserSessionRule userSession = UserSessionRule.standalone(); - WsTester ws; @Rule public DbTester db = DbTester.create(System2.INSTANCE); @Rule public ExpectedException expectedException = ExpectedException.none(); - private PermissionUpdater permissionUpdater; - private DbClient dbClient; - private DbSession dbSession; - private ArgumentCaptor permissionChangeCaptor = ArgumentCaptor.forClass(PermissionChange.class); + ResourceTypesRule resourceTypes = new ResourceTypesRule() + .setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV"); + + UserSessionRule userSession = UserSessionRule.standalone(); + WsTester ws; + PermissionUpdater permissionUpdater; + DbClient dbClient; + DbSession dbSession; + ArgumentCaptor permissionChangeCaptor = ArgumentCaptor.forClass(PermissionChange.class); @Before public void setUp() { @@ -75,7 +81,7 @@ public class AddUserActionTest { dbSession = db.getSession(); ComponentFinder componentFinder = new ComponentFinder(dbClient); ws = new WsTester(new PermissionsWs( - new AddUserAction(dbClient, permissionUpdater, new PermissionChangeBuilder(new PermissionDependenciesFinder(dbClient, componentFinder))))); + new AddUserAction(dbClient, permissionUpdater, new PermissionChangeBuilder(new PermissionDependenciesFinder(dbClient, componentFinder, resourceTypes))))); userSession.login("admin").setGlobalPermissions(SYSTEM_ADMIN); } @@ -124,6 +130,22 @@ public class AddUserActionTest { assertThat(permissionChange.componentKey()).isEqualTo("project-key"); } + @Test + public void add_user_permission_with_view_uuid() throws Exception { + dbClient.componentDao().insert(dbSession, newView("view-uuid").setKey("view-key")); + commit(); + + ws.newPostRequest(ENDPOINT, ACTION) + .setParam(PARAM_USER_LOGIN, "ray.bradbury") + .setParam(PARAM_PROJECT_ID, "view-uuid") + .setParam(PARAM_PERMISSION, SYSTEM_ADMIN) + .execute(); + + verify(permissionUpdater).addPermission(permissionChangeCaptor.capture()); + PermissionChange permissionChange = permissionChangeCaptor.getValue(); + assertThat(permissionChange.componentKey()).isEqualTo("view-key"); + } + @Test public void fail_when_project_uuid_is_unknown() throws Exception { expectedException.expect(NotFoundException.class); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/GroupsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/GroupsActionTest.java index 3ca4b95c5de..3d5ea0755a2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/GroupsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/GroupsActionTest.java @@ -26,6 +26,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.rules.ExpectedException; +import org.sonar.api.resources.Qualifiers; import org.sonar.api.security.DefaultGroups; import org.sonar.api.server.ws.WebService.Param; import org.sonar.api.server.ws.WebService.SelectionMode; @@ -35,7 +36,9 @@ import org.sonar.core.permission.GlobalPermissions; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.component.ComponentDbTester; import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ResourceTypesRule; import org.sonar.db.user.GroupDto; import org.sonar.db.user.GroupRoleDto; import org.sonar.server.component.ComponentFinder; @@ -53,6 +56,7 @@ import static org.sonar.api.web.UserRole.ISSUE_ADMIN; import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION; import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN; import static org.sonar.db.component.ComponentTesting.newProjectDto; +import static org.sonar.db.component.ComponentTesting.newView; import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PERMISSION; import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PROJECT_KEY; import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PROJECT_ID; @@ -67,6 +71,8 @@ public class GroupsActionTest { public UserSessionRule userSession = UserSessionRule.standalone(); @Rule public DbTester db = DbTester.create(System2.INSTANCE); + ComponentDbTester componentDb = new ComponentDbTester(db); + ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV"); DbClient dbClient; DbSession dbSession; @@ -79,7 +85,7 @@ public class GroupsActionTest { dbClient = db.getDbClient(); dbSession = db.getSession(); PermissionFinder permissionFinder = new PermissionFinder(dbClient); - underTest = new GroupsAction(dbClient, userSession, permissionFinder, new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient))); + underTest = new GroupsAction(dbClient, userSession, permissionFinder, new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes)); ws = new WsActionTester(underTest); userSession.login("login").setGlobalPermissions(SYSTEM_ADMIN); @@ -169,6 +175,26 @@ public class GroupsActionTest { .doesNotContain("group-3"); } + @Test + public void search_groups_on_views() { + ComponentDto view = componentDb.insertComponent(newView("view-uuid").setKey("view-key")); + GroupDto group = insertGroup(new GroupDto().setName("project-group-name")); + insertGroupRole(new GroupRoleDto() + .setGroupId(group.getId()) + .setRole(ISSUE_ADMIN) + .setResourceId(view.getId())); + + String result = ws.newRequest() + .setParam(PARAM_PERMISSION, ISSUE_ADMIN) + .setParam(PARAM_PROJECT_ID, "view-uuid") + .execute().getInput(); + + assertThat(result).contains("project-group-name") + .doesNotContain("group-1") + .doesNotContain("group-2") + .doesNotContain("group-3"); + } + @Test public void fail_if_project_permission_without_project() { expectedException.expect(BadRequestException.class); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/RemoveGroupActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/RemoveGroupActionTest.java index 0add9e68cd8..7a485b6660c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/RemoveGroupActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/RemoveGroupActionTest.java @@ -26,12 +26,14 @@ import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.rules.ExpectedException; import org.mockito.ArgumentCaptor; +import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ResourceTypesRule; import org.sonar.db.user.GroupDto; import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.BadRequestException; @@ -49,6 +51,7 @@ import static org.mockito.Mockito.verify; import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN; import static org.sonar.db.component.ComponentTesting.newFileDto; import static org.sonar.db.component.ComponentTesting.newProjectDto; +import static org.sonar.db.component.ComponentTesting.newView; import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_GROUP_ID; import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_GROUP_NAME; import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PERMISSION; @@ -64,6 +67,7 @@ public class RemoveGroupActionTest { public DbTester db = DbTester.create(System2.INSTANCE); @Rule public ExpectedException expectedException = ExpectedException.none(); + ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV"); private PermissionUpdater permissionUpdater; private ArgumentCaptor permissionChangeCaptor = ArgumentCaptor.forClass(PermissionChange.class); private DbSession dbSession = db.getSession(); @@ -74,7 +78,7 @@ public class RemoveGroupActionTest { DbClient dbClient = db.getDbClient(); ComponentFinder componentFinder = new ComponentFinder(dbClient); ws = new WsTester(new PermissionsWs( - new RemoveGroupAction(dbClient, new PermissionChangeBuilder(new PermissionDependenciesFinder(dbClient, componentFinder)), permissionUpdater))); + new RemoveGroupAction(dbClient, new PermissionChangeBuilder(new PermissionDependenciesFinder(dbClient, componentFinder, resourceTypes)), permissionUpdater))); userSession.login("admin").setGlobalPermissions(SYSTEM_ADMIN); } @@ -126,11 +130,28 @@ public class RemoveGroupActionTest { assertThat(permissionChange.componentKey()).isEqualTo("project-key"); } + @Test + public void remove_with_view_uuid() throws Exception { + insertComponent(newView("view-uuid").setKey("view-key")); + insertGroup("sonar-administrators"); + db.commit(); + + newRequest() + .setParam(PARAM_GROUP_NAME, "sonar-administrators") + .setParam(PARAM_PROJECT_ID, "view-uuid") + .setParam(PARAM_PERMISSION, SYSTEM_ADMIN) + .execute(); + + verify(permissionUpdater).removePermission(permissionChangeCaptor.capture()); + PermissionChange permissionChange = permissionChangeCaptor.getValue(); + assertThat(permissionChange.componentKey()).isEqualTo("view-key"); + } + @Test public void remove_with_project_key() throws Exception { insertComponent(newProjectDto("project-uuid").setKey("project-key")); insertGroup("sonar-administrators"); - commit(); + db.commit(); newRequest() .setParam(PARAM_GROUP_NAME, "sonar-administrators") diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/RemoveUserActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/RemoveUserActionTest.java index ff47ab463ec..6557350bc68 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/RemoveUserActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/RemoveUserActionTest.java @@ -26,12 +26,14 @@ import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.rules.ExpectedException; import org.mockito.ArgumentCaptor; +import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ResourceTypesRule; import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.NotFoundException; @@ -48,6 +50,7 @@ import static org.mockito.Mockito.verify; import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN; import static org.sonar.db.component.ComponentTesting.newFileDto; import static org.sonar.db.component.ComponentTesting.newProjectDto; +import static org.sonar.db.component.ComponentTesting.newView; import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PERMISSION; import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PROJECT_KEY; import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PROJECT_ID; @@ -61,6 +64,7 @@ public class RemoveUserActionTest { public DbTester db = DbTester.create(System2.INSTANCE); @Rule public ExpectedException expectedException = ExpectedException.none(); + ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV"); UserSessionRule userSession = UserSessionRule.standalone(); WsTester ws; PermissionUpdater permissionUpdater; @@ -75,7 +79,7 @@ public class RemoveUserActionTest { dbSession = db.getSession(); ComponentFinder componentFinder = new ComponentFinder(dbClient); ws = new WsTester(new PermissionsWs( - new RemoveUserAction(dbClient, permissionUpdater, new PermissionChangeBuilder(new PermissionDependenciesFinder(dbClient, componentFinder))))); + new RemoveUserAction(dbClient, permissionUpdater, new PermissionChangeBuilder(new PermissionDependenciesFinder(dbClient, componentFinder, resourceTypes))))); userSession.login("admin").setGlobalPermissions(SYSTEM_ADMIN); } @@ -122,6 +126,21 @@ public class RemoveUserActionTest { assertThat(permissionChange.componentKey()).isEqualTo("project-key"); } + @Test + public void remove_with_view_uuid() throws Exception { + insertComponent(newView("view-uuid").setKey("view-key")); + + ws.newPostRequest(PermissionsWs.ENDPOINT, ACTION) + .setParam(PARAM_USER_LOGIN, "ray.bradbury") + .setParam(PARAM_PROJECT_ID, "view-uuid") + .setParam(PARAM_PERMISSION, SYSTEM_ADMIN) + .execute(); + + verify(permissionUpdater).removePermission(permissionChangeCaptor.capture()); + PermissionChange permissionChange = permissionChangeCaptor.getValue(); + assertThat(permissionChange.componentKey()).isEqualTo("view-key"); + } + @Test public void fail_when_project_does_not_exist() throws Exception { expectedException.expect(NotFoundException.class); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/SearchProjectPermissionsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/SearchProjectPermissionsActionTest.java index a2c2649fa7a..8368c1276cd 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/SearchProjectPermissionsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/SearchProjectPermissionsActionTest.java @@ -85,7 +85,7 @@ public class SearchProjectPermissionsActionTest { resourceTypes = mock(ResourceTypes.class); when(resourceTypes.getRoots()).thenReturn(rootResourceTypes()); ComponentFinder componentFinder = new ComponentFinder(dbClient); - PermissionDependenciesFinder finder = new PermissionDependenciesFinder(dbClient, componentFinder); + PermissionDependenciesFinder finder = new PermissionDependenciesFinder(dbClient, componentFinder, resourceTypes); i18n.setProjectPermissions(); dataLoader = new SearchProjectPermissionsDataLoader(dbClient, finder, resourceTypes); @@ -225,7 +225,7 @@ public class SearchProjectPermissionsActionTest { insertComponent(newDeveloper("developer-name")); insertComponent(newProjectDto("project-uuid")); commit(); - dataLoader = new SearchProjectPermissionsDataLoader(dbClient, new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient)), resourceTypes); + dataLoader = new SearchProjectPermissionsDataLoader(dbClient, new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes), resourceTypes); underTest = new SearchProjectPermissionsAction(dbClient, userSession, i18n, dataLoader); ws = new WsActionTester(underTest); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/TemplateGroupsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/TemplateGroupsActionTest.java index 8c461c23727..53ed5888aa5 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/TemplateGroupsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/TemplateGroupsActionTest.java @@ -28,6 +28,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; 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; @@ -35,6 +36,7 @@ import org.sonar.core.permission.GlobalPermissions; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.component.ResourceTypesRule; import org.sonar.db.permission.PermissionTemplateDto; import org.sonar.db.permission.PermissionTemplateGroupDto; import org.sonar.db.user.GroupDto; @@ -71,6 +73,7 @@ public class TemplateGroupsActionTest { 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; DbSession dbSession; @@ -85,7 +88,7 @@ public class TemplateGroupsActionTest { public void setUp() { dbClient = db.getDbClient(); dbSession = db.getSession(); - underTest = new TemplateGroupsAction(dbClient, userSession, new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient))); + underTest = new TemplateGroupsAction(dbClient, userSession, new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes)); ws = new WsActionTester(underTest); userSession.login("login").setGlobalPermissions(SYSTEM_ADMIN); 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 5991d5796d2..bfd1f1f51e7 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 @@ -28,6 +28,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; 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; @@ -35,6 +36,7 @@ import org.sonar.core.permission.GlobalPermissions; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.component.ResourceTypesRule; import org.sonar.db.permission.PermissionTemplateDto; import org.sonar.db.permission.PermissionTemplateUserDto; import org.sonar.db.user.UserDto; @@ -66,6 +68,7 @@ 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(); DbSession dbSession = db.getSession(); WsActionTester ws; @@ -77,7 +80,7 @@ public class TemplateUsersActionTest { @Before public void setUp() { - PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient)); + PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes); underTest = new TemplateUsersAction(dbClient, userSession, dependenciesFinder); ws = new WsActionTester(underTest); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/UsersActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/UsersActionTest.java index 864de4c130a..9b24ebd01f3 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/UsersActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/UsersActionTest.java @@ -25,6 +25,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.rules.ExpectedException; +import org.sonar.api.resources.Qualifiers; import org.sonar.api.server.ws.WebService.Param; import org.sonar.api.server.ws.WebService.SelectionMode; import org.sonar.api.utils.System2; @@ -33,6 +34,7 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ResourceTypesRule; import org.sonar.db.user.UserDto; import org.sonar.db.user.UserRoleDto; import org.sonar.server.component.ComponentFinder; @@ -51,8 +53,8 @@ import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN; import static org.sonar.db.component.ComponentTesting.newProjectDto; import static org.sonar.db.user.UserTesting.newUserDto; import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PERMISSION; -import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PROJECT_KEY; import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PROJECT_ID; +import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PROJECT_KEY; import static org.sonar.test.JsonAssert.assertJson; @Category(DbTests.class) @@ -64,6 +66,7 @@ public class UsersActionTest { 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(); DbSession dbSession = db.getSession(); WsActionTester ws; @@ -73,7 +76,7 @@ public class UsersActionTest { @Before public void setUp() { PermissionFinder permissionFinder = new PermissionFinder(dbClient); - PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient)); + PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes); underTest = new UsersAction(dbClient, userSession, permissionFinder, dependenciesFinder); ws = new WsActionTester(underTest); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/AddGroupToTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/AddGroupToTemplateActionTest.java index b447951d92b..1d26b5ae5d8 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/AddGroupToTemplateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/AddGroupToTemplateActionTest.java @@ -29,12 +29,14 @@ import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.rules.ExpectedException; +import org.sonar.api.resources.Qualifiers; 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; import org.sonar.db.DbTester; +import org.sonar.db.component.ResourceTypesRule; import org.sonar.db.permission.GroupWithPermissionDto; import org.sonar.db.permission.PermissionQuery; import org.sonar.db.permission.PermissionTemplateDto; @@ -76,6 +78,7 @@ public class AddGroupToTemplateActionTest { public ExpectedException expectedException = ExpectedException.none(); @Rule public UserSessionRule userSession = UserSessionRule.standalone(); + ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV"); WsActionTester ws; DbClient dbClient; @@ -89,7 +92,7 @@ public class AddGroupToTemplateActionTest { dbSession = db.getSession(); userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); - PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient)); + PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes); ws = new WsActionTester(new AddGroupToTemplateAction(dbClient, dependenciesFinder, userSession)); group = insertGroup(newGroupDto().setName(GROUP_NAME)); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/AddUserToTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/AddUserToTemplateActionTest.java index 826dd24e4a3..6bc89246c15 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/AddUserToTemplateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/AddUserToTemplateActionTest.java @@ -29,11 +29,13 @@ import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.rules.ExpectedException; +import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.System2; import org.sonar.core.permission.GlobalPermissions; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.component.ResourceTypesRule; import org.sonar.db.permission.PermissionQuery; import org.sonar.db.permission.PermissionTemplateDto; import org.sonar.db.permission.UserWithPermissionDto; @@ -71,6 +73,7 @@ public class AddUserToTemplateActionTest { public ExpectedException expectedException = ExpectedException.none(); @Rule public UserSessionRule userSession = UserSessionRule.standalone(); + ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV"); WsActionTester ws; DbClient dbClient; @@ -84,7 +87,7 @@ public class AddUserToTemplateActionTest { dbSession = db.getSession(); userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); - PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient)); + PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes); ws = new WsActionTester(new AddUserToTemplateAction(dbClient, dependenciesFinder, userSession)); user = insertUser(newUserDto().setLogin(USER_LOGIN)); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/ApplyTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/ApplyTemplateActionTest.java index cbe4c96b6a5..5f676494966 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/ApplyTemplateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/ApplyTemplateActionTest.java @@ -30,6 +30,7 @@ import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.rules.ExpectedException; import org.sonar.api.config.Settings; +import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; import org.sonar.core.permission.GlobalPermissions; @@ -37,6 +38,7 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ResourceTypesRule; import org.sonar.db.permission.GroupWithPermissionDto; import org.sonar.db.permission.PermissionQuery; import org.sonar.db.permission.PermissionRepository; @@ -83,6 +85,7 @@ public class ApplyTemplateActionTest { public UserSessionRule userSession = UserSessionRule.standalone(); @Rule public ExpectedException expectedException = ExpectedException.none(); + ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV"); WsActionTester ws; DbClient dbClient; @@ -107,7 +110,7 @@ public class ApplyTemplateActionTest { PermissionFinder permissionFinder = new PermissionFinder(dbClient); ComponentFinder componentFinder = new ComponentFinder(dbClient); PermissionService permissionService = new PermissionService(dbClient, repository, issueAuthorizationIndexer, userSession, componentFinder); - PermissionDependenciesFinder permissionDependenciesFinder = new PermissionDependenciesFinder(dbClient, componentFinder); + PermissionDependenciesFinder permissionDependenciesFinder = new PermissionDependenciesFinder(dbClient, componentFinder, resourceTypes); ApplyTemplateAction underTest = new ApplyTemplateAction(dbClient, permissionService, permissionDependenciesFinder); ws = new WsActionTester(underTest); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java index 5b824c94637..2a6ecc9d7b0 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java @@ -26,12 +26,14 @@ 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.utils.System2; import org.sonar.api.web.UserRole; import org.sonar.core.permission.GlobalPermissions; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.component.ResourceTypesRule; import org.sonar.db.permission.PermissionTemplateDto; import org.sonar.db.user.GroupDto; import org.sonar.db.user.GroupTesting; @@ -67,6 +69,7 @@ public class DeleteTemplateActionTest { public UserSessionRule userSession = UserSessionRule.standalone(); @Rule public ExpectedException expectedException = ExpectedException.none(); + ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV"); WsActionTester ws; DbClient dbClient; @@ -83,7 +86,7 @@ public class DeleteTemplateActionTest { dbSession = db.getSession(); defaultTemplatePermissionFinder = mock(DefaultPermissionTemplateFinder.class); when(defaultTemplatePermissionFinder.getDefaultTemplateUuids()).thenReturn(Collections.emptySet()); - PermissionDependenciesFinder finder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient)); + PermissionDependenciesFinder finder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes); ws = new WsActionTester(new DeleteTemplateAction(dbClient, userSession, finder, defaultTemplatePermissionFinder)); permissionTemplate = insertTemplateAndAssociatedPermissions(newPermissionTemplateDto().setUuid(TEMPLATE_UUID)); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/RemoveGroupFromTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/RemoveGroupFromTemplateActionTest.java index d35995d9a45..cbb9772b6c4 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/RemoveGroupFromTemplateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/RemoveGroupFromTemplateActionTest.java @@ -29,11 +29,13 @@ import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.rules.ExpectedException; +import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.System2; import org.sonar.core.permission.GlobalPermissions; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.component.ResourceTypesRule; import org.sonar.db.permission.GroupWithPermissionDto; import org.sonar.db.permission.PermissionQuery; import org.sonar.db.permission.PermissionTemplateDto; @@ -74,6 +76,7 @@ public class RemoveGroupFromTemplateActionTest { public ExpectedException expectedException = ExpectedException.none(); @Rule public UserSessionRule userSession = UserSessionRule.standalone(); + ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV"); WsActionTester ws; DbClient dbClient; @@ -87,7 +90,7 @@ public class RemoveGroupFromTemplateActionTest { dbSession = db.getSession(); userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); - PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient)); + PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes); ws = new WsActionTester(new RemoveGroupFromTemplateAction(dbClient, dependenciesFinder, userSession)); group = insertGroup(newGroupDto().setName(GROUP_NAME)); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/RemoveUserFromTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/RemoveUserFromTemplateActionTest.java index 406bb72f18d..7bda0f44f05 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/RemoveUserFromTemplateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/RemoveUserFromTemplateActionTest.java @@ -29,11 +29,13 @@ import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.rules.ExpectedException; +import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.System2; import org.sonar.core.permission.GlobalPermissions; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.component.ResourceTypesRule; import org.sonar.db.permission.PermissionQuery; import org.sonar.db.permission.PermissionTemplateDto; import org.sonar.db.permission.UserWithPermissionDto; @@ -72,6 +74,7 @@ public class RemoveUserFromTemplateActionTest { public ExpectedException expectedException = ExpectedException.none(); @Rule public UserSessionRule userSession = UserSessionRule.standalone(); + ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV"); WsActionTester ws; DbClient dbClient; @@ -85,7 +88,7 @@ public class RemoveUserFromTemplateActionTest { dbSession = db.getSession(); userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); - PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient)); + PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes); ws = new WsActionTester(new RemoveUserFromTemplateAction(dbClient, dependenciesFinder, userSession)); user = insertUser(newUserDto().setLogin(USER_LOGIN)); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SetDefaultTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SetDefaultTemplateActionTest.java index 327c667003c..cd7dde8c148 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SetDefaultTemplateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SetDefaultTemplateActionTest.java @@ -94,7 +94,7 @@ public class SetDefaultTemplateActionTest { ws = new WsActionTester(new SetDefaultTemplateAction( dbClient, - new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient)), + new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes), resourceTypes, persistentSettings, userSession, i18n)); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/UpdateTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/UpdateTemplateActionTest.java index 0246c5a9eec..b882b9f82e5 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/UpdateTemplateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/UpdateTemplateActionTest.java @@ -26,11 +26,13 @@ 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.utils.System2; import org.sonar.core.permission.GlobalPermissions; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.component.ResourceTypesRule; import org.sonar.db.permission.PermissionTemplateDto; import org.sonar.db.user.GroupDto; import org.sonar.server.component.ComponentFinder; @@ -66,6 +68,7 @@ public class UpdateTemplateActionTest { WsActionTester ws; DbClient dbClient; DbSession dbSession; + ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV"); PermissionTemplateDto templateDto; @@ -77,7 +80,7 @@ public class UpdateTemplateActionTest { dbClient = db.getDbClient(); dbSession = db.getSession(); - PermissionDependenciesFinder finder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient)); + PermissionDependenciesFinder finder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes); ws = new WsActionTester(new UpdateTemplateAction(dbClient, userSession, system, finder)); diff --git a/sonar-db/src/test/java/org/sonar/db/component/ResourceTypesRule.java b/sonar-db/src/test/java/org/sonar/db/component/ResourceTypesRule.java new file mode 100644 index 00000000000..e305ca60a63 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/component/ResourceTypesRule.java @@ -0,0 +1,107 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.db.component; + +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import org.sonar.api.resources.ResourceType; +import org.sonar.api.resources.ResourceTypeTree; +import org.sonar.api.resources.ResourceTypes; + +public class ResourceTypesRule extends ResourceTypes { + private final Set allResourceTypes = new HashSet<>(); + private final Set rootResourceTypes = new HashSet<>(); + + @Override + public Collection getAll() { + return allResourceTypes; + } + + @Override + public Collection getRoots() { + return rootResourceTypes; + } + + public ResourceTypesRule setRootQualifiers(String... qualifiers) { + rootResourceTypes.clear(); + for (String qualifier : qualifiers) { + rootResourceTypes.add(ResourceType.builder(qualifier).build()); + } + + return this; + } + + public ResourceTypesRule setAllQualifiers(String... qualifiers) { + allResourceTypes.clear(); + for (String qualifier : qualifiers) { + allResourceTypes.add(ResourceType.builder(qualifier).build()); + } + + return this; + } + + @Override + public ResourceType get(String qualifier) { + throw new UnsupportedOperationException(); + } + + @Override + public Collection getAllWithPropertyKey(String propertyKey) { + throw new UnsupportedOperationException(); + } + + @Override + public Collection getAllWithPropertyValue(String propertyKey, String propertyValue) { + throw new UnsupportedOperationException(); + } + + @Override + public Collection getAllWithPropertyValue(String propertyKey, boolean propertyValue) { + throw new UnsupportedOperationException(); + } + + @Override + public List getChildrenQualifiers(String qualifier) { + throw new UnsupportedOperationException(); + } + + @Override + public List getChildren(String qualifier) { + throw new UnsupportedOperationException(); + } + + @Override + public List getLeavesQualifiers(String qualifier) { + throw new UnsupportedOperationException(); + } + + @Override + public ResourceTypeTree getTree(String qualifier) { + throw new UnsupportedOperationException(); + } + + @Override + public ResourceType getRoot(String qualifier) { + throw new UnsupportedOperationException(); + } +}