Quellcode durchsuchen

SONAR-9140 support public components in UserSessionRule

tags/6.4-RC1
Sébastien Lesaint vor 7 Jahren
Ursprung
Commit
69966b44e1

+ 5
- 3
server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java Datei anzeigen

@@ -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);


+ 1
- 2
server/sonar-server/src/test/java/org/sonar/server/issue/IssueQueryFactoryTest.java Datei anzeigen

@@ -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);


+ 12
- 11
server/sonar-server/src/test/java/org/sonar/server/issue/ws/SetTagsActionTest.java Datei anzeigen

@@ -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) {

+ 5
- 6
server/sonar-server/src/test/java/org/sonar/server/source/ws/HashActionTest.java Datei anzeigen

@@ -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());
}
}

+ 27
- 3
server/sonar-server/src/test/java/org/sonar/server/tester/AbstractMockUserSession.java Datei anzeigen

@@ -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));

+ 5
- 0
server/sonar-server/src/test/java/org/sonar/server/tester/UserSessionRule.java Datei anzeigen

@@ -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;

Laden…
Abbrechen
Speichern