diff --git a/components/credentials/src/test/java/org/cloudfoundry/credhub/services/DefaultCredentialVersionDataServiceTest.java b/components/credentials/src/test/java/org/cloudfoundry/credhub/services/DefaultCredentialVersionDataServiceTest.java index 07ab8b466..9371aaad2 100644 --- a/components/credentials/src/test/java/org/cloudfoundry/credhub/services/DefaultCredentialVersionDataServiceTest.java +++ b/components/credentials/src/test/java/org/cloudfoundry/credhub/services/DefaultCredentialVersionDataServiceTest.java @@ -31,11 +31,13 @@ import org.cloudfoundry.credhub.entity.CredentialVersionData; import org.cloudfoundry.credhub.entity.PasswordCredentialVersionData; import org.cloudfoundry.credhub.entity.SshCredentialVersionData; +import org.cloudfoundry.credhub.entity.UserCredentialVersionData; import org.cloudfoundry.credhub.entity.ValueCredentialVersionData; import org.cloudfoundry.credhub.exceptions.MaximumSizeException; import org.cloudfoundry.credhub.exceptions.ParameterizedValidationException; import org.cloudfoundry.credhub.repositories.CredentialRepository; import org.cloudfoundry.credhub.repositories.CredentialVersionRepository; +import org.cloudfoundry.credhub.repositories.EncryptedValueRepository; import org.cloudfoundry.credhub.util.CurrentTimeProvider; import org.cloudfoundry.credhub.utils.DatabaseProfileResolver; import org.cloudfoundry.credhub.utils.DatabaseUtilities; @@ -63,6 +65,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertEquals; @RunWith(SpringRunner.class) @ActiveProfiles(value = "unit-test", resolver = DatabaseProfileResolver.class) @@ -76,6 +79,9 @@ public class DefaultCredentialVersionDataServiceTest { @Autowired private CredentialRepository credentialRepository; + @Autowired + private EncryptedValueRepository encryptedValueRepository; + @Autowired private EncryptionKeyCanaryDataService encryptionKeyCanaryDataService; @@ -265,13 +271,17 @@ public void save_whenGivenCredentialWithMetadata() { @Test public void delete_onAnExistingCredential_returnsTrue() { + long nEncryptedValuesPre = encryptedValueRepository.count(); credentialDataService.save(new Credential("/my-credential")); assertThat(subject.delete("/my-credential"), equalTo(true)); + assertEquals("Associated encryptedValues are deleted when credential is deleted", + nEncryptedValuesPre, encryptedValueRepository.count()); } @Test public void delete_onACredentialName_deletesAllCredentialsWithTheName() { + long nEncryptedValuesPre = encryptedValueRepository.count(); final Credential credential = credentialDataService .save(new Credential("/my-credential")); @@ -301,10 +311,13 @@ public void delete_onACredentialName_deletesAllCredentialsWithTheName() { assertThat(subject.findAllByName("/my-credential"), hasSize(0)); assertNull(credentialDataService.find("/my-credential")); + assertEquals("Associated encryptedValues are deleted when password credential is deleted", + nEncryptedValuesPre, encryptedValueRepository.count()); } @Test public void delete_givenACredentialNameCasedDifferentlyFromTheActual_shouldBeCaseInsensitive() { + long nEncryptedValuesPre = encryptedValueRepository.count(); final Credential credentialName = credentialDataService .save(new Credential("/my-credential")); @@ -334,6 +347,36 @@ public void delete_givenACredentialNameCasedDifferentlyFromTheActual_shouldBeCas subject.delete("/MY-CREDENTIAL"); assertThat(subject.findAllByName("/my-credential"), empty()); + assertEquals("Associated encryptedValues are deleted when password credential is deleted", + nEncryptedValuesPre, encryptedValueRepository.count()); + } + + @Test + public void delete_UserTypeCredential() { + long nEncryptedValuesPre = encryptedValueRepository.count(); + final Credential credential = credentialDataService + .save(new Credential("/my-credential")); + + final EncryptedValue encryptedValueA = new EncryptedValue(); + encryptedValueA.setEncryptionKeyUuid(activeCanaryUuid); + encryptedValueA.setEncryptedValue("credential-password".getBytes(UTF_8)); + encryptedValueA.setNonce(new byte[]{}); + + final UserCredentialVersionData userCredential = + new UserCredentialVersionData("/test-user"); + userCredential.setUsername("test-user"); + userCredential.setCredential(credential); + userCredential.setEncryptedValueData(encryptedValueA); + userCredential.setSalt("salt"); + subject.save(userCredential); + + assertEquals("User credential is saved as expected", 1, + subject.findAllByName("/my-credential").size()); + + assertThat(subject.findAllByName("/my-credential"), hasSize(1)); + subject.delete("/my-credential"); + assertEquals("Associated encryptedValues are deleted when user credential is deleted", + nEncryptedValuesPre, encryptedValueRepository.count()); } @Test