use of com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult in project gocd by gocd.
the class StageServiceTest method findStageSummaryByIdentifierShouldRespondWith401WhenUserDoesNotHavePermissionToViewThePipeline.
@Test
public void findStageSummaryByIdentifierShouldRespondWith401WhenUserDoesNotHavePermissionToViewThePipeline() throws Exception {
SecurityService securityService = mock(SecurityService.class);
when(securityService.hasViewPermissionForPipeline(ALWAYS_ALLOW_USER, "pipeline_name")).thenReturn(false);
TransactionSynchronizationManager transactionSynchronizationManager = mock(TransactionSynchronizationManager.class);
StageService service = new StageService(stageDao, null, null, null, securityService, null, changesetService, goConfigService, transactionTemplate, transactionSynchronizationManager, goCache);
HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
StageSummaryModel model = service.findStageSummaryByIdentifier(new StageIdentifier("pipeline_name/10/stage_name/1"), ALWAYS_ALLOW_USER, result);
assertThat(result.httpCode(), is(401));
assertThat(model, is(nullValue()));
}
use of com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult in project gocd by gocd.
the class TemplateConfigServiceTest method shouldLoadClonedTemplateForEdit.
@Test
public void shouldLoadClonedTemplateForEdit() {
String templateName = "empty_template";
CaseInsensitiveString templateAdminUser = new CaseInsensitiveString("templateAdminUser");
Username templateUser = new Username(templateAdminUser);
PipelineTemplateConfig emptyTemplate = PipelineTemplateConfigMother.createTemplate(templateName, new Authorization(new AdminsConfig(new AdminUser(templateAdminUser))), StageConfigMother.stageConfig("some_stage"));
CruiseConfig cruiseConfig = new BasicCruiseConfig();
cruiseConfig.addTemplate(emptyTemplate);
when(securityService.isAuthorizedToEditTemplate(templateName, templateUser)).thenReturn(true);
when(goConfigService.getConfigHolder()).thenReturn(new GoConfigHolder(cruiseConfig, cruiseConfig));
HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
ConfigForEdit<PipelineTemplateConfig> configForEdit = service.loadForEdit(templateName, templateUser, result);
assertThat(configForEdit, is(not(nullValue())));
CaseInsensitiveString empty_template = new CaseInsensitiveString(templateName);
assertThat(configForEdit.getConfig().name(), is(empty_template));
PipelineTemplateConfig template = configForEdit.getConfig();
PipelineTemplateConfig templateForEdit = configForEdit.getCruiseConfig().findTemplate(empty_template);
PipelineTemplateConfig processedTemplate = configForEdit.getProcessedConfig().findTemplate(empty_template);
PipelineTemplateConfig serversTemplate = cruiseConfig.findTemplate(empty_template);
//modify the server's copy
serversTemplate.add(new StageConfig(new CaseInsensitiveString("stage-one"), new JobConfigs(new JobConfig("job"))));
assertThat(serversTemplate.size(), is(2));
//given copy should remain unmodified
assertThat(template.size(), is(1));
assertThat(templateForEdit.size(), is(1));
assertThat(processedTemplate.size(), is(1));
}
use of com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult in project gocd by gocd.
the class TemplateConfigServiceTest method shouldDeleteATemplateWithAGivenName.
@Test
public void shouldDeleteATemplateWithAGivenName() {
PipelineTemplateConfig emptyTemplate = template("empty_template");
CruiseConfig cruiseConfig = new BasicCruiseConfig();
cruiseConfig.addTemplate(emptyTemplate);
service.removeTemplate("empty_template", cruiseConfig, "md5", new HttpLocalizedOperationResult());
verify(goConfigService).updateConfig(new DeleteTemplateCommand("empty_template", "md5"));
}
use of com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult in project gocd by gocd.
the class TemplateConfigServiceTest method shouldErrorOutIfTemplateIsNotFound.
@Test
public void shouldErrorOutIfTemplateIsNotFound() {
PipelineTemplateConfig emptyTemplate = template("empty_template");
CruiseConfig cruiseConfig = new BasicCruiseConfig();
cruiseConfig.addTemplate(emptyTemplate);
when(securityService.isAuthorizedToEditTemplate(anyString(), any(Username.class))).thenReturn(true);
when(goConfigService.getConfigHolder()).thenReturn(new GoConfigHolder(cruiseConfig, cruiseConfig));
HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
ConfigForEdit<PipelineTemplateConfig> configForEdit = service.loadForEdit("blah", new Username(new CaseInsensitiveString("someuser")), result);
assertThat(configForEdit, is(nullValue()));
assertThat(result.isSuccessful(), is(false));
assertThat(result.httpCode(), is(404));
}
use of com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult in project gocd by gocd.
the class TemplateConfigServiceTest method shouldPopulateErrorInResultOnFailure.
@Test
public void shouldPopulateErrorInResultOnFailure() throws Exception {
HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
Username user = new Username(new CaseInsensitiveString("user"));
String templateName = "template-name";
PipelineTemplateConfig pipelineTemplateConfig = new PipelineTemplateConfig(new CaseInsensitiveString(templateName), StageConfigMother.oneBuildPlanWithResourcesAndMaterials("stage", "job"));
String errorMessage = "invalid template";
doThrow(new GoConfigInvalidException(new GoConfigMother().defaultCruiseConfig(), errorMessage)).when(goConfigService).updateConfig(any(CreateTemplateConfigCommand.class), any(Username.class));
service.createTemplateConfig(user, pipelineTemplateConfig, result);
HttpLocalizedOperationResult expectedResult = new HttpLocalizedOperationResult();
expectedResult.unprocessableEntity(LocalizedMessage.string("ENTITY_CONFIG_VALIDATION_FAILED", "template", templateName, errorMessage));
assertThat(result.toString(), is(expectedResult.toString()));
}
Aggregations