/
user_util.go
143 lines (115 loc) · 3.3 KB
/
user_util.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
package util
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
"github.com/BeanCodeDe/authi/pkg/adapter"
"github.com/google/uuid"
"gopkg.in/go-playground/assert.v1"
)
const (
createUserJson = `{"password":"%s"}`
DefaultPassword = "SomeDefaultPassowrd"
)
func sendCreateUserIdRequest() *http.Response {
client := &http.Client{}
req, err := http.NewRequest(http.MethodPost, Url+adapter.AuthiRootPath, nil)
if err != nil {
panic(err)
}
req.Header.Set(CorrelationId, uuid.NewString())
resp, err := client.Do(req)
if err != nil {
panic(err)
}
return resp
}
func sendCreateUserRequest(id string, userCreateJson string) *http.Response {
jsonReq := []byte(userCreateJson)
client := &http.Client{}
req, err := http.NewRequest(http.MethodPut, Url+adapter.AuthiRootPath+"/"+id, bytes.NewBuffer(jsonReq))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", adapter.ContentTyp)
req.Header.Set(CorrelationId, uuid.NewString())
resp, err := client.Do(req)
if err != nil {
panic(err)
}
return resp
}
func sendRefreshPasswordRequest(userId string, authenticate *Authenticate, token string) *http.Response {
userJson, err := json.Marshal(authenticate)
if err != nil {
panic(err)
}
req, err := http.NewRequest(http.MethodPatch, Url+adapter.AuthiRootPath+"/"+userId, bytes.NewBuffer(userJson))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", adapter.ContentTyp)
req.Header.Set(adapter.AuthorizationHeaderName, "Bearer "+token)
req.Header.Set(CorrelationId, uuid.NewString())
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
return resp
}
func sendDeleteUserRequest(userId string, token string) *http.Response {
req, err := http.NewRequest(http.MethodDelete, Url+adapter.AuthiRootPath+"/"+userId, nil)
if err != nil {
panic(err)
}
req.Header.Set(adapter.AuthorizationHeaderName, "Bearer "+token)
req.Header.Set(CorrelationId, uuid.NewString())
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
return resp
}
func CreateUserId() (string, int) {
response := sendCreateUserIdRequest()
defer response.Body.Close()
if response.StatusCode != http.StatusCreated {
return "", response.StatusCode
}
bodyBytes, err := io.ReadAll(response.Body)
if err != nil {
fmt.Printf("Could not parse response Body: %v", err)
return "", -1
}
uuidOfUser := string(bodyBytes)
return uuidOfUser, response.StatusCode
}
func CreateUser(id string, password string) int {
response := sendCreateUserRequest(id, fmt.Sprintf(createUserJson, password))
defer response.Body.Close()
return response.StatusCode
}
func CreateUserForFurtherTesting(t *testing.T) string {
userId, status := CreateUserId()
assert.Equal(t, status, http.StatusCreated)
_, err := uuid.Parse(userId)
assert.Equal(t, err, nil)
status = CreateUser(userId, DefaultPassword)
assert.Equal(t, status, http.StatusCreated)
return userId
}
func UpdatePassword(userId string, password string, tokenString string) int {
response := sendRefreshPasswordRequest(userId, &Authenticate{Password: password}, tokenString)
defer response.Body.Close()
return response.StatusCode
}
func DeleteUser(userId string, tokenString string) int {
response := sendDeleteUserRequest(userId, tokenString)
defer response.Body.Close()
return response.StatusCode
}