This repository has been archived by the owner on Mar 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
credentials_test.go
192 lines (173 loc) · 5.63 KB
/
credentials_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"os"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
// "io/ioutil"
// "fmt"
)
type TestWriter struct {
Written []byte
}
func (t *TestWriter) Write(p []byte) (n int, err error) {
t.Written = p
return 0, nil
}
type TestFileList struct {
testList []os.FileInfo
}
func (t *TestFileList) Readdir(n int) ([]os.FileInfo, error) {
return t.testList, nil
}
func (t *TestFileList) Name() string {
return "foo"
}
type TestFileInfo struct {
isDir bool
name string
}
func (t *TestFileInfo) IsDir() bool {
return t.isDir
}
func (t *TestFileInfo) Name() string {
return t.name
}
func (t *TestFileInfo) Size() int64 {
return 0
}
func (t *TestFileInfo) Mode() os.FileMode {
return 0
}
func (t *TestFileInfo) ModTime() time.Time {
return time.Now()
}
func (t *TestFileInfo) Sys() interface{} {
return nil
}
func TestGetDirs(t *testing.T) {
Convey("Test finding all dirs", t, func() {
Convey("Test with nothing", func() {
t := TestFileList{}
ents, err := getDirs(&t)
So(err, ShouldEqual, nil)
So(len(ents), ShouldEqual, 0)
})
Convey("Test with files only", func() {
i := []os.FileInfo{}
i = append(i, &TestFileInfo{isDir: false})
i = append(i, &TestFileInfo{isDir: false})
i = append(i, &TestFileInfo{isDir: false})
t := TestFileList{testList: i}
ents, err := getDirs(&t)
So(err, ShouldEqual, nil)
So(len(ents), ShouldEqual, 0)
})
Convey("Test with one dir", func() {
i := []os.FileInfo{}
i = append(i, &TestFileInfo{isDir: true})
t := TestFileList{testList: i}
ents, err := getDirs(&t)
So(err, ShouldEqual, nil)
So(len(ents), ShouldEqual, 1)
})
Convey("Test with multiple dirs", func() {
i := []os.FileInfo{}
i = append(i, &TestFileInfo{isDir: true})
i = append(i, &TestFileInfo{isDir: true})
i = append(i, &TestFileInfo{isDir: true})
i = append(i, &TestFileInfo{isDir: true})
t := TestFileList{testList: i}
ents, err := getDirs(&t)
So(err, ShouldEqual, nil)
So(len(ents), ShouldEqual, 4)
})
})
}
func TestFindDefaultDir(t *testing.T) {
Convey("Test Finding Default Dirs", t, func() {
Convey("With no files or directories", func() {
t := TestFileList{}
_, err := findDefaultDir(&t)
So(err.Error(), ShouldEqual, "No saved credentials found; please run 'credulous save' first")
})
Convey("With one file and no directories", func() {
i := []os.FileInfo{}
i = append(i, &TestFileInfo{isDir: false})
t := TestFileList{testList: i}
_, err := findDefaultDir(&t)
So(err, ShouldNotEqual, nil)
So(err.Error(), ShouldEqual, "No saved credentials found; please run 'credulous save' first")
})
Convey("With one file and one directory", func() {
i := []os.FileInfo{}
i = append(i, &TestFileInfo{isDir: false})
i = append(i, &TestFileInfo{isDir: true, name: "foo"})
t := TestFileList{testList: i}
name, err := findDefaultDir(&t)
So(err, ShouldEqual, nil)
So(name, ShouldEqual, "foo")
})
Convey("With no files and more than one directory", func() {
i := []os.FileInfo{}
i = append(i, &TestFileInfo{isDir: true, name: "foo"})
i = append(i, &TestFileInfo{isDir: true, name: "bar"})
i = append(i, &TestFileInfo{isDir: true, name: "baz"})
t := TestFileList{testList: i}
_, err := findDefaultDir(&t)
So(err, ShouldNotEqual, nil)
So(err.Error(), ShouldEqual, "More than one account found; please specify account and user")
})
})
}
func TestValidateCredentials(t *testing.T) {
Convey("Test credential validation", t, func() {
// we can't really test ValidateCredentials directly,
// because it calls verifyUserAndAccount, which
// creates its own IAM connection. This is probably not
// the best way to have implemented that function.
// goamz provides an iamtest package, and we should
// use that.
})
}
func TestReadFile(t *testing.T) {
Convey("Test Read File", t, func() {
Convey("Valid old Json returns Credential", func() {
cred, _ := readCredentialFile("testdata/credential.json", "testdata/testkey")
So(cred.LifeTime, ShouldEqual, 22)
So(cred.Encryptions[0].decoded.KeyId, ShouldEqual, "some plaintext")
})
Convey("Old credentials display correctly", func() {
cred, _ := readCredentialFile("testdata/credential.json", "testdata/testkey")
testWriter := TestWriter{}
cred.Display(&testWriter)
So(string(testWriter.Written), ShouldEqual, "export AWS_ACCESS_KEY_ID=\"some plaintext\"\nexport AWS_SECRET_ACCESS_KEY=\"some plaintext\"\n")
})
Convey("Valid new Json returns Credentials", func() {
cred, err := readCredentialFile("testdata/newcreds.json", "testdata/testkey")
So(err, ShouldEqual, nil)
So(cred.LifeTime, ShouldEqual, 0)
So(cred.CreateTime, ShouldEqual, "1401515273")
So(cred.Encryptions[0].Fingerprint, ShouldEqual, "c0:61:84:fc:e8:c9:52:dc:cd:a9:8e:82:a2:70:0a:30")
So(cred.Encryptions[0].decoded.KeyId, ShouldEqual, "plaintextkeyid")
So(cred.Encryptions[0].decoded.SecretKey, ShouldEqual, "plaintextsecret")
})
Convey("New credentials display correctly", func() {
cred, err := readCredentialFile("testdata/newcreds.json", "testdata/testkey")
testWriter := TestWriter{}
cred.Display(&testWriter)
So(string(testWriter.Written), ShouldEqual, "export AWS_ACCESS_KEY_ID=\"plaintextkeyid\"\nexport AWS_SECRET_ACCESS_KEY=\"plaintextsecret\"\n")
So(err, ShouldEqual, nil)
})
})
}
func TestListAvailableCreds(t *testing.T) {
Convey("Test listing available credentials", t, func() {
Convey("Test with no credentials", func() {
tmp := TestFileList{}
creds, err := listAvailableCredentials(&tmp)
So(len(creds), ShouldEqual, 0)
So(err.Error(), ShouldEqual, "No saved credentials found; please run 'credulous save' first")
})
})
}