Skip to content

Commit 51ce548

Browse files
authored
Add User Integration Test #414 (#1417)
1 parent 7aa8217 commit 51ce548

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

integration/users_test.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// This file is part of MinIO Console Server
2+
// Copyright (c) 2022 MinIO, Inc.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package integration
18+
19+
import (
20+
"bytes"
21+
"encoding/json"
22+
"fmt"
23+
"log"
24+
"net/http"
25+
"testing"
26+
"time"
27+
28+
"github.com/stretchr/testify/assert"
29+
)
30+
31+
func TestAddUser(t *testing.T) {
32+
/*
33+
This is an atomic API Test to add a user via api/v1/users, the intention
34+
is simple, add a user and make sure the response is 201 meaning that the
35+
user got added successfully.
36+
After test completion, it is expected that user is removed, so other
37+
tests like users.ts can run over clean data and we don't collide against
38+
it.
39+
*/
40+
41+
assert := assert.New(t)
42+
43+
client := &http.Client{
44+
Timeout: 3 * time.Second,
45+
}
46+
47+
var x [0]string
48+
fmt.Println(x)
49+
50+
fmt.Println(".......................TestAddUser(): Create Data to add user")
51+
requestDataAdd := map[string]interface{}{
52+
"accessKey": "accessKey",
53+
"secretKey": "secretKey",
54+
"groups": x,
55+
"policies": x,
56+
}
57+
58+
fmt.Println("..............................TestAddUser(): Prepare the POST")
59+
requestDataJSON, _ := json.Marshal(requestDataAdd)
60+
requestDataBody := bytes.NewReader(requestDataJSON)
61+
request, err := http.NewRequest(
62+
"POST", "http://localhost:9090/api/v1/users", requestDataBody)
63+
if err != nil {
64+
log.Println(err)
65+
return
66+
}
67+
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
68+
request.Header.Add("Content-Type", "application/json")
69+
70+
fmt.Println(".................................TestAddUser(): Make the POST")
71+
response, err := client.Do(request)
72+
if err != nil {
73+
log.Println(err)
74+
return
75+
}
76+
fmt.Println("..................................TestAddUser(): Verification")
77+
fmt.Println(".................................TestAddUser(): POST response")
78+
fmt.Println(response)
79+
fmt.Println("....................................TestAddUser(): POST error")
80+
fmt.Println(err)
81+
if response != nil {
82+
fmt.Println("POST StatusCode:", response.StatusCode)
83+
assert.Equal(201, response.StatusCode, "Status Code is incorrect")
84+
}
85+
86+
fmt.Println("...................................TestAddUser(): Remove user")
87+
// {{baseUrl}}/user?name=proident velit
88+
// Investiga como se borra en el browser.
89+
request, err = http.NewRequest(
90+
"DELETE", "http://localhost:9090/api/v1/user?name=accessKey", nil)
91+
if err != nil {
92+
log.Println(err)
93+
return
94+
}
95+
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
96+
request.Header.Add("Content-Type", "application/json")
97+
98+
fmt.Println("...............................TestAddUser(): Make the DELETE")
99+
response, err = client.Do(request)
100+
if err != nil {
101+
log.Println(err)
102+
return
103+
}
104+
fmt.Println("..................................TestAddUser(): Verification")
105+
fmt.Println("...............................TestAddUser(): DELETE response")
106+
fmt.Println(response)
107+
fmt.Println("..................................TestAddUser(): DELETE error")
108+
fmt.Println(err)
109+
if response != nil {
110+
fmt.Println("DELETE StatusCode:", response.StatusCode)
111+
assert.Equal(204, response.StatusCode, "has to be 204 when delete user")
112+
}
113+
114+
}

0 commit comments

Comments
 (0)