]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9140 support public components in UserSessionRule
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 20 Apr 2017 13:17:23 +0000 (15:17 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 27 Apr 2017 12:25:54 +0000 (14:25 +0200)
server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java
server/sonar-server/src/test/java/org/sonar/server/issue/IssueQueryFactoryTest.java
server/sonar-server/src/test/java/org/sonar/server/issue/ws/SetTagsActionTest.java
server/sonar-server/src/test/java/org/sonar/server/source/ws/HashActionTest.java
server/sonar-server/src/test/java/org/sonar/server/tester/AbstractMockUserSession.java
server/sonar-server/src/test/java/org/sonar/server/tester/UserSessionRule.java

index 72cad52a3300f1a3d04370a94c25ffeca6dfba2b..471210dd14790ce8f0b06a8ccdf88da76e29fc52 100644 (file)
@@ -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);
 
index 3f2a897ab74c9a73607a86bee90cd7e047f522b4..3bc04036e0fe951cc335cb3355d4d1b4796516b2 100644 (file)
@@ -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);
 
index 7df5e6852a12cc6d572912730d0c793009e61f40..957b757fccf87c8c2586b825449c3a1aba00e256 100644 (file)
@@ -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) {
index 9f82b15bddbd016bbfc790d5f67a8eee93ee01eb..e0ee6f3bfa1e579d75f1c31302b211e8eb5d670f 100644 (file)
@@ -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());
   }
 }
index c4f88e935b5ab751fa487d0ca61422fea0e8529f..f148c4793a9b2f42242c021fa53deb9464642a36 100644 (file)
@@ -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));
index 5e10fa044897821f3cbb4345b6b120630b1f2894..d048bb536cf02a43eadb411abdd07e8683db9260 100644 (file)
@@ -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;