aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-server
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-04-20 15:17:23 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-04-27 14:25:54 +0200
commit69966b44e1ae5c881f9970adfaa26763c95b2b47 (patch)
tree4d07e7a048c3ee972732762faf1f058f2098df7b /server/sonar-server
parent4f973d867847fe80f7c9eb03833f5d9fe1558ba7 (diff)
downloadsonarqube-69966b44e1ae5c881f9970adfaa26763c95b2b47.tar.gz
sonarqube-69966b44e1ae5c881f9970adfaa26763c95b2b47.zip
SONAR-9140 support public components in UserSessionRule
Diffstat (limited to 'server/sonar-server')
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java8
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/issue/IssueQueryFactoryTest.java3
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/issue/ws/SetTagsActionTest.java23
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/source/ws/HashActionTest.java11
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/tester/AbstractMockUserSession.java30
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/tester/UserSessionRule.java5
6 files changed, 55 insertions, 25 deletions
diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java
index 72cad52a330..471210dd147 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java
@@ -59,8 +59,8 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.component.ComponentTesting.newDirectory;
import static org.sonar.db.component.ComponentTesting.newModuleDto;
-import static org.sonar.db.component.ComponentTesting.newProjectCopy;
import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
+import static org.sonar.db.component.ComponentTesting.newProjectCopy;
import static org.sonar.db.component.ComponentTesting.newSubView;
import static org.sonar.db.component.ComponentTesting.newView;
import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_COMPONENT_ID;
@@ -234,7 +234,8 @@ public class TreeActionTest {
componentDb.insertComponent(newProjectCopy("project-uuid-1-copy", project, view));
componentDb.insertComponent(newSubView(view, "sub-view-uuid", "sub-view-key").setName("sub-view-name"));
db.commit();
- logInWithBrowsePermission(view);
+ userSession.logIn()
+ .registerComponents(view, project);
TreeWsResponse response = ws.newRequest()
.setParam(PARAM_STRATEGY, "children")
@@ -268,7 +269,8 @@ public class TreeActionTest {
ComponentDto view = newView(db.getDefaultOrganization(), "view-uuid");
componentDb.insertViewAndSnapshot(view);
componentDb.insertComponent(newProjectCopy("project-copy-uuid", project, view));
- logInWithBrowsePermission(view);
+ userSession.logIn()
+ .registerComponents(project, view);
TreeWsResponse response = ws.newRequest().setParam(PARAM_COMPONENT_ID, view.uuid()).executeProtobuf(TreeWsResponse.class);
diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueQueryFactoryTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueQueryFactoryTest.java
index 3f2a897ab74..3bc04036e0f 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueQueryFactoryTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueQueryFactoryTest.java
@@ -27,7 +27,6 @@ import org.sonar.api.resources.Qualifiers;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.System2;
-import org.sonar.api.web.UserRole;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
@@ -192,7 +191,7 @@ public class IssueQueryFactoryTest {
ComponentDto view = db.components().insertView();
SearchWsRequest request = new SearchWsRequest()
.setComponentRootUuids(asList(view.uuid()));
- userSession.addProjectPermission(UserRole.USER, view);
+ userSession.registerComponents(view);
IssueQuery query = underTest.create(request);
diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SetTagsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SetTagsActionTest.java
index 7df5e6852a1..957b757fccf 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SetTagsActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SetTagsActionTest.java
@@ -65,7 +65,6 @@ import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
-import static org.sonar.api.web.UserRole.USER;
import static org.sonar.core.util.Protobuf.setNullable;
import static org.sonar.core.util.stream.MoreCollectors.join;
import static org.sonar.db.component.ComponentTesting.newFileDto;
@@ -95,7 +94,7 @@ public class SetTagsActionTest {
@Test
public void set_tags() {
IssueDto issueDto = db.issues().insertIssue(newIssue().setTags(singletonList("old-tag")));
- setUserWithBrowsePermission(issueDto);
+ logIn(issueDto);
call(issueDto.getKey(), "bug", "todo");
@@ -107,7 +106,7 @@ public class SetTagsActionTest {
@Test
public void remove_existing_tags_when_value_is_not_set() {
IssueDto issueDto = db.issues().insertIssue(newIssue().setTags(singletonList("old-tag")));
- setUserWithBrowsePermission(issueDto);
+ logIn(issueDto);
call(issueDto.getKey());
@@ -118,7 +117,7 @@ public class SetTagsActionTest {
@Test
public void remove_existing_tags_when_value_is_empty_string() {
IssueDto issueDto = db.issues().insertIssue(newIssue().setTags(singletonList("old-tag")));
- setUserWithBrowsePermission(issueDto);
+ logIn(issueDto);
call(issueDto.getKey(), "");
@@ -129,7 +128,7 @@ public class SetTagsActionTest {
@Test
public void set_tags_using_deprecated_key_param() {
IssueDto issueDto = db.issues().insertIssue(newIssue().setTags(singletonList("old-tag")));
- setUserWithBrowsePermission(issueDto);
+ logIn(issueDto);
ws.newRequest().setParam("key", issueDto.getKey()).setParam("tags", "bug").execute();
@@ -140,7 +139,7 @@ public class SetTagsActionTest {
@Test
public void tags_are_stored_as_lowercase() {
IssueDto issueDto = db.issues().insertIssue(newIssue().setTags(singletonList("old-tag")));
- setUserWithBrowsePermission(issueDto);
+ logIn(issueDto);
call(issueDto.getKey(), "bug", "Convention");
@@ -151,7 +150,7 @@ public class SetTagsActionTest {
@Test
public void empty_tags_are_ignored() {
IssueDto issueDto = db.issues().insertIssue(newIssue().setTags(singletonList("old-tag")));
- setUserWithBrowsePermission(issueDto);
+ logIn(issueDto);
call(issueDto.getKey(), "security", "", "convention");
@@ -162,7 +161,7 @@ public class SetTagsActionTest {
@Test
public void insert_entry_in_changelog_when_setting_tags() throws Exception {
IssueDto issueDto = db.issues().insertIssue(newIssue().setTags(singletonList("old-tag")));
- setUserWithBrowsePermission(issueDto);
+ logIn(issueDto);
call(issueDto.getKey(), "new-tag");
@@ -176,7 +175,7 @@ public class SetTagsActionTest {
@Test
public void fail_when_bad_tag_format() {
IssueDto issueDto = db.issues().insertIssue(newIssue().setTags(singletonList("old-tag")));
- setUserWithBrowsePermission(issueDto);
+ logIn(issueDto);
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Tag 'pol op' is invalid. Rule tags accept only the characters: a-z, 0-9, '+', '-', '#', '.'");
@@ -237,8 +236,10 @@ public class SetTagsActionTest {
return IssueTesting.newIssue(rule, file, project);
}
- private void setUserWithBrowsePermission(IssueDto issueDto) {
- userSession.logIn("john").addProjectPermission(USER, dbClient.componentDao().selectByUuid(db.getSession(), issueDto.getProjectUuid()).get());
+ private void logIn(IssueDto issueDto) {
+ userSession.logIn("john").registerComponents(
+ dbClient.componentDao().selectByUuid(db.getSession(), issueDto.getProjectUuid()).get(),
+ dbClient.componentDao().selectByUuid(db.getSession(), issueDto.getComponentUuid()).get());
}
private void logInAndAddProjectPermission(IssueDto issueDto, String permission) {
diff --git a/server/sonar-server/src/test/java/org/sonar/server/source/ws/HashActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/source/ws/HashActionTest.java
index 9f82b15bddb..e0ee6f3bfa1 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/source/ws/HashActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/source/ws/HashActionTest.java
@@ -25,7 +25,6 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
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.server.component.ComponentFinder;
@@ -62,7 +61,7 @@ public class HashActionTest {
@Test
public void show_hashes() throws Exception {
db.prepareDbUnit(getClass(), "shared.xml");
- loginAndAddProjectPermission(PROJECT_UUID, UserRole.USER);
+ loginAndRegisterComponent(PROJECT_UUID);
WsTester.TestRequest request = tester.newGetRequest("api/sources", "hash").setParam("key", COMPONENT_KEY);
assertThat(request.execute().outputAsString()).isEqualTo("987654");
@@ -71,7 +70,7 @@ public class HashActionTest {
@Test
public void show_hashes_on_test_file() throws Exception {
db.prepareDbUnit(getClass(), "show_hashes_on_test_file.xml");
- loginAndAddProjectPermission(PROJECT_UUID, UserRole.USER);
+ loginAndRegisterComponent(PROJECT_UUID);
WsTester.TestRequest request = tester.newGetRequest("api/sources", "hash").setParam("key", "ActionTest.java");
assertThat(request.execute().outputAsString()).isEqualTo("987654");
@@ -80,7 +79,7 @@ public class HashActionTest {
@Test
public void hashes_empty_if_no_source() throws Exception {
db.prepareDbUnit(getClass(), "no_source.xml");
- loginAndAddProjectPermission(PROJECT_UUID, UserRole.USER);
+ loginAndRegisterComponent(PROJECT_UUID);
WsTester.TestRequest request = tester.newGetRequest("api/sources", "hash").setParam("key", COMPONENT_KEY);
request.execute().assertNoContent();
@@ -105,7 +104,7 @@ public class HashActionTest {
tester.newGetRequest("api/sources", "hash").setParam("key", COMPONENT_KEY).execute();
}
- private void loginAndAddProjectPermission(String componentUuid, String permission) {
- userSessionRule.logIn("polop").addProjectPermission(permission, db.getDbClient().componentDao().selectByUuid(db.getSession(), componentUuid).get());
+ private void loginAndRegisterComponent(String componentUuid) {
+ userSessionRule.logIn("polop").registerComponents(db.getDbClient().componentDao().selectByUuid(db.getSession(), componentUuid).get());
}
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/tester/AbstractMockUserSession.java b/server/sonar-server/src/test/java/org/sonar/server/tester/AbstractMockUserSession.java
index c4f88e935b5..f148c4793a9 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/tester/AbstractMockUserSession.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/tester/AbstractMockUserSession.java
@@ -25,10 +25,13 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
+import org.sonar.api.web.UserRole;
+import org.sonar.core.permission.ProjectPermissions;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.permission.OrganizationPermission;
import org.sonar.server.user.AbstractUserSession;
+import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Maps.newHashMap;
public abstract class AbstractMockUserSession<T extends AbstractMockUserSession> extends AbstractUserSession {
@@ -53,16 +56,37 @@ public abstract class AbstractMockUserSession<T extends AbstractMockUserSession>
return permissionsByOrganizationUuid.get(organizationUuid).contains(permission);
}
- public T addProjectPermission(String permission, ComponentDto... components) {
- this.projectPermissionsCheckedByUuid.add(permission);
+ /**
+ * Use this method to register public root component and non root components the UserSession must be aware of.
+ * (ie. this method can be used to emulate the content of the DB)
+ */
+ public T registerComponents(ComponentDto... components) {
Arrays.stream(components)
.forEach(component -> {
- this.projectUuidByPermission.put(permission, component.projectUuid());
+ if (component.projectUuid().equals(component.uuid()) && !component.isPrivate()) {
+ this.projectUuidByPermission.put(UserRole.USER, component.uuid());
+ this.projectUuidByPermission.put(UserRole.CODEVIEWER, component.uuid());
+ this.projectPermissionsCheckedByUuid.add(UserRole.USER);
+ this.projectPermissionsCheckedByUuid.add(UserRole.CODEVIEWER);
+ }
this.projectUuidByComponentUuid.put(component.uuid(), component.projectUuid());
});
return clazz.cast(this);
}
+ public T addProjectPermission(String permission, ComponentDto... components) {
+ Arrays.stream(components).forEach(component -> {
+ checkArgument(
+ component.isPrivate() || !ProjectPermissions.PUBLIC_PERMISSIONS.contains(permission),
+ "public component %s can't be granted public permission %s", component.uuid(), permission);
+ });
+ registerComponents(components);
+ this.projectPermissionsCheckedByUuid.add(permission);
+ Arrays.stream(components)
+ .forEach(component -> this.projectUuidByPermission.put(permission, component.projectUuid()));
+ return clazz.cast(this);
+ }
+
@Override
protected Optional<String> componentUuidToProjectUuid(String componentUuid) {
return Optional.ofNullable(projectUuidByComponentUuid.get(componentUuid));
diff --git a/server/sonar-server/src/test/java/org/sonar/server/tester/UserSessionRule.java b/server/sonar-server/src/test/java/org/sonar/server/tester/UserSessionRule.java
index 5e10fa04489..d048bb536cf 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/tester/UserSessionRule.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/tester/UserSessionRule.java
@@ -187,6 +187,11 @@ public class UserSessionRule implements TestRule, UserSession {
setCurrentUserSession(userSession);
}
+ public UserSessionRule registerComponents(ComponentDto... componentDtos) {
+ ensureAbstractMockUserSession().registerComponents(componentDtos);
+ return this;
+ }
+
public UserSessionRule addProjectPermission(String projectPermission, ComponentDto... components) {
ensureAbstractMockUserSession().addProjectPermission(projectPermission, components);
return this;