@Test(expected = IllegalArgumentException.class) public void shouldThrowUpWhenTheAttributeMapHasBothNameAndValueAreEmpty() throws CryptoException { EnvironmentVariableConfig environmentVariableConfig = new EnvironmentVariableConfig((GoCipher) null); HashMap attrs = new HashMap(); attrs.put(EnvironmentVariableConfig.VALUE, ""); environmentVariableConfig.setConfigAttributes(attrs); }
@Test public void shouldGetPlainTextValue() throws CryptoException { GoCipher mockGoCipher = mock(GoCipher.class); String plainText = "password"; EnvironmentVariableConfig environmentVariableConfig = new EnvironmentVariableConfig(mockGoCipher); HashMap attrs = getAttributeMap(plainText, "false", "1"); environmentVariableConfig.setConfigAttributes(attrs); assertThat(environmentVariableConfig.getValue(), is(plainText)); verify(mockGoCipher, never()).decrypt(anyString()); verify(mockGoCipher, never()).encrypt(anyString()); }
public void setConfigAttributes(Object attributes) { this.clear(); if (attributes != null) { for (Map attributeMap : (List<Map>) attributes) { EnvironmentVariableConfig environmentVariableConfig = new EnvironmentVariableConfig(new GoCipher()); try { environmentVariableConfig.setConfigAttributes(attributeMap); this.add(environmentVariableConfig); } catch (IllegalArgumentException e) { continue; } } } }
@Test public void shouldEncryptValueWhenChanged() throws CryptoException { GoCipher mockGoCipher = mock(GoCipher.class); String plainText = "password"; String cipherText = "encrypted"; when(mockGoCipher.encrypt(plainText)).thenReturn(cipherText); when(mockGoCipher.decrypt(cipherText)).thenReturn(plainText); EnvironmentVariableConfig environmentVariableConfig = new EnvironmentVariableConfig(mockGoCipher); HashMap firstSubmit = getAttributeMap(plainText, "true", "true"); environmentVariableConfig.setConfigAttributes(firstSubmit); assertThat(environmentVariableConfig.getEncryptedValue(), is(cipherText)); }
@Test public void shouldGetPlainTextValueFromAnEncryptedValue() throws CryptoException { GoCipher mockGoCipher = mock(GoCipher.class); String plainText = "password"; String cipherText = "encrypted"; when(mockGoCipher.encrypt(plainText)).thenReturn(cipherText); when(mockGoCipher.decrypt(cipherText)).thenReturn(plainText); EnvironmentVariableConfig environmentVariableConfig = new EnvironmentVariableConfig(mockGoCipher); HashMap attrs = getAttributeMap(plainText, "true", "true"); environmentVariableConfig.setConfigAttributes(attrs); assertThat(environmentVariableConfig.getValue(), is(plainText)); verify(mockGoCipher).decrypt(cipherText); }
@Test public void shouldAssignNameAndValueForAVanillaEnvironmentVariable() { EnvironmentVariableConfig environmentVariableConfig = new EnvironmentVariableConfig((GoCipher) null); HashMap attrs = new HashMap(); attrs.put(EnvironmentVariableConfig.NAME, "foo"); attrs.put(EnvironmentVariableConfig.VALUE, "password"); environmentVariableConfig.setConfigAttributes(attrs); assertThat(environmentVariableConfig.getValue(), is("password")); assertThat(environmentVariableConfig.getName(), is("foo")); assertThat(environmentVariableConfig.isSecure(), is(false)); }
@Test public void shouldRetainEncryptedVariableWhenNotEdited() throws CryptoException { GoCipher mockGoCipher = mock(GoCipher.class); String plainText = "password"; String cipherText = "encrypted"; when(mockGoCipher.encrypt(plainText)).thenReturn(cipherText); when(mockGoCipher.decrypt(cipherText)).thenReturn(plainText); when(mockGoCipher.encrypt(cipherText)).thenReturn("SHOULD NOT DO THIS"); EnvironmentVariableConfig environmentVariableConfig = new EnvironmentVariableConfig(mockGoCipher); HashMap firstSubmit = getAttributeMap(plainText, "true", "true"); environmentVariableConfig.setConfigAttributes(firstSubmit); HashMap secondSubmit = getAttributeMap(cipherText, "true", "false"); environmentVariableConfig.setConfigAttributes(secondSubmit); assertThat(environmentVariableConfig.getEncryptedValue(), is(cipherText)); assertThat(environmentVariableConfig.getName(), is("foo")); assertThat(environmentVariableConfig.isSecure(), is(true)); verify(mockGoCipher, never()).encrypt(cipherText); }
@Test public void shouldEncryptValueWhenConstructedAsSecure() throws CryptoException { GoCipher goCipher = mock(GoCipher.class); String encryptedText = "encrypted"; when(goCipher.encrypt("password")).thenReturn(encryptedText); EnvironmentVariableConfig environmentVariableConfig = new EnvironmentVariableConfig(goCipher); HashMap attrs = getAttributeMap("password", "true", "true"); environmentVariableConfig.setConfigAttributes(attrs); assertThat(environmentVariableConfig.getEncryptedValue(), is(encryptedText)); assertThat(environmentVariableConfig.getName(), is("foo")); assertThat(environmentVariableConfig.isSecure(), is(true)); }