-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuser_test.go
114 lines (100 loc) · 2.62 KB
/
user_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
package openproject
import (
"fmt"
"net/http"
"os"
"testing"
)
func TestUserService_Get_SearchListNoFiltersSuccess(t *testing.T) {
setup()
defer teardown()
raw, err := os.ReadFile("./mocks/get/get-users-no-filters.json")
if err != nil {
t.Error(err.Error())
}
testMux.HandleFunc("/api/v3/users", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testRequestURL(t, r, "/api/v3/users")
fmt.Fprint(w, string(raw))
})
userList, resp, err := testClient.User.GetList(nil, 0, 10)
if resp == nil {
t.Errorf("Null response: %+v", resp)
}
if err != nil {
t.Errorf("Error given: %s", err)
}
if userList.Total != 2 {
t.Errorf("Total should populate with 2, %v given", resp.Total)
}
if userList.Embedded.Elements[0].FirstName != "John" {
t.Errorf("Expected John as firstName of element 0 but received %s", userList.Embedded.Elements[0].FirstName)
}
}
func TestUserService_GetByID_Success(t *testing.T) {
setup()
defer teardown()
raw, err := os.ReadFile("./mocks/get/get-user.json")
if err != nil {
t.Error(err.Error())
}
testMux.HandleFunc("/api/v3/users", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testRequestURL(t, r, "/api/v3/users?id=1")
fmt.Fprint(w, string(raw))
})
if user, _, err := testClient.User.Get("1"); err != nil {
t.Errorf("Error given: %s", err)
} else if user == nil {
t.Error("Expected user. User is nil")
}
}
func TestUserService_Create(t *testing.T) {
setup()
defer teardown()
raw, err := os.ReadFile("./mocks/post/post-user.json")
if err != nil {
t.Error(err.Error())
}
testMux.HandleFunc("/api/v3/users", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testRequestURL(t, r, "/api/v3/users")
w.WriteHeader(http.StatusCreated)
fmt.Fprint(w, string(raw))
})
p := &User{
Type: "User",
Login: "[email protected]",
Admin: false,
FirstName: "John",
LastName: "Smith",
Email: "[email protected]",
Status: "active",
Language: "en",
Password: "AB12345pass",
}
wp, _, err := testClient.User.Create(p)
if wp == nil {
t.Error("Expected user but user is nil")
}
if err != nil {
t.Errorf("Error given: %s", err)
}
}
func TestUserService_Delete(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/api/v3/users/4", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
testRequestURL(t, r, "/api/v3/users/4")
w.WriteHeader(http.StatusNoContent)
fmt.Fprint(w, `{}`)
})
resp, err := testClient.User.Delete("4")
if resp.StatusCode != 204 {
t.Error("User not deleted.")
}
if err != nil {
t.Errorf("Error given: %s", err)
}
}