forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reminders_test.go
158 lines (151 loc) · 3.64 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
package slack
import (
"net/http"
"reflect"
"testing"
)
type remindersHandler struct {
gotParams map[string]string
response 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)
}
}
}