-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage_test.go
277 lines (237 loc) · 8.71 KB
/
message_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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package pushover
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"time"
)
/*
Messages seen during development
Empty Message
400 Bad Request
{"message":"cannot be blank","errors":["message cannot be blank"],"status":0,"request":"4e8c4667-5904-46b9-a367-ff4869a3f401"}
Invalid Token
400 Bad Request
{"token":"invalid","errors":["application token is invalid"],"status":0,"request":"2e8c0c49-8d8d-46d0-b4a6-020c68755037"}
Invalid User
400 Bad Request
{"user":"invalid","errors":["user identifier is not a valid user, group, or subscribed user key"],"status":0,"request":"3cf7ebfd-6f23-4fee-8189-790a08e96892"}
Fields: Token, User, Message
200 OK
{"status":1,"request":"3f7224d5-c408-422b-be1f-dc0da5172497"}
HTML and Monospace both set to 1
400 Bad Request
{"html":"cannot be set with monospace","monospace":"cannot be set with html","errors":["html and monospace are mutually exclusive"],"status":0,"request":"0acffab5-5023-4d97-8492-f4f5d860c83a"}
Invalid Priority
400 Bad Request
{"priority":"is invalid, can only be -2, -1, 0, 1, or 2","errors":["priority is invalid"],"status":0,"request":"8cb3b0d5-d5b7-4283-9bcf-908dcfacff6b"}
*/
const id = "deadbeef-dead-beef-dead-deadbeefdead"
func serverHandler(w http.ResponseWriter, r *http.Request) {
_ = r.ParseForm()
_ = r.ParseMultipartForm(0)
// Check message
value := r.Form["message"]
if len(value) == 0 || len(value[0]) == 0 {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, `{"message":"cannot be blank","errors":["message cannot be blank"],"status":0,"request":"%s"}`, id)
return
}
// Check token
value = r.Form["token"]
if len(value) == 0 || len(value[0]) == 0 {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, `{"token":"invalid","errors":["application token is invalid"],"status":0,"request":"%s"}`, id)
return
}
// Check user
user := r.Form["user"]
if len(user) == 0 || len(user[0]) == 0 {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, `{"user":"invalid","errors":["user identifier is not a valid user, group, or subscribed user key"],"status":0,"request":"%s"}`, id)
return
}
// Check html and monospace
html := r.Form["html"]
monospace := r.Form["monospace"]
if len(html) > 0 && len(monospace) > 0 && html[0] == "1" && monospace[0] == "1" {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, `{"html":"cannot be set with monospace","monospace":"cannot be set with html","errors":["html and monospace are mutually exclusive"],"status":0,"request":"%s"}`, id)
return
}
value = r.Form["priority"]
if len(value) > 0 && len(value[0]) > 0 {
priority, err := strconv.Atoi(value[0])
if err != nil || priority < -2 || priority > 2 {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, `{"priority":"is invalid, can only be -2, -1, 0, 1, or 2","errors":["priority is invalid"],"status":0,"request":"%s"}`, id)
return
} else if priority == 2 {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{"status":1,"request":"%s","receipt":"1337"}`, id)
return
}
}
if user[0] == "failstatus" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, `{"status":"abc","request":"%s"}`, id)
return
}
if user[0] == "failrequest" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, `{"status":1,"request":1337}`)
return
}
if user[0] == "failjson" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, `{"priority":"is invalid, can only be -2, -1, 0, 1, or 2","errors":"priority is invalid"],"status":0,"request":"%s"}`, id)
return
}
if user[0] == "failbody" {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Length", "1")
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{"status":1,"request":"%s"}`, id)
}
func TestPushoverMessage(t *testing.T) {
apiServer := httptest.NewServer(http.HandlerFunc(serverHandler))
defer apiServer.Close()
var request MessageRequest
// Default Pushover URL
messagesURL = apiServer.URL
r, _ := MessageContext(context.TODO(), request)
if r.HTTPStatusCode != http.StatusBadRequest || r.APIStatus != 0 || r.Request != id ||
r.Errors[0] != "message cannot be blank" || r.ErrorParameters["message"] != "cannot be blank" {
t.Error("Default Pushover URL")
}
// Invalid Pushover URL
request.PushoverURL = "\x7f"
_, e := MessageContext(context.TODO(), request)
if _, ok := e.(*ErrInvalidRequest); !ok {
t.Error("Invalid Pushover URL")
}
// Handling of no message
request.PushoverURL = apiServer.URL
r, _ = MessageContext(context.TODO(), request)
if r.HTTPStatusCode != http.StatusBadRequest || r.APIStatus != 0 || r.Request != id ||
r.Errors[0] != "message cannot be blank" || r.ErrorParameters["message"] != "cannot be blank" {
t.Error("Handling of no message without validation")
}
r, _ = Message(request)
if r.HTTPStatusCode != http.StatusBadRequest || r.APIStatus != 0 || r.Request != id ||
r.Errors[0] != "message cannot be blank" || r.ErrorParameters["message"] != "cannot be blank" {
t.Error("Handling of no message without validation")
}
// Handling of no token
request.Message = "test message"
r, _ = MessageContext(context.TODO(), request)
if r.HTTPStatusCode != http.StatusBadRequest || r.APIStatus != 0 || r.Request != id ||
r.Errors[0] != "application token is invalid" || r.ErrorParameters["token"] != "invalid" {
t.Error("Handling of no token without validation")
}
r, _ = Message(request)
if r.HTTPStatusCode != http.StatusBadRequest || r.APIStatus != 0 || r.Request != id ||
r.Errors[0] != "application token is invalid" || r.ErrorParameters["token"] != "invalid" {
t.Error("Handling of no token without validation")
}
// Handling of no user
request.Token = "testtoken"
r, _ = MessageContext(context.TODO(), request)
if r.HTTPStatusCode != http.StatusBadRequest || r.APIStatus != 0 || r.Request != id ||
r.Errors[0] != "user identifier is not a valid user, group, or subscribed user key" ||
r.ErrorParameters["user"] != "invalid" {
t.Error("Handling of no user without validation")
}
r, _ = Message(request)
if r.HTTPStatusCode != http.StatusBadRequest || r.APIStatus != 0 || r.Request != id ||
r.Errors[0] != "user identifier is not a valid user, group, or subscribed user key" ||
r.ErrorParameters["user"] != "invalid" {
t.Error("Handling of no user without validation")
}
// Valid submission
request.User = "testuser"
r, _ = Message(request)
if r.HTTPStatusCode != http.StatusOK || r.APIStatus != 1 || r.Request != id ||
len(r.Errors) > 0 || len(r.ErrorParameters) > 0 {
t.Error("Valid submit data")
}
// Invalid API Status in response
request.User = "failstatus"
_, e = Message(request)
if _, ok := e.(*ErrInvalidResponse); !ok {
t.Error("Invalid API status in response")
}
// Invalid request ID in response
request.User = "failrequest"
_, e = Message(request)
if _, ok := e.(*ErrInvalidResponse); !ok {
t.Error("Invalid request ID in response")
}
// Invalid json response
request.User = "failjson"
_, e = Message(request)
if _, ok := e.(*ErrInvalidResponse); !ok {
t.Error("Invalid response JSON")
}
// Invalid body
request.User = "failbody"
_, e = Message(request)
if _, ok := e.(*ErrInvalidResponse); !ok {
t.Error("Invalid response body")
}
// Load all the fields
request.User = "testuser"
request.Title = "title"
request.URL = "url"
request.URLTitle = "urlTitle"
request.HTML = "0"
request.Monospace = "0"
request.Sound = "sound"
request.Device = "device"
request.Priority = "0"
request.Timestamp = "timestamp"
r, _ = Message(request)
if r.HTTPStatusCode != http.StatusOK || r.APIStatus != 1 || r.Request != id ||
len(r.Errors) > 0 || len(r.ErrorParameters) > 0 {
t.Error("All fields submitted")
}
// Priority of 2 yields additional "receipt" parameter
request.Priority = "2"
r, _ = Message(request)
if r.HTTPStatusCode != http.StatusOK || r.APIStatus != 1 || r.Request != id ||
len(r.Errors) > 0 || len(r.ErrorParameters) > 0 {
t.Error("Priority 2")
}
request.Priority = "0"
// Context cancellation
ctx, cancel := context.WithTimeout(context.Background(), 0*time.Millisecond)
_, e = MessageContext(ctx, request)
if e != context.DeadlineExceeded {
t.Error("Context deadline exceeded")
}
cancel()
// Image attachment
request.ImageReader = strings.NewReader("image data")
r, _ = Message(request)
if r.HTTPStatusCode != http.StatusOK || r.APIStatus != 1 || r.Request != id ||
len(r.Errors) > 0 || len(r.ErrorParameters) > 0 {
t.Error("Image attachment")
}
// Test http.PostForm() returning error
apiServer.Close()
_, e = Message(request)
if e == nil {
t.Error("No API server")
}
}