-
Notifications
You must be signed in to change notification settings - Fork 15
/
env_test.go
190 lines (145 loc) · 4.45 KB
/
env_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package ejson2env
import (
"bytes"
"fmt"
"testing"
)
const TestKeyValue = "2ed65dd6a16eab833cc4d2a860baa60042da34a58ac43855e8554ca87a5e557d"
func formatInvalid(received, expected string) string {
return fmt.Sprintf("generated invalid export code: \n---\n%s\n---\nshould be: \n---\n%s\n---\n", received, expected)
}
func TestLoadSecrets(t *testing.T) {
rawValues, err := readSecrets("testdata/test-expected-usage.ejson", "./key", TestKeyValue)
if nil != err {
t.Fatal(err)
}
envValues, err := ExtractEnv(rawValues)
if nil != err {
t.Fatal(err)
}
if "test value" != envValues["test_key"] {
t.Error("Failed to decrypt")
}
}
func TestLoadNoEnvSecrets(t *testing.T) {
rawValues, err := readSecrets("testdata/test-public-key-only.ejson", "./key", TestKeyValue)
if nil != err {
t.Fatal(err)
}
_, err = ExtractEnv(rawValues)
if errNoEnv != err {
t.Fatal(err)
}
if !IsEnvError(err) {
t.Fatalf("shouldn't have caused a failure: %s", err)
}
}
func TestLoadBadEnvSecrets(t *testing.T) {
rawValues, err := readSecrets("testdata/test-environment-string-not-object.ejson", "./key", TestKeyValue)
if nil != err {
t.Fatal(err)
}
_, err = ExtractEnv(rawValues)
if errEnvNotMap != err {
t.Fatal(err)
}
if !IsEnvError(err) {
t.Fatalf("shouldn't have caused a failure: %s", err)
}
}
func TestLoadUnderscoreEnvSecrets(t *testing.T) {
rawValues, err := readSecrets("testdata/test-leading-underscore-env-key.ejson", "./key", TestKeyValue)
if nil != err {
t.Fatal(err)
}
envValues, err := ExtractEnv(rawValues)
if nil != err {
t.Fatal(err)
}
if "test value" != envValues["_test_key"] {
t.Error("Failed to decrypt")
}
}
func TestInvalidEnvironments(t *testing.T) {
testGood := map[string]interface{}{
"environment": map[string]interface{}{
"test_key": "test_value",
},
}
testBadNonMap := map[string]interface{}{
"environment": "bad",
}
testBadInvalidKey := map[string]interface{}{
"environment": map[string]interface{}{
"invalid key": "test_value",
},
}
var testNoEnv map[string]interface{}
_, err := ExtractEnv(testBadNonMap)
if nil == err {
t.Errorf("no error when passed a non-map environment")
} else if errEnvNotMap != err {
t.Errorf("wrong error when passed a non-map environment: %s", err)
}
_, err = ExtractEnv(testBadInvalidKey)
if nil == err {
t.Errorf("no error when passed an environment with invalid key")
} else if `invalid identifier as key in environment: "invalid key"` != err.Error() {
t.Errorf("wrong error when passed an environment with invalid key: %s", err)
}
_, err = ExtractEnv(testNoEnv)
if nil == err {
t.Errorf("no error when passed a non-existent environment")
} else if errNoEnv != err {
t.Errorf("wrong error when passed a non-existent environment: %s", err)
}
_, err = ExtractEnv(testGood)
if nil != err {
t.Errorf("error when passed correctly formatted environment: %s", err)
}
}
func TestEscaping(t *testing.T) {
buf := bytes.Buffer{}
testValues := map[string]string{
"test": "test value'; echo dangerous; echo 'done",
}
ExportEnv(&buf, testValues)
expectedOutput := "export test='test value'\\''; echo dangerous; echo '\\''done'\n"
if expectedOutput != buf.String() {
t.Fatal(formatInvalid(buf.String(), expectedOutput))
}
}
func TestIdentifierPattern(t *testing.T) {
key := "ALL_CAPS123"
if !validIdentifierPattern.MatchString(key) {
t.Errorf("key should match pattern %q: %q", validIdentifierPattern, key)
}
key = "lowercase"
if !validIdentifierPattern.MatchString(key) {
t.Errorf("key should match pattern %q: %q", validIdentifierPattern, key)
}
key = "a"
if !validIdentifierPattern.MatchString(key) {
t.Errorf("key should match pattern %q: %q", validIdentifierPattern, key)
}
key = "_leading_underscore"
if !validIdentifierPattern.MatchString(key) {
t.Errorf("key should match pattern %q: %q", validIdentifierPattern, key)
}
key = "1_leading_digit"
if validIdentifierPattern.MatchString(key) {
t.Errorf("key should not match pattern %q: %q", validIdentifierPattern, key)
}
key = "contains whitespace"
if validIdentifierPattern.MatchString(key) {
t.Errorf("key should not match pattern %q: %q", validIdentifierPattern, key)
}
key = "contains-dash"
if validIdentifierPattern.MatchString(key) {
t.Errorf("key should not match pattern %q: %q", validIdentifierPattern, key)
}
key = "contains_special_character;"
if validIdentifierPattern.MatchString(key) {
t.Errorf("key should not match pattern %q: %q", validIdentifierPattern, key)
}
}