forked from sogko/go-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 2
/
users_test.go
218 lines (192 loc) · 5.56 KB
/
users_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
package wordpress_test
import (
"github.com/sogko/go-wordpress"
"net/http"
"testing"
)
func factoryUser() *wordpress.User {
return &wordpress.User{
Username: "go-wordpress-test-user1",
Name: "go-wordpress-test-user1",
Email: "[email protected]",
Slug: "go-wordpress-test-user1",
Password: "password",
}
}
func cleanUpUser(t *testing.T, userID int) {
wp := initTestClient()
// Note that deleting a user requires `force=true` since `users` resource does not support trashing
deletedUser, resp, body, err := wp.Users().Delete(userID, "force=true")
if err != nil {
t.Errorf("Failed to clean up new user: %v", err.Error())
}
if body == nil {
t.Errorf("body should not be nil")
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 StatusOK, got %v", resp.Status)
}
if deletedUser.ID != userID {
t.Errorf("Deleted user ID should be the same as newly created user: %v != %v", deletedUser.ID, userID)
}
}
func getAnyOneUser(t *testing.T, wp *wordpress.Client) *wordpress.User {
users, resp, _, _ := wp.Users().List(nil)
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
if len(users) < 1 {
t.Fatalf("Should not return empty users")
}
userID := users[0].ID
user, resp, _, _ := wp.Users().Get(userID, "context=edit")
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
return user
}
func TestUsersList(t *testing.T) {
client := initTestClient()
users, resp, body, err := client.Users().List(nil)
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if body == nil {
t.Errorf("body should not be nil")
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 StatusOK, got %v", resp.Status)
}
if users == nil {
t.Errorf("Should not return nil users")
}
}
func TestUsersMe(t *testing.T) {
wp := initTestClient()
currentUser, resp, body, err := wp.Users().Me("context=edit")
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if body == nil {
t.Errorf("body should not be nil")
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 StatusOK, got %v", resp.Status)
}
if currentUser == nil {
t.Errorf("currentUser should not be nil")
}
if currentUser.Name != USER {
t.Errorf("Logged in user has a different username: %v != %v", currentUser.Username, USER)
}
}
func TestUsersGet(t *testing.T) {
wp := initTestClient()
u := getAnyOneUser(t, wp)
user, resp, body, err := wp.Users().Get(u.ID, nil)
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if body == nil {
t.Errorf("body should not be nil")
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 StatusOK, got %v", resp.Status)
}
if user == nil {
t.Errorf("user should not be nil")
}
}
func TestUsersCreate(t *testing.T) {
wp := initTestClient()
u := &wordpress.User{
Username: "go-wordpress-test-user1",
Email: "[email protected]",
Name: "go-wordpress-test-user1",
Slug: "go-wordpress-test-user1",
Password: "password",
}
newUser, resp, body, err := wp.Users().Create(u)
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if body == nil {
t.Errorf("body should not be nil")
}
if resp.StatusCode != http.StatusCreated {
t.Errorf("Expected 201 Created, got %v", resp.Status)
}
if newUser == nil {
t.Errorf("newUser should not be nil")
}
// clean up
cleanUpUser(t, newUser.ID)
}
func TestUsersDelete(t *testing.T) {
wp := initTestClient()
u := factoryUser()
newUser, resp, _, _ := wp.Users().Create(u)
if resp.StatusCode != http.StatusCreated {
t.Errorf("Expected 201 Created, got %v", resp.Status)
}
if newUser == nil {
t.Errorf("newUser should not be nil")
}
// Note that deleting a user requires `force=true` since `users` resource does not support trashing
// If not specified, a 501 NotImplemented will be returned
deletedUser, resp, body, err := wp.Users().Delete(newUser.ID, "force=true")
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if body == nil {
t.Errorf("body should not be nil")
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
if deletedUser.ID != newUser.ID {
t.Errorf("Deleted user ID should be the same as newly created user: %v != %v", deletedUser.ID, newUser.ID)
}
}
func TestUsersUpdate(t *testing.T) {
wp := initTestClient()
u := factoryUser()
// create user
newUser, resp, _, _ := wp.Users().Create(u)
if resp.StatusCode != http.StatusCreated {
t.Errorf("Expected 201 Created, got %v", resp.Status)
}
if newUser == nil {
t.Errorf("newUser should not be nil")
}
// get user in `edit` context
user, resp, _, _ := wp.Users().Get(newUser.ID, "context=edit")
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
if user == nil {
t.Errorf("user should not be nil")
}
// modify user
newUserEmail := "[email protected]"
if user.Email == newUserEmail {
t.Errorf("Warning: Data must be different for proper test, %v === %v", user.Email, newUserEmail)
}
user.Email = newUserEmail
// update
updatedUser, resp, body, err := wp.Users().Update(user.ID, user)
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if body == nil {
t.Errorf("body should not be nil")
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
if updatedUser.ID != user.ID {
t.Errorf("updatedUser ID should be the same ass specified user: %v != %v", updatedUser.ID, user.ID)
}
// clean up
cleanUpUser(t, newUser.ID)
}