-
Notifications
You must be signed in to change notification settings - Fork 181
/
auth_test.go
77 lines (70 loc) · 1.64 KB
/
auth_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
package s3gof3r
import (
"os"
"strings"
"testing"
)
type authT struct {
env []string
}
// save current environment for restoration
func (s *authT) saveEnv() {
s.env = os.Environ()
os.Clearenv()
}
// restore environment after each test
func (s *authT) restoreEnv() {
os.Clearenv()
for _, kv := range s.env {
l := strings.SplitN(kv, "=", 2)
os.Setenv(l[0], l[1])
}
}
func TestEnvKeysWithoutToken(t *testing.T) {
testKeys := Keys{
AccessKey: "AKIDEXAMPLE",
SecretKey: "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
}
s := authT{}
s.saveEnv()
os.Setenv("AWS_ACCESS_KEY_ID", testKeys.AccessKey)
os.Setenv("AWS_SECRET_ACCESS_KEY", testKeys.SecretKey)
keys, err := EnvKeys()
if err != nil {
t.Error(err)
}
if keys != testKeys {
t.Errorf("Keys do not match. Expected: %v. Actual: %v", testKeys, keys)
}
s.restoreEnv()
}
func TestEnvKeyWithToken(t *testing.T) {
testKeys := Keys{
AccessKey: "AKIDEXAMPLE",
SecretKey: "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
SecurityToken: "testtoken",
}
s := authT{}
s.saveEnv()
os.Setenv("AWS_ACCESS_KEY_ID", testKeys.AccessKey)
os.Setenv("AWS_SECRET_ACCESS_KEY", testKeys.SecretKey)
os.Setenv("AWS_SECURITY_TOKEN", testKeys.SecurityToken)
keys, err := EnvKeys()
if err != nil {
t.Error(err)
}
if keys != testKeys {
t.Errorf("Keys do not match. Expected: %v. Actual: %v", testKeys, keys)
}
s.restoreEnv()
}
func TestEnvKeysNotSet(t *testing.T) {
s := authT{}
s.saveEnv()
_, err := EnvKeys()
expErr := "keys not set in environment: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY"
if err.Error() != expErr {
t.Errorf("Expected error: %v. Actual: %v", expErr, err)
}
s.restoreEnv()
}