forked from rlmcpherson/s3gof3r
-
Notifications
You must be signed in to change notification settings - Fork 3
/
auth_test.go
63 lines (55 loc) · 1.23 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
package s3gof3r
import (
"os"
"strings"
"testing"
)
var testKeys = Keys{
AccessKey: "AKIDEXAMPLE",
SecretKey: "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
}
var testTokenKeys = Keys{
AccessKey: "AKIDEXAMPLE",
SecretKey: "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
SecurityToken: "testtoken",
}
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 TestEnvKeys(t *testing.T) {
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 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()
}