public void Rethink_ProtectedDocumentWithContentAndSuccessorIsNotConfigured_ReturnsInputDocument() { var password = "******"; var inputDocument = new Document { Content = "encrypted document content", IsProtected = true, Id = "document #1" }; var documentCryptography = new DocumentCryptography { DocumentId = inputDocument.Id, Iv = Enumerable.Range(1, 10).Select(it => (byte)it).ToArray() }; var cryptographyMock = new Mock <ICryptography>(); var cryptographyRepositoryMock = new Mock <ICryptographyRepository>(); cryptographyRepositoryMock.Setup(it => it.GetByDocumentId(inputDocument.Id)).Returns(documentCryptography); cryptographyMock.Setup(it => it.Decrypt(It.Is <EncryptedData>(encryptedData => encryptedData.Iv.SequenceEqual(documentCryptography.Iv) && encryptedData.CipherText.SequenceEqual(Convert.FromBase64String(inputDocument.Content)) ) , password)) .Returns("decrypted document content"); var decryptArchivist = new DecryptArchivist(cryptographyMock.Object, cryptographyRepositoryMock.Object) { Successor = null }; decryptArchivist.Configure(password); var result = decryptArchivist.Rethink(inputDocument); Assert.Equal(inputDocument, result); Assert.Equal(result.Content, "decrypted document content"); cryptographyRepositoryMock.Verify(it => it.GetByDocumentId(inputDocument.Id), Times.Once); cryptographyMock.Verify(it => it.Decrypt(It.Is <EncryptedData>(encryptedData => encryptedData.Iv.SequenceEqual(documentCryptography.Iv) && encryptedData.CipherText.SequenceEqual(Convert.FromBase64String("encrypted document content"))), password), Times.Once); }
public override Document Rethink(Document document) { if (!string.IsNullOrEmpty(document?.Content)) { if (!document.IsProtected) { var encryptedData = _cryptography.Encrypt(_configuration, document.Content); var cryptography = new DocumentCryptography { DocumentId = document.Id, Iv = encryptedData.Iv }; _cryptographyRepository.Add(cryptography); document.Content = Convert.ToBase64String(encryptedData.CipherText); document.IsProtected = true; } } if (_successor != null) { return(_successor.Rethink(document)); } return(document); }