-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathuser_test.go
235 lines (195 loc) · 6.04 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
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
package zendesk
import (
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
)
func TestUserRoleText(t *testing.T) {
for key := UserRoleEndUser; key <= UserRoleAdmin; key++ {
if text := UserRoleText(key); text == "" {
t.Fatalf("key=%d is undefined", key)
}
}
}
func TestGetUsers(t *testing.T) {
mockAPI := newMockAPI(http.MethodGet, "users.json")
client := newTestClient(mockAPI)
defer mockAPI.Close()
users, _, err := client.GetUsers(ctx, nil)
if err != nil {
t.Fatalf("Failed to get users: %s", err)
}
if len(users) != 2 {
t.Fatalf("expected length of users is 2, but got %d", len(users))
}
}
func TestGetOrganizationUsers(t *testing.T) {
mockAPI := newMockAPI(http.MethodGet, "users.json")
client := newTestClient(mockAPI)
defer mockAPI.Close()
users, _, err := client.GetOrganizationUsers(ctx, 1000006909040, nil)
if err != nil {
t.Fatalf("Failed to get users: %s", err)
}
if len(users) != 2 {
t.Fatalf("expected length of users is 2, but got %d", len(users))
}
}
func TestGetManyUsers(t *testing.T) {
mockAPI := newMockAPI(http.MethodGet, "users.json")
client := newTestClient(mockAPI)
defer mockAPI.Close()
users, _, err := client.GetManyUsers(ctx, nil)
if err != nil {
t.Fatalf("Failed to get many users: %s", err)
}
if len(users) != 2 {
t.Fatalf("expected length of many users is 2, but got %d", len(users))
}
}
func TestSearchUsers(t *testing.T) {
mockAPI := newMockAPI(http.MethodGet, "users.json")
client := newTestClient(mockAPI)
defer mockAPI.Close()
users, _, err := client.SearchUsers(ctx, nil)
if err != nil {
t.Fatalf("Failed to get many users: %s", err)
}
if len(users) != 2 {
t.Fatalf("expected length of many users is 2, but got %d", len(users))
}
}
func TestGetUser(t *testing.T) {
mockAPI := newMockAPIWithStatus(http.MethodGet, "user.json", http.StatusOK)
client := newTestClient(mockAPI)
defer mockAPI.Close()
user, err := client.GetUser(ctx, 369531345753)
if err != nil {
t.Fatalf("Failed to get user: %s", err)
}
expectedID := int64(369531345753)
if user.ID != expectedID {
t.Fatalf("Returned user does not have the expected ID %d. User id is %d", expectedID, user.ID)
}
}
func TestGetUserFailure(t *testing.T) {
mockAPI := newMockAPIWithStatus(http.MethodGet, "user.json", http.StatusInternalServerError)
client := newTestClient(mockAPI)
defer mockAPI.Close()
_, err := client.GetUser(ctx, 369531345753)
if err == nil {
t.Fatal("Client did not return error when api failed")
}
}
func TestGetUsersRolesEncodeCorrectly(t *testing.T) {
expected := "role%5B%5D=admin&role%5B%5D=end-user"
mockAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
queryString := r.URL.Query().Encode()
if queryString != expected {
t.Fatalf(`Did not get the expect query string: "%s". Was: "%s"`, expected, queryString)
}
w.Write(readFixture(filepath.Join(http.MethodGet, "users.json")))
}))
client := newTestClient(mockAPI)
defer mockAPI.Close()
opts := UserListOptions{
Roles: []string{
"admin",
"end-user",
},
}
_, _, err := client.GetUsers(ctx, &opts)
if err != nil {
t.Fatalf("Failed to get users: %s", err)
}
}
func TestCreateUser(t *testing.T) {
mockAPI := newMockAPIWithStatus(http.MethodPost, "users.json", http.StatusCreated)
client := newTestClient(mockAPI)
defer mockAPI.Close()
user, err := client.CreateUser(ctx, User{
Email: "test@example.com",
Name: "testuser",
})
if err != nil {
t.Fatalf("Failed to get valid response: %s", err)
}
if user.ID == 0 {
t.Fatal("Failed to create user")
}
}
func TestCreateOrUpdateUserCreated(t *testing.T) {
mockAPI := newMockAPIWithStatus(http.MethodPost, "users.json", http.StatusCreated)
client := newTestClient(mockAPI)
defer mockAPI.Close()
user, err := client.CreateOrUpdateUser(ctx, User{
Email: "test@example.com",
Name: "testuser",
})
if err != nil {
t.Fatalf("Failed to get valid response: %s", err)
}
if user.ID == 0 {
t.Fatal("Failed to create or update user")
}
}
func TestCreateOrUpdateUserUpdated(t *testing.T) {
mockAPI := newMockAPIWithStatus(http.MethodPost, "users.json", http.StatusOK)
client := newTestClient(mockAPI)
defer mockAPI.Close()
user, err := client.CreateOrUpdateUser(ctx, User{
Email: "test@example.com",
Name: "testuser",
})
if err != nil {
t.Fatalf("Failed to get valid response: %s", err)
}
if user.ID == 0 {
t.Fatal("Failed to create or update user")
}
}
func TestCreateOrUpdateUserFailure(t *testing.T) {
mockAPI := newMockAPIWithStatus(http.MethodPost, "users.json", http.StatusInternalServerError)
client := newTestClient(mockAPI)
defer mockAPI.Close()
_, err := client.CreateOrUpdateUser(ctx, User{})
if err == nil {
t.Fatal("Client did not return error when api failed")
}
}
func TestUpdateUser(t *testing.T) {
mockAPI := newMockAPIWithStatus(http.MethodPut, "user.json", http.StatusOK)
client := newTestClient(mockAPI)
defer mockAPI.Close()
user, err := client.UpdateUser(ctx, 369531345753, User{})
if err != nil {
t.Fatalf("Failed to update user: %s", err)
}
expectedID := int64(369531345753)
if user.ID != expectedID {
t.Fatalf("Returned user does not have the expected ID %d. User id is %d", expectedID, user.ID)
}
}
func TestUpdateUserFailure(t *testing.T) {
mockAPI := newMockAPIWithStatus(http.MethodPut, "user.json", http.StatusInternalServerError)
client := newTestClient(mockAPI)
defer mockAPI.Close()
_, err := client.UpdateUser(ctx, 369531345753, User{})
if err == nil {
t.Fatal("Client did not return error when api failed")
}
}
func TestGetUserRelated(t *testing.T) {
mockAPI := newMockAPIWithStatus(http.MethodGet, "user_related.json", http.StatusOK)
client := newTestClient(mockAPI)
defer mockAPI.Close()
userRelated, err := client.GetUserRelated(ctx, 369531345753)
if err != nil {
t.Fatalf("Failed to get user related information: %s", err)
}
expectedAssignedTickets := int64(5)
if userRelated.AssignedTickets != expectedAssignedTickets {
t.Fatalf("Returned user does not have the expected assigned tickets %d. It is %d", expectedAssignedTickets, userRelated.AssignedTickets)
}
}