-
Notifications
You must be signed in to change notification settings - Fork 13
/
message_test.go
295 lines (272 loc) · 7.06 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package pushover
import (
"bytes"
"fmt"
"math/rand"
"reflect"
"testing"
"time"
"unicode/utf8"
)
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ😊😂😉😎🎶🐱\u200d👓")
// Returns a random string with a fixed size
func getRandomString(size int) string {
b := make([]rune, size)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
func TestMessageValidation(t *testing.T) {
// Create random strings to be used in messages
randomStringsWithSize := make(map[int]string, 8)
for _, size := range []int{
MessageMaxLength,
MessageMaxLength + 1,
MessageTitleMaxLength,
MessageTitleMaxLength + 1,
MessageURLMaxLength,
MessageURLMaxLength + 1,
MessageURLTitleMaxLength,
MessageURLTitleMaxLength + 1,
} {
rands := getRandomString(size)
t := utf8.RuneCountInString(rands)
fmt.Println(t)
randomStringsWithSize[size] = rands
}
tt := []struct {
name string
message Message
expectedErr error
}{
{
name: "valid message",
message: Message{
Message: "Hello world !",
Title: "Example",
DeviceName: "My_Device",
URL: "http://google.com",
URLTitle: "Go check this URL",
Priority: PriorityNormal,
},
expectedErr: nil,
},
{
name: "empty message",
message: Message{},
expectedErr: ErrMessageEmpty,
},
{
name: "message with valid size",
message: Message{
Message: randomStringsWithSize[MessageMaxLength],
},
expectedErr: nil,
},
{
name: "message too long",
message: Message{
Message: randomStringsWithSize[MessageMaxLength+1],
},
expectedErr: ErrMessageTooLong,
},
{
name: "message with valid title length",
message: Message{
Message: "fake message",
Title: randomStringsWithSize[MessageTitleMaxLength],
},
expectedErr: nil,
},
{
name: "message with too long title",
message: Message{
Message: "fake message",
Title: randomStringsWithSize[MessageTitleMaxLength+1],
},
expectedErr: ErrMessageTitleTooLong,
},
{
name: "message with valid URL",
message: Message{
Message: "fake message",
URL: randomStringsWithSize[MessageURLMaxLength],
},
expectedErr: nil,
},
{
name: "message with too long URL",
message: Message{
Message: "fake message",
URL: randomStringsWithSize[MessageURLMaxLength+1],
},
expectedErr: ErrMessageURLTooLong,
},
{
name: "message with valid URL title",
message: Message{
Message: "Test message",
URL: "http://google.com",
URLTitle: randomStringsWithSize[MessageURLTitleMaxLength],
},
expectedErr: nil,
},
{
name: "message with too long URL title",
message: Message{
Message: "Test message",
URL: "http://google.com",
URLTitle: randomStringsWithSize[MessageURLTitleMaxLength+1],
},
expectedErr: ErrMessageURLTitleTooLong,
},
{
name: "message with URL without URL title",
message: Message{
Message: "Test message",
URLTitle: "URL Title",
},
expectedErr: ErrEmptyURL,
},
{
name: "message with emergency priority without emergency parameters",
message: Message{
Message: "Test message",
Priority: PriorityEmergency,
},
expectedErr: ErrMissingEmergencyParameter,
},
{
name: "message with emergency priority",
message: Message{
Message: "Test message",
Priority: PriorityEmergency,
Expire: time.Hour,
Retry: 60 * time.Second,
},
expectedErr: nil,
},
{
name: "message with invalid priority",
message: Message{
Message: "Test message",
Priority: 6,
},
expectedErr: ErrInvalidPriority,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
if err := tc.message.validate(); err != tc.expectedErr {
t.Errorf("expected %v; got %v", tc.expectedErr, err)
}
})
}
}
// TestMessageDeviceName tests the message device name format
func TestMessageDeviceName(t *testing.T) {
tt := []struct {
name string
device string
err error
}{
{"good device name 1", "yo_mama", nil},
{"good device name 2", "droid-2", nil},
{"good device name 2", "fasdfafdadfasdfa", nil},
{"multiple devices", "device1,device2", nil},
{"invalid multiple devices", "device1,device^2", ErrInvalidDeviceName},
{"invalid device name 1", "yo&mama", ErrInvalidDeviceName},
{"invalid device name 2", "my^device", ErrInvalidDeviceName},
{"invalid device name 3", "d34342fasdfasdfasdfasdfasdfasd", ErrInvalidDeviceName},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
message := Message{
Message: "Test message",
DeviceName: tc.device,
}
if err := message.validate(); err != tc.err {
t.Fatalf("expected %v, got %v", tc.err, err)
}
})
}
}
// TestNewMessageWithTitle
func TestNewMessageWithTitle(t *testing.T) {
message := NewMessageWithTitle("World", "Hello")
expected := &Message{
Title: "Hello",
Message: "World",
}
if !reflect.DeepEqual(message, expected) {
t.Errorf("Invalid message from NewMessage")
}
}
// MultipartRequest
func TestMultipartRequest(t *testing.T) {
tt := []struct {
name string
attachmentSize int64
expectedErr error
}{
{
name: "valid multipart form",
attachmentSize: 16,
},
{
name: "no attachment",
expectedErr: ErrMissingAttachment,
attachmentSize: 0,
},
{
name: "no attachment",
expectedErr: ErrMessageAttachmentTooLarge,
attachmentSize: MessageMaxAttachmentByte + 1,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
message := NewMessageWithTitle("World", "Hello")
if tc.attachmentSize > 0 {
buf := make([]byte, tc.attachmentSize)
attachment := bytes.NewBuffer(buf)
message.AddAttachment(attachment)
}
req, err := message.multipartRequest("pToken", "rToken", "url")
if err != tc.expectedErr {
t.Fatalf("expected %q, got %q", tc.expectedErr, err)
}
if err != nil {
return
}
if err := req.ParseMultipartForm(tc.attachmentSize * 2); err != nil {
t.Fatalf("expected no error, got %v", err)
}
expectedValues := map[string][]string{
"token": {"pToken"},
"user": {"rToken"},
"message": {"World"},
"priority": {"0"},
"title": {"Hello"},
}
if !reflect.DeepEqual(req.MultipartForm.Value, expectedValues) {
t.Fatalf("Invalid form values, expected %v, got %v", expectedValues, req.MultipartForm.Value)
}
fileHeaders, ok := req.MultipartForm.File["attachment"]
if !ok {
t.Fatalf("expected an attachment, got nothing")
}
if len(fileHeaders) != 1 {
t.Fatalf("expected one attachment, got %d", len(fileHeaders))
}
fileHeader := fileHeaders[0]
if fileHeader.Filename != "attachment" {
t.Fatalf("invalid attachment name: %s", fileHeader.Filename)
}
if fileHeader.Size != tc.attachmentSize {
t.Fatalf("invalid attachment size, expected %d, got %d", tc.attachmentSize, fileHeader.Size)
}
})
}
}