Skip to content

Commit 4c7c59f

Browse files
committed
add test for loading master key from env
1 parent 16c051b commit 4c7c59f

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

cmd/main

-2.56 MB
Binary file not shown.

config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ func (s *sicher) configure() {
1414
return
1515
}
1616
// read the encryption key
17-
1817
strKey := os.Getenv(masterKey)
1918
if strKey == "" {
2019
key, err := os.ReadFile(fmt.Sprintf("%s%s.key", s.Path, s.Environment))

dev.enc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
468e7c2c4da109de95c301e495e45e64467c20e02f0ac5c1a8bf696c4f6d5f7a860329==--==61aa2a052eca6690271d721d
1+
885d229381a64e84fea94463547ea607f2be6eecc155c688d4270dc90b217e16b0979e==--==bf775cc0dcd5876084419e90

sicher_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,46 @@ func TestLoadEnv(t *testing.T) {
310310

311311
}
312312

313+
func TestLoadEnv_KeyInEnv(t *testing.T) {
314+
315+
s, encPath, keyPath := setupTest()
316+
317+
s.Initialize(os.Stdin) // created key will not be used as a file
318+
319+
key, err := os.ReadFile(keyPath)
320+
if err != nil {
321+
t.Errorf("Expected key file to have been created; got error %v", err)
322+
}
323+
os.Remove(keyPath) // remove key file
324+
325+
f, err := os.Open(encPath)
326+
if err != nil {
327+
t.Errorf("Expected credential file to have been created; got error %v", err)
328+
}
329+
330+
os.Setenv(masterKey, string(key))
331+
mp := make(map[string]string)
332+
err = s.LoadEnv("", &mp)
333+
if err != nil {
334+
t.Errorf("Expected to load envirnoment variables; got error %v", err)
335+
}
336+
337+
if len(mp) != 1 {
338+
t.Errorf("Expected config file to be been populated with env variables")
339+
}
340+
341+
// get path to the gitignore file and cleanup
342+
gitPath := strings.Replace(encPath, fmt.Sprintf("%s.enc", s.Environment), ".gitignore", 1)
343+
344+
t.Cleanup(func() {
345+
os.Remove(encPath)
346+
os.Remove(keyPath)
347+
os.Remove(gitPath)
348+
f.Close()
349+
})
350+
351+
}
352+
313353
func TestSetEnv(t *testing.T) {
314354
s, _, _ := setupTest()
315355

0 commit comments

Comments
 (0)