-
Notifications
You must be signed in to change notification settings - Fork 1
/
secretstring_test.go
49 lines (43 loc) · 1.36 KB
/
secretstring_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
package secretstring
import (
"fmt"
"testing"
)
type getSecretStringFunc func(project, name string) (string, error)
func newTestClient(get getSecretStringFunc) *testClient {
return &testClient{getsecretstring: get}
}
type testClient struct {
getsecretstring func(project, name string) (string, error)
}
func (t *testClient) getSecretString(project, secretName string) (string, error) {
if t.getsecretstring != nil {
return t.getsecretstring(project, secretName)
}
return "I'm an important string", nil
}
func TestSecretString_UnmarshalText(t *testing.T) {
t.Run("test successful retrieval", func(t *testing.T) {
tc := newTestClient(func(project, name string) (string, error) {
return "IAMASECUREPASWORD", nil
})
secretManagerClient = tc
secretText := SecretString("")
err := secretText.UnmarshalText([]byte("ENCRYPTEDPASSWORD"))
if err != nil {
t.Error("Error retrieving secret:", err)
}
t.Log("Retrieved secret ", secretText)
})
t.Run("test failed retrieval", func(t *testing.T) {
tc := newTestClient(func(project, name string) (string, error) {
return "", fmt.Errorf("unable to retrieve secret for project %s, name %s", project, name)
})
secretManagerClient = tc
secretText := SecretString("")
err := secretText.UnmarshalText([]byte("ENCRYPTEDPASSWORD"))
if err == nil {
t.Error("Should of received an error but did not.")
}
})
}