0 | 0 |
package slack
|
1 | 1 |
|
2 | 2 |
import (
|
|
3 |
"encoding/json"
|
|
4 |
"fmt"
|
3 | 5 |
"net/http"
|
4 | 6 |
"testing"
|
5 | 7 |
)
|
|
34 | 36 |
}
|
35 | 37 |
}`)
|
36 | 38 |
rw.Write(response)
|
|
39 |
}
|
|
40 |
|
|
41 |
func httpTestErrReply(w http.ResponseWriter, clientErr bool, msg string) {
|
|
42 |
if clientErr {
|
|
43 |
w.WriteHeader(http.StatusBadRequest)
|
|
44 |
} else {
|
|
45 |
w.WriteHeader(http.StatusInternalServerError)
|
|
46 |
}
|
|
47 |
|
|
48 |
w.Header().Set("Content-Type", "application/json")
|
|
49 |
|
|
50 |
body, _ := json.Marshal(&SlackResponse{
|
|
51 |
Ok: false, Error: msg,
|
|
52 |
})
|
|
53 |
|
|
54 |
w.Write(body)
|
|
55 |
}
|
|
56 |
|
|
57 |
func newProfileHandler(up *UserProfile) (setter func(http.ResponseWriter, *http.Request)) {
|
|
58 |
return func(w http.ResponseWriter, r *http.Request) {
|
|
59 |
if up == nil {
|
|
60 |
httpTestErrReply(w, false, "err: UserProfile is nil")
|
|
61 |
return
|
|
62 |
}
|
|
63 |
|
|
64 |
if err := r.ParseForm(); err != nil {
|
|
65 |
httpTestErrReply(w, true, fmt.Sprintf("err parsing form: %s", err.Error()))
|
|
66 |
return
|
|
67 |
}
|
|
68 |
|
|
69 |
values := r.Form
|
|
70 |
|
|
71 |
if len(values["profile"]) == 0 {
|
|
72 |
httpTestErrReply(w, true, `POST data must include a "profile" field`)
|
|
73 |
return
|
|
74 |
}
|
|
75 |
|
|
76 |
profile := []byte(values["profile"][0])
|
|
77 |
|
|
78 |
userProfile := UserProfile{}
|
|
79 |
|
|
80 |
if err := json.Unmarshal(profile, &userProfile); err != nil {
|
|
81 |
httpTestErrReply(w, true, fmt.Sprintf("err parsing JSON: %s\n\njson: `%s`", err.Error(), profile))
|
|
82 |
return
|
|
83 |
}
|
|
84 |
|
|
85 |
*up = userProfile
|
|
86 |
|
|
87 |
// TODO(theckman): enhance this to return a full User object
|
|
88 |
fmt.Fprint(w, `{"ok":true}`)
|
|
89 |
}
|
37 | 90 |
}
|
38 | 91 |
|
39 | 92 |
func TestGetUserIdentity(t *testing.T) {
|
|
75 | 128 |
t.Fatal(ErrIncorrectResponse)
|
76 | 129 |
}
|
77 | 130 |
}
|
|
131 |
|
|
132 |
func TestUserCustomStatus(t *testing.T) {
|
|
133 |
up := &UserProfile{}
|
|
134 |
|
|
135 |
setUserProfile := newProfileHandler(up)
|
|
136 |
|
|
137 |
http.HandleFunc("/users.profile.set", setUserProfile)
|
|
138 |
|
|
139 |
once.Do(startServer)
|
|
140 |
SLACK_API = "http://" + serverAddr + "/"
|
|
141 |
api := New("testing-token")
|
|
142 |
|
|
143 |
testSetUserCustomStatus(api, up, t)
|
|
144 |
testUnsetUserCustomStatus(api, up, t)
|
|
145 |
}
|
|
146 |
|
|
147 |
func testSetUserCustomStatus(api *Client, up *UserProfile, t *testing.T) {
|
|
148 |
const (
|
|
149 |
statusText = "testStatus"
|
|
150 |
statusEmoji = ":construction:"
|
|
151 |
)
|
|
152 |
|
|
153 |
if err := api.SetUserCustomStatus(statusText, statusEmoji); err != nil {
|
|
154 |
t.Fatalf(`SetUserCustomStatus(%q, %q) = %#v, want <nil>`, statusText, statusEmoji, err)
|
|
155 |
}
|
|
156 |
|
|
157 |
if up.StatusText != statusText {
|
|
158 |
t.Fatalf(`UserProfile.StatusText = %q, want %q`, up.StatusText, statusText)
|
|
159 |
}
|
|
160 |
|
|
161 |
if up.StatusEmoji != statusEmoji {
|
|
162 |
t.Fatalf(`UserProfile.StatusEmoji = %q, want %q`, up.StatusEmoji, statusEmoji)
|
|
163 |
}
|
|
164 |
}
|
|
165 |
|
|
166 |
func testUnsetUserCustomStatus(api *Client, up *UserProfile, t *testing.T) {
|
|
167 |
if err := api.UnsetUserCustomStatus(); err != nil {
|
|
168 |
t.Fatalf(`UnsetUserCustomStatus() = %#v, want <nil>`, err)
|
|
169 |
}
|
|
170 |
|
|
171 |
if up.StatusText != "" {
|
|
172 |
t.Fatalf(`UserProfile.StatusText = %q, want %q`, up.StatusText, "")
|
|
173 |
}
|
|
174 |
|
|
175 |
if up.StatusEmoji != "" {
|
|
176 |
t.Fatalf(`UserProfile.StatusEmoji = %q, want %q`, up.StatusEmoji, "")
|
|
177 |
}
|
|
178 |
}
|