use of com.thoughtworks.go.config.validation.GoConfigValidity in project gocd by gocd.
the class GoConfigServiceTest method shouldReturnInvalidWhenPipelineGroupPartialIsInvalid.
@Test
public void shouldReturnInvalidWhenPipelineGroupPartialIsInvalid() throws Exception {
String groupName = "group_name";
String md5 = "md5";
cruiseConfig = new BasicCruiseConfig();
expectLoad(cruiseConfig);
new GoConfigMother().addPipelineWithGroup(cruiseConfig, groupName, "pipeline_name", "stage_name", "job_name");
expectLoadForEditing(cruiseConfig);
when(goConfigDao.md5OfConfigFile()).thenReturn(md5);
String pipelineGroupContent = groupXmlWithInvalidElement(groupName);
GoConfigValidity validity = goConfigService.groupSaver(groupName).saveXml(pipelineGroupContent, "md5");
assertThat(validity.isValid(), Matchers.is(false));
assertThat(validity.errorMessage(), containsString("Invalid content was found starting with element 'unknown'"));
verify(goConfigDao, never()).updateConfig(any(UpdateConfigCommand.class));
}
use of com.thoughtworks.go.config.validation.GoConfigValidity in project gocd by gocd.
the class PipelineConfigsServiceTest method shouldUpdateXmlAndReturnPipelineConfigsIfUserHasEditPermissionsForTheGroupAndUpdateWasSuccessful.
@Test
public void shouldUpdateXmlAndReturnPipelineConfigsIfUserHasEditPermissionsForTheGroupAndUpdateWasSuccessful() throws Exception {
final String groupName = "group_name";
when(securityService.isUserAdminOfGroup(validUser.getUsername(), groupName)).thenReturn(true);
String md5 = "md5";
when(goConfigService.configFileMd5()).thenReturn(md5);
GoConfigService.XmlPartialSaver groupSaver = mock(GoConfigService.XmlPartialSaver.class);
when(goConfigService.groupSaver(groupName)).thenReturn(groupSaver);
String updatedPartial = groupXml();
when(groupSaver.saveXml(updatedPartial, md5)).thenReturn(GoConfigValidity.valid());
GoConfigOperationalResponse<PipelineConfigs> actual = service.updateXml(groupName, updatedPartial, md5, validUser, result);
PipelineConfigs configs = actual.getConfigElement();
GoConfigValidity validity = actual.getValidity();
assertThat(configs, is(not(nullValue())));
assertThat(configs.getGroup(), is("renamed_group_name"));
assertThat(result.httpCode(), is(200));
assertThat(result.isSuccessful(), is(true));
assertThat(validity.isValid(), is(true));
verify(groupSaver).saveXml(updatedPartial, md5);
}
use of com.thoughtworks.go.config.validation.GoConfigValidity in project gocd by gocd.
the class PipelineConfigsServiceTest method shouldReturnUnauthorizedResultWhenUserIsNotAuthorizedToViewGroup_onUpdateXml.
@Test
public void shouldReturnUnauthorizedResultWhenUserIsNotAuthorizedToViewGroup_onUpdateXml() throws Exception {
String groupName = "some-secret-group";
Localizer localizer = mock(Localizer.class);
when(localizer.localize("UNAUTHORIZED_TO_EDIT_GROUP", groupName)).thenReturn("Unauthorized!");
Username invalidUser = new Username(new CaseInsensitiveString("invalidUser"));
when(securityService.isUserAdminOfGroup(invalidUser.getUsername(), groupName)).thenReturn(false);
when(goConfigService.configFileMd5()).thenReturn("md5");
GoConfigOperationalResponse<PipelineConfigs> actual = service.updateXml(groupName, "", "md5", invalidUser, result);
PipelineConfigs configElement = actual.getConfigElement();
GoConfigValidity validity = actual.getValidity();
assertThat(configElement, is(nullValue()));
assertThat(result.httpCode(), is(401));
assertThat(result.isSuccessful(), is(false));
assertThat(result.message(localizer), is("Unauthorized!"));
assertThat(validity.isValid(), is(true));
verify(securityService, times(1)).isUserAdminOfGroup(invalidUser.getUsername(), groupName);
}
use of com.thoughtworks.go.config.validation.GoConfigValidity in project gocd by gocd.
the class PipelineConfigsServiceTest method shouldSetSuccessMessageOnSuccessfulUpdate.
@Test
public void shouldSetSuccessMessageOnSuccessfulUpdate() throws Exception {
String groupName = "renamed_group_name";
String md5 = "md5";
GoConfigService.XmlPartialSaver groupSaver = mock(GoConfigService.XmlPartialSaver.class);
when(securityService.isUserAdminOfGroup(validUser.getUsername(), groupName)).thenReturn(true);
when(goConfigService.configFileMd5()).thenReturn(md5);
when(goConfigService.groupSaver(groupName)).thenReturn(groupSaver);
when(groupSaver.saveXml(groupXml(), md5)).thenReturn(GoConfigValidity.valid(ConfigSaveState.UPDATED));
GoConfigOperationalResponse<PipelineConfigs> actual = service.updateXml(groupName, groupXml(), md5, validUser, result);
GoConfigValidity validity = actual.getValidity();
assertThat(result.localizable(), is(LocalizedMessage.string("SAVED_CONFIGURATION_SUCCESSFULLY")));
assertThat(validity.isValid(), is(true));
}
use of com.thoughtworks.go.config.validation.GoConfigValidity in project gocd by gocd.
the class GoConfigServiceIntegrationTest method shouldThrowUpOnConfigSaveValidationError_ViaNormalFlow.
@Test
public void shouldThrowUpOnConfigSaveValidationError_ViaNormalFlow() throws Exception {
// User 1 loads page
CruiseConfig user1SeeingConfig = goConfigDao.loadForEditing();
String user1SeeingMd5 = user1SeeingConfig.getMd5();
// User 1 edits old config
new GoConfigMother().addPipelineWithGroup(user1SeeingConfig, "defaultGroup", "user1_pipeline", "user1_stage", "user1_job");
ByteArrayOutputStream os = new ByteArrayOutputStream();
configHelper.getXml(user1SeeingConfig, os);
// Introduce validation error on xml
String xml = os.toString();
xml = xml.replace("user1_pipeline", "user1 pipeline");
// User 1 saves edited config
GoConfigService.XmlPartialSaver saver = goConfigService.fileSaver(false);
GoConfigValidity validity = saver.saveXml(xml, user1SeeingMd5);
assertThat(validity.isValid(), is(false));
assertThat(validity.isType(GoConfigValidity.VT_CONFLICT), is(true));
assertThat(validity.errorMessage(), containsString("Name is invalid. \"user1 pipeline\""));
}
Aggregations