package org.sonar.server.batch;
import com.google.common.collect.ImmutableMap;
-import java.util.Date;
import java.util.Map;
-import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Scopes;
-import org.sonar.api.utils.System2;
import org.sonar.api.web.UserRole;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbClient;
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.db.property.PropertyDto;
-import org.sonar.db.qualityprofile.DefaultQProfileDto;
-import org.sonar.db.qualityprofile.QProfileDto;
import org.sonar.db.source.FileSourceDto;
-import org.sonar.db.source.FileSourceDto.Type;
import org.sonar.scanner.protocol.input.FileData;
import org.sonar.scanner.protocol.input.ProjectRepositories;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.qualityprofile.QProfileName;
import org.sonar.server.tester.UserSessionRule;
import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
-import static org.sonar.api.utils.DateUtils.formatDateTime;
import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
+import static org.sonar.db.component.ComponentTesting.newFileDto;
+import static org.sonar.db.component.ComponentTesting.newModuleDto;
import static org.sonar.db.permission.OrganizationPermission.SCAN;
-import static org.sonar.server.qualityprofile.QProfileTesting.newQProfileDto;
public class ProjectDataLoaderTest {
@Rule
- public DbTester dbTester = DbTester.create(System2.INSTANCE);
+ public DbTester db = DbTester.create();
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
- private DbClient dbClient = dbTester.getDbClient();
- private DbSession dbSession = dbTester.getSession();
-
- private ProjectDataLoader underTest = new ProjectDataLoader(dbClient, userSession);
-
- @After
- public void tearDown() throws Exception {
- dbSession.close();
- }
-
- @Test
- public void load_fails_with_NPE_if_query_is_null() {
- expectedException.expect(NullPointerException.class);
-
- underTest.load(null);
- }
-
- @Test
- public void load_fails_with_NFE_if_query_is_empty() {
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("Project or module with key 'null' is not found");
-
- underTest.load(ProjectDataQuery.create());
- }
-
- @Test
- public void load_throws_NotFoundException_if_component_does_not_exist() {
- String key = "theKey";
-
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("Project or module with key '" + key + "' is not found");
-
- underTest.load(ProjectDataQuery.create().setModuleKey(key));
- }
-
+ private DbClient dbClient = db.getDbClient();
+ private DbSession dbSession = db.getSession();
private int uuidCounter = 0;
- @Test
- public void load_fails_with_BRE_if_component_is_neither_a_project_or_a_module() {
- String[][] allScopesAndQualifierButProjectAndModule = {
- {Scopes.PROJECT, Qualifiers.VIEW},
- {Scopes.PROJECT, Qualifiers.SUBVIEW},
- {Scopes.PROJECT, Qualifiers.APP},
- {Scopes.FILE, Qualifiers.PROJECT},
- {Scopes.DIRECTORY, Qualifiers.DIRECTORY},
- {Scopes.FILE, Qualifiers.UNIT_TEST_FILE},
- {Scopes.PROJECT, "DEV"},
- {Scopes.PROJECT, "DEV_PRJ"}
- };
-
- OrganizationDto organizationDto = dbTester.organizations().insert();
- for (String[] scopeAndQualifier : allScopesAndQualifierButProjectAndModule) {
- String scope = scopeAndQualifier[0];
- String qualifier = scopeAndQualifier[1];
- String key = "theKey_" + scope + "_" + qualifier;
- String uuid = "uuid_" + uuidCounter++;
- dbClient.componentDao().insert(dbSession, new ComponentDto()
- .setOrganizationUuid(organizationDto.getUuid())
- .setUuid(uuid)
- .setUuidPath(uuid + ".")
- .setRootUuid(uuid)
- .setProjectUuid(uuid)
- .setScope(scope)
- .setQualifier(qualifier)
- .setDbKey(key));
- dbSession.commit();
-
- try {
- underTest.load(ProjectDataQuery.create().setModuleKey(key));
- fail(format("A NotFoundException should have been raised because scope (%s) or qualifier (%s) is not project", scope, qualifier));
- } catch (BadRequestException e) {
- assertThat(e).hasMessage("Key '" + key + "' belongs to a component which is not a Project");
- }
- }
- }
-
- @Test
- public void throw_ForbiddenException_if_no_browse_permission_nor_scan_permission() {
- ComponentDto project = dbTester.components().insertPrivateProject();
- userSession.logIn();
-
- expectedException.expect(ForbiddenException.class);
- expectedException.expectMessage("You're not authorized to execute any SonarQube analysis");
-
- underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()));
- }
-
- @Test
- public void throw_ForbiddenException_if_browse_permission_but_not_scan_permission() {
- ComponentDto project = dbTester.components().insertPrivateProject();
- 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");
-
- underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()));
- }
-
- @Test
- public void issues_mode_is_allowed_if_user_has_browse_permission() {
- ComponentDto project = dbTester.components().insertPrivateProject();
- userSession.logIn().addProjectPermission(UserRole.USER, project);
-
- ProjectRepositories repositories = underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()).setIssuesMode(true));
-
- assertThat(repositories).isNotNull();
- }
-
- @Test
- public void issues_mode_is_forbidden_if_user_doesnt_have_browse_permission() {
- ComponentDto project = dbTester.components().insertPrivateProject();
- userSession.logIn().addProjectPermission(GlobalPermissions.SCAN_EXECUTION, project);
-
- expectedException.expect(ForbiddenException.class);
- expectedException.expectMessage("You don't have the required permissions to access this project");
-
- underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()).setIssuesMode(true));
- }
-
- @Test
- public void scan_permission_on_organization_is_enough_even_without_scan_permission_on_project() {
- ComponentDto project = dbTester.components().insertPrivateProject();
- userSession.logIn().addPermission(SCAN, project.getOrganizationUuid());
- userSession.logIn().addProjectPermission(UserRole.USER, project);
-
- ProjectRepositories repositories = underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()).setIssuesMode(true));
-
- assertThat(repositories).isNotNull();
- }
-
+ private ProjectDataLoader underTest = new ProjectDataLoader(dbClient, userSession);
@Test
public void return_project_settings_with_global_scan_permission() {
- OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
- dbClient.organizationDao().insert(dbSession, organizationDto, false);
- ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
+ OrganizationDto organizationDto = db.organizations().insert();
+ ComponentDto project = db.components().insertPrivateProject(organizationDto);
userSession.logIn().addProjectPermission(SCAN_EXECUTION, project);
- dbClient.componentDao().insert(dbSession, project);
- addDefaultProfile();
-
// Project properties
dbClient.propertiesDao().saveProperty(
dbSession, new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR").setResourceId(project.getId()));
@Test
public void return_project_settings_with_project_scan_permission() {
- OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
- dbClient.organizationDao().insert(dbSession, organizationDto, false);
- ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
+ OrganizationDto organizationDto = db.organizations().insert();
+ ComponentDto project = db.components().insertPrivateProject(organizationDto);
userSession.logIn("john").addProjectPermission(SCAN_EXECUTION, project);
- dbClient.componentDao().insert(dbSession, project);
- addDefaultProfile();
// Project properties
dbClient.propertiesDao().saveProperty(
@Test
public void not_returned_secured_settings_when_lgged_but_no_scan_permission() {
- OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
- dbClient.organizationDao().insert(dbSession, organizationDto, false);
- ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
+ OrganizationDto organizationDto = db.organizations().insert();
+ ComponentDto project = db.components().insertPrivateProject(organizationDto);
userSession.logIn("john").addProjectPermission(UserRole.USER, project);
- dbClient.componentDao().insert(dbSession, project);
- addDefaultProfile();
// Project properties
dbClient.propertiesDao().saveProperty(
@Test
public void return_project_with_module_settings() {
- OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
- dbClient.organizationDao().insert(dbSession, organizationDto, false);
- ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
+ OrganizationDto organizationDto = db.organizations().insert();
+ ComponentDto project = db.components().insertPrivateProject(organizationDto);
userSession.logIn().addProjectPermission(SCAN_EXECUTION, project);
- dbClient.componentDao().insert(dbSession, project);
- addDefaultProfile();
// Project properties
dbClient.propertiesDao().saveProperty(
@Test
public void return_project_with_module_settings_inherited_from_project() {
- OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
- dbClient.organizationDao().insert(dbSession, organizationDto, false);
- ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
+ OrganizationDto organizationDto = db.organizations().insert();
+ ComponentDto project = db.components().insertPrivateProject(organizationDto);
userSession.logIn().addProjectPermission(SCAN_EXECUTION, project);
- dbClient.componentDao().insert(dbSession, project);
- addDefaultProfile();
// Project properties
dbClient.propertiesDao().saveProperty(
@Test
public void return_project_with_module_with_sub_module() {
- OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
- dbClient.organizationDao().insert(dbSession, organizationDto, false);
- ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
+ OrganizationDto organizationDto = db.organizations().insert();
+ ComponentDto project = db.components().insertPrivateProject(organizationDto);
userSession.logIn().addProjectPermission(SCAN_EXECUTION, project);
- dbClient.componentDao().insert(dbSession, project);
- addDefaultProfile();
// Project properties
dbClient.propertiesDao().saveProperty(
@Test
public void return_project_with_two_modules() {
- OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
- dbClient.organizationDao().insert(dbSession, organizationDto, false);
- ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
+ OrganizationDto organizationDto = db.organizations().insert();
+ ComponentDto project = db.components().insertPrivateProject(organizationDto);
userSession.logIn().addProjectPermission(SCAN_EXECUTION, project);
- dbClient.componentDao().insert(dbSession, project);
- addDefaultProfile();
// Project properties
dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR").setResourceId(project.getId()));
@Test
public void return_provisioned_project_settings() {
- OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
- dbClient.organizationDao().insert(dbSession, organizationDto, false);
- // No snapshot attached on the project -> provisioned project
- ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
+ OrganizationDto organizationDto = db.organizations().insert();
+ ComponentDto project = db.components().insertPrivateProject(organizationDto);
userSession.logIn().addProjectPermission(SCAN_EXECUTION, project);
- dbClient.componentDao().insert(dbSession, project);
- addDefaultProfile();
// Project properties
dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR").setResourceId(project.getId()));
@Test
public void return_sub_module_settings() {
- OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
- dbClient.organizationDao().insert(dbSession, organizationDto, false);
- ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
- dbClient.componentDao().insert(dbSession, project);
- addDefaultProfile();
+ OrganizationDto organizationDto = db.organizations().insert();
+ ComponentDto project = db.components().insertPrivateProject(organizationDto);
+ userSession.logIn().addProjectPermission(SCAN_EXECUTION, project);
// No project properties
-
- ComponentDto module = ComponentTesting.newModuleDto(project);
- dbClient.componentDao().insert(dbSession, module);
+ ComponentDto module = db.components().insertComponent(newModuleDto(project));
// No module properties
- ComponentDto subModule = ComponentTesting.newModuleDto(module);
- userSession.logIn().addProjectPermission(SCAN_EXECUTION, project);
- dbClient.componentDao().insert(dbSession, subModule);
-
// Sub module properties
+ ComponentDto subModule = db.components().insertComponent(newModuleDto(module));
dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR").setResourceId(subModule.getId()));
dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey("sonar.jira.login.secured").setValue("john").setResourceId(subModule.getId()));
dbClient.propertiesDao()
@Test
public void return_sub_module_settings_including_settings_from_parent_modules() {
- OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
- dbClient.organizationDao().insert(dbSession, organizationDto, false);
- ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
- dbClient.componentDao().insert(dbSession, project);
- addDefaultProfile();
+ OrganizationDto organizationDto = db.organizations().insert();
+ ComponentDto project = db.components().insertPrivateProject(organizationDto);
// Project property
dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR").setResourceId(project.getId()));
@Test
public void return_sub_module_settings_only_inherited_from_project() {
- OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
- dbClient.organizationDao().insert(dbSession, organizationDto, false);
- ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
- dbClient.componentDao().insert(dbSession, project);
- addDefaultProfile();
+ OrganizationDto organizationDto = db.organizations().insert();
+ ComponentDto project = db.components().insertPrivateProject(organizationDto);
// Project properties
dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR").setResourceId(project.getId()));
@Test
public void return_sub_module_settings_inherited_from_project_and_module() {
- OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
- dbClient.organizationDao().insert(dbSession, organizationDto, false);
- ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
- dbClient.componentDao().insert(dbSession, project);
- addDefaultProfile();
+ OrganizationDto organizationDto = db.organizations().insert();
+ ComponentDto project = db.components().insertPrivateProject(organizationDto);
// Project properties
dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey("sonar.jira.login.secured").setValue("john").setResourceId(project.getId()));
}
@Test
- public void fail_when_no_browse_permission_nor_scan_permission() {
- userSession.logIn();
+ public void return_file_data_from_single_project() {
+ OrganizationDto organizationDto = db.organizations().insert();
+ ComponentDto project = db.components().insertPrivateProject(organizationDto);
+ userSession.logIn().addProjectPermission(SCAN_EXECUTION, project);
+ ComponentDto file = db.components().insertComponent(newFileDto(project));
+ dbClient.fileSourceDao().insert(dbSession, newFileSourceDto(file).setSrcHash("123456"));
+ db.commit();
- OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
- dbClient.organizationDao().insert(dbSession, organizationDto, false);
- ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
- dbClient.componentDao().insert(dbSession, project);
- dbSession.commit();
+ ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()));
- try {
- underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()));
- fail();
- } catch (Exception e) {
- assertThat(e).isInstanceOf(ForbiddenException.class).hasMessage(Messages.NO_PERMISSION);
- }
+ assertThat(ref.fileDataByPath(project.getDbKey())).hasSize(1);
+ FileData fileData = ref.fileData(project.getDbKey(), file.path());
+ assertThat(fileData.hash()).isEqualTo("123456");
}
@Test
- public void fail_when_not_preview_and_only_browse_permission_without_scan_permission() {
- OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
- dbClient.organizationDao().insert(dbSession, organizationDto, false);
- ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
- dbClient.componentDao().insert(dbSession, project);
+ public void return_file_data_from_multi_modules() {
+ OrganizationDto organizationDto = db.organizations().insert();
+ ComponentDto project = db.components().insertPrivateProject(organizationDto);
+ userSession.logIn().addProjectPermission(SCAN_EXECUTION, project);
+ ComponentDto module = db.components().insertComponent(newModuleDto(project));
+ // File on project
+ ComponentDto projectFile = db.components().insertComponent(newFileDto(project));
+ dbClient.fileSourceDao().insert(dbSession, newFileSourceDto(projectFile).setSrcHash("123456"));
+ // File on module
+ ComponentDto moduleFile = db.components().insertComponent(newFileDto(module));
+ dbClient.fileSourceDao().insert(dbSession, newFileSourceDto(moduleFile).setSrcHash("789456"));
dbSession.commit();
- userSession.logIn("john").addProjectPermission(UserRole.USER, project);
+ ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()));
- 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. " +
- "Please contact your SonarQube administrator.");
- underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()).setIssuesMode(false));
+ assertThat(ref.fileData(project.getDbKey(), projectFile.path()).hash()).isEqualTo("123456");
+ assertThat(ref.fileData(module.getDbKey(), moduleFile.path()).hash()).isEqualTo("789456");
}
@Test
- public void fail_when_preview_and_only_scan_permission_without_browse_permission() {
- OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
- dbClient.organizationDao().insert(dbSession, organizationDto, false);
- ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
- dbClient.componentDao().insert(dbSession, project);
+ public void return_file_data_from_module() {
+ OrganizationDto organizationDto = db.organizations().insert();
+ ComponentDto project = db.components().insertPrivateProject(organizationDto);
+ userSession.logIn().addProjectPermission(SCAN_EXECUTION, project);
+ ComponentDto module = db.components().insertComponent(newModuleDto(project));
+ // File on project
+ ComponentDto projectFile = db.components().insertComponent(newFileDto(project));
+ dbClient.fileSourceDao().insert(dbSession, newFileSourceDto(projectFile).setSrcHash("123456"));
+ // File on module
+ ComponentDto moduleFile = db.components().insertComponent(newFileDto(module));
+ dbClient.fileSourceDao().insert(dbSession, newFileSourceDto(moduleFile).setSrcHash("789456"));
dbSession.commit();
- userSession.logIn("john").addProjectPermission(GlobalPermissions.SCAN_EXECUTION, project);
+ ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(module.getDbKey()));
- expectedException.expect(ForbiddenException.class);
- expectedException.expectMessage("You don't have the required permissions to access this project. Please contact your SonarQube administrator.");
- underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()).setIssuesMode(true));
+ assertThat(ref.fileData(module.getDbKey(), moduleFile.path()).hash()).isEqualTo("789456");
+ assertThat(ref.fileData(module.getDbKey(), moduleFile.path()).revision()).isEqualTo("123456789");
+ assertThat(ref.fileData(project.getDbKey(), projectFile.path())).isNull();
}
@Test
- public void return_file_data_from_single_project() {
- OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
- dbClient.organizationDao().insert(dbSession, organizationDto, false);
- ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
- userSession.logIn().addProjectPermission(SCAN_EXECUTION, project);
- dbClient.componentDao().insert(dbSession, project);
- addDefaultProfile();
+ public void fails_with_NPE_if_query_is_null() {
+ expectedException.expect(NullPointerException.class);
- ComponentDto file = ComponentTesting.newFileDto(project, null, "file");
- dbClient.componentDao().insert(dbSession, file);
- dbClient.fileSourceDao().insert(dbSession, newFileSourceDto(file).setSrcHash("123456"));
- dbSession.commit();
+ underTest.load(null);
+ }
- ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()));
- assertThat(ref.fileDataByPath(project.getDbKey())).hasSize(1);
- FileData fileData = ref.fileData(project.getDbKey(), file.path());
- assertThat(fileData.hash()).isEqualTo("123456");
+ @Test
+ public void fails_with_NFE_if_query_is_empty() {
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage("Component key 'null' not found");
+
+ underTest.load(ProjectDataQuery.create());
}
@Test
- public void return_file_data_from_multi_modules() {
- OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
- dbClient.organizationDao().insert(dbSession, organizationDto, false);
- ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
- userSession.logIn().addProjectPermission(SCAN_EXECUTION, project);
- dbClient.componentDao().insert(dbSession, project);
- addDefaultProfile();
+ public void throws_NotFoundException_if_component_does_not_exist() {
+ String key = "theKey";
- // File on project
- ComponentDto projectFile = ComponentTesting.newFileDto(project, null, "projectFile");
- dbClient.componentDao().insert(dbSession, projectFile);
- dbClient.fileSourceDao().insert(dbSession, newFileSourceDto(projectFile).setSrcHash("123456"));
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage("Component key 'theKey' not found");
- ComponentDto module = ComponentTesting.newModuleDto(project);
- dbClient.componentDao().insert(dbSession, module);
+ underTest.load(ProjectDataQuery.create().setModuleKey(key));
+ }
- // File on module
- ComponentDto moduleFile = ComponentTesting.newFileDto(module, null, "moduleFile");
- dbClient.componentDao().insert(dbSession, moduleFile);
- dbClient.fileSourceDao().insert(dbSession, newFileSourceDto(moduleFile).setSrcHash("789456"));
+ @Test
+ public void fails_with_BRE_if_component_is_neither_a_project_or_a_module() {
+ String[][] allScopesAndQualifierButProjectAndModule = {
+ {Scopes.PROJECT, Qualifiers.VIEW},
+ {Scopes.PROJECT, Qualifiers.SUBVIEW},
+ {Scopes.PROJECT, Qualifiers.APP},
+ {Scopes.FILE, Qualifiers.PROJECT},
+ {Scopes.DIRECTORY, Qualifiers.DIRECTORY},
+ {Scopes.FILE, Qualifiers.UNIT_TEST_FILE},
+ {Scopes.PROJECT, "DEV"},
+ {Scopes.PROJECT, "DEV_PRJ"}
+ };
- dbSession.commit();
+ OrganizationDto organizationDto = db.organizations().insert();
+ for (String[] scopeAndQualifier : allScopesAndQualifierButProjectAndModule) {
+ String scope = scopeAndQualifier[0];
+ String qualifier = scopeAndQualifier[1];
+ String key = "theKey_" + scope + "_" + qualifier;
+ String uuid = "uuid_" + uuidCounter++;
+ dbClient.componentDao().insert(dbSession, new ComponentDto()
+ .setOrganizationUuid(organizationDto.getUuid())
+ .setUuid(uuid)
+ .setUuidPath(uuid + ".")
+ .setRootUuid(uuid)
+ .setProjectUuid(uuid)
+ .setScope(scope)
+ .setQualifier(qualifier)
+ .setDbKey(key));
+ dbSession.commit();
- ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()));
- assertThat(ref.fileData(project.getDbKey(), projectFile.path()).hash()).isEqualTo("123456");
- assertThat(ref.fileData(module.getDbKey(), moduleFile.path()).hash()).isEqualTo("789456");
+ try {
+ underTest.load(ProjectDataQuery.create().setModuleKey(key));
+ fail(format("A NotFoundException should have been raised because scope (%s) or qualifier (%s) is not project", scope, qualifier));
+ } catch (BadRequestException e) {
+ assertThat(e).hasMessage("Key '" + key + "' belongs to a component which is not a Project");
+ }
+ }
}
@Test
- public void return_file_data_from_module() {
- OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
- dbClient.organizationDao().insert(dbSession, organizationDto, false);
- ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
- dbClient.componentDao().insert(dbSession, project);
- addDefaultProfile();
+ public void throw_ForbiddenException_if_no_browse_permission_nor_scan_permission() {
+ ComponentDto project = db.components().insertPrivateProject();
+ userSession.logIn();
- // File on project
- ComponentDto projectFile = ComponentTesting.newFileDto(project, null, "projectFile");
- dbClient.componentDao().insert(dbSession, projectFile);
- dbClient.fileSourceDao().insert(dbSession, newFileSourceDto(projectFile).setSrcHash("123456").setRevision("987654321"));
+ expectedException.expect(ForbiddenException.class);
+ expectedException.expectMessage("You're not authorized to execute any SonarQube analysis");
- ComponentDto module = ComponentTesting.newModuleDto(project);
- userSession.logIn().addProjectPermission(SCAN_EXECUTION, project);
- dbClient.componentDao().insert(dbSession, module);
+ underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()));
+ }
- // File on module
- ComponentDto moduleFile = ComponentTesting.newFileDto(module, null, "moduleFile");
- dbClient.componentDao().insert(dbSession, moduleFile);
- dbClient.fileSourceDao().insert(dbSession, newFileSourceDto(moduleFile).setSrcHash("789456").setRevision("123456789"));
+ @Test
+ public void throw_ForbiddenException_if_browse_permission_but_not_scan_permission() {
+ ComponentDto project = db.components().insertPrivateProject();
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
- dbSession.commit();
+ 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");
- ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(module.getDbKey()));
- assertThat(ref.fileData(module.getDbKey(), moduleFile.path()).hash()).isEqualTo("789456");
- assertThat(ref.fileData(module.getDbKey(), moduleFile.path()).revision()).isEqualTo("123456789");
- assertThat(ref.fileData(project.getDbKey(), projectFile.path())).isNull();
+ underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()));
}
- private void addDefaultProfile() {
- OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
- dbClient.organizationDao().insert(dbSession, organizationDto, false);
- QProfileDto profileDto = newQProfileDto(organizationDto, QProfileName.createFor("xoo", "SonarQube way"), "abcd").setRulesUpdatedAt(
- formatDateTime(new Date()));
- dbClient.qualityProfileDao().insert(dbSession, profileDto);
- dbClient.defaultQProfileDao().insertOrUpdate(dbSession, DefaultQProfileDto.from(profileDto));
+ @Test
+ public void issues_mode_is_allowed_if_user_has_browse_permission() {
+ ComponentDto project = db.components().insertPrivateProject();
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+
+ ProjectRepositories repositories = underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()).setIssuesMode(true));
+
+ assertThat(repositories).isNotNull();
+ }
+
+ @Test
+ public void issues_mode_is_forbidden_if_user_doesnt_have_browse_permission() {
+ ComponentDto project = db.components().insertPrivateProject();
+ userSession.logIn().addProjectPermission(GlobalPermissions.SCAN_EXECUTION, project);
+
+ expectedException.expect(ForbiddenException.class);
+ expectedException.expectMessage("You don't have the required permissions to access this project");
+
+ underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()).setIssuesMode(true));
+ }
+
+ @Test
+ public void scan_permission_on_organization_is_enough_even_without_scan_permission_on_project() {
+ ComponentDto project = db.components().insertPrivateProject();
+ userSession.logIn().addPermission(SCAN, project.getOrganizationUuid());
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+
+ ProjectRepositories repositories = underTest.load(ProjectDataQuery.create().setModuleKey(project.getDbKey()).setIssuesMode(true));
+
+ assertThat(repositories).isNotNull();
}
private static FileSourceDto newFileSourceDto(ComponentDto file) {
.setLineHashes("8d7b3d6b83c0a517eac07e1aac94b773")
.setCreatedAt(System.currentTimeMillis())
.setUpdatedAt(System.currentTimeMillis())
- .setDataType(Type.SOURCE)
+ .setDataType(FileSourceDto.Type.SOURCE)
.setRevision("123456789")
.setSrcHash("123456");
}