github.com/wunderio/silta-cli@v0.0.0-20240508100559-3017e4ab3a20/tests/secrets_test.go (about) 1 package cmd_test 2 3 import ( 4 "fmt" 5 "os" 6 "os/exec" 7 "strings" 8 "testing" 9 ) 10 11 func TestSecretsEncryptDecryptCmd(t *testing.T) { 12 13 // Go to main directory 14 wd, _ := os.Getwd() 15 os.Chdir("..") 16 17 secretMessage := "test:yes" 18 19 // Create secret 20 command := fmt.Sprintf("echo -n '%s' > tests/test-secret", secretMessage) 21 exec.Command("bash", "-c", command).Run() 22 23 // Set different filename for encrypted file 24 command = "secrets encrypt --file tests/test-secret --secret-key test --output-file tests/test-secret-2" 25 environment := []string{} 26 testString := "Encrypting tests/test-secret\nSaving encrypted file to tests/test-secret-2\nSuccess\n" 27 CliExecTest(t, command, environment, testString, true) 28 29 // Verify file 30 out, _ := exec.Command("bash", "-c", "cat tests/test-secret-2").CombinedOutput() 31 if !strings.HasPrefix(string(out), "Salted") { 32 t.Error("File not encrypted") 33 } 34 35 // Encrypt and replace file 36 command = "secrets encrypt --file tests/test-secret --secret-key test" 37 environment = []string{} 38 testString = "Encrypting tests/test-secret\nSuccess\n" 39 CliExecTest(t, command, environment, testString, true) 40 41 // Verify file 42 out, _ = exec.Command("bash", "-c", "cat tests/test-secret").CombinedOutput() 43 if !strings.HasPrefix(string(out), "Salted") { 44 t.Error("File not encrypted") 45 } 46 47 // Test double-encription prevention 48 command = "secrets encrypt --file tests/test-secret --secret-key test" 49 environment = []string{} 50 testString = "File seems to be been encrypted already, skipping\n" 51 CliExecTest(t, command, environment, testString, false) 52 53 // Test decryption using wrong key 54 command = "secrets decrypt --file tests/test-secret --secret-key wrongkey" 55 environment = []string{} 56 testString = "Decryption error: invalid padding" 57 CliExecTest(t, command, environment, testString, false) 58 59 // Test decryption using correct key 60 command = "secrets decrypt --file tests/test-secret --secret-key test" 61 environment = []string{} 62 testString = "Decrypting tests/test-secret\nSuccess\n" 63 CliExecTest(t, command, environment, testString, true) 64 65 // Verify file 66 out, _ = exec.Command("bash", "-c", "cat tests/test-secret").CombinedOutput() 67 if string(out) != secretMessage { 68 t.Error("Decrypted file incorrect") 69 } 70 71 // Set different filename for decrypted file 72 command = "secrets decrypt --file tests/test-secret-2 --secret-key test --output-file tests/test-secret" 73 environment = []string{} 74 testString = "Decrypting tests/test-secret-2\nSaving decrypted file to tests/test-secret\nSuccess\n" 75 CliExecTest(t, command, environment, testString, true) 76 77 // Verify file 78 out, _ = exec.Command("bash", "-c", "cat tests/test-secret").CombinedOutput() 79 if string(out) != secretMessage { 80 t.Error("Decrypted file incorrect") 81 } 82 83 // Test multiple file separator 84 command = "secrets decrypt --file 'tests/test-secret1,tests/test-secret2, tests/test-secret3 tests/test-secret4' --secret-key test --debug" 85 environment = []string{} 86 testString = `Decrypting tests/test-secret1 87 ..skipping 88 Decrypting tests/test-secret2 89 ..skipping 90 Decrypting tests/test-secret3 91 ..skipping 92 Decrypting tests/test-secret4 93 ..skipping 94 ` 95 CliExecTest(t, command, environment, testString, true) 96 97 // Change dir back to previous 98 os.Chdir(wd) 99 }