Search in sources :

Example 16 with Localizer

use of com.thoughtworks.go.i18n.Localizer in project gocd by gocd.

the class PipelineConfigsServiceTest method shouldReturnUnsuccessfulResultWhenTheGroupIsNotFound_onUpdateXml.

@Test
public void shouldReturnUnsuccessfulResultWhenTheGroupIsNotFound_onUpdateXml() throws Exception {
    String groupName = "non-existent-group_name";
    Localizer localizer = mock(Localizer.class);
    when(localizer.localize("PIPELINE_GROUP_NOT_FOUND", groupName)).thenReturn("Not found");
    when(securityService.isUserAdminOfGroup(validUser.getUsername(), groupName)).thenThrow(new PipelineGroupNotFoundException());
    when(goConfigService.configFileMd5()).thenReturn("md5");
    GoConfigOperationalResponse<PipelineConfigs> actual = service.updateXml(groupName, "", "md5", validUser, result);
    PipelineConfigs configs = actual.getConfigElement();
    GoConfigValidity validity = actual.getValidity();
    assertThat(configs, is(nullValue()));
    assertThat(result.httpCode(), is(404));
    assertThat(result.isSuccessful(), is(false));
    assertThat(result.message(localizer), is("Not found"));
    assertThat(validity.isValid(), is(true));
    verify(securityService, times(1)).isUserAdminOfGroup(validUser.getUsername(), groupName);
}
Also used : Localizer(com.thoughtworks.go.i18n.Localizer) PipelineGroupNotFoundException(com.thoughtworks.go.config.exceptions.PipelineGroupNotFoundException) GoConfigValidity(com.thoughtworks.go.config.validation.GoConfigValidity) Test(org.junit.Test)

Example 17 with Localizer

use of com.thoughtworks.go.i18n.Localizer in project gocd by gocd.

the class PipelineConfigsServiceTest method shouldReturnUnsuccessfulResultWhenXmlIsInvalid_onUpdateXml.

@Test
public void shouldReturnUnsuccessfulResultWhenXmlIsInvalid_onUpdateXml() throws Exception {
    String errorMessage = "Can not parse xml";
    final String groupName = "group_name";
    Localizer localizer = mock(Localizer.class);
    when(localizer.localize("UPDATE_GROUP_XML_FAILED", groupName, errorMessage)).thenReturn("Invalid");
    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 = "foobar";
    when(groupSaver.saveXml(updatedPartial, md5)).thenReturn(GoConfigValidity.invalid(errorMessage));
    GoConfigOperationalResponse<PipelineConfigs> actual = service.updateXml(groupName, updatedPartial, md5, validUser, result);
    PipelineConfigs configs = actual.getConfigElement();
    GoConfigValidity validity = actual.getValidity();
    assertThat(configs, is(nullValue()));
    assertThat(result.httpCode(), is(500));
    assertThat(result.isSuccessful(), is(false));
    assertThat(result.message(localizer), is("Invalid"));
    assertThat(validity.isValid(), is(false));
    verify(securityService, times(1)).isUserAdminOfGroup(validUser.getUsername(), groupName);
}
Also used : Localizer(com.thoughtworks.go.i18n.Localizer) GoConfigValidity(com.thoughtworks.go.config.validation.GoConfigValidity) Test(org.junit.Test)

Example 18 with Localizer

use of com.thoughtworks.go.i18n.Localizer in project gocd by gocd.

the class PipelineConfigsServiceTest method shouldReturnUnauthorizedResultWhenUserIsNotAuthorizedToViewGroup_onGetXml.

@Test
public void shouldReturnUnauthorizedResultWhenUserIsNotAuthorizedToViewGroup_onGetXml() {
    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);
    String actual = service.getXml(groupName, invalidUser, result);
    assertThat(actual, is(nullValue()));
    assertThat(result.httpCode(), is(401));
    assertThat(result.isSuccessful(), is(false));
    assertThat(result.message(localizer), is("Unauthorized!"));
    verify(goConfigService, never()).getConfigForEditing();
    verify(securityService, times(1)).isUserAdminOfGroup(invalidUser.getUsername(), groupName);
}
Also used : Username(com.thoughtworks.go.server.domain.Username) Localizer(com.thoughtworks.go.i18n.Localizer) Test(org.junit.Test)

