* Using {@link #hasComponentPermission(String, ComponentDto)} is recommended
* because it does not have to load project if the referenced component
* is not a project.
+ *
+ * @deprecated use {@link #hasComponentPermission(String, ComponentDto)} instead
*/
+ @Deprecated
boolean hasComponentUuidPermission(String permission, String componentUuid);
/**
/**
* Ensures that {@link #hasComponentUuidPermission(String, String)} is {@code true},
* otherwise throws a {@link org.sonar.server.exceptions.ForbiddenException}.
+ *
+ * @deprecated use {@link #checkComponentPermission(String, ComponentDto)} instead
*/
+ @Deprecated
UserSession checkComponentUuidPermission(String permission, String componentUuid);
/**
}
private void addBrowsePermissionOnComponent(ComponentDto project) {
- userSessionRule.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSessionRule.addProjectPermission(UserRole.USER, project);
}
private ServerIssue call(String componentKey) {
OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
dbClient.organizationDao().insert(dbSession, organizationDto);
ComponentDto project = ComponentTesting.newProjectDto(organizationDto);
- userSessionRule.logIn().addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
+ userSessionRule.logIn().addProjectPermission(SCAN_EXECUTION, project);
dbClient.componentDao().insert(dbSession, project);
addDefaultProfile();
OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
dbClient.organizationDao().insert(dbSession, organizationDto);
ComponentDto project = ComponentTesting.newProjectDto(organizationDto);
- userSessionRule.logIn("john").addProjectUuidPermissions(SCAN_EXECUTION, project.projectUuid());
+ userSessionRule.logIn("john").addProjectPermission(SCAN_EXECUTION, project);
dbClient.componentDao().insert(dbSession, project);
addDefaultProfile();
OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
dbClient.organizationDao().insert(dbSession, organizationDto);
ComponentDto project = ComponentTesting.newProjectDto(organizationDto);
- userSessionRule.logIn("john").addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSessionRule.logIn("john").addProjectPermission(UserRole.USER, project);
dbClient.componentDao().insert(dbSession, project);
addDefaultProfile();
OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
dbClient.organizationDao().insert(dbSession, organizationDto);
ComponentDto project = ComponentTesting.newProjectDto(organizationDto);
- userSessionRule.logIn().addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
+ userSessionRule.logIn().addProjectPermission(SCAN_EXECUTION, project);
dbClient.componentDao().insert(dbSession, project);
addDefaultProfile();
OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
dbClient.organizationDao().insert(dbSession, organizationDto);
ComponentDto project = ComponentTesting.newProjectDto(organizationDto);
- userSessionRule.logIn().addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
+ userSessionRule.logIn().addProjectPermission(SCAN_EXECUTION, project);
dbClient.componentDao().insert(dbSession, project);
addDefaultProfile();
OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
dbClient.organizationDao().insert(dbSession, organizationDto);
ComponentDto project = ComponentTesting.newProjectDto(organizationDto);
- userSessionRule.logIn().addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
+ userSessionRule.logIn().addProjectPermission(SCAN_EXECUTION, project);
dbClient.componentDao().insert(dbSession, project);
addDefaultProfile();
OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
dbClient.organizationDao().insert(dbSession, organizationDto);
ComponentDto project = ComponentTesting.newProjectDto(organizationDto);
- userSessionRule.logIn().addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
+ userSessionRule.logIn().addProjectPermission(SCAN_EXECUTION, project);
dbClient.componentDao().insert(dbSession, project);
addDefaultProfile();
dbClient.organizationDao().insert(dbSession, organizationDto);
// No snapshot attached on the project -> provisioned project
ComponentDto project = ComponentTesting.newProjectDto(organizationDto);
- userSessionRule.logIn().addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
+ userSessionRule.logIn().addProjectPermission(SCAN_EXECUTION, project);
dbClient.componentDao().insert(dbSession, project);
addDefaultProfile();
// No module properties
ComponentDto subModule = ComponentTesting.newModuleDto(module);
- userSessionRule.logIn().addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
+ userSessionRule.logIn().addProjectPermission(SCAN_EXECUTION, project);
dbClient.componentDao().insert(dbSession, subModule);
// Sub module properties
dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey("sonar.jira.login.secured").setValue("john").setResourceId(module.getId()));
ComponentDto subModule = ComponentTesting.newModuleDto(module);
- userSessionRule.logIn().addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
+ userSessionRule.logIn().addProjectPermission(SCAN_EXECUTION, project);
dbClient.componentDao().insert(dbSession, subModule);
// Sub module properties
// No module property
ComponentDto subModule = ComponentTesting.newModuleDto(module);
- userSessionRule.logIn().addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
+ userSessionRule.logIn().addProjectPermission(SCAN_EXECUTION, project);
dbClient.componentDao().insert(dbSession, subModule);
// No sub module property
dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR-SERVER").setResourceId(module.getId()));
ComponentDto subModule = ComponentTesting.newModuleDto(module);
- userSessionRule.logIn().addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
+ userSessionRule.logIn().addProjectPermission(SCAN_EXECUTION, project);
dbClient.componentDao().insert(dbSession, subModule);
// No sub module property
dbClient.componentDao().insert(dbSession, project);
dbSession.commit();
- userSessionRule.logIn("john").addProjectUuidPermissions(UserRole.USER, project.projectUuid());
+ userSessionRule.logIn("john").addProjectPermission(UserRole.USER, project);
thrown.expect(ForbiddenException.class);
thrown.expectMessage("You're only authorized to execute a local (preview) SonarQube analysis without pushing the results to the SonarQube server. " +
dbClient.componentDao().insert(dbSession, project);
dbSession.commit();
- userSessionRule.logIn("john").addProjectUuidPermissions(GlobalPermissions.SCAN_EXECUTION, project.projectUuid());
+ userSessionRule.logIn("john").addProjectPermission(GlobalPermissions.SCAN_EXECUTION, project);
thrown.expect(ForbiddenException.class);
thrown.expectMessage("You don't have the required permissions to access this project. Please contact your SonarQube administrator.");
OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
dbClient.organizationDao().insert(dbSession, organizationDto);
ComponentDto project = ComponentTesting.newProjectDto(organizationDto);
- userSessionRule.logIn().addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
+ userSessionRule.logIn().addProjectPermission(SCAN_EXECUTION, project);
dbClient.componentDao().insert(dbSession, project);
addDefaultProfile();
OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
dbClient.organizationDao().insert(dbSession, organizationDto);
ComponentDto project = ComponentTesting.newProjectDto(organizationDto);
- userSessionRule.logIn().addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
+ userSessionRule.logIn().addProjectPermission(SCAN_EXECUTION, project);
dbClient.componentDao().insert(dbSession, project);
addDefaultProfile();
tester.get(FileSourceDao.class).insert(dbSession, newFileSourceDto(projectFile).setSrcHash("123456").setRevision("987654321"));
ComponentDto module = ComponentTesting.newModuleDto(project);
- userSessionRule.logIn().addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
+ userSessionRule.logIn().addProjectPermission(SCAN_EXECUTION, project);
dbClient.componentDao().insert(dbSession, module);
// File on module
@Test
public void throw_ForbiddenException_if_browse_permission_but_not_scan_permission() {
ComponentDto project = dbTester.components().insertProject();
- userSession.logIn().addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
expectedException.expect(ForbiddenException.class);
expectedException.expectMessage("You're only authorized to execute a local (preview) SonarQube analysis without pushing the results to the SonarQube server");
@Test
public void issues_mode_is_allowed_if_user_has_browse_permission() {
ComponentDto project = dbTester.components().insertProject();
- userSession.logIn().addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
ProjectRepositories repositories = underTest.load(ProjectDataQuery.create().setModuleKey(project.key()).setIssuesMode(true));
@Test
public void issues_mode_is_forbidden_if_user_doesnt_have_browse_permission() {
ComponentDto project = dbTester.components().insertProject();
- userSession.logIn().addProjectUuidPermissions(GlobalPermissions.SCAN_EXECUTION, project.uuid());
+ userSession.logIn().addProjectPermission(GlobalPermissions.SCAN_EXECUTION, project);
expectedException.expect(ForbiddenException.class);
expectedException.expectMessage("You don't have the required permissions to access this project");
public void scan_permission_on_organization_is_enough_even_without_scan_permission_on_project() {
ComponentDto project = dbTester.components().insertProject();
userSession.logIn().addPermission(SCAN, project.getOrganizationUuid());
- userSession.logIn().addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
ProjectRepositories repositories = underTest.load(ProjectDataQuery.create().setModuleKey(project.key()).setIssuesMode(true));
@Test
public void project_administrator_can_access_his_project_activity() {
- dbTester.components().insertProject(dbTester.organizations().insert(), "PROJECT_1");
+ ComponentDto project = dbTester.components().insertProject(dbTester.organizations().insert(), "PROJECT_1");
// no need to be a system admin
- userSession.logIn().addComponentUuidPermission(UserRole.ADMIN, "PROJECT_1", "PROJECT_1");
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
insertActivity("T1", "PROJECT_1", CeActivityDto.Status.SUCCESS);
insertActivity("T2", "PROJECT_2", CeActivityDto.Status.FAILED);
// is reserved to roots
ComponentDto view = dbTester.components().insertView();
insertActivity("T1", view.uuid(), CeActivityDto.Status.SUCCESS);
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, view.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, view);
expectedException.expect(ForbiddenException.class);
expectedException.expectMessage("Insufficient privileges");
ComponentDto project = dbTester.components().insertProject(dbTester.getDefaultOrganization(), "PROJECT_1");
insertQueue("T1", "PROJECT_1", CeQueueDto.Status.IN_PROGRESS);
insertActivity("T1", "PROJECT_1", CeActivityDto.Status.SUCCESS);
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
ActivityResponse result = call(ws.newRequest()
.setParam(PARAM_COMPONENT_ID, "PROJECT_1")
public void status_for_a_project_as_project_admin() {
String projectUuid = "project-uuid";
String anotherProjectUuid = "another-project-uuid";
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, projectUuid);
OrganizationDto organizationDto = db.organizations().insert();
- db.components().insertComponent(newProjectDto(organizationDto, projectUuid));
+ ComponentDto project = newProjectDto(organizationDto, projectUuid);
+ db.components().insertComponent(project);
db.components().insertComponent(newProjectDto(organizationDto, anotherProjectUuid));
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
// pending tasks returned
insertInQueue(CeQueueDto.Status.PENDING, projectUuid);
insertInQueue(CeQueueDto.Status.PENDING, projectUuid);
@Test
public void empty_queue_and_empty_activity() {
- dbTester.components().insertComponent(newProjectDto(dbTester.organizations().insert(), "PROJECT_1"));
- userSession.addComponentUuidPermission(UserRole.USER, "PROJECT_1", "PROJECT_1");
+ ComponentDto project = dbTester.components().insertComponent(newProjectDto(dbTester.organizations().insert(), "PROJECT_1"));
+ userSession.addProjectPermission(UserRole.USER, project);
WsCe.ProjectResponse response = ws.newRequest()
.setParam("componentId", "PROJECT_1")
@Test
public void project_tasks() {
OrganizationDto organizationDto = dbTester.organizations().insert();
- dbTester.components().insertComponent(newProjectDto(organizationDto, "PROJECT_1"));
- userSession.addComponentUuidPermission(UserRole.USER, "PROJECT_1", "PROJECT_1");
+ ComponentDto project = dbTester.components().insertComponent(newProjectDto(organizationDto, "PROJECT_1"));
+ userSession.addProjectPermission(UserRole.USER, project);
insertActivity("T1", "PROJECT_1", CeActivityDto.Status.SUCCESS);
insertActivity("T2", "PROJECT_2", CeActivityDto.Status.FAILED);
insertActivity("T3", "PROJECT_1", CeActivityDto.Status.FAILED);
@Test
public void canceled_tasks_must_not_be_picked_as_current_analysis() {
- dbTester.components().insertComponent(newProjectDto(dbTester.getDefaultOrganization(), "PROJECT_1"));
- userSession.addComponentUuidPermission(UserRole.USER, "PROJECT_1", "PROJECT_1");
+ ComponentDto project = dbTester.components().insertComponent(newProjectDto(dbTester.getDefaultOrganization(), "PROJECT_1"));
+ userSession.addProjectPermission(UserRole.USER, project);
insertActivity("T1", "PROJECT_1", CeActivityDto.Status.SUCCESS);
insertActivity("T2", "PROJECT_2", CeActivityDto.Status.FAILED);
insertActivity("T3", "PROJECT_1", CeActivityDto.Status.SUCCESS);
@Test
public void fail_with_404_when_component_does_not_exist() throws Exception {
- userSession.addComponentUuidPermission(UserRole.USER, "PROJECT_1", "PROJECT_1");
-
expectedException.expect(NotFoundException.class);
ws.newRequest()
.setParam("componentId", "UNKNOWN")
}
private void logInWithBrowsePermission(ComponentDto project) {
- userSession.logIn().addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
}
private CeQueueDto insertQueue(String taskUuid, String componentUuid, CeQueueDto.Status status) {
@Test
public void get_project_queue_task_with_scan_permission_on_project() {
- userSession.logIn().addProjectUuidPermissions(GlobalPermissions.SCAN_EXECUTION, project.uuid());
+ userSession.logIn().addProjectPermission(GlobalPermissions.SCAN_EXECUTION, project);
CeQueueDto task = createAndPersistQueueTask(project);
call(task.getUuid());
@Test
public void get_project_archived_task_with_scan_permission_on_project() {
- userSession.logIn().addProjectUuidPermissions(GlobalPermissions.SCAN_EXECUTION, project.uuid());
+ userSession.logIn().addProjectPermission(GlobalPermissions.SCAN_EXECUTION, project);
CeActivityDto task = createAndPersistArchivedTask(project);
call(task.getUuid());
expectedException.expect(ForbiddenException.class);
ComponentDto project = insertSampleRootProject();
- userSession.logIn("john").addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.logIn("john").addProjectPermission(UserRole.USER, project);
underTest.updateKey(dbSession, project, "sample2:root");
}
}
private void logInAsProjectAdministrator(ComponentDto provisionedProject) {
- userSession.logIn("john").addProjectUuidPermissions(UserRole.ADMIN, provisionedProject.uuid());
+ userSession.logIn("john").addProjectPermission(UserRole.ADMIN, provisionedProject);
}
}
@Test
public void file_without_measures() throws Exception {
- insertComponentsAndAnalysis();
+ ComponentDto[] components = insertComponentsAndAnalysis();
dbTester.commit();
- userSessionRule.logIn("john").addComponentUuidPermission(UserRole.USER, PROJECT_UUID, FILE_UUID);
+ userSessionRule.logIn("john").addProjectPermission(UserRole.USER, components);
TestRequest request = wsTester.newRequest().setParam("uuid", FILE_UUID);
jsonAssert(request, "app.json");
}
@Test
public void file_with_measures() throws Exception {
- insertComponentsAndAnalysis();
+ ComponentDto[] components = insertComponentsAndAnalysis();
insertFileMeasure(metricsByKey.get(LINES_KEY).getId(), 200d, null);
insertFileMeasure(metricsByKey.get(DUPLICATED_LINES_DENSITY_KEY).getId(), 7.4, null);
insertFileMeasure(metricsByKey.get(SQALE_RATING_KEY).getId(), null, "C");
userSessionRule
.logIn("john")
- .addComponentUuidPermission(UserRole.USER, PROJECT_UUID, FILE_UUID);
+ .addProjectPermission(UserRole.USER, components);
TestRequest request = wsTester.newRequest().setParam("uuid", FILE_UUID);
jsonAssert(request, "app_with_measures.json");
}
@Test
public void file_with_coverage() throws Exception {
- insertComponentsAndAnalysis();
+ ComponentDto[] components = insertComponentsAndAnalysis();
insertFileMeasure(metricsByKey.get(COVERAGE_KEY).getId(), 95.4, null);
dbTester.commit();
- userSessionRule.logIn("john").addComponentUuidPermission(UserRole.USER, PROJECT_UUID, FILE_UUID);
+ userSessionRule.logIn("john")
+ .addProjectPermission(UserRole.USER, components);
TestRequest request = wsTester.newRequest().setParam("uuid", FILE_UUID);
jsonAssert(request, "app_with_ut_measure.json");
}
dbTester.commit();
}
- private void insertComponentsAndAnalysis() {
+ private ComponentDto[] insertComponentsAndAnalysis() {
ComponentDto project = ComponentTesting.newProjectDto(dbTester.getDefaultOrganization(), PROJECT_UUID)
.setLongName("SonarQube")
.setKey(PROJECT_KEY);
dbTester.getDbClient().componentDao().insert(dbTester.getSession(), project, module, file);
SnapshotDto analysis = SnapshotTesting.newAnalysis(project).setUuid(ANALYSIS_UUID);
dbTester.getDbClient().snapshotDao().insert(dbTester.getSession(), analysis);
+ return new ComponentDto[] {project, module, file};
}
private void insertFileMeasure(int metricId, @Nullable Double value, @Nullable String data) {
@Test
public void show_with_browse_permission() {
- userSession.logIn().addProjectUuidPermissions(UserRole.USER, "project-uuid");
- componentDb.insertProjectAndSnapshot(newProjectDto(db.organizations().insert(), "project-uuid"));
+ ComponentDto project = newProjectDto(db.organizations().insert(), "project-uuid");
+ componentDb.insertProjectAndSnapshot(project);
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
ShowWsResponse response = newRequest("project-uuid", null);
ComponentDto module = componentDb.insertComponent(newModuleDto(project));
ComponentDto directory = componentDb.insertComponent(newDirectory(module, "dir"));
ComponentDto file = componentDb.insertComponent(newFileDto(directory));
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
ShowWsResponse response = newRequest(null, file.key());
public void show_without_ancestors_when_project() throws Exception {
ComponentDto project = componentDb.insertProject();
componentDb.insertComponent(newModuleDto(project));
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
ShowWsResponse response = newRequest(null, project.key());
componentDb.insertSnapshot(newAnalysis(project).setCreatedAt(1_000_000_000L).setLast(false));
componentDb.insertSnapshot(newAnalysis(project).setCreatedAt(2_000_000_000L).setLast(false));
componentDb.insertSnapshot(newAnalysis(project).setCreatedAt(3_000_000_000L).setLast(true));
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
ShowWsResponse response = newRequest(null, project.key());
ComponentDto module = componentDb.insertComponent(newModuleDto(project));
ComponentDto directory = componentDb.insertComponent(newDirectory(module, "dir"));
ComponentDto file = componentDb.insertComponent(newFileDto(directory));
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
ShowWsResponse response = newRequest(null, file.key());
@Test
public void fail_when_not_enough_privileges() {
+ ComponentDto project = componentDb.insertComponent(newProjectDto(db.organizations().insert(), "project-uuid"));
userSession.logIn()
- .addProjectUuidPermissions(UserRole.CODEVIEWER, "project-uuid");
- componentDb.insertComponent(newProjectDto(db.organizations().insert(), "project-uuid"));
+ .addProjectPermission(UserRole.CODEVIEWER, project);
db.commit();
expectedException.expect(ForbiddenException.class);
}
private void logInWithBrowsePermission(ComponentDto project) {
- userSession.logIn().addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
}
}
import org.sonar.db.DbTester;
import org.sonar.db.ce.CeTaskTypes;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ComponentTesting;
import org.sonar.db.organization.OrganizationDto;
+import org.sonar.db.permission.OrganizationPermission;
import org.sonar.server.component.ComponentUpdater;
import org.sonar.server.component.NewComponent;
import org.sonar.server.exceptions.ForbiddenException;
@Test
public void submit_a_report_on_existing_project() {
ComponentDto project = db.components().insertProject(db.getDefaultOrganization());
- userSession.logIn().addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
+ userSession.logIn().addProjectPermission(SCAN_EXECUTION, project);
mockSuccessfulPrepareSubmitCall();
public void provision_project_if_does_not_exist() throws Exception {
OrganizationDto organization = db.organizations().insert();
userSession
- .addProjectUuidPermissions(SCAN_EXECUTION, PROJECT_UUID)
+ .addPermission(OrganizationPermission.SCAN, organization.getUuid())
.addPermission(PROVISION_PROJECTS, organization);
mockSuccessfulPrepareSubmitCall();
@Test
public void no_favorite_when_no_project_creator_permission_on_permission_template() {
userSession
- .addProjectUuidPermissions(SCAN_EXECUTION, PROJECT_UUID)
+ .addPermission(OrganizationPermission.SCAN, db.getDefaultOrganization().getUuid())
.addPermission(PROVISION_PROJECTS, db.getDefaultOrganization());
mockSuccessfulPrepareSubmitCall();
@Test
public void submit_a_report_on_new_project_with_scan_permission_on_organization() {
userSession
- .addProjectUuidPermissions(SCAN_EXECUTION, PROJECT_UUID)
+ .addPermission(OrganizationPermission.SCAN, db.getDefaultOrganization().getUuid())
.addPermission(PROVISION_PROJECTS, db.getDefaultOrganization());
mockSuccessfulPrepareSubmitCall();
@Test
public void submit_a_report_on_existing_project_with_project_scan_permission() {
ComponentDto project = db.components().insertProject(db.getDefaultOrganization());
- userSession.addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
+ userSession.addProjectPermission(SCAN_EXECUTION, project);
mockSuccessfulPrepareSubmitCall();
@Test
public void fail_with_forbidden_exception_on_new_project_when_only_project_scan_permission() {
- userSession.addProjectUuidPermissions(SCAN_EXECUTION, PROJECT_UUID);
+ userSession.addProjectPermission(SCAN_EXECUTION, ComponentTesting.newProjectDto(db.getDefaultOrganization(), PROJECT_UUID));
mockSuccessfulPrepareSubmitCall();
when(componentUpdater.create(any(DbSession.class), any(NewComponent.class), eq(null))).thenReturn(new ComponentDto().setUuid(PROJECT_UUID).setKey(PROJECT_KEY));
@Test
public void project_branch_must_not_benefit_from_the_scan_permission_on_main_project() {
ComponentDto mainProject = db.components().insertProject();
- userSession.addProjectUuidPermissions(GlobalPermissions.SCAN_EXECUTION, mainProject.uuid());
+ userSession.addProjectPermission(GlobalPermissions.SCAN_EXECUTION, mainProject);
// user does not have the "scan" permission on the branch, so it can't scan it
String branchName = "branchFoo";
ComponentDto file = db.components().insertComponent(newFileDto(project).setKey("foo.js"));
db.components().insertSnapshot(newAnalysis(project));
- userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, project.uuid());
+ userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project);
WsTester.Result result = newBaseRequest().setParam("key", file.key()).execute();
db.getDbClient().measureDao().insert(db.getSession(), newMeasureDto(dataMetric, file, snapshot).setData(xml));
db.commit();
- userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, project.uuid());
+ userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project);
WsTester.TestRequest request = requestFactory.apply(file);
WsTester.Result result = request.execute();
public void add_a_file() {
ComponentDto project = insertProjectAndPermissions();
ComponentDto file = db.components().insertComponent(newFileDto(project));
- userSession.addComponentUuidPermission(UserRole.USER, PROJECT_UUID, file.uuid());
+ userSession.addProjectPermission(UserRole.USER, project, file);
call(file.key());
@Test
public void fail_when_no_browse_permission_on_the_project() {
- insertProject();
+ ComponentDto project = insertProject();
userSession.logIn();
- userSession.addProjectUuidPermissions(UserRole.ADMIN, PROJECT_UUID);
+ userSession.addProjectPermission(UserRole.ADMIN, project);
expectedException.expect(ForbiddenException.class);
}
private ComponentDto insertProjectAndPermissions() {
+ ComponentDto project = insertProject();
userSession
.logIn()
.setUserId(USER_ID)
- .addProjectUuidPermissions(UserRole.USER, PROJECT_UUID);
+ .addProjectPermission(UserRole.USER, project);
- return insertProject();
+ return project;
}
private TestResponse call(@Nullable String componentKey) {
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ComponentTesting;
import org.sonar.db.issue.IssueDto;
import org.sonar.db.organization.OrganizationTesting;
import org.sonar.server.tester.UserSessionRule;
@Test
public void return_provided_actions_with_set_severity_and_set_type_when_issue_admin() {
- userSession.addProjectUuidPermissions(ISSUE_ADMIN, PROJECT_UUID);
+ userSession.addProjectPermission(ISSUE_ADMIN, ComponentTesting.newProjectDto(OrganizationTesting.newOrganizationDto(), PROJECT_UUID));
assertThat(underTest.listAvailableActions(issue)).containsOnly("comment", "assign", "set_tags", "set_type", "assign_to_me", "set_severity");
}
@Test
public void get_by_issue_key() throws Exception {
IssueDto issueDto = insertIssue();
- userSession.addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ String permission = USER;
+ addProjectPermission(issueDto, permission);
IssueDto result = underTest.getByKey(db.getSession(), issueDto.getKey());
@Test
public void fail_when_issue_key_does_not_exist() throws Exception {
IssueDto issueDto = insertIssue();
- userSession.addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ addProjectPermission(issueDto, USER);
expectedException.expect(NotFoundException.class);
expectedException.expectMessage("Issue with key 'UNKNOWN' does not exist");
@Test
public void fail_when_not_enough_permission() throws Exception {
IssueDto issueDto = insertIssue();
- userSession.addProjectUuidPermissions(CODEVIEWER, issueDto.getProjectUuid());
+ addProjectPermission(issueDto, CODEVIEWER);
expectedException.expect(ForbiddenException.class);
underTest.getByKey(db.getSession(), issueDto.getKey());
ComponentDto file = componentDbTester.insertComponent(newFileDto(project));
return issueDbTester.insertIssue(newDto(rule, file, project));
}
+
+ private void addProjectPermission(IssueDto issueDto, String permission) {
+ userSession.addProjectPermission(permission, db.getDbClient().componentDao().selectByUuid(db.getSession(), issueDto.getProjectUuid()).get());
+ }
}
ComponentDto view = db.components().insertView();
SearchWsRequest request = new SearchWsRequest()
.setComponentRootUuids(asList(view.uuid()));
- userSession.addProjectUuidPermissions(UserRole.USER, view.uuid());
+ userSession.addProjectPermission(UserRole.USER, view);
IssueQuery query = underTest.create(request);
ComponentDto project = ComponentTesting.newProjectDto(organization);
tester.get(ComponentDao.class).insert(session, project);
- userSessionRule.logIn().addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSessionRule.logIn().addProjectPermission(UserRole.USER, project);
session.commit();
// project can be seen by group "anyone"
@Test
public void set_severity() {
- DefaultIssue issue = newIssue().setSeverity(MAJOR).toDefaultIssue();
- setUserWithBrowseAndAdministerIssuePermission(issue.projectUuid());
+ IssueDto issueDto = newIssue().setSeverity(MAJOR);
+ DefaultIssue issue = issueDto.toDefaultIssue();
+ setUserWithBrowseAndAdministerIssuePermission(issueDto);
BulkChangeAction.ActionContext context = new BulkChangeAction.ActionContext(issue, IssueChangeContext.createUser(NOW, userSession.getLogin()), null);
action.execute(ImmutableMap.of("severity", MINOR), context);
@Test
public void support_only_unresolved_issues() {
- DefaultIssue issue = newIssue().setSeverity(MAJOR).toDefaultIssue();
- setUserWithBrowseAndAdministerIssuePermission(issue.projectUuid());
+ IssueDto issueDto = newIssue().setSeverity(MAJOR);
+ DefaultIssue issue = issueDto.toDefaultIssue();
+ setUserWithBrowseAndAdministerIssuePermission(issueDto);
assertThat(action.supports(issue.setResolution(null))).isTrue();
assertThat(action.supports(issue.setResolution(Issue.RESOLUTION_FIXED))).isFalse();
@Test
public void support_only_issues_with_issue_admin_permission() {
- DefaultIssue authorizedIssue = newIssue().setSeverity(MAJOR).toDefaultIssue();
- setUserWithBrowseAndAdministerIssuePermission(authorizedIssue.projectUuid());
- DefaultIssue unauthorizedIssue = newIssue().setSeverity(MAJOR).toDefaultIssue();
+ IssueDto authorizedIssue = newIssue().setSeverity(MAJOR);
+ setUserWithBrowseAndAdministerIssuePermission(authorizedIssue);
+ IssueDto unauthorizedIssue = newIssue().setSeverity(MAJOR);
- assertThat(action.supports(authorizedIssue.setResolution(null))).isTrue();
- assertThat(action.supports(unauthorizedIssue.setResolution(null))).isFalse();
+ assertThat(action.supports(authorizedIssue.toDefaultIssue().setResolution(null))).isTrue();
+ assertThat(action.supports(unauthorizedIssue.toDefaultIssue().setResolution(null))).isFalse();
}
- private void setUserWithBrowseAndAdministerIssuePermission(String projectUuid) {
+ private void setUserWithBrowseAndAdministerIssuePermission(IssueDto issue) {
+ ComponentDto project = db.getDbClient().componentDao().selectByUuid(db.getSession(), issue.getProjectUuid()).get();
+ ComponentDto component = db.getDbClient().componentDao().selectByUuid(db.getSession(), issue.getComponentUuid()).get();
userSession.logIn(USER_LOGIN)
- .addProjectUuidPermissions(ISSUE_ADMIN, projectUuid)
- .addProjectUuidPermissions(USER, projectUuid);
+ .addProjectPermission(ISSUE_ADMIN, project, component)
+ .addProjectPermission(USER, project, component);
}
private IssueDto newIssue() {
@Test
public void set_type() {
- DefaultIssue issue = newIssue().setType(BUG).toDefaultIssue();
- setUserWithBrowseAndAdministerIssuePermission(issue.projectUuid());
+ IssueDto issueDto = newIssue().setType(BUG);
+ DefaultIssue issue = issueDto.toDefaultIssue();
+ setUserWithBrowseAndAdministerIssuePermission(issueDto);
action.execute(ImmutableMap.of("type", VULNERABILITY.name()),
new BulkChangeAction.ActionContext(issue, IssueChangeContext.createUser(NOW, userSession.getLogin()), null));
@Test
public void support_only_unresolved_issues() {
- DefaultIssue issue = newIssue().setType(BUG).toDefaultIssue();
- setUserWithBrowseAndAdministerIssuePermission(issue.projectUuid());
+ IssueDto issueDto = newIssue().setType(BUG);
+ DefaultIssue issue = issueDto.toDefaultIssue();
+ setUserWithBrowseAndAdministerIssuePermission(issueDto);
assertThat(action.supports(issue.setResolution(null))).isTrue();
assertThat(action.supports(issue.setResolution(Issue.RESOLUTION_FIXED))).isFalse();
@Test
public void support_only_issues_with_issue_admin_permission() {
- DefaultIssue authorizedIssue = newIssue().setType(BUG).toDefaultIssue();
- setUserWithBrowseAndAdministerIssuePermission(authorizedIssue.projectUuid());
+ IssueDto authorizedIssueDto = newIssue().setType(BUG);
+ DefaultIssue authorizedIssue = authorizedIssueDto.toDefaultIssue();
+ setUserWithBrowseAndAdministerIssuePermission(authorizedIssueDto);
DefaultIssue unauthorizedIssue = newIssue().setType(BUG).toDefaultIssue();
assertThat(action.supports(authorizedIssue.setResolution(null))).isTrue();
assertThat(action.supports(unauthorizedIssue.setResolution(null))).isFalse();
}
- private void setUserWithBrowseAndAdministerIssuePermission(String projectUuid) {
+ private void setUserWithBrowseAndAdministerIssuePermission(IssueDto issueDto) {
+ ComponentDto project = db.getDbClient().componentDao().selectByUuid(db.getSession(), issueDto.getProjectUuid()).get();
+ ComponentDto component = db.getDbClient().componentDao().selectByUuid(db.getSession(), issueDto.getComponentUuid()).get();
userSession.logIn(USER_LOGIN)
- .addProjectUuidPermissions(ISSUE_ADMIN, projectUuid)
- .addProjectUuidPermissions(USER, projectUuid);
+ .addProjectPermission(ISSUE_ADMIN, project, component)
+ .addProjectPermission(USER, project, component);
}
private IssueDto newIssue() {
@Test
public void execute() {
- userSession.logIn("john").addProjectUuidPermissions(ISSUE_ADMIN, issue.projectUuid());
+ loginAndAddProjectPermission("john", ISSUE_ADMIN);
issue.setStatus(Issue.STATUS_RESOLVED);
issue.setResolution(Issue.RESOLUTION_FIXED);
@Test
public void does_not_execute_if_transition_is_not_available() {
- userSession.logIn("john").addProjectUuidPermissions(ISSUE_ADMIN, issue.projectUuid());
+ loginAndAddProjectPermission("john", ISSUE_ADMIN);
issue.setStatus(Issue.STATUS_CLOSED);
action.execute(ImmutableMap.of("transition", "reopen"), context);
return newDto(rule, file, project);
}
+ private void loginAndAddProjectPermission(String login, String permission) {
+ userSession.logIn(login).addProjectPermission(permission, ComponentTesting.newProjectDto(OrganizationTesting.newOrganizationDto(), issue.projectUuid()));
+ }
+
}
@Test
public void list_transitions() throws Exception {
IssueDto issue = newIssue().setStatus(STATUS_OPEN).setResolution(null);
- userSession.logIn("john").addProjectUuidPermissions(ISSUE_ADMIN, issue.getProjectUuid());
+ userSession.logIn("john").addProjectPermission(ISSUE_ADMIN, ComponentTesting.newProjectDto(OrganizationTesting.newOrganizationDto(), issue.getProjectUuid()));
List<Transition> result = underTest.listTransitions(issue.toDefaultIssue());
@Test
public void add_comment() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue();
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ loginWithBrowsePermission(issueDto, USER);
call(issueDto.getKey(), "please fix it");
@Test
public void fail_when_empty_comment_text() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue();
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ loginWithBrowsePermission(issueDto, USER);
expectedException.expect(IllegalArgumentException.class);
call(issueDto.getKey(), "");
@Test
public void fail_when_not_enough_permission() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue();
- userSession.logIn("john").addProjectUuidPermissions(CODEVIEWER, issueDto.getProjectUuid());
+ loginWithBrowsePermission(issueDto, CODEVIEWER);
expectedException.expect(ForbiddenException.class);
call(issueDto.getKey(), "please fix it");
return request.execute();
}
+ private void loginWithBrowsePermission(IssueDto issueDto, String permission) {
+ userSession.logIn("john").addProjectPermission(permission,
+ dbClient.componentDao().selectByUuid(dbTester.getSession(), issueDto.getProjectUuid()).get(),
+ dbClient.componentDao().selectByUuid(dbTester.getSession(), issueDto.getComponentUuid()).get());
+ }
+
}
@Rule
public ExpectedException expectedException = ExpectedException.none();
-
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
-
@Rule
public EsTester es = new EsTester(new IssueIndexDefinition(new MapSettings()));
-
@Rule
public DbTester db = DbTester.create(system2);
@Test
public void fail_when_missing_browse_permission() throws Exception {
IssueDto issue = newIssue();
- userSession.logIn(CURRENT_USER_LOGIN).addProjectUuidPermissions(CODEVIEWER, issue.getProjectUuid());
+ setUserWithPermission(issue, CODEVIEWER);
expectedException.expect(ForbiddenException.class);
}
private IssueDto newIssue() {
- return db.issues().insertIssue(
+ IssueDto issue = db.issues().insertIssue(
issueDto -> issueDto
.setAssignee(PREVIOUS_ASSIGNEE)
.setCreatedAt(PAST).setIssueCreationTime(PAST)
.setUpdatedAt(PAST).setIssueUpdateTime(PAST));
+ return issue;
}
private IssueDto newIssueWithBrowsePermission() {
}
private void setUserWithBrowsePermission(IssueDto issue) {
+ setUserWithPermission(issue, USER);
+ }
+
+ private void setUserWithPermission(IssueDto issue, String permission) {
insertUser(CURRENT_USER_LOGIN);
- userSession.logIn(CURRENT_USER_LOGIN).addProjectUuidPermissions(USER, issue.getProjectUuid());
+ userSession.logIn(CURRENT_USER_LOGIN)
+ .addProjectPermission(permission,
+ db.getDbClient().componentDao().selectByUuid(db.getSession(), issue.getProjectUuid()).get(),
+ db.getDbClient().componentDao().selectByUuid(db.getSession(), issue.getComponentUuid()).get());
}
private void checkIssueAssignee(String issueKey, @Nullable String expectedAssignee) {
private void addUserProjectPermissions(ComponentDto project, String... permissions) {
for (String permission : permissions) {
db.users().insertProjectPermissionOnUser(user, permission, project);
- userSession.addProjectUuidPermissions(permission, project.uuid());
+ userSession.addProjectPermission(permission, project);
}
}
package org.sonar.server.issue.ws;
import javax.annotation.Nullable;
+import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
+ private ComponentDto project;
+ private ComponentDto file;
private WsActionTester tester = new WsActionTester(new ChangelogAction(db.getDbClient(), new IssueFinder(db.getDbClient(), userSession), new AvatarResolverImpl()));
+ @Before
+ public void setUp() throws Exception {
+ project = db.components().insertProject();
+ file = db.components().insertComponent(newFileDto(project));
+ }
+
@Test
public void return_changelog() throws Exception {
UserDto user = insertUser();
IssueDto issueDto = db.issues().insertIssue(newIssue());
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ userSession.logIn("john").addProjectPermission(USER, project, file);
db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(user.getLogin()).setDiff("severity", "MAJOR", "BLOCKER"));
ChangelogWsResponse result = call(issueDto.getKey());
ComponentDto file1 = db.components().insertComponent(newFileDto(project));
ComponentDto file2 = db.components().insertComponent(newFileDto(project));
IssueDto issueDto = db.issues().insertIssue(newDto(rule, file2, project));
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ userSession.logIn("john").addProjectPermission(USER, project, file1, file2);
db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setDiff("file", file1.uuid(), file2.uuid()));
ChangelogWsResponse result = call(issueDto.getKey());
@Test
public void changelog_of_file_move_is_empty_when_files_does_not_exists() throws Exception {
IssueDto issueDto = db.issues().insertIssue(newIssue());
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ userSession.logIn("john").addProjectPermission(USER, project, file);
db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setDiff("file", "UNKNOWN_1", "UNKNOWN_2"));
ChangelogWsResponse result = call(issueDto.getKey());
public void return_changelog_on_user_without_email() throws Exception {
UserDto user = db.users().insertUser(UserTesting.newUserDto("john", "John", null));
IssueDto issueDto = db.issues().insertIssue(newIssue());
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ userSession.logIn("john").addProjectPermission(USER, project, file);
db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(user.getLogin()).setDiff("severity", "MAJOR", "BLOCKER"));
ChangelogWsResponse result = call(issueDto.getKey());
@Test
public void return_changelog_not_having_user() throws Exception {
IssueDto issueDto = db.issues().insertIssue(newIssue());
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ userSession.logIn("john").addProjectPermission(USER, project, file);
db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(null).setDiff("severity", "MAJOR", "BLOCKER"));
ChangelogWsResponse result = call(issueDto.getKey());
@Test
public void return_changelog_on_none_existing_user() throws Exception {
IssueDto issueDto = db.issues().insertIssue(newIssue());
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ userSession.logIn("john").addProjectPermission(USER, project, file);
db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin("UNKNOWN").setDiff("severity", "MAJOR", "BLOCKER"));
ChangelogWsResponse result = call(issueDto.getKey());
public void return_multiple_diffs() throws Exception {
UserDto user = insertUser();
IssueDto issueDto = db.issues().insertIssue(newIssue());
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ userSession.logIn("john").addProjectPermission(USER, project, file);
db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(user.getLogin()).setDiff("severity", "MAJOR", "BLOCKER").setDiff("status", "RESOLVED", "CLOSED"));
ChangelogWsResponse result = call(issueDto.getKey());
public void return_changelog_when_no_old_value() throws Exception {
UserDto user = insertUser();
IssueDto issueDto = db.issues().insertIssue(newIssue());
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ userSession.logIn("john").addProjectPermission(USER, project, file);
db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(user.getLogin()).setDiff("severity", null, "BLOCKER"));
ChangelogWsResponse result = call(issueDto.getKey());
public void return_changelog_when_no_new_value() throws Exception {
UserDto user = insertUser();
IssueDto issueDto = db.issues().insertIssue(newIssue());
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ userSession.logIn("john").addProjectPermission(USER, project, file);
db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(user.getLogin()).setDiff("severity", "MAJOR", null));
ChangelogWsResponse result = call(issueDto.getKey());
public void return_many_changelog() throws Exception {
UserDto user = insertUser();
IssueDto issueDto = db.issues().insertIssue(newIssue());
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ userSession.logIn("john").addProjectPermission(USER, project, file);
db.issues().insertFieldDiffs(issueDto,
new FieldDiffs().setUserLogin(user.getLogin()).setDiff("severity", "MAJOR", "BLOCKER"),
new FieldDiffs().setDiff("status", "RESOLVED", "CLOSED"));
public void replace_technical_debt_key_by_effort() throws Exception {
UserDto user = insertUser();
IssueDto issueDto = db.issues().insertIssue(newIssue());
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ userSession.logIn("john").addProjectPermission(USER, project, file);
db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserLogin(user.getLogin()).setDiff("technicalDebt", "10", "20"));
ChangelogWsResponse result = call(issueDto.getKey());
@Test
public void return_empty_changelog_when_no_changes_on_issue() {
IssueDto issueDto = db.issues().insertIssue(newIssue());
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ userSession.logIn("john").addProjectPermission(USER, project, file);
ChangelogWsResponse result = call(issueDto.getKey());
@Test
public void fail_when_not_enough_permission() {
IssueDto issueDto = db.issues().insertIssue(newIssue());
- userSession.logIn("john").addProjectUuidPermissions(CODEVIEWER, issueDto.getProjectUuid());
+ userSession.logIn("john").addProjectPermission(CODEVIEWER, project, file);
expectedException.expect(ForbiddenException.class);
call(issueDto.getKey());
public void test_example() throws Exception {
UserDto user = db.users().insertUser(newUserDto("john.smith", "John Smith", "john@smith.com"));
IssueDto issueDto = db.issues().insertIssue(newIssue());
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ userSession.logIn("john").addProjectPermission(USER, project, file);
db.issues().insertFieldDiffs(issueDto, new FieldDiffs()
.setUserLogin(user.getLogin())
.setDiff("severity", "MAJOR", "BLOCKER")
private IssueDto newIssue() {
RuleDto rule = db.rules().insertRule(newRuleDto());
- ComponentDto project = db.components().insertProject();
- ComponentDto file = db.components().insertComponent(newFileDto(project));
return newDto(rule, file, project);
}
public void delete_comment() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue();
IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, "john", "please fix it");
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ loginAndAddProjectPermission("john", issueDto, USER);
call(commentDto.getKey());
public void delete_comment_using_deprecated_key_parameter() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue();
IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, "john", "please fix it");
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ loginAndAddProjectPermission("john", issueDto, USER);
tester.newRequest().setParam("key", commentDto.getKey()).setParam("text", "please have a look").execute();
public void fail_when_comment_does_not_belong_to_current_user() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue();
IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, "john", "please fix it");
- userSession.logIn("another").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ loginAndAddProjectPermission("another", issueDto, USER);
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("You can only delete your own comments");
public void fail_when_comment_has_not_user() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue();
IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, null, "please fix it");
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ loginAndAddProjectPermission("john", issueDto, USER);
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("You can only delete your own comments");
public void fail_when_not_enough_permission() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue();
IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, "john", "please fix it");
- userSession.logIn("john").addProjectUuidPermissions(CODEVIEWER, issueDto.getProjectUuid());
+ loginAndAddProjectPermission("john", issueDto, CODEVIEWER);
expectedException.expect(ForbiddenException.class);
call(commentDto.getKey());
return request.execute();
}
+ private void loginAndAddProjectPermission(String login, IssueDto issueDto, String permission) {
+ userSession.logIn(login).addProjectPermission(permission, dbClient.componentDao().selectByUuid(dbTester.getSession(), issueDto.getProjectUuid()).get());
+ }
+
}
private IssueIndexer issueIndexer = new IssueIndexer(esTester.client(), new IssueIteratorFactory(dbClient));
private IssueUpdater issueUpdater = new IssueUpdater(dbClient,
new ServerIssueStorage(system2, new DefaultRuleFinder(dbClient, defaultOrganizationProvider), dbClient, issueIndexer), mock(NotificationManager.class));
+ private ComponentDto project;
+ private ComponentDto file;
private WsAction underTest = new DoTransitionAction(dbClient, userSession, new IssueFinder(dbClient, userSession), issueUpdater, transitionService, responseWriter);
private WsActionTester tester = new WsActionTester(underTest);
@Before
public void setUp() throws Exception {
+ project = componentDbTester.insertProject();
+ file = componentDbTester.insertComponent(newFileDto(project));
workflow.start();
}
@Test
public void do_transition() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue(newIssue().setStatus(STATUS_OPEN).setResolution(null));
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ userSession.logIn("john").addProjectPermission(USER, project, file);
call(issueDto.getKey(), "confirm");
@Test
public void fail_if_no_transition_param() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue(newIssue().setStatus(STATUS_OPEN).setResolution(null));
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ userSession.logIn("john").addProjectPermission(USER, project, file);
expectedException.expect(IllegalArgumentException.class);
call(issueDto.getKey(), null);
@Test
public void fail_if_not_enough_permission_to_access_issue() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue(newIssue().setStatus(STATUS_OPEN).setResolution(null));
- userSession.logIn("john").addProjectUuidPermissions(CODEVIEWER, issueDto.getProjectUuid());
+ userSession.logIn("john").addProjectPermission(CODEVIEWER, project, file);
expectedException.expect(ForbiddenException.class);
call(issueDto.getKey(), "confirm");
@Test
public void fail_if_not_enough_permission_to_apply_transition() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue(newIssue().setStatus(STATUS_OPEN).setResolution(null));
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ userSession.logIn("john").addProjectPermission(USER, project, file);
// False-positive transition is requiring issue admin permission
expectedException.expect(ForbiddenException.class);
private IssueDto newIssue() {
RuleDto rule = ruleDbTester.insertRule(newRuleDto());
- ComponentDto project = componentDbTester.insertProject();
- ComponentDto file = componentDbTester.insertComponent(newFileDto(project));
return newDto(rule, file, project);
}
public void edit_comment() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue();
IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, "john", "please fix it");
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ loginWithBrowsePermission("john", USER, issueDto);
call(commentDto.getKey(), "please have a look");
public void edit_comment_using_deprecated_key_parameter() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue();
IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, "john", "please fix it");
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ loginWithBrowsePermission("john", USER, issueDto);
tester.newRequest().setParam("key", commentDto.getKey()).setParam("text", "please have a look").execute();
public void fail_when_comment_does_not_belong_to_current_user() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue();
IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, "john", "please fix it");
- userSession.logIn("another").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ loginWithBrowsePermission("another", USER, issueDto);
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("You can only edit your own comments");
public void fail_when_comment_has_not_user() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue();
IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, null, "please fix it");
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ loginWithBrowsePermission("john", USER, issueDto);
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("You can only edit your own comments");
public void fail_when_empty_comment_text() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue();
IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, "john", "please fix it");
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ loginWithBrowsePermission("john", USER, issueDto);
expectedException.expect(IllegalArgumentException.class);
call(commentDto.getKey(), "");
public void fail_when_not_enough_permission() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue();
IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, "john", "please fix it");
- userSession.logIn("john").addProjectUuidPermissions(CODEVIEWER, issueDto.getProjectUuid());
+ loginWithBrowsePermission("john", CODEVIEWER, issueDto);
expectedException.expect(ForbiddenException.class);
call(commentDto.getKey(), "please have a look");
setNullable(commentText, comment -> request.setParam("text", comment));
return request.execute();
}
+
+ private void loginWithBrowsePermission(String login, String permission, IssueDto issueDto) {
+ userSession.logIn(login).addProjectPermission(permission,
+ dbClient.componentDao().selectByUuid(dbTester.getSession(), issueDto.getProjectUuid()).get(),
+ dbClient.componentDao().selectByUuid(dbTester.getSession(), issueDto.getComponentUuid()).get());
+ }
}
indexView(view.uuid(), newArrayList(project.uuid()));
setAnyoneProjectPermission(view, UserRole.USER);
- userSessionRule.logIn("john").addProjectUuidPermissions(UserRole.USER, view.uuid());
+ userSessionRule.logIn("john").addProjectPermission(UserRole.USER, view);
wsTester.newGetRequest(CONTROLLER_ISSUES, ACTION_SEARCH)
.setParam(IssuesWsParameters.PARAM_COMPONENT_UUIDS, view.uuid())
setAnyoneProjectPermission(view, UserRole.USER);
// User has wrong permission on the view, no issue will be returned
- userSessionRule.logIn("john").addProjectUuidPermissions(UserRole.CODEVIEWER, view.uuid());
+ userSessionRule.logIn("john").addProjectPermission(UserRole.CODEVIEWER, view);
wsTester.newGetRequest(CONTROLLER_ISSUES, ACTION_SEARCH)
.setParam(IssuesWsParameters.PARAM_COMPONENT_UUIDS, view.uuid())
indexView(subView.uuid(), newArrayList(project.uuid()));
setAnyoneProjectPermission(view, UserRole.USER);
- userSessionRule.logIn("john").addComponentUuidPermission(UserRole.USER, view.uuid(), subView.uuid());
+ userSessionRule.logIn("john").addProjectPermission(UserRole.USER, view, subView);
wsTester.newGetRequest(CONTROLLER_ISSUES, ACTION_SEARCH)
.setParam(IssuesWsParameters.PARAM_COMPONENT_UUIDS, subView.uuid())
setAnyoneProjectPermission(view, UserRole.USER);
// User has wrong permission on the view, no issue will be returned
- userSessionRule.logIn("john").addComponentUuidPermission(UserRole.CODEVIEWER, view.uuid(), subView.uuid());
+ userSessionRule.logIn("john").addProjectPermission(UserRole.CODEVIEWER, view, subView);
wsTester.newGetRequest(CONTROLLER_ISSUES, ACTION_SEARCH)
.setParam(IssuesWsParameters.PARAM_COMPONENT_UUIDS, subView.uuid())
private void setDefaultProjectPermission(ComponentDto project) {
// project can be seen by anyone and by code viewer
- userSessionRule.logIn().addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSessionRule.logIn().addProjectPermission(UserRole.USER, project);
// TODO correctly feed default organization. Not a problem as long as issues search does not support "anyone"
// for each organization
GroupPermissionChange permissionChange = new GroupPermissionChange(PermissionChange.Operation.ADD, UserRole.USER, new ProjectId(project), GroupIdOrAnyone.forAnyone(project.getOrganizationUuid()));
private void setProjectPermission(ComponentDto project, String... permissions) {
// project can be seen by anyone and by code viewer
userSessionRule.logIn("admin");
- Arrays.stream(permissions).forEach(permission -> userSessionRule.addProjectUuidPermissions(permission, project.uuid()));
+ Arrays.stream(permissions).forEach(permission -> userSessionRule.addProjectPermission(permission, project));
tester.get(PermissionUpdater.class).apply(session, Arrays.stream(permissions)
// TODO correctly feed default organization. Not a problem as long as issues search does not support "anyone" for each organization
.map(permission -> new GroupPermissionChange(PermissionChange.Operation.ADD, permission, new ProjectId(project), GroupIdOrAnyone.forAnyone(project.getOrganizationUuid())))
@Test
public void set_severity() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue(newIssue().setSeverity(MAJOR));
- setUserWithBrowseAndAdministerIssuePermission(issueDto.getProjectUuid());
+ setUserWithBrowseAndAdministerIssuePermission(issueDto);
call(issueDto.getKey(), MINOR);
@Test
public void insert_entry_in_changelog_when_setting_severity() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue(newIssue().setSeverity(MAJOR));
- setUserWithBrowseAndAdministerIssuePermission(issueDto.getProjectUuid());
+ setUserWithBrowseAndAdministerIssuePermission(issueDto);
call(issueDto.getKey(), MINOR);
@Test
public void fail_if_bad_severity() {
IssueDto issueDto = issueDbTester.insertIssue(newIssue().setSeverity("unknown"));
- setUserWithBrowseAndAdministerIssuePermission(issueDto.getProjectUuid());
+ setUserWithBrowseAndAdministerIssuePermission(issueDto);
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Value of parameter 'severity' (unknown) must be one of: [INFO, MINOR, MAJOR, CRITICAL, BLOCKER]");
@Test
public void fail_when_missing_browse_permission() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue();
- userSession.logIn("john").addProjectUuidPermissions(ISSUE_ADMIN, issueDto.getProjectUuid());
+ logInAndAddProjectPermission(issueDto, ISSUE_ADMIN);
expectedException.expect(ForbiddenException.class);
call(issueDto.getKey(), MAJOR);
@Test
public void fail_when_missing_administer_issue_permission() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue();
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ logInAndAddProjectPermission(issueDto, USER);
expectedException.expect(ForbiddenException.class);
call(issueDto.getKey(), MAJOR);
return newDto(rule, file, project);
}
- private void setUserWithBrowseAndAdministerIssuePermission(String projectUuid) {
+ private void logInAndAddProjectPermission(IssueDto issueDto, String permission) {
+ userSession.logIn("john").addProjectPermission(permission, dbClient.componentDao().selectByUuid(dbTester.getSession(), issueDto.getProjectUuid()).get());
+ }
+
+ private void setUserWithBrowseAndAdministerIssuePermission(IssueDto issueDto) {
+ ComponentDto project = dbClient.componentDao().selectByUuid(dbTester.getSession(), issueDto.getProjectUuid()).get();
userSession.logIn("john")
- .addProjectUuidPermissions(ISSUE_ADMIN, projectUuid)
- .addProjectUuidPermissions(USER, projectUuid);
+ .addProjectPermission(ISSUE_ADMIN, project)
+ .addProjectPermission(USER, project);
}
}
@Test
public void set_tags() {
IssueDto issueDto = db.issues().insertIssue(newIssue().setTags(singletonList("old-tag")));
- setUserWithBrowsePermission(issueDto.getProjectUuid());
+ setUserWithBrowsePermission(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.getProjectUuid());
+ setUserWithBrowsePermission(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.getProjectUuid());
+ setUserWithBrowsePermission(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.getProjectUuid());
+ setUserWithBrowsePermission(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.getProjectUuid());
+ setUserWithBrowsePermission(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.getProjectUuid());
+ setUserWithBrowsePermission(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.getProjectUuid());
+ setUserWithBrowsePermission(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.getProjectUuid());
+ setUserWithBrowsePermission(issueDto);
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Tag 'pol op' is invalid. Rule tags accept only the characters: a-z, 0-9, '+', '-', '#', '.'");
@Test
public void fail_when_missing_browse_permission() throws Exception {
IssueDto issueDto = db.issues().insertIssue();
- userSession.logIn("john").addProjectUuidPermissions(ISSUE_ADMIN, issueDto.getProjectUuid());
+ logInAndAddProjectPermission(issueDto, ISSUE_ADMIN);
expectedException.expect(ForbiddenException.class);
return IssueTesting.newIssue(rule, file, project);
}
- private void setUserWithBrowsePermission(String projectUuid) {
- userSession.logIn("john").addProjectUuidPermissions(USER, projectUuid);
+ private void setUserWithBrowsePermission(IssueDto issueDto) {
+ logInAndAddProjectPermission(issueDto, USER);
+ }
+
+ private void logInAndAddProjectPermission(IssueDto issueDto, String permission) {
+ userSession.logIn("john").addProjectPermission(permission, dbClient.componentDao().selectByUuid(db.getSession(), issueDto.getProjectUuid()).get());
}
}
@Test
public void set_type() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue(newIssue().setType(CODE_SMELL));
- setUserWithBrowseAndAdministerIssuePermission(issueDto.getProjectUuid());
+ setUserWithBrowseAndAdministerIssuePermission(issueDto);
call(issueDto.getKey(), BUG.name());
@Test
public void insert_entry_in_changelog_when_setting_type() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue(newIssue().setType(CODE_SMELL));
- setUserWithBrowseAndAdministerIssuePermission(issueDto.getProjectUuid());
+ setUserWithBrowseAndAdministerIssuePermission(issueDto);
call(issueDto.getKey(), BUG.name());
@Test
public void fail_if_bad_type_value() {
IssueDto issueDto = issueDbTester.insertIssue(newIssue().setType(CODE_SMELL));
- setUserWithBrowseAndAdministerIssuePermission(issueDto.getProjectUuid());
+ setUserWithBrowseAndAdministerIssuePermission(issueDto);
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Value of parameter 'type' (unknown) must be one of: [CODE_SMELL, BUG, VULNERABILITY]");
@Test
public void fail_when_missing_browse_permission() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue();
- userSession.logIn("john").addProjectUuidPermissions(ISSUE_ADMIN, issueDto.getProjectUuid());
+ String login = "john";
+ String permission = ISSUE_ADMIN;
+ logInAndAddProjectPermission(login, issueDto, permission);
expectedException.expect(ForbiddenException.class);
call(issueDto.getKey(), BUG.name());
@Test
public void fail_when_missing_administer_issue_permission() throws Exception {
IssueDto issueDto = issueDbTester.insertIssue();
- userSession.logIn("john").addProjectUuidPermissions(USER, issueDto.getProjectUuid());
+ logInAndAddProjectPermission("john", issueDto, USER);
expectedException.expect(ForbiddenException.class);
call(issueDto.getKey(), BUG.name());
return newDto(rule, file, project);
}
- private void setUserWithBrowseAndAdministerIssuePermission(String projectUuid) {
+ private void setUserWithBrowseAndAdministerIssuePermission(IssueDto issueDto) {
+ ComponentDto project = dbClient.componentDao().selectByUuid(dbTester.getSession(), issueDto.getProjectUuid()).get();
userSession.logIn("john")
- .addProjectUuidPermissions(ISSUE_ADMIN, projectUuid)
- .addProjectUuidPermissions(USER, projectUuid);
+ .addProjectPermission(ISSUE_ADMIN, project)
+ .addProjectPermission(USER, project);
+ }
+
+ private void logInAndAddProjectPermission(String login, IssueDto issueDto, String permission) {
+ userSession.logIn(login).addProjectPermission(permission, dbClient.componentDao().selectByUuid(dbTester.getSession(), issueDto.getProjectUuid()).get());
}
}
}
private void setUserWithBrowsePermission(IssueDto issue) {
- userSession.logIn("john").addProjectUuidPermissions(USER, issue.getProjectUuid());
+ userSession.logIn("john").addProjectPermission(USER, db.getDbClient().componentDao().selectByUuid(db.getSession(), issue.getProjectUuid()).get());
}
private IssueDto insertIssueWithBrowsePermission(RuleDefinitionDto rule, String... tags) {
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
-
@Rule
public ExpectedException expectedException = ExpectedException.none();
-
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
-
@Rule
public EsTester es = new EsTester(new UserIndexDefinition(new MapSettings()));
DbClient dbClient = db.getDbClient();
+ ComponentDto project;
final DbSession dbSession = db.getSession();
.setActive(true));
dbSession.commit();
- userSession.logIn("login").addProjectUuidPermissions(UserRole.ADMIN, DEFAULT_PROJECT_UUID);
+ OrganizationDto organizationDto = db.organizations().insert();
+ project = ComponentTesting.newProjectDto(organizationDto, DEFAULT_PROJECT_UUID).setKey(DEFAULT_PROJECT_KEY);
+ dbClient.componentDao().insert(dbSession, project);
+ dbSession.commit();
+ userSession.logIn("login").addProjectPermission(UserRole.ADMIN, project);
}
@Test
public void create_boolean_custom_measure_in_db() throws Exception {
- insertProject(DEFAULT_PROJECT_UUID);
MetricDto metric = insertMetric(BOOL);
newRequest()
@Test
public void create_int_custom_measure_in_db() throws Exception {
- insertProject(DEFAULT_PROJECT_UUID);
MetricDto metric = insertMetric(INT);
newRequest()
@Test
public void create_text_custom_measure_in_db() throws Exception {
- insertProject(DEFAULT_PROJECT_UUID);
MetricDto metric = insertMetric(STRING);
newRequest()
@Test
public void create_text_custom_measure_as_project_admin() throws Exception {
- insertProject(DEFAULT_PROJECT_UUID);
MetricDto metric = insertMetric(STRING);
- userSession.logIn("login").addProjectUuidPermissions(UserRole.ADMIN, DEFAULT_PROJECT_UUID);
+ userSession.logIn("login").addProjectPermission(UserRole.ADMIN, project);
newRequest()
.setParam(CreateAction.PARAM_PROJECT_ID, DEFAULT_PROJECT_UUID)
@Test
public void create_text_custom_measure_with_metric_key() throws Exception {
- insertProject(DEFAULT_PROJECT_UUID);
MetricDto metric = insertMetric(STRING);
newRequest()
@Test
public void create_text_custom_measure_with_project_key() throws Exception {
- insertProject(DEFAULT_PROJECT_UUID);
MetricDto metric = insertMetric(STRING);
newRequest()
@Test
public void create_float_custom_measure_in_db() throws Exception {
- insertProject(DEFAULT_PROJECT_UUID);
MetricDto metric = insertMetric(FLOAT);
newRequest()
@Test
public void create_work_duration_custom_measure_in_db() throws Exception {
- insertProject(DEFAULT_PROJECT_UUID);
MetricDto metric = insertMetric(WORK_DUR);
newRequest()
@Test
public void create_level_type_custom_measure_in_db() throws Exception {
- insertProject(DEFAULT_PROJECT_UUID);
MetricDto metric = insertMetric(LEVEL);
newRequest()
@Test
public void response_with_object_and_id() throws Exception {
- insertProject(DEFAULT_PROJECT_UUID);
MetricDto metric = insertMetric(STRING);
WsTester.Result response = newRequest()
@Test
public void create_custom_measure_on_a_view() throws Exception {
String viewUuid = "VIEW_UUID";
- dbClient.componentDao().insert(dbSession, ComponentTesting.newView(db.organizations().insert(), viewUuid));
+ ComponentDto view = ComponentTesting.newView(db.organizations().insert(), viewUuid);
+ dbClient.componentDao().insert(dbSession, view);
dbSession.commit();
MetricDto metric = insertMetric(BOOL);
- userSession.logIn("login").addProjectUuidPermissions(UserRole.ADMIN, viewUuid);
+ userSession.logIn("login").addProjectPermission(UserRole.ADMIN, view);
newRequest()
.setParam(CreateAction.PARAM_PROJECT_ID, viewUuid)
dbClient.componentDao().insert(dbSession, ComponentTesting.newSubView(view, subViewUuid, "SUB_VIEW_KEY"));
dbSession.commit();
MetricDto metric = insertMetric(BOOL);
- userSession.logIn("login").addProjectUuidPermissions(UserRole.ADMIN, view.uuid());
+ userSession.logIn("login").addProjectPermission(UserRole.ADMIN, view);
newRequest()
.setParam(CreateAction.PARAM_PROJECT_ID, subViewUuid)
public void fail_when_project_id_nor_project_key_provided() throws Exception {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Either 'projectId' or 'projectKey' must be provided, not both");
- insertProject(DEFAULT_PROJECT_UUID);
MetricDto metric = insertMetric(STRING);
newRequest()
public void fail_when_project_id_and_project_key_are_provided() throws Exception {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Either 'projectId' or 'projectKey' must be provided, not both");
- insertProject(DEFAULT_PROJECT_UUID);
MetricDto metric = insertMetric(STRING);
newRequest()
public void fail_when_project_key_does_not_exist_in_db() throws Exception {
expectedException.expect(NotFoundException.class);
expectedException.expectMessage("Component key 'another-project-key' not found");
- insertProject(DEFAULT_PROJECT_UUID);
- MetricDto metric = insertMetric(STRING);
+ insertMetric(STRING);
newRequest()
.setParam(CreateAction.PARAM_PROJECT_KEY, "another-project-key")
public void fail_when_project_id_does_not_exist_in_db() throws Exception {
expectedException.expect(NotFoundException.class);
expectedException.expectMessage("Component id 'another-project-uuid' not found");
- insertProject(DEFAULT_PROJECT_UUID);
MetricDto metric = insertMetric(STRING);
newRequest()
public void fail_when_metric_id_nor_metric_key_is_provided() throws Exception {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("The metric id or the metric key must be provided, not both.");
- insertProject(DEFAULT_PROJECT_UUID);
- MetricDto metric = insertMetric(STRING);
+ insertMetric(STRING);
newRequest()
.setParam(CreateAction.PARAM_PROJECT_ID, DEFAULT_PROJECT_UUID)
public void fail_when_metric_id_and_metric_key_are_provided() throws Exception {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("The metric id or the metric key must be provided, not both.");
- insertProject(DEFAULT_PROJECT_UUID);
MetricDto metric = insertMetric(STRING);
newRequest()
@Test
public void fail_when_metric_is_not_found_in_db() throws Exception {
- dbClient.componentDao().insert(dbSession, ComponentTesting.newProjectDto(db.organizations().insert(), DEFAULT_PROJECT_UUID));
- dbSession.commit();
-
expectedException.expect(RowNotFoundException.class);
expectedException.expectMessage("Metric id '42' not found");
@Test
public void fail_when_measure_already_exists_on_same_project_and_same_metric() throws Exception {
- insertProject(DEFAULT_PROJECT_UUID);
MetricDto metric = insertMetric(STRING);
expectedException.expect(ServerException.class);
@Test
public void fail_when_value_is_not_well_formatted() throws Exception {
- insertProject(DEFAULT_PROJECT_UUID);
MetricDto metric = insertMetric(BOOL);
expectedException.expect(BadRequestException.class);
@Test
public void fail_when_not_project_administrator() throws Exception {
userSession.logIn();
- insertProject(DEFAULT_PROJECT_UUID);
MetricDto metric = insertMetric(STRING);
expectedException.expect(ForbiddenException.class);
public void fail_when_not_a_project() throws Exception {
MetricDto metric = MetricTesting.newMetricDto().setEnabled(true).setValueType(STRING.name()).setKey("metric-key");
dbClient.metricDao().insert(dbSession, metric);
- ComponentDto project = ComponentTesting.newProjectDto(db.organizations().insert(), DEFAULT_PROJECT_UUID).setKey(DEFAULT_PROJECT_KEY);
- dbClient.componentDao().insert(dbSession, project);
dbClient.componentDao().insert(dbSession, ComponentTesting.newDirectory(project, "directory-uuid", "path/to/directory").setKey("directory-key"));
dbSession.commit();
return metric;
}
- private void insertProject(String projectUuid) {
- OrganizationDto organizationDto = db.organizations().insert();
- dbClient.componentDao().insert(dbSession, ComponentTesting.newProjectDto(organizationDto, projectUuid).setKey(DEFAULT_PROJECT_KEY));
- dbSession.commit();
- }
}
@Test
public void project_administrator_can_delete_custom_measures() throws Exception {
ComponentDto project = db.components().insertProject();
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
long id = insertCustomMeasure(project);
newRequest().setParam(PARAM_ID, valueOf(id)).execute();
.setActive(true));
ws = new WsTester(new CustomMeasuresWs(new MetricsAction(dbClient, userSession, new ComponentFinder(dbClient))));
defaultProject = insertDefaultProject();
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, defaultProject.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, defaultProject);
}
@Test
@Test
public void list_metrics_as_a_project_admin() throws Exception {
insertCustomMetric("metric-key-1");
- userSession.logIn("login").addProjectUuidPermissions(UserRole.ADMIN, defaultProject.uuid());
+ userSession.logIn("login").addProjectPermission(UserRole.ADMIN, defaultProject);
String response = newRequest().outputAsString();
CustomMeasureJsonWriter customMeasureJsonWriter = new CustomMeasureJsonWriter(new UserJsonWriter(userSessionRule));
ws = new WsTester(new CustomMeasuresWs(new SearchAction(dbClient, customMeasureJsonWriter, userSessionRule, new ComponentFinder(dbClient))));
defaultProject = insertDefaultProject();
- userSessionRule.logIn().addProjectUuidPermissions(UserRole.ADMIN, defaultProject.uuid());
+ userSessionRule.logIn().addProjectPermission(UserRole.ADMIN, defaultProject);
db.getDbClient().userDao().insert(dbSession, new UserDto()
.setLogin("login")
@Test
public void search_as_project_admin() throws Exception {
- userSessionRule.logIn("login").addProjectUuidPermissions(UserRole.ADMIN, DEFAULT_PROJECT_UUID);
+ userSessionRule.logIn("login").addProjectPermission(UserRole.ADMIN, defaultProject);
MetricDto metric1 = insertCustomMetric(1);
insertCustomMeasure(1, metric1);
}
private void logInAsProjectAdministrator(ComponentDto component) {
- userSessionRule.logIn("login").addProjectUuidPermissions(UserRole.ADMIN, component.uuid());
+ userSessionRule.logIn("login").addProjectPermission(UserRole.ADMIN, component);
}
}
@Test
public void provided_project() {
- componentDb.insertComponent(newProjectDto(db.getDefaultOrganization(), PROJECT_UUID));
- userSession.addProjectUuidPermissions(UserRole.USER, PROJECT_UUID);
+ ComponentDto project = componentDb.insertComponent(newProjectDto(db.getDefaultOrganization(), PROJECT_UUID));
+ userSession.addProjectPermission(UserRole.USER, project);
insertNclocMetric();
ComponentWsResponse response = newRequest(PROJECT_UUID, "ncloc");
.setPeriodDate(System.currentTimeMillis())
.setPeriodMode("last_version")
.setPeriodDate(System.currentTimeMillis()));
- userSession.anonymous().addProjectUuidPermissions(UserRole.USER, "project-uuid");
+ userSession.anonymous().addProjectPermission(UserRole.USER, projectDto);
ComponentDto directoryDto = newDirectory(projectDto, "directory-uuid", "path/to/directory").setName("directory-1");
componentDb.insertComponent(directoryDto);
ComponentDto file = newFileDto(directoryDto, null, "file-uuid").setName("file-1");
public void load_measures_with_best_value() {
ComponentDto projectDto = newProjectDto(db.getDefaultOrganization(), "project-uuid");
SnapshotDto projectSnapshot = componentDb.insertProjectAndSnapshot(projectDto);
- userSession.anonymous().addProjectUuidPermissions(UserRole.USER, "project-uuid");
+ userSession.anonymous().addProjectPermission(UserRole.USER, projectDto);
ComponentDto directoryDto = newDirectory(projectDto, "directory-uuid", "path/to/directory").setName("directory-1");
componentDb.insertComponent(directoryDto);
ComponentDto file = newFileDto(directoryDto, null, "file-uuid").setName("file-1");
@Test
public void use_best_value_for_rating() {
- userSession.anonymous().addProjectUuidPermissions(UserRole.USER, "project-uuid");
ComponentDto projectDto = newProjectDto(db.getDefaultOrganization(), "project-uuid");
+ userSession.anonymous().addProjectPermission(UserRole.USER, projectDto);
componentDb.insertComponent(projectDto);
SnapshotDto projectSnapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(projectDto)
.setPeriodDate(parseDateTime("2016-01-11T10:49:50+0100").getTime())
public void setUp() {
project = newProjectDto(db.getDefaultOrganization());
analysis = db.components().insertProjectAndSnapshot(project);
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
nclocMetric = insertNclocMetric();
complexityMetric = insertComplexityMetric();
newViolationMetric = insertNewViolationMetric();
@Test
public void empty_response() {
project = db.components().insertProject();
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
wsRequest
.setComponent(project.getKey())
.setMetrics(singletonList(complexityMetric.getKey()));
@Test
public void pagination_applies_to_analyses() {
project = db.components().insertProject();
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
List<String> analysisDates = LongStream.rangeClosed(1, 9)
.mapToObj(i -> dbClient.snapshotDao().insert(dbSession, newAnalysis(project).setCreatedAt(i * 1_000_000_000)))
.peek(a -> dbClient.measureDao().insert(dbSession, newMeasureDto(complexityMetric, project, a).setValue(101d)))
@Test
public void inclusive_from_and_to_dates() {
project = db.components().insertProject();
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
List<String> analysisDates = LongStream.rangeClosed(1, 9)
.mapToObj(i -> dbClient.snapshotDao().insert(dbSession, newAnalysis(project).setCreatedAt(System2.INSTANCE.now() + i * 1_000_000_000L)))
.peek(a -> dbClient.measureDao().insert(dbSession, newMeasureDto(complexityMetric, project, a).setValue(Double.valueOf(a.getCreatedAt()))))
@Test
public void fail_if_not_enough_permissions() {
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
expectedException.expect(ForbiddenException.class);
@Test
public void json_example() {
project = db.components().insertProject();
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
long now = parseDateTime("2017-01-23T17:00:53+0100").getTime();
LongStream.rangeClosed(0, 2)
.mapToObj(i -> dbClient.snapshotDao().insert(dbSession, newAnalysis(project).setCreatedAt(now + i * 24 * 1_000 * 60 * 60)))
public void adding_project_permission_is_allowed_to_project_administrators() throws Exception {
GroupDto group = db.users().insertGroup(db.getDefaultOrganization(), "sonar-administrators");
ComponentDto project = db.components().insertProject();
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
newRequest()
.setParam(PARAM_GROUP_NAME, group.getName())
public void adding_project_permission_is_allowed_to_project_administrators() throws Exception {
ComponentDto project = db.components().insertProject();
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
newRequest()
.setParam(PARAM_USER_LOGIN, user.getLogin())
GroupDto groupWithoutPermission = db.users().insertGroup(organizationDto, "group-without-permission");
- userSession.logIn().addProjectUuidPermissions(ADMIN, "project-uuid");
+ userSession.logIn().addProjectPermission(ADMIN, project);
String result = newRequest()
.setParam(PARAM_PERMISSION, ISSUE_ADMIN)
.setParam(PARAM_PROJECT_ID, "project-uuid")
db.users().insertProjectPermissionOnGroup(aGroup, CODEVIEWER, project);
db.users().insertProjectPermissionOnGroup(aGroup, ISSUE_ADMIN, project);
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
newRequest()
.setParam(PARAM_GROUP_NAME, aGroup.getName())
.setParam(PARAM_PROJECT_ID, project.uuid())
ComponentDto project = db.components().insertProject();
db.users().insertProjectPermissionOnUser(user, CODEVIEWER, project);
db.users().insertProjectPermissionOnUser(user, ISSUE_ADMIN, project);
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
newRequest()
.setParam(PARAM_USER_LOGIN, user.getLogin())
@Test
public void search_project_permissions_with_project_permission() throws Exception {
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, "project-uuid");
- db.components().insertComponent(newProjectDto(db.getDefaultOrganization(), "project-uuid"));
+ ComponentDto project = db.components().insertComponent(newProjectDto(db.getDefaultOrganization(), "project-uuid"));
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
String result = newRequest()
.setParam(PARAM_PROJECT_ID, "project-uuid")
UserDto withoutPermission = db.users().insertUser(newUserDto());
db.organizations().addMember(db.getDefaultOrganization(), withoutPermission);
- userSession.logIn().addProjectUuidPermissions(SYSTEM_ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(SYSTEM_ADMIN, project);
String result = newRequest()
.setParam(PARAM_PERMISSION, ISSUE_ADMIN)
.setParam(PARAM_PROJECT_ID, project.uuid())
@Before
public void setUp() {
ws = new WsTester(new ProjectsWs(
- new DeleteAction(
- componentCleanerService,
- new ComponentFinder(dbClient),
- dbClient,
- userSessionRule)));
+ new DeleteAction(
+ componentCleanerService,
+ new ComponentFinder(dbClient),
+ dbClient,
+ userSessionRule)));
}
@Test
@Test
public void project_administrator_deletes_the_project_by_uuid() throws Exception {
ComponentDto project = componentDbTester.insertProject();
- userSessionRule.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSessionRule.logIn().addProjectPermission(UserRole.ADMIN, project);
call(newRequest().setParam(PARAM_PROJECT_ID, project.uuid()));
@Test
public void project_administrator_deletes_the_project_by_key() throws Exception {
ComponentDto project = componentDbTester.insertProject();
- userSessionRule.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSessionRule.logIn().addProjectPermission(UserRole.ADMIN, project);
call(newRequest().setParam(PARAM_PROJECT, project.key()));
public void return_403_if_not_project_admin_nor_org_admin() throws Exception {
ComponentDto project = componentDbTester.insertProject();
- userSessionRule.logIn().addProjectUuidPermissions(project.uuid(), UserRole.CODEVIEWER, UserRole.ISSUE_ADMIN, UserRole.USER);
+ userSessionRule.logIn()
+ .addProjectPermission(UserRole.CODEVIEWER, project)
+ .addProjectPermission(UserRole.ISSUE_ADMIN, project)
+ .addProjectPermission(UserRole.USER, project);
expectedException.expect(ForbiddenException.class);
call(newRequest().setParam(PARAM_PROJECT_ID, project.uuid()));
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.WsActionTester;
-import org.sonarqube.ws.MediaTypes;
import org.sonarqube.ws.ProjectAnalyses;
import org.sonarqube.ws.ProjectAnalyses.CreateEventResponse;
import org.sonarqube.ws.client.projectanalysis.CreateEventRequest;
}
private void logInAsProjectAdministrator(ComponentDto project) {
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
}
private CreateEventResponse call(CreateEventRequest.Builder requestBuilder) {
}
private void logInAsProjectAdministrator(ComponentDto project) {
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
}
}
}
private void logInAsProjectAdministrator(ComponentDto project) {
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
}
}
public void json_example() {
OrganizationDto organizationDto = db.organizations().insert();
ComponentDto project = db.components().insertComponent(newProjectDto(organizationDto).setKey(KEY_PROJECT_EXAMPLE_001));
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(project).setUuid("A1").setCreatedAt(parseDateTime("2016-12-11T17:12:45+0100").getTime()));
SnapshotDto a2 = db.components().insertSnapshot(newAnalysis(project).setUuid("A2").setCreatedAt(parseDateTime("2016-12-12T17:12:45+0100").getTime()));
db.events().insertEvent(newEvent(a1).setUuid("E11")
@Test
public void return_analyses_ordered_by_analysis_date() {
ComponentDto project = db.components().insertComponent(newProjectDto(db.organizations().insert()).setKey("P1"));
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
db.components().insertSnapshot(newAnalysis(project).setUuid("A1").setCreatedAt(1_000_000L));
db.components().insertSnapshot(newAnalysis(project).setUuid("A2").setCreatedAt(2_000_000L));
db.components().insertSnapshot(newAnalysis(project).setUuid("A3").setCreatedAt(3_000_000L));
@Test
public void return_only_processed_analyses() {
ComponentDto project = db.components().insertComponent(newProjectDto(db.getDefaultOrganization()).setKey("P1"));
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
db.components().insertSnapshot(newAnalysis(project).setUuid("A1"));
db.components().insertSnapshot(newAnalysis(project).setUuid("A2").setStatus(SnapshotDto.STATUS_UNPROCESSED));
public void return_events() {
OrganizationDto organizationDto = db.organizations().insert();
ComponentDto project = db.components().insertComponent(newProjectDto(organizationDto).setKey("P1"));
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(project).setUuid("A1"));
SnapshotDto a42 = db.components().insertSnapshot(newAnalysis(newProjectDto(organizationDto)).setUuid("A42"));
EventDto e1 = db.events().insertEvent(newEvent(a1).setUuid("E1").setName("N1").setCategory(EventCategory.QUALITY_GATE.getLabel()).setDescription("D1"));
@Test
public void paginate_analyses() {
ComponentDto project = db.components().insertProject();
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
IntStream.rangeClosed(1, 9).forEach(i -> db.components().insertSnapshot(newAnalysis(project).setCreatedAt(1_000_000L * i).setUuid("A" + i)));
SearchResponse result = call(SearchRequest.builder()
@Test
public void filter_by_category() {
ComponentDto project = db.components().insertComponent(newProjectDto(db.organizations().insert()).setKey("P1"));
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(project).setUuid("A1"));
SnapshotDto a2 = db.components().insertSnapshot(newAnalysis(project).setUuid("A2"));
SnapshotDto a42 = db.components().insertSnapshot(newAnalysis(project).setUuid("A42"));
@Test
public void paginate_with_filter_on_category() {
ComponentDto project = db.components().insertComponent(newProjectDto(db.organizations().insert()).setKey("P1"));
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(project).setUuid("A1").setCreatedAt(1_000_000L));
SnapshotDto a2 = db.components().insertSnapshot(newAnalysis(project).setUuid("A2").setCreatedAt(2_000_000L));
SnapshotDto a3 = db.components().insertSnapshot(newAnalysis(project).setUuid("A3").setCreatedAt(3_000_000L));
assertJson(result).isSimilarTo(getClass().getResource("update_event-example.json"));
}
+
@Test
public void update_name_in_db() {
SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6");
ComponentDto project = newProjectDto(db.organizations().insert());
SnapshotDto analysis = db.components().insertProjectAndSnapshot(project);
db.events().insertEvent(newEvent(analysis).setUuid("E1"));
- userSession.logIn().addProjectUuidPermissions(project.uuid(), UserRole.USER);
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
expectedException.expect(ForbiddenException.class);
}
private void logInAsProjectAdministrator(ComponentDto project) {
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
}
private SnapshotDto createAnalysisAndLogInAsProjectAdministrator(String version) {
}
private void logInAsProjectAdministrator(ComponentDto project) {
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
}
}
ComponentLinkDto customLink2 = insertCustomLink(project2.uuid());
Long id1 = customLink1.getId();
Long id2 = customLink2.getId();
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project1.uuid(), project2.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project1, project2);
deleteLink(id1);
assertLinkIsDeleted(id1);
}
private void logInAsProjectAdministrator(ComponentDto project) {
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
}
}
@Test
public void project_administrator_can_search_for_links() throws IOException {
ComponentDto project = insertProject();
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
checkItWorks(project);
}
@Test
public void project_user_can_search_for_links() throws IOException {
ComponentDto project = insertProject();
- userSession.logIn().addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
checkItWorks(project);
}
}
private void logInAsProjectAdministrator(ComponentDto project) {
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
}
}
@Test
public void set_tags_as_project_admin() {
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
call(project.key(), "platform, lambda");
@Test
public void fail_if_not_project_admin() {
- userSession.logIn().addProjectUuidPermissions(UserRole.USER, project.key());
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
expectedException.expect(ForbiddenException.class);
@Test
public void does_not_fail_when_user_has_not_project_browse_permission() throws Exception {
- userSession.logIn("project-admin").addProjectUuidPermissions(CODEVIEWER, project.uuid());
+ userSession.logIn("project-admin").addProjectPermission(CODEVIEWER, project);
definitions.addComponent(PropertyDefinition.builder("foo").build());
propertyDb.insertProperties(newComponentPropertyDto(project).setKey("foo").setValue("one"));
}
private void logInAsProjectUser() {
- userSession.logIn().addProjectUuidPermissions(USER, project.uuid());
+ userSession.logIn().addProjectPermission(USER, project);
}
private void logInAsSystemAdministrator() {
private void logInAsProjectAdmin() {
userSession.logIn()
- .addProjectUuidPermissions(ADMIN, project.uuid())
- .addProjectUuidPermissions(USER, project.uuid());
+ .addProjectPermission(ADMIN, project)
+ .addProjectPermission(USER, project);
}
protected static URL resource(String s) {
String gateId = String.valueOf(gate.getId());
associateProjectToQualityGate(project.getId(), gateId);
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
callByKey(gateId, project.getKey());
public void fail_when_not_project_admin() throws Exception {
String gateId = String.valueOf(gate.getId());
- userSession.logIn().addProjectUuidPermissions(UserRole.ISSUE_ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ISSUE_ADMIN, project);
expectedException.expect(ForbiddenException.class);
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.WsActionTester;
-import org.sonarqube.ws.MediaTypes;
import org.sonarqube.ws.WsQualityGates;
import org.sonarqube.ws.WsQualityGates.GetByProjectWsResponse;
@Test
public void get_with_project_admin_permission() {
ComponentDto project = componentDb.insertProject();
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
QualityGateDto dbQualityGate = insertQualityGate("Sonar way");
setDefaultQualityGate(dbQualityGate.getId());
@Test
public void get_with_project_user_permission() {
ComponentDto project = componentDb.insertProject();
- userSession.logIn().addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
QualityGateDto dbQualityGate = insertQualityGate("Sonar way");
setDefaultQualityGate(dbQualityGate.getId());
}
private void logInAsProjectUser(ComponentDto project) {
- userSession.logIn().addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
}
}
@Test
public void json_example() throws IOException {
ComponentDto project = db.components().insertProject(db.organizations().insert());
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(project)
.setPeriodMode("last_version")
newMeasureDto(metric, project, snapshot)
.setData(IOUtils.toString(getClass().getResource("ProjectStatusActionTest/measure_data.json"))));
dbSession.commit();
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
String response = ws.newRequest()
.setParam(PARAM_PROJECT_ID, project.uuid())
newMeasureDto(metric, project, snapshot)
.setData(IOUtils.toString(getClass().getResource("ProjectStatusActionTest/measure_data.json"))));
dbSession.commit();
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
String response = ws.newRequest()
.setParam(PARAM_PROJECT_KEY, "project-key")
ComponentDto project = db.components().insertProject(db.organizations().insert());
SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(project));
dbSession.commit();
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
ProjectStatusWsResponse result = call(snapshot.getUuid());
@Test
public void return_undefined_status_if_snapshot_is_not_found() {
ComponentDto project = db.components().insertProject(db.organizations().insert());
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
ProjectStatusWsResponse result = callByProjectUuid(project.uuid());
ComponentDto project = db.components().insertProject(db.organizations().insert());
SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(project));
dbSession.commit();
- userSession.addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.addProjectPermission(UserRole.ADMIN, project);
call(snapshot.getUuid());
}
ComponentDto project = db.components().insertProject(db.organizations().insert());
SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(project));
dbSession.commit();
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
call(snapshot.getUuid());
}
@Test
public void project_admin() throws Exception {
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
String gateId = String.valueOf(gate.getId());
callByKey(gateId, project.getKey());
public void fail_when_not_project_admin() throws Exception {
String gateId = String.valueOf(gate.getId());
- userSession.logIn().addProjectUuidPermissions(UserRole.ISSUE_ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ISSUE_ADMIN, project);
expectedException.expect(ForbiddenException.class);
callByKey(gateId, project.getKey());
public void project_administrator_can_change_profile() throws Exception {
ComponentDto project = db.components().insertProject(db.getDefaultOrganization());
QualityProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization());
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
call(project, profile);
ComponentDto project = db.components().insertProject(db.getDefaultOrganization());
QualityProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization());
db.qualityProfiles().associateProjectWithQualityProfile(project, profile);
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
call(project, profile);
@Test
public void fail_when_user_has_not_project_browse_permission() throws Exception {
- userSession.logIn("project-admin").addProjectUuidPermissions(CODEVIEWER, project.uuid());
+ userSession.logIn("project-admin").addProjectPermission(CODEVIEWER, project);
propertyDefinitions.addComponent(PropertyDefinition.builder("foo").build());
expectedException.expect(ForbiddenException.class);
}
private void logInAsProjectUser() {
- userSession.logIn().addProjectUuidPermissions(USER, project.uuid());
+ userSession.logIn().addProjectPermission(USER, project);
}
private void logInAsAdmin(OrganizationDto org) {
private void logInAsProjectAdmin() {
userSession.logIn()
- .addProjectUuidPermissions(ADMIN, project.uuid())
- .addProjectUuidPermissions(USER, project.uuid());
+ .addProjectPermission(ADMIN, project)
+ .addProjectPermission(USER, project);
}
}
@Test
public void throw_ForbiddenException_if_project_setting_and_not_project_administrator() throws Exception {
- userSession.logIn().addProjectUuidPermissions(USER, project.uuid());
+ userSession.logIn().addProjectPermission(USER, project);
definitions.addComponent(PropertyDefinition.builder("foo").build());
expectedException.expect(ForbiddenException.class);
}
private void logInAsProjectAdmin() {
- userSession.logIn().addProjectUuidPermissions(ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(ADMIN, project);
}
private void assertGlobalPropertyDoesNotExist(String key) {
}
private void logInAsProjectAdministrator(ComponentDto project) {
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
}
}
@Test
public void return_component_secured_settings_when_not_authenticated_but_with_scan_permission() throws Exception {
userSession
- .addProjectUuidPermissions(USER, project.uuid())
- .addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
+ .addProjectPermission(USER, project)
+ .addProjectPermission(SCAN_EXECUTION, project);
definitions.addComponents(asList(
PropertyDefinition.builder("foo").onQualifiers(PROJECT).build(),
PropertyDefinition.builder("global.secret.secured").build(),
@Test
public void return_component_secured_settings_even_if_not_defined_when_not_authenticated_but_with_scan_permission() throws Exception {
userSession
- .addProjectUuidPermissions(USER, project.uuid())
- .addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
+ .addProjectPermission(USER, project)
+ .addProjectPermission(SCAN_EXECUTION, project);
propertyDb.insertProperties(newComponentPropertyDto(project).setKey("not-defined.secured").setValue("123"));
ValuesWsResponse result = executeRequestForProjectProperties("not-defined.secured");
@Test
public void fail_when_user_has_not_project_browse_permission() throws Exception {
- userSession.logIn("project-admin").addProjectUuidPermissions(CODEVIEWER, project.uuid());
+ userSession.logIn("project-admin").addProjectPermission(CODEVIEWER, project);
definitions.addComponent(PropertyDefinition.builder("foo").build());
expectedException.expect(ForbiddenException.class);
}
private void logInAsProjectUser() {
- userSession.logIn().addProjectUuidPermissions(USER, project.uuid());
+ userSession.logIn().addProjectPermission(USER, project);
}
private void logInAsAdmin() {
private void logInAsProjectAdmin() {
userSession.logIn()
- .addProjectUuidPermissions(ADMIN, project.uuid())
- .addProjectUuidPermissions(USER, project.uuid());
+ .addProjectPermission(ADMIN, project)
+ .addProjectPermission(USER, project);
}
private void assertSetting(Settings.Setting setting, String expectedKey, String expectedValue, boolean expectedInherited) {
@Test
public void show_hashes() throws Exception {
db.prepareDbUnit(getClass(), "shared.xml");
- userSessionRule.logIn("polop").addProjectUuidPermissions(UserRole.USER, PROJECT_UUID);
+ loginAndAddProjectPermission(PROJECT_UUID, UserRole.USER);
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");
- userSessionRule.logIn("polop").addProjectUuidPermissions(UserRole.USER, PROJECT_UUID);
+ loginAndAddProjectPermission(PROJECT_UUID, UserRole.USER);
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");
- userSessionRule.logIn("polop").addProjectUuidPermissions(UserRole.USER, PROJECT_UUID);
+ loginAndAddProjectPermission(PROJECT_UUID, UserRole.USER);
WsTester.TestRequest request = tester.newGetRequest("api/sources", "hash").setParam("key", COMPONENT_KEY);
request.execute().assertNoContent();
@Test
public void fail_to_show_hashes_if_file_does_not_exist() {
- userSessionRule.logIn("polop").addProjectUuidPermissions(UserRole.USER, PROJECT_UUID);
try {
WsTester.TestRequest request = tester.newGetRequest("api/sources", "hash").setParam("key", COMPONENT_KEY);
request.execute();
userSessionRule.logIn("polop");
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());
+ }
}
@Test
public void get_json() throws Exception {
ComponentDto project = db.components().insertProject();
- userSession.addProjectUuidPermissions(CODEVIEWER, project.uuid());
+ userSession.addProjectPermission(CODEVIEWER, project);
ComponentDto file = db.components().insertComponent(newFileDto(project));
insertFileWithData(file, newData("public class HelloWorld {", "}"));
@Test
public void limit_range() throws Exception {
ComponentDto project = db.components().insertProject();
- userSession.addProjectUuidPermissions(CODEVIEWER, project.uuid());
+ userSession.addProjectPermission(CODEVIEWER, project);
ComponentDto file = db.components().insertComponent(newFileDto(project));
insertFileWithData(file, newData("/**", " */", "public class HelloWorld {", "}", "", "foo"));
@Test
public void fail_when_missing_code_viewer_permission() throws Exception {
ComponentDto project = db.components().insertProject();
- userSession.addProjectUuidPermissions(USER, project.uuid());
+ userSession.addProjectPermission(USER, project);
ComponentDto file = db.components().insertComponent(newFileDto(project));
expectedException.expect(ForbiddenException.class);
public UserSessionRule userSessionRule = UserSessionRule.standalone();
SourceService sourceService;
-
HtmlSourceDecorator htmlSourceDecorator;
-
ComponentDao componentDao;
+ ComponentDto project;
+ ComponentDto file;
+
WsTester wsTester;
@Before
componentDao = new ComponentDao();
wsTester = new WsTester(new SourcesWs(
new LinesAction(new ComponentFinder(dbTester.getDbClient()), dbTester.getDbClient(), sourceService, htmlSourceDecorator, userSessionRule)));
+ project = ComponentTesting.newProjectDto(dbTester.organizations().insert(), PROJECT_UUID);
+ file = ComponentTesting.newFileDto(project, null, FILE_UUID).setKey(FILE_KEY);
}
@Test
public void show_source() throws Exception {
- setUserWithValidPermission();
insertFileWithData(FileSourceTesting.newFakeData(3).build());
+ setUserWithValidPermission();
WsTester.TestRequest request = wsTester.newGetRequest("api/sources", "lines").setParam("uuid", FILE_UUID);
request.execute().assertJson(getClass(), "show_source.json");
@Test
public void display_deprecated_fields() throws Exception {
- setUserWithValidPermission();
insertFileWithData(FileSourceTesting.newFakeData(1).build());
+ setUserWithValidPermission();
WsTester.TestRequest request = wsTester
.newGetRequest("api/sources", "lines")
@Test
public void use_deprecated_overall_coverage_fields_if_exists() throws Exception {
- setUserWithValidPermission();
DbFileSources.Data.Builder dataBuilder = DbFileSources.Data.newBuilder();
insertFileWithData(dataBuilder.addLines(newLineBuilder()
.setDeprecatedOverallLineHits(1)
.setDeprecatedItLineHits(1)
.setDeprecatedItConditions(2)
.setDeprecatedItCoveredConditions(3)).build());
+ setUserWithValidPermission();
WsTester.TestRequest request = wsTester
.newGetRequest("api/sources", "lines")
@Test
public void use_deprecated_ut_coverage_fields_if_exists() throws Exception {
- setUserWithValidPermission();
DbFileSources.Data.Builder dataBuilder = DbFileSources.Data.newBuilder();
insertFileWithData(dataBuilder.addLines(newLineBuilder()
.setDeprecatedUtLineHits(1)
.setDeprecatedItLineHits(1)
.setDeprecatedItConditions(2)
.setDeprecatedItCoveredConditions(3)).build());
+ setUserWithValidPermission();
WsTester.TestRequest request = wsTester
.newGetRequest("api/sources", "lines")
@Test
public void use_deprecated_it_coverage_fields_if_exists() throws Exception {
- setUserWithValidPermission();
DbFileSources.Data.Builder dataBuilder = DbFileSources.Data.newBuilder();
insertFileWithData(dataBuilder.addLines(newLineBuilder()
.setDeprecatedItLineHits(1)
.setDeprecatedItConditions(2)
.setDeprecatedItCoveredConditions(3)).build());
+ setUserWithValidPermission();
WsTester.TestRequest request = wsTester
.newGetRequest("api/sources", "lines")
}
private void setUserWithValidPermission() {
- userSessionRule.logIn("login").addProjectUuidPermissions(UserRole.CODEVIEWER, PROJECT_UUID);
+ userSessionRule.logIn("login").addProjectPermission(UserRole.CODEVIEWER, project, file);
}
private void insertFile() throws IOException {
- ComponentDto project = ComponentTesting.newProjectDto(dbTester.organizations().insert(), PROJECT_UUID);
- ComponentDto file = ComponentTesting.newFileDto(project, null, FILE_UUID).setKey(FILE_KEY);
componentDao.insert(dbTester.getSession(), project, file);
dbTester.getSession().commit();
}
@Test
public void get_txt() throws Exception {
String fileKey = "src/Foo.java";
- userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, project.uuid());
+ userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project);
when(componentDao.selectByKey(session, fileKey)).thenReturn(com.google.common.base.Optional.of(file));
Iterable<String> lines = newArrayList(
private WsTester tester;
private DbClient dbClient = dbTester.getDbClient();
private DbSession dbSession = dbTester.getSession();
+ private ComponentDto project;
+ private ComponentDto file;
@Before
public void setUp() {
tester = new WsTester(
new SourcesWs(new ScmAction(dbClient, new SourceService(dbTester.getDbClient(), new HtmlSourceDecorator()), userSessionRule, new ComponentFinder(dbClient))));
+
+ project = ComponentTesting.newProjectDto(dbTester.organizations().insert(), PROJECT_UUID);
+ file = ComponentTesting.newFileDto(project, null, FILE_UUID).setKey(FILE_KEY);
+ dbClient.componentDao().insert(dbTester.getSession(), project, file);
+ dbTester.getSession().commit();
}
@Test
public void show_scm() throws Exception {
- initFile();
- userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, PROJECT_UUID);
+ userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project, file);
dbTester.getDbClient().fileSourceDao().insert(dbSession, new FileSourceDto()
.setProjectUuid(PROJECT_UUID)
@Test
public void show_scm_from_given_range_lines() throws Exception {
- initFile();
- userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, PROJECT_UUID);
+ userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project, file);
dbTester.getDbClient().fileSourceDao().insert(dbSession, new FileSourceDto()
.setProjectUuid(PROJECT_UUID)
@Test
public void not_group_lines_by_commit() throws Exception {
- initFile();
- userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, PROJECT_UUID);
+ userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project, file);
// lines 1 and 2 are the same commit, but not 3 (different date)
dbTester.getDbClient().fileSourceDao().insert(dbSession, new FileSourceDto()
@Test
public void group_lines_by_commit() throws Exception {
- initFile();
- userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, PROJECT_UUID);
+ userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project, file);
// lines 1 and 2 are the same commit, but not 3 (different date)
dbTester.getDbClient().fileSourceDao().insert(dbSession, new FileSourceDto()
@Test
public void accept_negative_value_in_from_parameter() throws Exception {
- initFile();
- userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, PROJECT_UUID);
+ userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project, file);
dbTester.getDbClient().fileSourceDao().insert(dbSession, new FileSourceDto()
.setProjectUuid(PROJECT_UUID)
@Test
public void return_empty_value_when_no_scm() throws Exception {
- initFile();
- userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, PROJECT_UUID);
+ userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project, file);
dbTester.getDbClient().fileSourceDao().insert(dbSession, new FileSourceDto()
.setProjectUuid(PROJECT_UUID)
@Test(expected = ForbiddenException.class)
public void fail_without_code_viewer_permission() throws Exception {
- initFile();
- userSessionRule.addProjectUuidPermissions(UserRole.USER, PROJECT_UUID);
+ userSessionRule.addProjectPermission(UserRole.USER, project, file);
WsTester.TestRequest request = tester.newGetRequest("api/sources", "scm").setParam("key", FILE_KEY);
request.execute();
}
- private void initFile() {
- ComponentDto project = ComponentTesting.newProjectDto(dbTester.organizations().insert(), PROJECT_UUID);
- dbClient.componentDao().insert(dbTester.getSession(), project, ComponentTesting.newFileDto(project, null, FILE_UUID).setKey(FILE_KEY));
- dbTester.getSession().commit();
- }
-
private DbFileSources.Line newSourceLine(String author, String revision, Date date, int line) {
return DbFileSources.Line.newBuilder()
.setScmAuthor(author)
@Test
public void show_source() throws Exception {
String fileKey = "src/Foo.java";
- userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, project.uuid());
+ userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project);
when(componentDao.selectByKey(session, fileKey)).thenReturn(com.google.common.base.Optional.of(file));
when(sourceService.getLinesAsHtml(eq(session), eq(file.uuid()), anyInt(), anyInt())).thenReturn(Optional.of(newArrayList(
"/*",
@Test
public void show_source_with_from_and_to_params() throws Exception {
String fileKey = "src/Foo.java";
- userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, project.uuid());
+ userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project);
when(componentDao.selectByKey(session, fileKey)).thenReturn(com.google.common.base.Optional.of(file));
when(sourceService.getLinesAsHtml(session, file.uuid(), 3, 5)).thenReturn(Optional.of(newArrayList(
" */",
@Test
public void show_source_accept_from_less_than_one() throws Exception {
String fileKey = "src/Foo.java";
- userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, project.uuid());
+ userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project);
when(componentDao.selectByKey(session, fileKey)).thenReturn(com.google.common.base.Optional.of(file));
when(sourceService.getLinesAsHtml(session, file.uuid(), 1, 5)).thenReturn(Optional.of(newArrayList(
" */",
import org.sonar.api.web.UserRole;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ComponentTesting;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.organization.OrganizationTesting;
import org.sonar.server.exceptions.NotFoundException;
public class CoveredFilesActionTest {
- public static final String FILE_1_ID = "FILE1";
- public static final String FILE_2_ID = "FILE2";
+ private static final String FILE_1_ID = "FILE1";
+ private static final String FILE_2_ID = "FILE2";
@Rule
public UserSessionRule userSessionRule = UserSessionRule.standalone();
@Rule
public ExpectedException expectedException = ExpectedException.none();
- WsActionTester ws;
- DbClient dbClient;
- TestIndex testIndex;
+ private WsActionTester ws;
+ private DbClient dbClient;
+ private TestIndex testIndex;
@Before
public void setUp() {
@Test
public void covered_files() {
- userSessionRule.addComponentUuidPermission(UserRole.CODEVIEWER, "SonarQube", "test-file-uuid");
+ ComponentDto project = ComponentTesting.newProjectDto(OrganizationTesting.newOrganizationDto(), "SonarQube");
+ ComponentDto file = ComponentTesting.newFileDto(project, null, "test-file-uuid");
+ userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project, file);
when(testIndex.getNullableByTestUuid(anyString())).thenReturn(Optional.of(new TestDoc().setFileUuid("test-file-uuid")));
when(testIndex.coveredFiles("test-uuid")).thenReturn(Arrays.asList(
@Test
public void fail_when_test_uuid_is_unknown() {
- userSessionRule.addComponentUuidPermission(UserRole.CODEVIEWER, "SonarQube", "test-file-uuid");
+ ComponentDto project = ComponentTesting.newProjectDto(OrganizationTesting.newOrganizationDto(), "SonarQube");
+ ComponentDto file = ComponentTesting.newFileDto(project);
+ userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project, file);
when(testIndex.getNullableByTestUuid(anyString())).thenReturn(Optional.<TestDoc>absent());
when(testIndex.coveredFiles("test-uuid")).thenReturn(Arrays.asList(
@Test
public void list_tests() throws Exception {
- userSessionRule.addProjectUuidPermissions(CODEVIEWER, project.uuid());
+ userSessionRule.addProjectPermission(CODEVIEWER, project);
DbFileSources.Test test = newTest(mainFile, 10, 11, 12, 20, 21, 25).setStatus(OK).build();
insertTests(testFile, test);
@Test
public void list_tests_by_test_uuid() throws Exception {
- userSessionRule.addProjectUuidPermissions(CODEVIEWER, project.uuid());
+ userSessionRule.addProjectPermission(CODEVIEWER, project);
DbFileSources.Test test1 = newTest(mainFile, 10).build();
DbFileSources.Test test2 = newTest(mainFile, 11).build();
insertTests(testFile, test1, test2);
@Test
public void list_tests_by_test_file_uuid() throws Exception {
- userSessionRule.addProjectUuidPermissions(CODEVIEWER, project.uuid());
+ userSessionRule.addProjectPermission(CODEVIEWER, project);
ComponentDto anotherTestFile = db.components().insertComponent(newFileDto(project));
DbFileSources.Test test1 = newTest(mainFile, 10).build();
DbFileSources.Test test2 = newTest(mainFile, 11).build();
@Test
public void list_tests_by_test_file_key() throws Exception {
- userSessionRule.addProjectUuidPermissions(CODEVIEWER, project.uuid());
+ userSessionRule.addProjectPermission(CODEVIEWER, project);
ComponentDto anotherTestFile = db.components().insertComponent(newFileDto(project));
DbFileSources.Test test1 = newTest(mainFile, 10).build();
DbFileSources.Test test2 = newTest(mainFile, 11).build();
@Test
public void list_tests_by_source_file_uuid_and_line_number() throws Exception {
- userSessionRule.addProjectUuidPermissions(CODEVIEWER, project.uuid());
+ userSessionRule.addProjectPermission(CODEVIEWER, project);
ComponentDto anotherMainFile = db.components().insertComponent(newFileDto(project));
DbFileSources.Test test1 = newTest(mainFile, 10, 11, 12).build();
DbFileSources.Test test2 = newTest(mainFile, 9, 11).build();
@Test
public void list_tests_by_source_file_key_and_line_number() throws Exception {
- userSessionRule.addProjectUuidPermissions(CODEVIEWER, project.uuid());
+ userSessionRule.addProjectPermission(CODEVIEWER, project);
ComponentDto anotherMainFile = db.components().insertComponent(newFileDto(project));
DbFileSources.Test test1 = newTest(mainFile, 10, 11, 12).build();
DbFileSources.Test test2 = newTest(mainFile, 9, 11).build();
@Test
public void tests_are_paginated() throws Exception {
- userSessionRule.addProjectUuidPermissions(CODEVIEWER, project.uuid());
+ userSessionRule.addProjectPermission(CODEVIEWER, project);
insertTests(testFile, newTest(mainFile, 10).build(), newTest(mainFile, 11).build(), newTest(mainFile, 12).build());
ListResponse request = call(ws.newRequest().setParam(TEST_FILE_ID, testFile.uuid()));
@Test
public void fail_when_no_argument() throws Exception {
- userSessionRule.addProjectUuidPermissions(CODEVIEWER, project.uuid());
+ userSessionRule.addProjectPermission(CODEVIEWER, project);
expectedException.expect(IllegalArgumentException.class);
call(ws.newRequest());
@Test
public void fail_when_source_file_uuid_without_line_number() throws Exception {
- userSessionRule.addProjectUuidPermissions(CODEVIEWER, project.uuid());
+ userSessionRule.addProjectPermission(CODEVIEWER, project);
expectedException.expect(IllegalArgumentException.class);
call(ws.newRequest().setParam(SOURCE_FILE_ID, mainFile.uuid()));
@Test
public void fail_when_not_enough_privilege_on_test_uuid() throws Exception {
- userSessionRule.addProjectUuidPermissions(USER, project.uuid());
+ userSessionRule.addProjectPermission(USER, project);
DbFileSources.Test test = newTest(mainFile, 10).build();
insertTests(testFile, test);
@Test
public void fail_when_no_enough_privilege_on_test_file_id() throws Exception {
- userSessionRule.addProjectUuidPermissions(USER, project.uuid());
+ userSessionRule.addProjectPermission(USER, project);
insertTests(testFile, newTest(mainFile, 10).build());
expectedException.expect(ForbiddenException.class);
@Test
public void fail_when_not_enough_privilege_on_test_file_key() throws Exception {
- userSessionRule.addProjectUuidPermissions(USER, project.uuid());
+ userSessionRule.addProjectPermission(USER, project);
insertTests(testFile, newTest(mainFile, 10).build());
expectedException.expect(ForbiddenException.class);
@Test
public void fail_when_not_enough_privilege_on_main_file_uuid() throws Exception {
- userSessionRule.addProjectUuidPermissions(USER, project.uuid());
+ userSessionRule.addProjectPermission(USER, project);
insertTests(testFile, newTest(mainFile, 10).build());
expectedException.expect(ForbiddenException.class);
package org.sonar.server.tester;
import com.google.common.collect.HashMultimap;
-import java.util.List;
+import java.util.Arrays;
+import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
+import java.util.Set;
+import org.sonar.db.component.ComponentDto;
import org.sonar.db.permission.OrganizationPermission;
import org.sonar.server.user.AbstractUserSession;
-import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.newHashMap;
public abstract class AbstractMockUserSession<T extends AbstractMockUserSession> extends AbstractUserSession {
private HashMultimap<String, String> projectUuidByPermission = HashMultimap.create();
private final HashMultimap<String, OrganizationPermission> permissionsByOrganizationUuid = HashMultimap.create();
private Map<String, String> projectUuidByComponentUuid = newHashMap();
- private List<String> projectPermissionsCheckedByUuid = newArrayList();
+ private Set<String> projectPermissionsCheckedByUuid = new HashSet<>();
private boolean systemAdministrator = false;
protected AbstractMockUserSession(Class<T> clazz) {
return permissionsByOrganizationUuid.get(organizationUuid).contains(permission);
}
-
- public T addProjectUuidPermissions(String projectPermission, String... projectUuids) {
- this.projectPermissionsCheckedByUuid.add(projectPermission);
- this.projectUuidByPermission.putAll(projectPermission, newArrayList(projectUuids));
- for (String projectUuid : projectUuids) {
- this.projectUuidByComponentUuid.put(projectUuid, projectUuid);
- }
+ public T addProjectPermission(String permission, ComponentDto... components) {
+ this.projectPermissionsCheckedByUuid.add(permission);
+ Arrays.stream(components)
+ .forEach(component -> {
+ this.projectUuidByPermission.put(permission, component.projectUuid());
+ this.projectUuidByComponentUuid.put(component.uuid(), component.projectUuid());
+ });
return clazz.cast(this);
}
- public T addComponentUuidPermission(String projectPermission, String projectUuid, String componentUuid) {
- this.projectUuidByComponentUuid.put(componentUuid, projectUuid);
- addProjectUuidPermissions(projectPermission, projectUuid);
- return clazz.cast(this);
- }
-
@Override
protected Optional<String> componentUuidToProjectUuid(String componentUuid) {
return Optional.ofNullable(projectUuidByComponentUuid.get(componentUuid));
import org.junit.runners.model.Statement;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.organization.OrganizationDto;
+import org.sonar.db.permission.OrganizationPermission;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
-import org.sonar.db.permission.OrganizationPermission;
import org.sonar.server.user.ThreadLocalUserSession;
import org.sonar.server.user.UserSession;
setCurrentUserSession(userSession);
}
- public UserSessionRule addProjectUuidPermissions(String projectPermission, String... projectUuids) {
- ensureAbstractMockUserSession().addProjectUuidPermissions(projectPermission, projectUuids);
+ public UserSessionRule addProjectPermission(String projectPermission, ComponentDto... components) {
+ ensureAbstractMockUserSession().addProjectPermission(projectPermission, components);
return this;
}
return this;
}
- public UserSessionRule addComponentUuidPermission(String projectPermission, String projectUuid, String componentUuid) {
- ensureAbstractMockUserSession().addComponentUuidPermission(projectPermission, projectUuid, componentUuid);
- return this;
- }
-
public UserSessionRule setName(@Nullable String s) {
ensureMockUserSession().setName(s);
return this;
public void return_info_if_user_has_browse_permission_on_project() throws Exception {
init();
componentDbTester.insertComponent(project);
- userSession.logIn().addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
verifySuccess(project.key());
}
public void return_info_if_user_has_administration_permission_on_project() throws Exception {
init();
componentDbTester.insertComponent(project);
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
verifySuccess(project.key());
}
public void return_component_info_when_anonymous_no_snapshot() throws Exception {
init();
componentDbTester.insertComponent(project);
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
executeAndVerify(project.key(), "return_component_info_when_anonymous_no_snapshot.json");
}
UserDto user = dbTester.users().insertUser("obiwan");
componentDbTester.insertComponent(project);
propertyDbTester.insertProperty(new PropertyDto().setKey("favourite").setResourceId(project.getId()).setUserId(user.getId()));
- userSession.logIn(user).addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.logIn(user).addProjectPermission(UserRole.USER, project);
executeAndVerify(project.key(), "return_component_info_with_favourite.json");
}
.setCreatedAt(DateUtils.parseDateTime("2015-04-22T11:44:00+0200").getTime())
.setVersion("3.14")
.setLast(true));
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
executeAndVerify(project.key(), "return_component_info_when_snapshot.json");
}
addQualityProfiles(project, analysis,
createQProfile("qp1", "Sonar Way Java", "java"),
createQProfile("qp2", "Sonar Way Xoo", "xoo"));
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
executeAndVerify(project.key(), "return_quality_profiles.json");
}
public void return_empty_quality_profiles_when_no_measure() throws Exception {
init();
componentDbTester.insertComponent(project);
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
executeAndVerify(project.key(), "return_empty_quality_profiles_when_no_measure.json");
}
componentDbTester.insertComponent(project);
QualityGateDto qualityGateDto = dbTester.qualityGates().insertQualityGate("Sonar way");
dbTester.qualityGates().associateProjectToQualityGate(project, qualityGateDto);
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
executeAndVerify(project.key(), "return_quality_gate.json");
}
init();
componentDbTester.insertComponent(project);
dbTester.qualityGates().createDefaultQualityGate("Sonar way");
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
executeAndVerify(project.key(), "return_default_quality_gate.json");
}
public void return_no_quality_gate_when_not_defined_on_project_and_no_default_one() throws Exception {
init();
componentDbTester.insertComponent(project);
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
String json = execute(project.key());
assertThat(json).doesNotContain("qualityGate");
public void return_extensions() throws Exception {
init(createPages());
componentDbTester.insertComponent(project);
- userSession.anonymous().addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.anonymous().addProjectPermission(UserRole.USER, project);
executeAndVerify(project.key(), "return_extensions.json");
}
init(createPages());
componentDbTester.insertComponent(project);
userSession.anonymous()
- .addProjectUuidPermissions(UserRole.USER, project.uuid())
- .addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ .addProjectPermission(UserRole.USER, project)
+ .addProjectPermission(UserRole.ADMIN, project);
executeAndVerify(project.key(), "return_extensions_for_admin.json");
}
UserDto user = dbTester.users().insertUser();
componentDbTester.insertComponent(project);
userSession.logIn(user)
- .addProjectUuidPermissions(UserRole.USER, "abcd")
- .addProjectUuidPermissions(UserRole.ADMIN, "abcd");
+ .addProjectPermission(UserRole.USER, project)
+ .addProjectPermission(UserRole.ADMIN, project);
Page page1 = Page.builder("my_plugin/first_page")
.setName("First Page")
init();
componentDbTester.insertComponent(project);
userSession.anonymous()
- .addProjectUuidPermissions(UserRole.USER, "abcd")
- .addProjectUuidPermissions(UserRole.ADMIN, "abcd");
+ .addProjectPermission(UserRole.USER, project)
+ .addProjectPermission(UserRole.ADMIN, project);
ResourceType projectResourceType = ResourceType.builder(project.qualifier())
.setProperty("comparable", true)
ComponentDto project = componentDbTester.insertComponent(this.project);
ComponentDto module = componentDbTester.insertComponent(newModuleDto("bcde", project).setKey("palap").setName("Palap"));
userSession.anonymous()
- .addProjectUuidPermissions(UserRole.USER, "abcd")
- .addProjectUuidPermissions(UserRole.ADMIN, "abcd");
+ .addProjectPermission(UserRole.USER, project)
+ .addProjectPermission(UserRole.ADMIN, project);
executeAndVerify(module.key(), "return_breadcrumbs_on_module.json");
}
init();
componentDbTester.insertComponent(project);
userSession.logIn()
- .addProjectUuidPermissions(UserRole.USER, project.uuid())
+ .addProjectPermission(UserRole.USER, project)
.addPermission(ADMINISTER_QUALITY_PROFILES, project.getOrganizationUuid());
executeAndVerify(project.key(), "return_configuration_for_quality_profile_admin.json");
init();
componentDbTester.insertComponent(project);
userSession.logIn()
- .addProjectUuidPermissions(UserRole.USER, project.uuid())
+ .addProjectPermission(UserRole.USER, project)
.addPermission(ADMINISTER_QUALITY_GATES, project.getOrganizationUuid());
executeAndVerify(project.key(), "return_configuration_for_quality_gate_admin.json");
ComponentDto file = componentDbTester.insertComponent(newFileDto(directory, directory, "cdef").setName("Source.xoo")
.setKey("palap:src/main/xoo/Source.xoo")
.setPath(directory.path()));
- userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.addProjectPermission(UserRole.USER, project);
executeAndVerify(file.key(), "return_bread_crumbs_on_several_levels.json");
}
public void project_administrator_is_allowed_to_get_information() throws Exception {
init(createPages());
componentDbTester.insertProjectAndSnapshot(project);
- userSession.addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.addProjectPermission(UserRole.ADMIN, project);
execute(project.key());
}
QualityGateDto qualityGateDto = dbTester.qualityGates().insertQualityGate("Sonar way");
dbTester.qualityGates().associateProjectToQualityGate(project, qualityGateDto);
userSession.logIn(user)
- .addProjectUuidPermissions(UserRole.USER, project.uuid())
- .addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ .addProjectPermission(UserRole.USER, project)
+ .addProjectPermission(UserRole.ADMIN, project);
String result = execute(project.key());
assertJson(result).ignoreFields("snapshotDate", "key", "qualityGate.key").isSimilarTo(ws.getDef().responseExampleAsString());
ComponentDto project = dbTester.components().insertProject(org);
userSession.logIn()
- .addProjectUuidPermissions(UserRole.ADMIN, project.uuid())
+ .addProjectPermission(UserRole.ADMIN, project)
.addPermission(OrganizationPermission.ADMINISTER, org);
assertJson(execute(project.key())).isSimilarTo("{\"configuration\": {\"canApplyPermissionTemplate\": true}}");
userSession.logIn()
- .addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ .addProjectPermission(UserRole.ADMIN, project);
assertJson(execute(project.key())).isSimilarTo("{\"configuration\": {\"canApplyPermissionTemplate\": false}}");
}
@Test
public void search_by_component_and_return_no_records() throws Exception {
- userSession.logIn().addProjectUuidPermissions(project.uuid(), UserRole.ADMIN);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
Webhooks.DeliveriesWsResponse response = ws.newRequest()
.setParam("componentKey", project.getKey())
@Test
public void search_by_task_and_return_no_records() throws Exception {
- userSession.logIn().addProjectUuidPermissions(project.uuid(), UserRole.ADMIN);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
Webhooks.DeliveriesWsResponse response = ws.newRequest()
.setParam("ceTaskId", "t1")
.setHttpStatus(200);
dbClient.webhookDeliveryDao().insert(db.getSession(), dto);
db.commit();
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
String json = ws.newRequest()
.setParam("componentKey", project.getKey())
dbClient.webhookDeliveryDao().insert(db.getSession(), dto2);
dbClient.webhookDeliveryDao().insert(db.getSession(), dto3);
db.commit();
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
Webhooks.DeliveriesWsResponse response = ws.newRequest()
.setParam("ceTaskId", "t1")
.setComponentUuid(project.uuid());
dbClient.webhookDeliveryDao().insert(db.getSession(), dto);
db.commit();
- userSession.logIn().addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
expectedException.expect(ForbiddenException.class);
expectedException.expectMessage("Insufficient privileges");
.setComponentUuid(project.uuid());
dbClient.webhookDeliveryDao().insert(db.getSession(), dto);
db.commit();
- userSession.logIn().addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
expectedException.expect(ForbiddenException.class);
expectedException.expectMessage("Insufficient privileges");
.setPayload("{\"status\"=\"SUCCESS\"}");
dbClient.webhookDeliveryDao().insert(db.getSession(), dto);
db.commit();
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
String json = ws.newRequest()
.setParam("deliveryId", dto.getUuid())
.setErrorStacktrace("IOException -> can not connect");
dbClient.webhookDeliveryDao().insert(db.getSession(), dto);
db.commit();
- userSession.logIn().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
Webhooks.DeliveryWsResponse response = ws.newRequest()
.setParam("deliveryId", dto.getUuid())
.setComponentUuid(project.uuid());
dbClient.webhookDeliveryDao().insert(db.getSession(), dto);
db.commit();
- userSession.logIn().addProjectUuidPermissions(UserRole.USER, project.uuid());
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
expectedException.expect(ForbiddenException.class);
expectedException.expectMessage("Insufficient privileges");