Skip to content

Commit dc60d0c

Browse files
committed
more groups tests
1 parent 4001f14 commit dc60d0c

File tree

1 file changed

+204
-13
lines changed

1 file changed

+204
-13
lines changed

integration/groups_test.go

Lines changed: 204 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ func Test_AddGroupAPI(t *testing.T) {
3434
AddUser("member1", "testtest", []string{}, []string{"consoleAdmin"})
3535

3636
type args struct {
37-
api string
3837
group string
3938
members []string
4039
}
@@ -47,7 +46,6 @@ func Test_AddGroupAPI(t *testing.T) {
4746
{
4847
name: "Create Group - Valid",
4948
args: args{
50-
api: "/groups",
5149
group: "test",
5250
members: []string{"member1"},
5351
},
@@ -57,7 +55,6 @@ func Test_AddGroupAPI(t *testing.T) {
5755
{
5856
name: "Create Group - Invalid",
5957
args: args{
60-
api: "/groups",
6158
group: "test",
6259
members: []string{},
6360
},
@@ -73,16 +70,203 @@ func Test_AddGroupAPI(t *testing.T) {
7370
Timeout: 3 * time.Second,
7471
}
7572

76-
// Add policy
77-
7873
requestDataPolicy := map[string]interface{}{}
7974
requestDataPolicy["group"] = tt.args.group
8075
requestDataPolicy["members"] = tt.args.members
8176

8277
requestDataJSON, _ := json.Marshal(requestDataPolicy)
8378
requestDataBody := bytes.NewReader(requestDataJSON)
8479
request, err := http.NewRequest(
85-
"POST", fmt.Sprintf("http://localhost:9090/api/v1%s", tt.args.api), requestDataBody)
80+
"POST", "http://localhost:9090/api/v1/groups", requestDataBody)
81+
if err != nil {
82+
log.Println(err)
83+
return
84+
}
85+
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
86+
request.Header.Add("Content-Type", "application/json")
87+
response, err := client.Do(request)
88+
if err != nil {
89+
log.Println(err)
90+
return
91+
}
92+
if response != nil {
93+
assert.Equal(tt.expectedStatus, response.StatusCode, "Status Code is incorrect")
94+
}
95+
96+
})
97+
}
98+
99+
}
100+
101+
func Test_GetGroupAPI(t *testing.T) {
102+
assert := assert.New(t)
103+
104+
AddUser("member2", "testtest", []string{}, []string{"consoleAdmin"})
105+
AddGroup("getgroup1", []string{"member2"})
106+
107+
type args struct {
108+
api string
109+
}
110+
tests := []struct {
111+
name string
112+
args args
113+
expectedStatus int
114+
expectedError error
115+
}{
116+
{
117+
name: "Get Group - Valid",
118+
args: args{
119+
api: "?name=getgroup1",
120+
},
121+
expectedStatus: 200,
122+
expectedError: nil,
123+
},
124+
{
125+
name: "Get Group - Invalid",
126+
args: args{
127+
api: "?name=askfjalkd",
128+
},
129+
expectedStatus: 500,
130+
expectedError: nil,
131+
},
132+
}
133+
134+
for _, tt := range tests {
135+
t.Run(tt.name, func(t *testing.T) {
136+
137+
client := &http.Client{
138+
Timeout: 3 * time.Second,
139+
}
140+
141+
requestDataPolicy := map[string]interface{}{}
142+
143+
requestDataJSON, _ := json.Marshal(requestDataPolicy)
144+
requestDataBody := bytes.NewReader(requestDataJSON)
145+
request, err := http.NewRequest(
146+
"GET", fmt.Sprintf("http://localhost:9090/api/v1/group%s", tt.args.api), requestDataBody)
147+
if err != nil {
148+
log.Println(err)
149+
return
150+
}
151+
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
152+
request.Header.Add("Content-Type", "application/json")
153+
response, err := client.Do(request)
154+
if err != nil {
155+
log.Println(err)
156+
return
157+
}
158+
if response != nil {
159+
assert.Equal(tt.expectedStatus, response.StatusCode, "Status Code is incorrect")
160+
}
161+
162+
})
163+
}
164+
165+
}
166+
167+
func Test_ListGroupsAPI(t *testing.T) {
168+
assert := assert.New(t)
169+
170+
tests := []struct {
171+
name string
172+
expectedStatus int
173+
expectedError error
174+
}{
175+
{
176+
name: "Get Group - Valid",
177+
expectedStatus: 200,
178+
expectedError: nil,
179+
},
180+
}
181+
182+
for _, tt := range tests {
183+
t.Run(tt.name, func(t *testing.T) {
184+
185+
client := &http.Client{
186+
Timeout: 3 * time.Second,
187+
}
188+
189+
requestDataPolicy := map[string]interface{}{}
190+
191+
requestDataJSON, _ := json.Marshal(requestDataPolicy)
192+
requestDataBody := bytes.NewReader(requestDataJSON)
193+
request, err := http.NewRequest(
194+
"GET", "http://localhost:9090/api/v1/groups", requestDataBody)
195+
if err != nil {
196+
log.Println(err)
197+
return
198+
}
199+
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
200+
request.Header.Add("Content-Type", "application/json")
201+
response, err := client.Do(request)
202+
if err != nil {
203+
log.Println(err)
204+
return
205+
}
206+
if response != nil {
207+
assert.Equal(tt.expectedStatus, response.StatusCode, "Status Code is incorrect")
208+
}
209+
210+
})
211+
}
212+
213+
}
214+
215+
func Test_PutGroupsAPI(t *testing.T) {
216+
assert := assert.New(t)
217+
218+
AddUser("member3", "testtest", []string{}, []string{"consoleAdmin"})
219+
AddGroup("putgroup1", []string{})
220+
221+
type args struct {
222+
api string
223+
members []string
224+
status string
225+
}
226+
tests := []struct {
227+
name string
228+
args args
229+
expectedStatus int
230+
expectedError error
231+
}{
232+
{
233+
name: "Put Group - Valid",
234+
args: args{
235+
api: "?name=putgroup1",
236+
members: []string{"member3"},
237+
status: "enabled",
238+
},
239+
expectedStatus: 200,
240+
expectedError: nil,
241+
},
242+
{
243+
name: "Put Group - Invalid",
244+
args: args{
245+
api: "?name=gdgfdfgd",
246+
members: []string{"member3"},
247+
status: "enabled",
248+
},
249+
expectedStatus: 500,
250+
expectedError: nil,
251+
},
252+
}
253+
254+
for _, tt := range tests {
255+
t.Run(tt.name, func(t *testing.T) {
256+
257+
client := &http.Client{
258+
Timeout: 3 * time.Second,
259+
}
260+
261+
requestDataPolicy := map[string]interface{}{}
262+
263+
requestDataPolicy["members"] = tt.args.members
264+
requestDataPolicy["status"] = tt.args.status
265+
266+
requestDataJSON, _ := json.Marshal(requestDataPolicy)
267+
requestDataBody := bytes.NewReader(requestDataJSON)
268+
request, err := http.NewRequest(
269+
"PUT", fmt.Sprintf("http://localhost:9090/api/v1/group%s", tt.args.api), requestDataBody)
86270
if err != nil {
87271
log.Println(err)
88272
return
@@ -120,19 +304,28 @@ func Test_DeleteGroupAPI(t *testing.T) {
120304
}{
121305
{
122306
name: "Delete Group - Valid",
123-
verb: "DELETE",
124307
args: args{
125-
api: "/group?name=grouptests1",
308+
api: "?name=grouptests1",
126309
},
310+
verb: "DELETE",
127311
expectedStatus: 204,
128312
expectedError: nil,
129313
},
314+
{
315+
name: "Delete Group - Invalid",
316+
args: args{
317+
api: "?name=grouptests12345",
318+
},
319+
verb: "DELETE",
320+
expectedStatus: 500,
321+
expectedError: nil,
322+
},
130323
{
131324
name: "Access Group After Delete - Invalid",
132-
verb: "GET",
133325
args: args{
134-
api: "/group?name=grouptests1",
326+
api: "?name=grouptests1",
135327
},
328+
verb: "GET",
136329
expectedStatus: 500,
137330
expectedError: nil,
138331
},
@@ -145,14 +338,12 @@ func Test_DeleteGroupAPI(t *testing.T) {
145338
Timeout: 3 * time.Second,
146339
}
147340

148-
// Add policy
149-
150341
requestDataPolicy := map[string]interface{}{}
151342

152343
requestDataJSON, _ := json.Marshal(requestDataPolicy)
153344
requestDataBody := bytes.NewReader(requestDataJSON)
154345
request, err := http.NewRequest(
155-
tt.verb, fmt.Sprintf("http://localhost:9090/api/v1%s", tt.args.api), requestDataBody)
346+
tt.verb, fmt.Sprintf("http://localhost:9090/api/v1/group%s", tt.args.api), requestDataBody)
156347
if err != nil {
157348
log.Println(err)
158349
return

0 commit comments

Comments
 (0)