private PermissionTemplateService permissionTemplateService = mock(PermissionTemplateService.class);
private ComponentUpdater underTest = new ComponentUpdater(db.getDbClient(), i18n, system2,
- permissionTemplateService,
- new FavoriteUpdater(db.getDbClient()),
- projectIndexer);
+ permissionTemplateService,
+ new FavoriteUpdater(db.getDbClient()),
+ projectIndexer);
@Test
public void should_persist_and_index_when_creating_project() throws Exception {
NewComponent project = NewComponent.newComponentBuilder()
- .setKey(DEFAULT_PROJECT_KEY)
- .setName(DEFAULT_PROJECT_NAME)
- .setOrganizationUuid(db.getDefaultOrganization().getUuid())
- .build();
+ .setKey(DEFAULT_PROJECT_KEY)
+ .setName(DEFAULT_PROJECT_NAME)
+ .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+ .build();
ComponentDto returned = underTest.create(db.getSession(), project, null);
ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid());
@Test
public void create_project_with_branch() throws Exception {
ComponentDto project = underTest.create(db.getSession(),
- NewComponent.newComponentBuilder()
- .setKey(DEFAULT_PROJECT_KEY)
- .setName(DEFAULT_PROJECT_NAME)
- .setBranch("origin/master")
- .setOrganizationUuid(db.getDefaultOrganization().getUuid())
- .build(),
- null);
+ NewComponent.newComponentBuilder()
+ .setKey(DEFAULT_PROJECT_KEY)
+ .setName(DEFAULT_PROJECT_NAME)
+ .setBranch("origin/master")
+ .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+ .build(),
+ null);
assertThat(project.getKey()).isEqualTo("project-key:origin/master");
}
public void should_apply_default_permission_template() throws Exception {
long userId = 42;
NewComponent project = NewComponent.newComponentBuilder()
- .setKey(DEFAULT_PROJECT_KEY)
- .setName(DEFAULT_PROJECT_NAME)
- .setOrganizationUuid(db.getDefaultOrganization().getUuid())
- .build();
+ .setKey(DEFAULT_PROJECT_KEY)
+ .setName(DEFAULT_PROJECT_NAME)
+ .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+ .build();
ComponentDto dto = underTest.create(db.getSession(), project, userId);
verify(permissionTemplateService).applyDefault(db.getSession(), dto.getOrganizationUuid(), dto, userId);
public void should_add_project_to_user_favorites_if_project_creator_is_defined_in_permission_template() throws Exception {
UserDto userDto = db.users().insertUser();
NewComponent project = NewComponent.newComponentBuilder()
- .setKey(DEFAULT_PROJECT_KEY)
- .setName(DEFAULT_PROJECT_NAME)
- .setOrganizationUuid(db.getDefaultOrganization().getUuid())
- .build();
+ .setKey(DEFAULT_PROJECT_KEY)
+ .setName(DEFAULT_PROJECT_NAME)
+ .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+ .build();
when(permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(eq(db.getSession()), eq(project.getOrganizationUuid()), any(ComponentDto.class)))
- .thenReturn(true);
+ .thenReturn(true);
ComponentDto dto = underTest.create(db.getSession(),
- project,
- userDto.getId());
+ project,
+ userDto.getId());
assertThat(db.favorites().hasFavorite(dto, userDto.getId())).isTrue();
}
@Test
public void does_not_add_project_to_favorite_when_anonymously_created() throws Exception {
ComponentDto project = underTest.create(db.getSession(),
- NewComponent.newComponentBuilder()
- .setKey(DEFAULT_PROJECT_KEY)
- .setName(DEFAULT_PROJECT_NAME)
- .setOrganizationUuid(db.getDefaultOrganization().getUuid())
- .build(),
- null);
+ NewComponent.newComponentBuilder()
+ .setKey(DEFAULT_PROJECT_KEY)
+ .setName(DEFAULT_PROJECT_NAME)
+ .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+ .build(),
+ null);
assertThat(db.favorites().hasNoFavorite(project)).isTrue();
}
@Test
public void does_not_add_project_to_favorite_when_project_has_no_permission_on_template() throws Exception {
ComponentDto project = underTest.create(db.getSession(),
- NewComponent.newComponentBuilder()
- .setKey(DEFAULT_PROJECT_KEY)
- .setName(DEFAULT_PROJECT_NAME)
- .setOrganizationUuid(db.getDefaultOrganization().getUuid())
- .build(),
- null);
+ NewComponent.newComponentBuilder()
+ .setKey(DEFAULT_PROJECT_KEY)
+ .setName(DEFAULT_PROJECT_NAME)
+ .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+ .build(),
+ null);
assertThat(db.favorites().hasNoFavorite(project)).isTrue();
}
expectedException.expectMessage("Could not create Project, key already exists: " + existing.key());
underTest.create(db.getSession(),
- NewComponent.newComponentBuilder()
- .setKey(existing.key())
- .setName(DEFAULT_PROJECT_NAME)
- .setOrganizationUuid(existing.getOrganizationUuid())
- .build(),
- null);
+ NewComponent.newComponentBuilder()
+ .setKey(existing.key())
+ .setName(DEFAULT_PROJECT_NAME)
+ .setOrganizationUuid(existing.getOrganizationUuid())
+ .build(),
+ null);
}
@Test
expectedException.expectMessage("Malformed key for Project: 1234");
underTest.create(db.getSession(),
- NewComponent.newComponentBuilder()
- .setKey("1234")
- .setName(DEFAULT_PROJECT_NAME)
- .setOrganizationUuid(db.getDefaultOrganization().getUuid())
- .build(),
- null);
+ NewComponent.newComponentBuilder()
+ .setKey("1234")
+ .setName(DEFAULT_PROJECT_NAME)
+ .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+ .build(),
+ null);
}
@Test
expectedException.expectMessage("Malformed branch for Project: origin?branch. Allowed characters are alphanumeric, '-', '_', '.' and '/', with at least one non-digit.");
underTest.create(db.getSession(),
- NewComponent.newComponentBuilder()
- .setKey(DEFAULT_PROJECT_KEY)
- .setName(DEFAULT_PROJECT_NAME)
- .setBranch("origin?branch")
- .setOrganizationUuid(db.getDefaultOrganization().getUuid())
- .build(),
- null);
+ NewComponent.newComponentBuilder()
+ .setKey(DEFAULT_PROJECT_KEY)
+ .setName(DEFAULT_PROJECT_NAME)
+ .setBranch("origin?branch")
+ .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+ .build(),
+ null);
}
@Test
public void persist_and_index_when_creating_view() {
NewComponent view = NewComponent.newComponentBuilder()
- .setKey("view-key")
- .setName("view-name")
- .setQualifier(VIEW)
- .setOrganizationUuid(db.getDefaultOrganization().getUuid())
- .build();
+ .setKey("view-key")
+ .setName("view-name")
+ .setQualifier(VIEW)
+ .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+ .build();
ComponentDto returned = underTest.create(db.getSession(), view, null);
GroupDto group = dbTester.users().insertGroup(organization);
dbTester.users().insertMember(group, user);
PermissionTemplateDto template = templateDb.insertTemplate(organization);
- dbTester.organizations().setDefaultTemplates(organization, template.getUuid(), null);
+ dbTester.organizations().setDefaultTemplates(template, null);
templateDb.addProjectCreatorToTemplate(template.getId(), SCAN_EXECUTION);
templateDb.addUserToTemplate(template.getId(), user.getId(), UserRole.USER);
templateDb.addGroupToTemplate(template.getId(), group.getId(), UserRole.CODEVIEWER);
@Test
public void would_user_have_permission_with_empty_template() {
PermissionTemplateDto template = templateDb.insertTemplate(dbTester.getDefaultOrganization());
- dbTester.organizations().setDefaultTemplates(dbTester.getDefaultOrganization(), template.getUuid(), null);
+ dbTester.organizations().setDefaultTemplates(template, null);
checkWouldUserHavePermission(dbTester.getDefaultOrganization(), null, UserRole.ADMIN, false);
}
runOnAllUnderTests((underTest) -> {
OrganizationDto organization = db.organizations().insert();
PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
- db.organizations().setDefaultTemplates(organization, "foo", "bar");
+ db.organizations().setDefaultTemplates(
+ db.permissionTemplates().insertTemplate(organization),
+ db.permissionTemplates().insertTemplate(organization));
loginAsAdmin(organization);
TestResponse result = newRequestByUuid(underTest, template.getUuid());
public void delete_template_by_name_case_insensitive() throws Exception {
runOnAllUnderTests((underTest) -> {
OrganizationDto organization = db.organizations().insert();
- db.organizations().setDefaultTemplates(organization, "project def template uuid", "view def template uuid");
+ db.organizations().setDefaultTemplates(
+ db.permissionTemplates().insertTemplate(organization),
+ db.permissionTemplates().insertTemplate(organization));
PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
loginAsAdmin(organization);
newRequestByName(underTest, organization, template);
@Test
public void delete_template_by_name_returns_empty_when_no_organization_is_provided_and_templates_does_not_belong_to_default_organization() throws Exception {
OrganizationDto organization = db.organizations().insert();
- db.organizations().setDefaultTemplates(organization, "project def template uuid", "view def template uuid");
+ db.organizations().setDefaultTemplates(
+ db.permissionTemplates().insertTemplate(organization),
+ db.permissionTemplates().insertTemplate(organization));
PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
loginAsAdmin(organization);
@Test
public void delete_template_by_name_returns_empty_when_wrong_organization_is_provided() throws Exception {
OrganizationDto organization = db.organizations().insert();
- db.organizations().setDefaultTemplates(organization, "project def template uuid", "view def template uuid");
+ db.organizations().setDefaultTemplates(
+ db.permissionTemplates().insertTemplate(organization),
+ db.permissionTemplates().insertTemplate(organization));
PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
OrganizationDto otherOrganization = db.organizations().insert();
loginAsAdmin(organization);
private void fail_to_delete_by_uuid_if_template_is_default_template_for_project(WsActionTester underTest) throws Exception {
OrganizationDto organization = db.organizations().insert();
- PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
- db.organizations().setDefaultTemplates(organization, template.getUuid(), "view def template uuid");
+ PermissionTemplateDto projectTemplate = insertTemplateAndAssociatedPermissions(organization);
+ db.organizations().setDefaultTemplates(projectTemplate,
+ db.permissionTemplates().insertTemplate(organization));
loginAsAdmin(organization);
expectedException.expect(BadRequestException.class);
expectedException.expectMessage("It is not possible to delete the default permission template for projects");
- newRequestByUuid(underTest, template.getUuid());
+ newRequestByUuid(underTest, projectTemplate.getUuid());
}
@Test
private void fail_to_delete_by_name_if_template_is_default_template_for_project(WsActionTester underTest) throws Exception {
OrganizationDto organization = db.organizations().insert();
- PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
- db.organizations().setDefaultTemplates(organization, template.getUuid(), "view def template uuid");
+ PermissionTemplateDto projectTemplate = insertTemplateAndAssociatedPermissions(organization);
+ db.organizations().setDefaultTemplates(projectTemplate, db.permissionTemplates().insertTemplate(organization));
loginAsAdmin(organization);
expectedException.expect(BadRequestException.class);
expectedException.expectMessage("It is not possible to delete the default permission template for projects");
- newRequestByName(underTest, organization.getKey(), template.getName());
+ newRequestByName(underTest, organization.getKey(), projectTemplate.getName());
}
@Test
public void fail_to_delete_by_uuid_if_template_is_default_template_for_view_with_views() throws Exception {
OrganizationDto organization = db.organizations().insert();
PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
- db.organizations().setDefaultTemplates(organization, "project def template uuid", template.getUuid());
+ db.organizations().setDefaultTemplates(db.permissionTemplates().insertTemplate(organization), template);
loginAsAdmin(organization);
expectedException.expect(BadRequestException.class);
@Test
public void default_template_for_views_can_be_deleted_by_uuid_if_views_is_not_installed_and_default_template_for_views_is_reset() throws Exception {
OrganizationDto organization = db.organizations().insert();
- PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
- db.organizations().setDefaultTemplates(organization, "project def template uuid", template.getUuid());
+ PermissionTemplateDto projectTemplate = db.permissionTemplates().insertTemplate(organization);
+ PermissionTemplateDto viewTemplate = insertTemplateAndAssociatedPermissions(organization);
+ db.organizations().setDefaultTemplates(projectTemplate, viewTemplate);
loginAsAdmin(organization);
- newRequestByUuid(this.underTestWithoutViews, template.getUuid());
+ newRequestByUuid(this.underTestWithoutViews, viewTemplate.getUuid());
- assertTemplateDoesNotExist(template);
+ assertTemplateDoesNotExist(viewTemplate);
assertThat(db.getDbClient().organizationDao().getDefaultTemplates(db.getSession(), organization.getUuid())
.get().getViewUuid())
addGroupToTemplate(viewsTemplate.getId(), group2.getId(), UserRole.ISSUE_ADMIN);
addGroupToTemplate(viewsTemplate.getId(), group3.getId(), UserRole.ISSUE_ADMIN);
- db.organizations().setDefaultTemplates(organization, projectTemplate.getUuid(), viewsTemplate.getUuid());
+ db.organizations().setDefaultTemplates(projectTemplate, viewsTemplate);
String result = newRequest().execute().getInput();
@Test
public void search_by_name_in_default_organization() {
- db.organizations().setDefaultTemplates(db.getDefaultOrganization(), "foo", null);
+ db.organizations().setDefaultTemplates(db.permissionTemplates().insertTemplate(db.getDefaultOrganization()), null);
insertProjectTemplate(db.getDefaultOrganization());
insertViewsTemplate(db.getDefaultOrganization());
@Test
public void search_in_organization() throws Exception {
OrganizationDto org = db.organizations().insert();
- db.organizations().setDefaultTemplates(org, "foo", null);
+ PermissionTemplateDto projectDefaultTemplate = db.permissionTemplates().insertTemplate(org);
+ db.organizations().setDefaultTemplates(projectDefaultTemplate, null);
PermissionTemplateDto templateInOrg = insertProjectTemplate(org);
insertProjectTemplate(db.getDefaultOrganization());
db.commit();
.execute()
.getInputStream());
- assertThat(result.getPermissionTemplatesCount()).isEqualTo(1);
- assertThat(result.getPermissionTemplates(0).getId()).isEqualTo(templateInOrg.getUuid());
+ assertThat(result.getPermissionTemplatesCount()).isEqualTo(2);
+ assertThat(result.getPermissionTemplatesList())
+ .extracting(WsPermissions.PermissionTemplate::getId)
+ .containsOnly(projectDefaultTemplate.getUuid(), templateInOrg.getUuid());
}
@Test
@Test
public void display_all_project_permissions() {
- db.organizations().setDefaultTemplates(db.getDefaultOrganization(), "foo", "bar");
+ db.organizations().setDefaultTemplates(db.permissionTemplates().insertTemplate(db.getDefaultOrganization()), null);
String result = newRequest().execute().getInput();
@Test
public void update_project_default_template() throws Exception {
- db.organizations().setDefaultTemplates(db.getDefaultOrganization(), "project-template-uuid", "view-template-uuid");
+ PermissionTemplateDto viewDefaultTemplate = db.permissionTemplates().insertTemplate(db.getDefaultOrganization());
+ db.organizations().setDefaultTemplates(db.permissionTemplates().insertTemplate(db.getDefaultOrganization()),
+ viewDefaultTemplate);
PermissionTemplateDto template = insertTemplate(db.getDefaultOrganization());
loginAsAdmin(db.getDefaultOrganization());
newRequest(template.getUuid(), Qualifiers.PROJECT);
- assertDefaultTemplates(db.getDefaultOrganization(), template.getUuid(), "view-template-uuid");
+ assertDefaultTemplates(db.getDefaultOrganization(), template.getUuid(), viewDefaultTemplate.getUuid());
}
@Test
@Test
public void update_project_default_template_by_template_name() throws Exception {
OrganizationDto organization = db.organizations().insert();
- db.organizations().setDefaultTemplates(organization, "bar", "roh");
+ PermissionTemplateDto viewDefaultTemplate = db.permissionTemplates().insertTemplate(organization);
+ db.organizations().setDefaultTemplates(db.permissionTemplates().insertTemplate(organization),
+ viewDefaultTemplate);
PermissionTemplateDto template = insertTemplate(organization);
loginAsAdmin(organization);
.execute();
db.getSession().commit();
- assertDefaultTemplates(organization, template.getUuid(), "roh");
+ assertDefaultTemplates(organization, template.getUuid(), viewDefaultTemplate.getUuid());
}
@Test
public void update_view_default_template() throws Exception {
OrganizationDto organization = db.organizations().insert();
- db.organizations().setDefaultTemplates(organization, "foo", null);
+ PermissionTemplateDto projectDefaultTemplate = db.permissionTemplates().insertTemplate(organization);
+ db.organizations().setDefaultTemplates(projectDefaultTemplate, null);
PermissionTemplateDto template = insertTemplate(organization);
loginAsAdmin(organization);
newRequest(template.getUuid(), VIEW);
- assertDefaultTemplates(organization, "foo", template.getUuid());
+ assertDefaultTemplates(organization, projectDefaultTemplate.getUuid(), template.getUuid());
}
@Test
@Test
public void do_not_fail_if_default_template_exists_and_is_registered() {
- db.permissionTemplates().insertTemplate(newPermissionTemplateDto()
- .setOrganizationUuid(db.getDefaultOrganization().getUuid())
- .setUuid(DEFAULT_TEMPLATE_UUID));
- db.organizations().setDefaultTemplates(db.getDefaultOrganization(), DEFAULT_TEMPLATE_UUID, null);
+ PermissionTemplateDto projectTemplate = db.permissionTemplates().insertTemplate(newPermissionTemplateDto()
+ .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+ .setUuid(DEFAULT_TEMPLATE_UUID));
+ db.organizations().setDefaultTemplates(projectTemplate, null);
underTest.start();
import javax.annotation.Nullable;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.permission.template.PermissionTemplateDto;
+
+import static com.google.common.base.Preconditions.checkArgument;
public class OrganizationDbTester {
private final DbTester dbTester;
return dto;
}
+ public void setDefaultTemplates(PermissionTemplateDto projectDefaultTemplate, @Nullable PermissionTemplateDto viewDefaultTemplate) {
+ checkArgument(viewDefaultTemplate == null
+ || viewDefaultTemplate.getOrganizationUuid().equals(projectDefaultTemplate.getOrganizationUuid()),
+ "default template for project and view must belong to the same organization");
+
+ DbSession dbSession = dbTester.getSession();
+ dbTester.getDbClient().organizationDao().setDefaultTemplates(dbSession, projectDefaultTemplate.getOrganizationUuid(),
+ new DefaultTemplates()
+ .setProjectUuid(projectDefaultTemplate.getUuid())
+ .setViewUuid(viewDefaultTemplate == null ? null : viewDefaultTemplate.getUuid()));
+ dbSession.commit();
+ }
+
public void setDefaultTemplates(OrganizationDto defaultOrganization,
String projectDefaultTemplateUuid, @Nullable String viewDefaultTemplateUuid) {
DbSession dbSession = dbTester.getSession();
- dbTester.getDbClient().organizationDao().setDefaultTemplates(dbSession, defaultOrganization.getUuid(), new DefaultTemplates().setProjectUuid(projectDefaultTemplateUuid).setViewUuid(viewDefaultTemplateUuid));
+ dbTester.getDbClient().organizationDao().setDefaultTemplates(dbSession, defaultOrganization.getUuid(),
+ new DefaultTemplates().setProjectUuid(projectDefaultTemplateUuid).setViewUuid(viewDefaultTemplateUuid));
dbSession.commit();
}