@Test public void shouldNotUpdateEnvironmentIfEditingOverAStaleCopy() throws Exception { configHelper.addEnvironments("prod"); String staleMd5 = goConfigDao.md5OfConfigFile(); service.updateEnvironment( "prod", environmentConfig("uat1"), new Username(new CaseInsensitiveString("foo")), staleMd5); assertThat("md5 should be stale", goConfigDao.md5OfConfigFile(), is(not(staleMd5))); HttpLocalizedOperationResult result = service.updateEnvironment( "prod", environmentConfig("uat2"), new Username(new CaseInsensitiveString("foo")), staleMd5); assertThat(result.isSuccessful(), is(false)); assertThat( result.message(localizer), is( "Failed to update environment 'prod'. " + ConfigFileHasChangedException.CONFIG_CHANGED_PLEASE_REFRESH)); }
@Test public void shouldReturnTheCorrectLocalizedMessageForDuplicatePipelinesInAnEnvironment() { BasicEnvironmentConfig environmentConfig = environmentConfig("uat"); goConfigService.addPipeline( PipelineConfigMother.createPipelineConfig("foo", "dev", "job"), "foo-grp"); environmentConfig.addPipeline(new CaseInsensitiveString("foo")); goConfigService.addEnvironment(environmentConfig); ArrayList<String> pipelines = new ArrayList<>(); pipelines.add("foo"); HttpLocalizedOperationResult result = new HttpLocalizedOperationResult(); service.createEnvironment( env("foo-env", pipelines, new ArrayList<Map<String, String>>(), new ArrayList<String>()), new Username(new CaseInsensitiveString("any")), result); result = new HttpLocalizedOperationResult(); service.createEnvironment( env("env", pipelines, new ArrayList<Map<String, String>>(), new ArrayList<String>()), new Username(new CaseInsensitiveString("any")), result); assertThat( result.message(localizer), is( "Failed to add environment. Associating pipeline(s) which is already part of uat environment")); }
@Test public void shouldReturnAClonedInstanceOfEnvironmentConfig() throws NoSuchEnvironmentException { HttpLocalizedOperationResult result = new HttpLocalizedOperationResult(); configHelper.addEnvironments("foo-env"); assertThat(service.named("foo-env"), sameInstance(service.named("foo-env"))); assertThat( service.named("foo-env"), not(sameInstance(service.forEdit("foo-env", result).getConfigElement()))); assertThat(result.isSuccessful(), is(true)); }
@Test public void shouldPopulateResultWithErrorIfEnvNotFound() throws NoSuchEnvironmentException { HttpLocalizedOperationResult result = new HttpLocalizedOperationResult(); ConfigElementForEdit<EnvironmentConfig> edit = service.forEdit("foo-env", result); assertThat(result.message(localizer), is("Environment named 'foo-env' not found.")); assertThat(edit, is(nullValue())); }
@Test public void shouldReturnTheCorrectLocalizedMessageWhenUserDoesNotHavePermissionToDelete() throws IOException, NoSuchEnvironmentException { configHelper.addEnvironments("foo"); configHelper.turnOnSecurity(); configHelper.addAdmins("super_hero"); HttpLocalizedOperationResult result = new HttpLocalizedOperationResult(); service.deleteEnvironment( service.getEnvironmentConfig("foo"), new Username(new CaseInsensitiveString("evil_hacker")), result); assertThat( result.message(localizer), is( "Failed to delete environment 'foo'. User 'evil_hacker' does not have permission to update environments")); }
@Test public void shouldDeleteAnEnvironment() throws Exception { String environmentName = "dev"; HttpLocalizedOperationResult result = new HttpLocalizedOperationResult(); goConfigService.addEnvironment( new BasicEnvironmentConfig(new CaseInsensitiveString(environmentName))); assertTrue(goConfigService.hasEnvironmentNamed(new CaseInsensitiveString(environmentName))); service.deleteEnvironment( service.getEnvironmentConfig(environmentName), new Username(new CaseInsensitiveString("foo")), result); assertFalse(goConfigService.hasEnvironmentNamed(new CaseInsensitiveString(environmentName))); assertThat( result.message(localizer), containsString("Environment 'dev' was deleted successfully.")); }
@Test public void shouldReturnBadRequestForUpdateWhenUsingInvalidEnvName_ForNewUpdateEnvironmentMethod_ForNewUpdateEnvironmentMethod() throws NoSuchEnvironmentException { configHelper.addEnvironments("foo-env"); HttpLocalizedOperationResult result = new HttpLocalizedOperationResult(); service.updateEnvironment( service.getEnvironmentConfig("foo-env"), env( "foo env", new ArrayList<String>(), new ArrayList<Map<String, String>>(), new ArrayList<String>()), new Username(new CaseInsensitiveString("any")), result); assertThat(result.httpCode(), is(HttpServletResponse.SC_BAD_REQUEST)); assertThat( result.message(localizer), containsString("Failed to update environment 'foo-env'.")); }
@Test public void shouldUpdateExistingEnvironment() throws Exception { BasicEnvironmentConfig uat = environmentConfig("uat"); goConfigService.addPipeline( PipelineConfigMother.createPipelineConfig("foo", "dev", "job"), "foo-grp"); goConfigService.addPipeline( PipelineConfigMother.createPipelineConfig("bar", "dev", "job"), "foo-grp"); Username user = Username.ANONYMOUS; agentConfigService.addAgent(new AgentConfig("uuid-1", "host-1", "192.168.1.2"), user); agentConfigService.addAgent(new AgentConfig("uuid-2", "host-2", "192.168.1.3"), user); uat.addPipeline(new CaseInsensitiveString("foo")); uat.addAgent("uuid-2"); uat.addEnvironmentVariable("env-one", "ONE"); uat.addEnvironmentVariable("env-two", "TWO"); goConfigService.addEnvironment(new BasicEnvironmentConfig(new CaseInsensitiveString("dev"))); goConfigService.addEnvironment(new BasicEnvironmentConfig(new CaseInsensitiveString("qa"))); goConfigService.addEnvironment(uat); goConfigService.addEnvironment( new BasicEnvironmentConfig(new CaseInsensitiveString("acceptance"))); goConfigService.addEnvironment( new BasicEnvironmentConfig(new CaseInsensitiveString("function_testing"))); EnvironmentConfig newUat = new BasicEnvironmentConfig(new CaseInsensitiveString("prod")); newUat.addPipeline(new CaseInsensitiveString("bar")); newUat.addAgent("uuid-1"); newUat.addEnvironmentVariable("env-three", "THREE"); HttpLocalizedOperationResult result = service.updateEnvironment( "uat", newUat, new Username(new CaseInsensitiveString("foo")), goConfigDao.md5OfConfigFile()); EnvironmentConfig updatedEnv = service.named("prod"); assertThat(updatedEnv.name(), is(new CaseInsensitiveString("prod"))); assertThat(updatedEnv.getAgents().getUuids(), is(Arrays.asList("uuid-1"))); assertThat(updatedEnv.getPipelineNames(), is(Arrays.asList(new CaseInsensitiveString("bar")))); EnvironmentVariablesConfig updatedVariables = new EnvironmentVariablesConfig(); updatedVariables.add("env-three", "THREE"); assertThat(updatedEnv.getVariables(), is(updatedVariables)); EnvironmentsConfig currentEnvironments = goConfigService.getCurrentConfig().getEnvironments(); assertThat(currentEnvironments.indexOf(updatedEnv), is(2)); assertThat(currentEnvironments.size(), is(5)); }
@Test public void shouldReturnTheCorrectLocalizedMessageForUpdateWhenDuplicateEnvironmentExists_ForNewUpdateEnvironmentMethod() throws NoSuchEnvironmentException { configHelper.addEnvironments("foo-env"); configHelper.addEnvironments("bar-env"); HttpLocalizedOperationResult result = new HttpLocalizedOperationResult(); service.updateEnvironment( service.getEnvironmentConfig("bar-env"), env( "foo-env", new ArrayList<String>(), new ArrayList<Map<String, String>>(), new ArrayList<String>()), new Username(new CaseInsensitiveString("any")), result); assertThat( result.message(localizer), is( "Failed to update environment 'bar-env'. failed to save : Duplicate unique value [foo-env] declared for identity constraint \"uniqueEnvironmentName\" of element \"environments\".")); }
@Test public void shouldReturnTheCorrectLocalizedMessageWhenUserDoesNotHavePermissionToUpdate_ForNewUpdateEnvironmentMethod() throws Exception { configHelper.addEnvironments("foo"); configHelper.turnOnSecurity(); configHelper.addAdmins("super_hero"); HttpLocalizedOperationResult result = new HttpLocalizedOperationResult(); service.updateEnvironment( service.getEnvironmentConfig("foo"), env( "foo-env", new ArrayList<String>(), new ArrayList<Map<String, String>>(), new ArrayList<String>()), new Username(new CaseInsensitiveString("evil_hacker")), result); assertThat( result.message(localizer), is( "Failed to update environment 'foo'. User 'evil_hacker' does not have permission to update environments")); }
@Test public void shouldReturnBadRequestForInvalidEnvName() { HttpLocalizedOperationResult result = new HttpLocalizedOperationResult(); service.createEnvironment( env( "foo env", new ArrayList<String>(), new ArrayList<Map<String, String>>(), new ArrayList<String>()), new Username(new CaseInsensitiveString("any")), result); assertThat(result.httpCode(), is(HttpServletResponse.SC_BAD_REQUEST)); assertThat(result.message(localizer), containsString("Failed to add environment.")); }
@Test public void shouldReturnTheCorrectLocalizedMessageForDuplicateEnvironment() { configHelper.addEnvironments("foo-env"); HttpLocalizedOperationResult result = new HttpLocalizedOperationResult(); service.createEnvironment( env( "foo-env", new ArrayList<String>(), new ArrayList<Map<String, String>>(), new ArrayList<String>()), new Username(new CaseInsensitiveString("any")), result); assertThat( result.message(localizer), is("Failed to add environment. Environment 'foo-env' already exists.")); }
@Test public void shouldReturnBadRequestForUpdateWhenUsingInvalidEnvName() { configHelper.addEnvironments("foo-env"); HttpLocalizedOperationResult result = service.updateEnvironment( "foo-env", env( "foo env", new ArrayList<String>(), new ArrayList<Map<String, String>>(), new ArrayList<String>()), new Username(new CaseInsensitiveString("any")), goConfigDao.md5OfConfigFile()); assertThat(result.httpCode(), is(HttpServletResponse.SC_BAD_REQUEST)); assertThat( result.message(localizer), containsString("Failed to update environment 'foo-env'.")); }
@Test public void shouldReturnTheCorrectLocalizedMessageForNoPermission() throws IOException { configHelper.turnOnSecurity(); configHelper.addAdmins("super_hero"); HttpLocalizedOperationResult result = new HttpLocalizedOperationResult(); service.createEnvironment( env( "foo-env", new ArrayList<String>(), new ArrayList<Map<String, String>>(), new ArrayList<String>()), new Username(new CaseInsensitiveString("evil_hacker")), result); assertThat( result.message(localizer), is( "Failed to add environment. User 'evil_hacker' does not have permission to add environments")); }
@Test public void shouldReturnTheCorrectLocalizedMessageForUpdateWhenDuplicateEnvironmentExists() { configHelper.addEnvironments("foo-env"); configHelper.addEnvironments("bar-env"); HttpLocalizedOperationResult result = service.updateEnvironment( "bar-env", env( "foo-env", new ArrayList<String>(), new ArrayList<Map<String, String>>(), new ArrayList<String>()), new Username(new CaseInsensitiveString("any")), goConfigDao.md5OfConfigFile()); assertThat( result.message(localizer), is( "Failed to update environment 'bar-env'. Duplicate unique value [foo-env] declared for identity constraint \"uniqueEnvironmentName\" of element \"environments\".")); }