forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users_test.go
179 lines (148 loc) · 5.63 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
package slack
import (
"encoding/json"
"fmt"
"net/http"
"testing"
)
func getUserIdentity(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
response := []byte(`{
"ok": true,
"user": {
"id": "UXXXXXXXX",
"name": "Test User",
"email": "[email protected]",
"image_24": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_24.jpg",
"image_32": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_32.jpg",
"image_48": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_48.jpg",
"image_72": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_72.jpg",
"image_192": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_192.jpg",
"image_512": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_512.jpg"
},
"team": {
"id": "TXXXXXXXX",
"name": "team-name",
"domain": "team-domain",
"image_34": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_34.jpg",
"image_44": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_44.jpg",
"image_68": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_68.jpg",
"image_88": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_88.jpg",
"image_102": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_102.jpg",
"image_132": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_132.jpg",
"image_230": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_230.jpg",
"image_original": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_original.jpg"
}
}`)
rw.Write(response)
}
func httpTestErrReply(w http.ResponseWriter, clientErr bool, msg string) {
if clientErr {
w.WriteHeader(http.StatusBadRequest)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
body, _ := json.Marshal(&SlackResponse{
Ok: false, Error: msg,
})
w.Write(body)
}
func newProfileHandler(up *UserProfile) (setter func(http.ResponseWriter, *http.Request)) {
return func(w http.ResponseWriter, r *http.Request) {
if up == nil {
httpTestErrReply(w, false, "err: UserProfile is nil")
return
}
if err := r.ParseForm(); err != nil {
httpTestErrReply(w, true, fmt.Sprintf("err parsing form: %s", err.Error()))
return
}
values := r.Form
if len(values["profile"]) == 0 {
httpTestErrReply(w, true, `POST data must include a "profile" field`)
return
}
profile := []byte(values["profile"][0])
userProfile := UserProfile{}
if err := json.Unmarshal(profile, &userProfile); err != nil {
httpTestErrReply(w, true, fmt.Sprintf("err parsing JSON: %s\n\njson: `%s`", err.Error(), profile))
return
}
*up = userProfile
// TODO(theckman): enhance this to return a full User object
fmt.Fprint(w, `{"ok":true}`)
}
}
func TestGetUserIdentity(t *testing.T) {
http.HandleFunc("/users.identity", getUserIdentity)
once.Do(startServer)
SLACK_API = "http://" + serverAddr + "/"
api := New("testing-token")
identity, err := api.GetUserIdentity()
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
// t.Fatal refers to -> t.Errorf & return
if identity.User.ID != "UXXXXXXXX" {
t.Fatal(ErrIncorrectResponse)
}
if identity.User.Name != "Test User" {
t.Fatal(ErrIncorrectResponse)
}
if identity.User.Email != "[email protected]" {
t.Fatal(ErrIncorrectResponse)
}
if identity.Team.ID != "TXXXXXXXX" {
t.Fatal(ErrIncorrectResponse)
}
if identity.Team.Name != "team-name" {
t.Fatal(ErrIncorrectResponse)
}
if identity.Team.Domain != "team-domain" {
t.Fatal(ErrIncorrectResponse)
}
if identity.User.Image24 == "" {
t.Fatal(ErrIncorrectResponse)
}
if identity.Team.Image34 == "" {
t.Fatal(ErrIncorrectResponse)
}
}
func TestUserCustomStatus(t *testing.T) {
up := &UserProfile{}
setUserProfile := newProfileHandler(up)
http.HandleFunc("/users.profile.set", setUserProfile)
once.Do(startServer)
SLACK_API = "http://" + serverAddr + "/"
api := New("testing-token")
testSetUserCustomStatus(api, up, t)
testUnsetUserCustomStatus(api, up, t)
}
func testSetUserCustomStatus(api *Client, up *UserProfile, t *testing.T) {
const (
statusText = "testStatus"
statusEmoji = ":construction:"
)
if err := api.SetUserCustomStatus(statusText, statusEmoji); err != nil {
t.Fatalf(`SetUserCustomStatus(%q, %q) = %#v, want <nil>`, statusText, statusEmoji, err)
}
if up.StatusText != statusText {
t.Fatalf(`UserProfile.StatusText = %q, want %q`, up.StatusText, statusText)
}
if up.StatusEmoji != statusEmoji {
t.Fatalf(`UserProfile.StatusEmoji = %q, want %q`, up.StatusEmoji, statusEmoji)
}
}
func testUnsetUserCustomStatus(api *Client, up *UserProfile, t *testing.T) {
if err := api.UnsetUserCustomStatus(); err != nil {
t.Fatalf(`UnsetUserCustomStatus() = %#v, want <nil>`, err)
}
if up.StatusText != "" {
t.Fatalf(`UserProfile.StatusText = %q, want %q`, up.StatusText, "")
}
if up.StatusEmoji != "" {
t.Fatalf(`UserProfile.StatusEmoji = %q, want %q`, up.StatusEmoji, "")
}
}