forked from winston-stripe/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reminders_test.go
211 lines (197 loc) · 4.85 KB
/
reminders_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
package slack
import (
"bytes"
"io/ioutil"
"net/http"
"reflect"
"testing"
)
type remindersHandler struct {
gotParams map[string]string
}
func newRemindersHandler() *remindersHandler {
return &remindersHandler{
gotParams: make(map[string]string),
}
}
func (rh *remindersHandler) accumulateFormValue(k string, r *http.Request) {
if v := r.FormValue(k); v != "" {
rh.gotParams[k] = v
}
}
func (rh *remindersHandler) handler(w http.ResponseWriter, r *http.Request) {
rh.accumulateFormValue("channel", r)
rh.accumulateFormValue("user", r)
rh.accumulateFormValue("text", r)
rh.accumulateFormValue("time", r)
rh.accumulateFormValue("reminder", r)
w.Header().Set("Content-Type", "application/json")
if rh.gotParams["text"] == "trigger-error" || rh.gotParams["reminder"] == "trigger-error" {
w.Write([]byte(`{ "ok": false, "error": "oh no" }`))
} else {
w.Write([]byte(`{ "ok": true }`))
}
}
func TestSlack_AddReminder(t *testing.T) {
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
tests := []struct {
chanID string
userID string
text string
time string
wantParams map[string]string
expectErr bool
}{
{
"someChannelID",
"",
"hello world",
"tomorrow at 9am",
map[string]string{
"text": "hello world",
"time": "tomorrow at 9am",
"channel": "someChannelID",
},
false,
},
{
"someChannelID",
"",
"trigger-error",
"tomorrow at 9am",
map[string]string{
"text": "trigger-error",
"time": "tomorrow at 9am",
"channel": "someChannelID",
},
true,
},
{
"",
"someUserID",
"hello world",
"tomorrow at 9am",
map[string]string{
"text": "hello world",
"time": "tomorrow at 9am",
"user": "someUserID",
},
false,
},
{
"",
"someUserID",
"trigger-error",
"tomorrow at 9am",
map[string]string{
"text": "trigger-error",
"time": "tomorrow at 9am",
"user": "someUserID",
},
true,
},
}
var rh *remindersHandler
http.HandleFunc("/reminders.add", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
for i, test := range tests {
rh = newRemindersHandler()
var err error
if test.chanID != "" {
_, err = api.AddChannelReminder(test.chanID, test.text, test.time)
} else {
_, err = api.AddUserReminder(test.userID, test.text, test.time)
}
if test.expectErr == false && err != nil {
t.Fatalf("%d: Unexpected error: %s", i, err)
} else if test.expectErr == true && err == nil {
t.Fatalf("%d: Expected error but got none!", i)
}
if !reflect.DeepEqual(rh.gotParams, test.wantParams) {
t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams)
}
}
}
func TestSlack_DeleteReminder(t *testing.T) {
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
tests := []struct {
reminder string
wantParams map[string]string
expectErr bool
}{
{
"foo",
map[string]string{
"reminder": "foo",
},
false,
},
{
"trigger-error",
map[string]string{
"reminder": "trigger-error",
},
true,
},
}
var rh *remindersHandler
http.HandleFunc("/reminders.delete", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
for i, test := range tests {
rh = newRemindersHandler()
err := api.DeleteReminder(test.reminder)
if test.expectErr == false && err != nil {
t.Fatalf("%d: Unexpected error: %s", i, err)
} else if test.expectErr == true && err == nil {
t.Fatalf("%d: Expected error but got none!", i)
}
if !reflect.DeepEqual(rh.gotParams, test.wantParams) {
t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams)
}
}
}
type mockRemindersListHTTPClient struct{}
func (m *mockRemindersListHTTPClient) Do(*http.Request) (*http.Response, error) {
responseString := `{
"ok": true,
"reminders": [
{
"id": "Rm12345678",
"creator": "U18888888",
"user": "U18888888",
"text": "eat a banana",
"recurring": false,
"time": 1458678068,
"complete_ts": 0
},
{
"id": "Gm12345678",
"creator": "U18888888",
"user": "U18888888",
"text": "drink some water",
"recurring": false,
"time": 1458678090,
"complete_ts": 0
}
]
}`
return &http.Response{StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBufferString(responseString))}, nil
}
func TestSlack_ListReminders(t *testing.T) {
expectedIDs := []string{"Rm12345678", "Gm12345678"}
once.Do(startServer)
api := &Client{
endpoint: "http://" + serverAddr + "/",
token: "testing-token",
httpclient: &mockRemindersListHTTPClient{},
}
reminders, err := api.ListReminders()
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
for i := range reminders {
if reminders[i].ID != expectedIDs[i] {
t.Fatalf("List Reminders data wasn't correctly populated: wanted %v, got %v", expectedIDs[i], reminders[i].ID)
}
}
}