-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin_api_integration_test.go
330 lines (309 loc) · 7.34 KB
/
admin_api_integration_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
// This file is part of GuinsooLab Console Server
// Copyright (c) 2020-2022 GuinsooLab, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// These tests are for AdminAPI Tag based on swagger-console.yml
package integration
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"testing"
"time"
"github.com/GuinsooLab/console/models"
"github.com/stretchr/testify/assert"
)
func RestartService() (*http.Response, error) {
/*
Helper function to restart service
HTTP Verb: POST
URL: /api/v1/service/restart
*/
request, err := http.NewRequest(
"POST",
"http://localhost:9090/api/v1/service/restart",
nil,
)
if err != nil {
log.Println(err)
}
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
request.Header.Add("Content-Type", "application/json")
client := &http.Client{
Timeout: 2000 * time.Second, // increased timeout since restart takes time, more than other APIs.
}
response, err := client.Do(request)
return response, err
}
func GetNodes() (*http.Response, error) {
/*
Helper function to get nodes
HTTP Verb: GET
URL: /api/v1/nodes
*/
request, err := http.NewRequest(
"GET",
"http://localhost:9090/api/v1/nodes",
nil,
)
if err != nil {
log.Println(err)
}
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
request.Header.Add("Content-Type", "application/json")
client := &http.Client{
Timeout: 2000 * time.Second, // increased timeout since restart takes time, more than other APIs.
}
response, err := client.Do(request)
return response, err
}
func NotifyPostgres() (*http.Response, error) {
/*
Helper function to add Postgres Notification
HTTP Verb: PUT
URL: api/v1/configs/notify_postgres
Body:
{
"key_values":[
{
"key":"connection_string",
"value":"user=postgres password=password host=localhost dbname=postgres port=5432 sslmode=disable"
},
{
"key":"table",
"value":"accountsssss"
},
{
"key":"format",
"value":"namespace"
},
{
"key":"queue_limit",
"value":"10000"
},
{
"key":"comment",
"value":"comment"
}
]
}
*/
Body := models.SetConfigRequest{
KeyValues: []*models.ConfigurationKV{
{
Key: "connection_string",
Value: "user=postgres password=password host=173.18.0.3 dbname=postgres port=5432 sslmode=disable",
},
{
Key: "table",
Value: "accountsssss",
},
{
Key: "format",
Value: "namespace",
},
{
Key: "queue_limit",
Value: "10000",
},
{
Key: "comment",
Value: "comment",
},
},
}
requestDataJSON, _ := json.Marshal(Body)
requestDataBody := bytes.NewReader(requestDataJSON)
request, err := http.NewRequest(
"PUT",
"http://localhost:9090/api/v1/configs/notify_postgres",
requestDataBody,
)
if err != nil {
log.Println(err)
}
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
request.Header.Add("Content-Type", "application/json")
client := &http.Client{
Timeout: 2 * time.Second,
}
response, err := client.Do(request)
return response, err
}
func TestNotifyPostgres(t *testing.T) {
// Variables
assert := assert.New(t)
// Test
response, err := NotifyPostgres()
finalResponse := inspectHTTPResponse(response)
assert.Nil(err)
if err != nil {
log.Println(err)
assert.Fail(finalResponse)
return
}
if response != nil {
assert.Equal(200, response.StatusCode, finalResponse)
}
}
func TestRestartService(t *testing.T) {
assert := assert.New(t)
restartResponse, restartError := RestartService()
assert.Nil(restartError)
if restartError != nil {
log.Println(restartError)
return
}
addObjRsp := inspectHTTPResponse(restartResponse)
if restartResponse != nil {
assert.Equal(
204,
restartResponse.StatusCode,
addObjRsp,
)
}
}
func ListPoliciesWithBucket(bucketName string) (*http.Response, error) {
/*
Helper function to List Policies With Given Bucket
HTTP Verb: GET
URL: /bucket-policy/{bucket}
*/
request, err := http.NewRequest(
"GET", "http://localhost:9090/api/v1/bucket-policy/"+bucketName, nil)
if err != nil {
log.Println(err)
}
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
request.Header.Add("Content-Type", "application/json")
client := &http.Client{
Timeout: 2 * time.Second,
}
response, err := client.Do(request)
return response, err
}
func TestListPoliciesWithBucket(t *testing.T) {
// Test Variables
bucketName := "testlistpolicieswithbucket"
assert := assert.New(t)
// Test
response, err := ListPoliciesWithBucket(bucketName)
assert.Nil(err)
if err != nil {
log.Println(err)
return
}
parsedResponse := inspectHTTPResponse(response)
if response != nil {
assert.Equal(
200,
response.StatusCode,
parsedResponse,
)
}
}
func ListUsersWithAccessToBucket(bucketName string) (*http.Response, error) {
/*
Helper function to List Users With Access to a Given Bucket
HTTP Verb: GET
URL: /bucket-users/{bucket}
*/
request, err := http.NewRequest(
"GET", "http://localhost:9090/api/v1/bucket-users/"+bucketName, nil)
if err != nil {
log.Println(err)
}
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
request.Header.Add("Content-Type", "application/json")
client := &http.Client{
Timeout: 2 * time.Second,
}
response, err := client.Do(request)
return response, err
}
func TestListUsersWithAccessToBucket(t *testing.T) {
// Test Variables
bucketName := "testlistuserswithaccesstobucket1"
assert := assert.New(t)
// Test
response, err := ListUsersWithAccessToBucket(bucketName)
assert.Nil(err)
if err != nil {
log.Println(err)
return
}
parsedResponse := inspectHTTPResponse(response)
if response != nil {
assert.Equal(
200,
response.StatusCode,
parsedResponse,
)
}
}
func TestGetNodes(t *testing.T) {
assert := assert.New(t)
getNodesResponse, getNodesError := GetNodes()
assert.Nil(getNodesError)
if getNodesError != nil {
log.Println(getNodesError)
return
}
addObjRsp := inspectHTTPResponse(getNodesResponse)
if getNodesResponse != nil {
assert.Equal(
200,
getNodesResponse.StatusCode,
addObjRsp,
)
}
}
func ArnList() (*http.Response, error) {
/*
Helper function to get arn list
HTTP Verb: GET
URL: /api/v1/admin/arns
*/
request, err := http.NewRequest(
"GET", "http://localhost:9090/api/v1/admin/arns", nil)
if err != nil {
log.Println(err)
}
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
request.Header.Add("Content-Type", "application/json")
client := &http.Client{
Timeout: 2 * time.Second,
}
response, err := client.Do(request)
return response, err
}
func TestArnList(t *testing.T) {
assert := assert.New(t)
resp, err := ArnList()
assert.Nil(err)
if err != nil {
log.Println(err)
return
}
objRsp := inspectHTTPResponse(resp)
if resp != nil {
assert.Equal(
200,
resp.StatusCode,
objRsp,
)
}
}