forked from sendgridlabs/go-kinesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_test.go
118 lines (93 loc) · 2.91 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
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
package kinesis
import (
"os"
"testing"
)
func TestAuthInterfaceIsImplemented(t *testing.T) {
var auth Auth = &AuthCredentials{}
if auth == nil {
t.Error("Invalid nil auth credentials value")
}
}
func TestGetSecretKey(t *testing.T) {
auth := NewAuth("BAD_ACCESS_KEY", "BAD_SECRET_KEY", "BAD_SECURITY_TOKEN")
if auth.GetAccessKey() != "BAD_ACCESS_KEY" {
t.Error("incorrect value for auth#accessKey")
}
}
func TestGetAccessKey(t *testing.T) {
auth := NewAuth("BAD_ACCESS_KEY", "BAD_SECRET_KEY", "BAD_SECURITY_TOKEN")
if auth.GetSecretKey() != "BAD_SECRET_KEY" {
t.Error("incorrect value for auth#secretKey")
}
}
func TestGetToken(t *testing.T) {
auth := NewAuth("BAD_ACCESS_KEY", "BAD_SECRET_KEY", "BAD_SECURITY_TOKEN")
if auth.GetToken() != "BAD_SECURITY_TOKEN" {
t.Error("incorrect value for auth#token")
}
}
func TestNewAuthFromEnv(t *testing.T) {
os.Setenv(AccessEnvKey, "asdf")
os.Setenv(SecretEnvKey, "asdf2")
os.Setenv(SecurityTokenEnvKey, "dummy_token")
// Validate that the fallback environment variables will also work
defer os.Unsetenv(AccessEnvKey)
defer os.Unsetenv(SecretEnvKey)
defer os.Unsetenv(SecurityTokenEnvKey)
auth, _ := NewAuthFromEnv()
if auth.GetAccessKey() != "asdf" {
t.Error("Expected AccessKey to be inferred as \"asdf\"")
}
if auth.GetSecretKey() != "asdf2" {
t.Error("Expected SecretKey to be inferred as \"asdf2\"")
}
if auth.GetToken() != "dummy_token" {
t.Error("Expected SecurityToken to be inferred as \"dummy_token\"")
}
}
func TestNewAuthFromEnvWithoutSecurityToken(t *testing.T) {
os.Setenv(AccessEnvKey, "asdf")
os.Setenv(SecretEnvKey, "asdf2")
os.Unsetenv(SecurityTokenEnvKey)
// Validate that the fallback environment variables will also work
defer os.Unsetenv(AccessEnvKey)
defer os.Unsetenv(SecretEnvKey)
auth, _ := NewAuthFromEnv()
if auth.GetAccessKey() != "asdf" {
t.Error("Expected AccessKey to be inferred as \"asdf\"")
}
if auth.GetSecretKey() != "asdf2" {
t.Error("Expected SecretKey to be inferred as \"asdf2\"")
}
if auth.GetToken() != "" {
t.Error("Expected SecurityToken to be an empty string")
}
}
func TestNewAuthFromEnvWithoutVars(t *testing.T) {
os.Unsetenv(AccessEnvKey)
os.Unsetenv(SecretEnvKey)
os.Unsetenv(SecurityTokenEnvKey)
auth, err := NewAuthFromEnv()
if auth != nil {
t.Error("Expected auth instance to be nil but was non-nil")
}
if err == nil {
t.Error("Expected error to be non-nil but was nil")
}
}
func TestNewAuthFromEnvWithFallbackVars(t *testing.T) {
os.Setenv(AccessEnvKeyId, "asdf")
os.Setenv(SecretEnvAccessKey, "asdf2")
os.Setenv(SecurityTokenEnvKey, "dummy_token")
defer os.Unsetenv(AccessEnvKey)
defer os.Unsetenv(SecretEnvKey)
defer os.Unsetenv(SecurityTokenEnvKey)
auth, _ := NewAuthFromEnv()
if auth.GetAccessKey() != "asdf" {
t.Error("Expected AccessKey to be inferred as \"asdf\"")
}
if auth.GetSecretKey() != "asdf2" {
t.Error("Expected SecretKey to be inferred as \"asdf2\"")
}
}