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;
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")
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);
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;
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);
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;
@Test
public void set_tags() {
IssueDto issueDto = db.issues().insertIssue(newIssue().setTags(singletonList("old-tag")));
- setUserWithBrowsePermission(issueDto);
+ logIn(issueDto);
call(issueDto.getKey(), "bug", "todo");
@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());
@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(), "");
@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();
@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");
@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");
@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");
@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, '+', '-', '#', '.'");
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) {
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;
@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");
@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");
@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();
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());
}
}
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 {
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));
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;