-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgodotenvvault_test.go
80 lines (68 loc) · 2.16 KB
/
godotenvvault_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package godotenvvault
import (
"os"
"strings"
"testing"
)
type testKey struct {
key string
ok bool
environmentKey string
}
var TEST_KEYS = []testKey{
{"dotenv://:key_1234@dotenv.org/vault/.env.vault?environment=production", true, "DOTENV_VAULT_PRODUCTION"},
{"dotenv://:key_0dec82bea24ada79a983dcc11b431e28838eae59a07a8f983247c7ca9027a925@dotenv.local/vault/.env.vault?environment=development", true, "DOTENV_VAULT_DEVELOPMENT"},
// Missing key value.
{"dotenv://dotenv.org/vault/.env.vault?environment=production", false, "DOTENV_VAULT_PRODUCTION"},
// Missing environment.
{"dotenv://:key_1234@dotenv.org/vault/.env.vault", false, ""},
}
func TestKeyParsing(t *testing.T) {
for itest, test := range TEST_KEYS {
key, err := parseKey(test.key)
if !test.ok {
if err == nil {
t.Errorf("Parse should have failed but didn't! (test key #%d)", itest+1)
}
continue
}
if err != nil {
t.Errorf("Parse failed (test key #%d): %v", itest+1, err)
continue
}
if key.environmentKey != test.environmentKey {
t.Errorf("Parse failed (test key #%d): bad environment key = '%s'", itest+1, key.environmentKey)
}
}
}
const PARSE_TEST_KEY = "dotenv://:key_0dec82bea24ada79a983dcc11b431e28838eae59a07a8f983247c7ca9027a925@dotenv.local/vault/.env.vault?environment=development"
const PARSE_TEST_VAULT = `# .env.vault (generated with npx dotenv-vault local build)
DOTENV_VAULT_DEVELOPMENT="H2A2wOUZU+bjKH3kTpeua9iIhtK/q7/VpAn+LLVNnms+CtQ/cwXqiw=="
`
func TestVaultParsing(t *testing.T) {
// Do this manually instead of using t.Setenv to support Go 1.16.
// (testing.Setenv was introduced in 1.17)
key := "DOTENV_KEY"
prevValue, ok := os.LookupEnv(key)
if err := os.Setenv(key, PARSE_TEST_KEY); err != nil {
t.Fatalf("cannot set environment variable: %v", err)
}
if ok {
t.Cleanup(func() {
os.Setenv(key, prevValue)
})
} else {
t.Cleanup(func() {
os.Unsetenv(key)
})
}
vaultReader := strings.NewReader(PARSE_TEST_VAULT)
vals, err := Parse(vaultReader)
if err != nil {
t.Errorf("Parse failed: %v", err)
}
check, ok := vals["HELLO"]
if !ok || check != "world" {
t.Errorf("Parse returned invalid contents: %v", vals)
}
}