55# Since the encrypted values use openssl and are non-deterministic, we can never know the
66# actual value to test against. All we can do is ensure the value is in the correct format
77# for the encrypted content, which verifies it's not in the original state
8- ENCRYPTED_VALUE_PATTERN = %(' ?([a-z0-9A-Z=/+]+)\- \- ([a-z0-9A-Z=/+]+)\- \- ([a-z0-9A-Z=/+]+)' ?)
8+ ENCRYPTED_VALUE_PATTERN = %(['"] ?([a-z0-9A-Z=/+]+)\- \- ([a-z0-9A-Z=/+]+)\- \- ([a-z0-9A-Z=/+]+)['"] ?)
99
1010class Diffcrypt ::EncryptorTest < Minitest ::Test
11+ def test_it_includes_client_info_at_root
12+ content = "---\n key: value"
13+ expected_pattern = /---\n client: diffcrypt-#{ Diffcrypt ::VERSION } \n cipher: #{ Diffcrypt ::Encryptor ::CIPHER } \n data:\n key: #{ ENCRYPTED_VALUE_PATTERN } \n /
14+ assert_match expected_pattern , Diffcrypt ::Encryptor . new ( TEST_KEY ) . encrypt ( content )
15+ end
16+
1117 def test_it_decrypts_root_values
1218 encrypted_content = <<~CONTENT
13- secret_key_base: 88Ry6HESUoXBr6QUFXmni9zzfCIYt9qGNFvIWFcN--4xoecI5mqbNRBibI--62qPJbkzzh5h8lhFEFOSaQ==
19+ data:
20+ secret_key_base: 88Ry6HESUoXBr6QUFXmni9zzfCIYt9qGNFvIWFcN--4xoecI5mqbNRBibI--62qPJbkzzh5h8lhFEFOSaQ==
1421 CONTENT
1522 expected = <<~CONTENT
1623 ---
@@ -27,14 +34,15 @@ def test_it_encrypts_root_values
2734 CONTENT
2835 expected_pattern = /---\n secret_key_base: #{ ENCRYPTED_VALUE_PATTERN } \n /
2936
30- assert_match expected_pattern , Diffcrypt ::Encryptor . new ( TEST_KEY ) . encrypt ( content )
37+ assert_match expected_pattern , Diffcrypt ::Encryptor . new ( TEST_KEY ) . encrypt_data ( content ) . to_yaml
3138 end
3239
3340 def test_it_decrypts_nested_structures
3441 encrypted_content = <<~CONTENT
35- secret_key_base: 88Ry6HESUoXBr6QUFXmni9zzfCIYt9qGNFvIWFcN--4xoecI5mqbNRBibI--62qPJbkzzh5h8lhFEFOSaQ==
36- aws:
37- access_key_id: Ot/uCTEL+8kp61EPctnxNlg=--Be6sg7OdvjZlfxgR--7qRbbf0lA4VgjnUGUrrFwg==
42+ data:
43+ secret_key_base: 88Ry6HESUoXBr6QUFXmni9zzfCIYt9qGNFvIWFcN--4xoecI5mqbNRBibI--62qPJbkzzh5h8lhFEFOSaQ==
44+ aws:
45+ access_key_id: Ot/uCTEL+8kp61EPctnxNlg=--Be6sg7OdvjZlfxgR--7qRbbf0lA4VgjnUGUrrFwg==
3846 CONTENT
3947 expected = <<~CONTENT
4048 ---
@@ -55,16 +63,16 @@ def test_it_encrypts_nested_structures
5563 CONTENT
5664 expected_pattern = /---\n secret_key_base: #{ ENCRYPTED_VALUE_PATTERN } \n aws:\n access_key_id: #{ ENCRYPTED_VALUE_PATTERN } \n /
5765
58- assert_match expected_pattern , Diffcrypt ::Encryptor . new ( TEST_KEY ) . encrypt ( content )
66+ assert_match expected_pattern , Diffcrypt ::Encryptor . new ( TEST_KEY ) . encrypt_data ( content ) . to_yaml
5967 end
6068
6169 # Verifies that a change to one key does not cause the encrypted values for other keys to be recomputed
6270 # Mainly used in conjunction with rails credentials editor
6371 def test_it_only_updates_changed_values
64- original_encrypted_content = "---\n secret_key_base_1: 88Ry6HESUoXBr6QUFXmni9zzfCIYt9qGNFvIWFcN--4xoecI5mqbNRBibI--62qPJbkzzh5h8lhFEFOSaQ==\n aws:\n secret_access_key: 88Ry6HESUoXBr6QUFXmni9zzfCIYt9qGNFvIWFcN--4xoecI5mqbNRBibI--62qPJbkzzh5h8lhFEFOSaQ==\n "
72+ original_encrypted_content = "---\n data: \n secret_key_base_1: 88Ry6HESUoXBr6QUFXmni9zzfCIYt9qGNFvIWFcN--4xoecI5mqbNRBibI--62qPJbkzzh5h8lhFEFOSaQ==\n aws:\n secret_access_key: 88Ry6HESUoXBr6QUFXmni9zzfCIYt9qGNFvIWFcN--4xoecI5mqbNRBibI--62qPJbkzzh5h8lhFEFOSaQ==\n "
6573 updated_content = "---\n secret_key_base_1: secret_key_base_test\n aws:\n secret_access_key: secret_access_key_2"
6674 expected_pattern = /---\n secret_key_base_1: 88Ry6HESUoXBr6QUFXmni9zzfCIYt9qGNFvIWFcN--4xoecI5mqbNRBibI--62qPJbkzzh5h8lhFEFOSaQ==\n aws:\n secret_access_key: #{ ENCRYPTED_VALUE_PATTERN } \n /
6775
68- assert_match expected_pattern , Diffcrypt ::Encryptor . new ( TEST_KEY ) . encrypt ( updated_content , original_encrypted_content )
76+ assert_match expected_pattern , Diffcrypt ::Encryptor . new ( TEST_KEY ) . encrypt_data ( updated_content , original_encrypted_content ) . to_yaml
6977 end
7078end
0 commit comments