@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 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 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 shouldPopulateErrorsForCheckConnectionOnPlugin() throws Exception { String pluginId = "yum"; PackageRepository packageRepository = packageRepository(pluginId); HttpLocalizedOperationResult result = new HttpLocalizedOperationResult(); PackageRepositoryService service = new PackageRepositoryService( pluginManager, packageAsRepositoryExtension, goConfigService, securityService, entityHashingService, localizer); ArgumentCaptor<RepositoryConfiguration> argumentCaptor = ArgumentCaptor.forClass(RepositoryConfiguration.class); when(packageAsRepositoryExtension.checkConnectionToRepository( eq(pluginId), argumentCaptor.capture())) .thenReturn(new Result().withErrorMessages("Repo invalid!!", "Could not connect")); service.checkConnection(packageRepository, result); RepositoryConfiguration packageConfigurations = argumentCaptor.getValue(); PackageMaterialTestHelper.assertPackageConfiguration( packageConfigurations.list(), packageRepository.getConfiguration()); assertThat(result.isSuccessful(), is(false)); when(localizer.localize( "PACKAGE_REPOSITORY_CHECK_CONNECTION_FAILED", "Repo invalid!!\nCould not connect")) .thenReturn("error_msg"); assertThat(result.message(localizer), is("error_msg")); verify(packageAsRepositoryExtension) .checkConnectionToRepository(eq(pluginId), any(RepositoryConfiguration.class)); }
@Test public void shouldPerformCheckConnectionOnPluginAndCatchAnyExceptionsThrownByThePlugin() throws Exception { String pluginId = "yum"; PackageRepository packageRepository = packageRepository(pluginId); HttpLocalizedOperationResult result = new HttpLocalizedOperationResult(); PackageRepositoryService service = new PackageRepositoryService( pluginManager, packageAsRepositoryExtension, goConfigService, securityService, entityHashingService, localizer); ArgumentCaptor<RepositoryConfiguration> argumentCaptor = ArgumentCaptor.forClass(RepositoryConfiguration.class); when(packageAsRepositoryExtension.checkConnectionToRepository( eq(pluginId), argumentCaptor.capture())) .thenThrow(new RuntimeException("Check Connection not implemented!!")); service.checkConnection(packageRepository, result); assertThat(result.isSuccessful(), is(false)); when(localizer.localize( "PACKAGE_REPOSITORY_CHECK_CONNECTION_FAILED", "Check Connection not implemented!!")) .thenReturn("error_msg"); assertThat(result.message(localizer), is("error_msg")); verify(packageAsRepositoryExtension) .checkConnectionToRepository(eq(pluginId), any(RepositoryConfiguration.class)); }
@Test public void shouldPerformCheckConnectionOnPlugin() throws Exception { String pluginId = "yum"; PackageRepository packageRepository = packageRepository(pluginId); HttpLocalizedOperationResult result = new HttpLocalizedOperationResult(); PackageRepositoryService service = new PackageRepositoryService( pluginManager, packageAsRepositoryExtension, goConfigService, securityService, entityHashingService, localizer); ArgumentCaptor<RepositoryConfiguration> argumentCaptor = ArgumentCaptor.forClass(RepositoryConfiguration.class); when(packageAsRepositoryExtension.checkConnectionToRepository( eq(pluginId), argumentCaptor.capture())) .thenReturn(new Result().withSuccessMessages("Accessed Repo File!!!")); service.checkConnection(packageRepository, result); RepositoryConfiguration packageConfigurations = argumentCaptor.getValue(); PackageMaterialTestHelper.assertPackageConfiguration( packageConfigurations.list(), packageRepository.getConfiguration()); assertThat(result.isSuccessful(), is(true)); when(localizer.localize("CONNECTION_OK", "Accessed Repo File!!!")).thenReturn("success_msg"); assertThat(result.message(localizer), is("success_msg")); verify(packageAsRepositoryExtension) .checkConnectionToRepository(eq(pluginId), any(RepositoryConfiguration.class)); }
@Test public void shouldNotAllowUnauthorizedUserToGetMingleConfig() { HttpLocalizedOperationResult result = new HttpLocalizedOperationResult(); MingleConfig mingleConfig = mingleConfigService.mingleConfigForPipelineNamed( "foo", new Username(new CaseInsensitiveString("some_loser")), result); assertThat(mingleConfig, is(nullValue())); assertThat(result.isSuccessful(), is(false)); assertThat( result.message(localizer), is("You do not have view permissions for pipeline 'foo'.")); assertThat(result.httpCode(), is(401)); }
@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 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() { 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 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 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\".")); }
@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() throws IOException { configHelper.addEnvironments("foo"); configHelper.turnOnSecurity(); configHelper.addAdmins("super_hero"); HttpLocalizedOperationResult result = service.updateEnvironment( "foo", env( "foo-env", new ArrayList<String>(), new ArrayList<Map<String, String>>(), new ArrayList<String>()), new Username(new CaseInsensitiveString("evil_hacker")), goConfigDao.md5OfConfigFile()); assertThat( result.message(localizer), is( "Failed to update environment 'foo'. User 'evil_hacker' does not have permission to update environments")); }