-
Notifications
You must be signed in to change notification settings - Fork 11
/
aws_account_test.go
336 lines (302 loc) · 9.56 KB
/
aws_account_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package cloudhealth
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
var defaultAWSAccount = AwsAccount{
ID: 1234567890,
Name: "test",
}
var defaultPerPage int = 10
func sliceAwsAccountsEqual(a, b []AwsAccount) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
func TestGetAllAwsAccountsOK(t *testing.T) {
var allAWSAccounts []AwsAccount
// Construct a 15 item dummy slice of test AWS accounts
for i := 0; i < 15; i++ {
account := defaultAWSAccount
account.ID = defaultAWSAccount.ID + i
account.Name = fmt.Sprintf("%s-%02d", defaultAWSAccount.Name, i)
//fmt.Printf("%#v\n", account)
allAWSAccounts = append(allAWSAccounts, account)
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if r.Method != "GET" {
t.Errorf("Expected ‘GET’ request, got ‘%s’", r.Method)
}
expectedURL := "/aws_accounts"
if r.URL.EscapedPath() != expectedURL {
t.Errorf("Expected request to ‘%s’, got ‘%s’", expectedURL, r.URL.EscapedPath())
}
body, _ := json.Marshal(AwsAccounts{Accounts: allAWSAccounts})
w.Write(body)
}))
defer ts.Close()
c, err := NewClient("apiKey", ts.URL)
if err != nil {
t.Errorf("NewClient() returned an error: %s", err)
return
}
returnedAwsAccounts, err := c.GetAllAwsAccounts(defaultPerPage)
if err != nil {
t.Errorf("GetAllAwsAccounts() returned an error: %s", err)
return
}
if !sliceAwsAccountsEqual(returnedAwsAccounts, allAWSAccounts) {
got, _ := json.MarshalIndent(allAWSAccounts, "", " ")
expected, _ := json.MarshalIndent(returnedAwsAccounts, "", " ")
t.Errorf("GetAllAwsAccounts()\nexpected: %s\ngot: %s\n", expected, got)
return
}
}
func TestGetAwsAccountOK(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if r.Method != "GET" {
t.Errorf("Expected ‘GET’ request, got ‘%s’", r.Method)
}
expectedURL := fmt.Sprintf("/aws_accounts/%d", defaultAWSAccount.ID)
if r.URL.EscapedPath() != expectedURL {
t.Errorf("Expected request to ‘%s’, got ‘%s’", expectedURL, r.URL.EscapedPath())
}
body, _ := json.Marshal(defaultAWSAccount)
w.Write(body)
}))
defer ts.Close()
c, err := NewClient("apiKey", ts.URL)
if err != nil {
t.Errorf("NewClient() returned an error: %s", err)
return
}
returnedAwsAccount, err := c.GetAwsAccount(defaultAWSAccount.ID)
if err != nil {
t.Errorf("GetAwsAccount() returned an error: %s", err)
return
}
if returnedAwsAccount.ID != defaultAWSAccount.ID {
t.Errorf("GetAwsAccount() expected ID `%d`, got `%d`", defaultAWSAccount.ID, returnedAwsAccount.ID)
return
}
}
func TestGetAwsAccountDoesntExist(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
if r.Method != "GET" {
t.Errorf("Expected ‘GET’ request, got ‘%s’", r.Method)
}
expectedURL := fmt.Sprintf("/aws_accounts/%d", defaultAWSAccount.ID)
if r.URL.EscapedPath() != expectedURL {
t.Errorf("Expected request to ‘%s’, got ‘%s’", expectedURL, r.URL.EscapedPath())
}
}))
defer ts.Close()
c, err := NewClient("apiKey", ts.URL)
if err != nil {
t.Errorf("NewClient() returned an error: %s", err)
return
}
_, err = c.GetAwsAccount(defaultAWSAccount.ID)
if err != ErrAwsAccountNotFound {
t.Errorf("GetAwsAccount() returned the wrong error: %s", err)
return
}
}
func TestCreateAwsAccountOk(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
if r.Method != "POST" {
t.Errorf("Expected ‘POST’ request, got ‘%s’", r.Method)
}
if r.URL.EscapedPath() != "/aws_accounts" {
t.Errorf("Expected request to ‘/aws_accounts, got ‘%s’", r.URL.EscapedPath())
}
if ctype := r.Header.Get("Content-Type"); ctype != "application/json" {
t.Errorf("Expected response to be content-type ‘application/json’, got ‘%s’", ctype)
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Error("Unable to read response body")
}
account := new(AwsAccount)
err = json.Unmarshal(body, &account)
if err != nil {
t.Errorf("Unable to unmarshal AwsAccount, got `%s`", body)
}
if account.Name != "test" {
t.Errorf("Expected request to include AWS Account name ‘test’, got ‘%s’", account.Name)
}
account.ID = 1234567890
js, err := json.Marshal(account)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(js)
}))
defer ts.Close()
c, err := NewClient("apiKey", ts.URL)
if err != nil {
t.Errorf("NewClient() returned an error: %s", err)
return
}
returnedAccount, err := c.CreateAwsAccount(AwsAccount{
Name: "test",
})
if err != nil {
t.Errorf("CreateAwsAccount() returned an error: %s", err)
return
}
if returnedAccount.ID != 1234567890 {
t.Errorf("CreateAwsAccount() expected ID 1234567890, got `%d`", returnedAccount.ID)
return
}
}
func TestUpdateAwsAccountAlreadyExists(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnprocessableEntity)
if r.Method != "POST" {
t.Errorf("Expected ‘POST’ request, got ‘%s’", r.Method)
}
if r.URL.EscapedPath() != "/aws_accounts" {
t.Errorf("Expected request to ‘/aws_accounts, got ‘%s’", r.URL.EscapedPath())
}
if ctype := r.Header.Get("Content-Type"); ctype != "application/json" {
t.Errorf("Expected response to be content-type ‘application/json’, got ‘%s’", ctype)
}
}))
defer ts.Close()
c, err := NewClient("apiKey", ts.URL)
if err != nil {
t.Errorf("NewClient() returned an error: %s", err)
return
}
_, err = c.CreateAwsAccount(AwsAccount{
Name: "test",
})
if err == nil {
t.Errorf("CreateAwsAccount() did not return an error: %s", err)
return
}
}
func TestUpdateAwsAccountOK(t *testing.T) {
updatedAwsAccount := defaultAWSAccount
updatedAwsAccount.Name = "Updated"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if r.Method != "PUT" {
t.Errorf("Expected ‘PUT’ request, got ‘%s’", r.Method)
}
expectedURL := fmt.Sprintf("/aws_accounts/%d", defaultAWSAccount.ID)
if r.URL.EscapedPath() != expectedURL {
t.Errorf("Expected request to ‘%s’, got ‘%s’", expectedURL, r.URL.EscapedPath())
}
body, _ := json.Marshal(updatedAwsAccount)
w.Write(body)
}))
defer ts.Close()
c, err := NewClient("apiKey", ts.URL)
if err != nil {
t.Errorf("NewClient() returned an error: %s", err)
return
}
returnedAwsAccount, err := c.UpdateAwsAccount(updatedAwsAccount)
if err != nil {
t.Errorf("UpdateAwsAccount() returned an error: %s", err)
return
}
if returnedAwsAccount.ID != updatedAwsAccount.ID {
t.Errorf("UpdateAwsAccount() expected ID `%d`, got `%d`", defaultAWSAccount.ID, returnedAwsAccount.ID)
return
}
if returnedAwsAccount.Name == defaultAWSAccount.Name {
t.Errorf("UpdateAwsAccount() did not update the name")
return
}
}
func TestUpdateAwsAccountNameConflict(t *testing.T) {
updatedAwsAccount := defaultAWSAccount
updatedAwsAccount.Name = "Updated"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnprocessableEntity)
if r.Method != "PUT" {
t.Errorf("Expected ‘PUT’ request, got ‘%s’", r.Method)
}
expectedURL := fmt.Sprintf("/aws_accounts/%d", defaultAWSAccount.ID)
if r.URL.EscapedPath() != expectedURL {
t.Errorf("Expected request to ‘%s’, got ‘%s’", expectedURL, r.URL.EscapedPath())
}
body, _ := json.Marshal(updatedAwsAccount)
w.Write(body)
}))
defer ts.Close()
c, err := NewClient("apiKey", ts.URL)
if err != nil {
t.Errorf("NewClient() returned an error: %s", err)
return
}
_, err = c.UpdateAwsAccount(updatedAwsAccount)
if err == nil {
t.Errorf("UpdateAwsAccount() did not return an error: %s", err)
return
}
}
func TestDeleteAwsAccountOK(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if r.Method != "DELETE" {
t.Errorf("Expected ‘DELETE’ request, got ‘%s’", r.Method)
}
expectedURL := fmt.Sprintf("/aws_accounts/%d", defaultAWSAccount.ID)
if r.URL.EscapedPath() != expectedURL {
t.Errorf("Expected request to ‘%s’, got ‘%s’", expectedURL, r.URL.EscapedPath())
}
}))
defer ts.Close()
c, err := NewClient("apiKey", ts.URL)
if err != nil {
t.Errorf("NewClient() returned an error: %s", err)
return
}
err = c.DeleteAwsAccount(defaultAWSAccount.ID)
if err != nil {
t.Errorf("DeleteAwsAccount() returned an error: %s", err)
return
}
}
func TestDeleteAwsAccountDoesntExist(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
if r.Method != "DELETE" {
t.Errorf("Expected ‘DELETE’ request, got ‘%s’", r.Method)
}
expectedURL := fmt.Sprintf("/aws_accounts/%d", defaultAWSAccount.ID)
if r.URL.EscapedPath() != expectedURL {
t.Errorf("Expected request to ‘%s’, got ‘%s’", expectedURL, r.URL.EscapedPath())
}
}))
defer ts.Close()
c, err := NewClient("apiKey", ts.URL)
if err != nil {
t.Errorf("NewClient() returned an error: %s", err)
return
}
err = c.DeleteAwsAccount(defaultAWSAccount.ID)
if err != ErrAwsAccountNotFound {
t.Errorf("DeleteAwsAccount() returned the wrong error: %s", err)
return
}
}