Example 19 with Localizer

use of com.thoughtworks.go.i18n.Localizer in project gocd by gocd.

the class TemplateConfigServiceTest method shouldErrorOutIfUserIsNotAllowedToAdministerTheGivenTemplate.

@Test
public void shouldErrorOutIfUserIsNotAllowedToAdministerTheGivenTemplate() {
    Username username = new Username(new CaseInsensitiveString("user"));
    String templateName = "templateName";
    PipelineTemplateConfig emptyTemplate = PipelineTemplateConfigMother.createTemplate(templateName);
    CruiseConfig cruiseConfig = new BasicCruiseConfig();
    cruiseConfig.addTemplate(emptyTemplate);
    when(securityService.isAuthorizedToEditTemplate(new CaseInsensitiveString(templateName), username)).thenReturn(false);
    when(goConfigService.getConfigHolder()).thenReturn(new GoConfigHolder(cruiseConfig, cruiseConfig));
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    ConfigForEdit<PipelineTemplateConfig> configForEdit = service.loadForEdit(templateName, username, result);
    assertThat(configForEdit, is(nullValue()));
    assertThat(result.isSuccessful(), is(false));
    assertThat(result.httpCode(), is(401));
    Localizer localizer = mock(Localizer.class);
    when(localizer.localize("UNAUTHORIZED_TO_EDIT_TEMPLATE", templateName)).thenReturn("No template for you");
    assertThat(result.message(localizer), is("No template for you"));
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Username(com.thoughtworks.go.server.domain.Username) Localizer(com.thoughtworks.go.i18n.Localizer) Test(org.junit.Test)

Example 20 with Localizer

use of com.thoughtworks.go.i18n.Localizer in project gocd by gocd.

the class TemplateConfigServiceTest method shouldReturnUnauthorizedIfTheUserIsNotAdmin.

@Test
public void shouldReturnUnauthorizedIfTheUserIsNotAdmin() {
    Username user = new Username(new CaseInsensitiveString("user"));
    when(securityService.isUserAdmin(user)).thenReturn(false);
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    List<PipelineConfig> pipelineConfigs = service.allPipelinesNotUsingTemplates(user, result);
    assertThat(result.isSuccessful(), is(false));
    Localizer localizer = mock(Localizer.class);
    when(localizer.localize("UNAUTHORIZED_TO_ADMINISTER", new Object[0])).thenReturn("foo");
    assertThat(result.message(localizer), is("foo"));
    assertThat(pipelineConfigs, is(nullValue()));
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Username(com.thoughtworks.go.server.domain.Username) Localizer(com.thoughtworks.go.i18n.Localizer) Test(org.junit.Test)

Aggregations

Localizer (com.thoughtworks.go.i18n.Localizer)21 Test (org.junit.Test)14 Username (com.thoughtworks.go.server.domain.Username)5 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)5 Before (org.junit.Before)5 GoConfigValidity (com.thoughtworks.go.config.validation.GoConfigValidity)3 PipelineGroupNotFoundException (com.thoughtworks.go.config.exceptions.PipelineGroupNotFoundException)2 Localizable (com.thoughtworks.go.i18n.Localizable)2 SchedulingPerformanceLogger (com.thoughtworks.go.server.perf.SchedulingPerformanceLogger)2 TestTransactionSynchronizationManager (com.thoughtworks.go.server.transaction.TestTransactionSynchronizationManager)2 TestTransactionTemplate (com.thoughtworks.go.server.transaction.TestTransactionTemplate)2 ServerHealthService (com.thoughtworks.go.serverhealth.ServerHealthService)2 TimeProvider (com.thoughtworks.go.util.TimeProvider)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 TaskExtension (com.thoughtworks.go.plugin.access.pluggabletask.TaskExtension)1 TaskPreference (com.thoughtworks.go.plugin.access.pluggabletask.TaskPreference)1 TaskConfig (com.thoughtworks.go.plugin.api.task.TaskConfig)1 TaskConfigProperty (com.thoughtworks.go.plugin.api.task.TaskConfigProperty)1 SecurityAuthConfigService (com.thoughtworks.go.server.service.SecurityAuthConfigService)1