-
Notifications
You must be signed in to change notification settings - Fork 13
/
setup_test.go
371 lines (366 loc) · 9.09 KB
/
setup_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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package mailout
import (
"errors"
"testing"
"time"
"github.com/caddyserver/caddy"
"github.com/stretchr/testify/assert"
)
func TestSetupParse(t *testing.T) {
tests := []struct {
config string
expectErr error
expectConf func() *config
}{
{
`mailout`,
nil,
func() *config {
return newConfig()
},
},
{
`mailout {
recipient_to@domain.email testdata/B06469EE_nopw.pub.asc
}`,
nil,
func() *config {
c := newConfig()
c.pgpEmailKeys = []string{`recipient_to@domain.email`, `testdata/B06469EE_nopw.pub.asc`}
return c
},
},
{
`mailout /karate`,
nil,
func() *config {
c := newConfig()
c.endpoint = "/karate"
return c
},
},
{
`mailout /kungfu {
recipient_to@domain.email testdata/B06469EE_nopw.pub.asc
}`,
nil,
func() *config {
c := newConfig()
c.endpoint = "/kungfu"
c.pgpEmailKeys = []string{`recipient_to@domain.email`, `testdata/B06469EE_nopw.pub.asc`}
return c
},
},
{
`mailout {
recipient_to@domain.email testdata/B06469EE_nopw.pub.asc
to recipient_to@domain.email
cc "recipient_cc1@domain.email, recipient_cc2@domain.email"
bcc "recipient_bcc1@domain.email, recipient_bcc2@domain.email"
subject "Email from {{.firstname}} {{.lastname}}"
body testdata/mail_tpl.html
host 127.0.0.1
port 25
skip_tls_verify
}`,
nil,
func() *config {
c := newConfig()
c.pgpEmailKeys = []string{`recipient_to@domain.email`, `testdata/B06469EE_nopw.pub.asc`}
c.to = []string{"recipient_to@domain.email"}
c.cc = []string{"recipient_cc1@domain.email", "recipient_cc2@domain.email"}
c.bcc = []string{"recipient_bcc1@domain.email", "recipient_bcc2@domain.email"}
c.subject = `Email from {{.firstname}} {{.lastname}}`
c.body = `testdata/mail_tpl.html`
c.host = "127.0.0.1"
c.portRaw = "25"
c.skipTLSVerify = true
return c
},
},
{
`mailout /testFrom {
recipient_to@domain.email testdata/B06469EE_nopw.pub.asc
to recipient_to@domain.email
from_email opensource@maintainer.org
from_name "Open Source Maintainer"
}`,
nil,
func() *config {
c := newConfig()
c.pgpEmailKeys = []string{"recipient_to@domain.email", "testdata/B06469EE_nopw.pub.asc"}
c.endpoint = "/testFrom"
c.to = []string{"recipient_to@domain.email"}
c.fromEmail = "opensource@maintainer.org"
c.fromName = "Open Source Maintainer"
return c
},
},
// <NOT_IMPLEMENTED>
// You cannot define two endpoints for one host. this feature needs a total refactoring.
// therefore the /sales endpoint gets overwritten by the /repairs endpoint.
{
`mailout /sales {
salesteam@domain.email testdata/B06469EE_nopw.pub.asc
to salesteam@domain.email
subject "Sales Email from {{.firstname}} {{.lastname}}"
body testdata/mail_tpl.html
host 127.0.0.1
port 25
}
mailout /repairs {
repairteam@domain.email testdata/B06469EE_nopw.pub.asc
to repairteam@domain.email
subject "Repair Email from {{.firstname}} {{.lastname}}"
body testdata/mail_tpl.html
host 127.0.0.1
port 25
}
`,
nil,
func() *config {
c := newConfig()
c.endpoint = "/repairs"
c.pgpEmailKeys = []string{"salesteam@domain.email", "testdata/B06469EE_nopw.pub.asc", "repairteam@domain.email", "testdata/B06469EE_nopw.pub.asc"}
c.to = []string{"repairteam@domain.email"}
c.subject = `Repair Email from {{.firstname}} {{.lastname}}`
c.body = `testdata/mail_tpl.html`
c.host = "127.0.0.1"
c.portRaw = "25"
return c
},
},
// </NOT_IMPLEMENTED>
{
`mailout {
to recipient_to@domain.email
cc "recipient_cc1@domain.email, recipient_cc2@domain.email"
bcc "recipient_bcc1@domain.email, recipient_bcc2@domain.email"
recipient_to@domain.email testdata/B06469EE_nopw.pub.asc
recipient_cc1@domain.email https://keybase.io/cyrill/key.asc
recipient_cc2@domain.email testdata/B06469EE_nopw.pub.asc
recipient_bcc2@domain.email testdata/B06469EE_nopw.pub.asc
}`,
nil,
func() *config {
c := newConfig()
c.pgpEmailKeys = []string{
"recipient_to@domain.email", "testdata/B06469EE_nopw.pub.asc",
"recipient_cc1@domain.email", "https://keybase.io/cyrill/key.asc",
"recipient_cc2@domain.email", "testdata/B06469EE_nopw.pub.asc",
"recipient_bcc2@domain.email", "testdata/B06469EE_nopw.pub.asc",
}
c.to = []string{"recipient_to@domain.email"}
c.cc = []string{"recipient_cc1@domain.email", "recipient_cc2@domain.email"}
c.bcc = []string{"recipient_bcc1@domain.email", "recipient_bcc2@domain.email"}
return c
},
},
{
`mailout {
to recipient_to@domain.email
recipient_to@domain.email testdata/B06469EE_nopw.pub.asc
recipient_cc1@domain.email
}`,
errors.New("Testfile:4 - Error during parsing: Wrong argument count or unexpected line ending after 'recipient_cc1@domain.email'"),
func() *config {
c := newConfig()
c.pgpEmailKeys = []string{
"recipient_to@domain.email", "testdata/B06469EE_nopw.pub.asc",
}
c.to = []string{"recipient_to@domain.email"}
return c
},
},
{
`mailout /sendmail {
username g0ph3r
password release1.6
host 127.0.0.2
port 25
}`,
nil,
func() *config {
c := newConfig()
c.endpoint = "/sendmail"
c.username = "g0ph3r"
c.password = "release1.6"
c.host = "127.0.0.2"
c.portRaw = "25"
return c
},
},
{
`mailout /sendmail {
to "reci@email.de,"
}`,
errors.New("[mailout] Incorrect Email address found in: \"reci@email.de,\""),
func() *config {
c := newConfig()
c.endpoint = defaultEndpoint
c.username = "g0ph3r"
c.password = "release1.6"
c.host = "127.0.0.2"
c.portRaw = "25"
return c
},
},
{
`mailout /sendmail {
to
cc
bcc
}`,
errors.New("Testfile:2 - Error during parsing: Wrong argument count or unexpected line ending after 'to'"),
func() *config {
c := newConfig()
c.endpoint = defaultEndpoint
c.username = "g0ph3r"
c.password = "release1.6"
c.host = "127.0.0.2"
c.portRaw = "25"
return c
},
},
{
`mailout {
maillog testdata
errorlog testdata
}`,
nil,
func() *config {
c := newConfig()
c.maillog.MailDir = "testdata"
c.maillog.ErrDir = "testdata"
return c
},
},
{
`mailout {
maillog testdata
}`,
nil,
func() *config {
c := newConfig()
c.maillog.MailDir = "testdata"
return c
},
},
{
`mailout {
errorlog testdata
}`,
nil,
func() *config {
c := newConfig()
c.maillog.ErrDir = "testdata"
return c
},
},
{
`mailout {
errorlog testdata
maillog testdata
}`,
nil,
func() *config {
c := newConfig()
c.maillog.MailDir = "testdata"
c.maillog.ErrDir = "testdata"
return c
},
},
{
`mailout {
publickeyAttachmentFileName "encrypted.asc"
}`,
nil,
func() *config {
c := newConfig()
c.pgpAttachmentName = "encrypted.asc"
return c
},
},
{
`mailout {
publickeyAttachmentFileName
}`,
errors.New("Testfile:2 - Error during parsing: Wrong argument count or unexpected line ending after 'publickeyAttachmentFileName'"),
func() *config {
c := newConfig()
c.pgpAttachmentName = "encrypted.asc"
return c
},
},
{
`mailout {
ratelimit_interval 12h
ratelimit_capacity 500
}`,
nil,
func() *config {
c := newConfig()
c.rateLimitInterval = time.Hour * 12
c.rateLimitCapacity = 500
return c
},
},
{
`mailout {
ratelimit_interval
ratelimit_capacity 500
}`,
errors.New("Testfile:2 - Error during parsing: Wrong argument count or unexpected line ending after 'ratelimit_interval'"),
func() *config {
c := newConfig()
return c
},
},
{
`mailout {
ratelimit_interval 6h
ratelimit_capacity
}`,
errors.New("Testfile:3 - Error during parsing: Wrong argument count or unexpected line ending after 'ratelimit_capacity'"),
func() *config {
c := newConfig()
return c
},
},
{
`mailout {
ratelimit_interval 12x
ratelimit_capacity 500
}`,
errors.New("time: unknown unit x in duration 12x"),
func() *config {
c := newConfig()
c.rateLimitCapacity = 500
return c
},
},
{
`mailout {
ratelimit_interval 12s
ratelimit_capacity 5x
}`,
errors.New("strconv.ParseInt: parsing \"5x\": invalid syntax"),
func() *config {
c := newConfig()
c.rateLimitInterval = time.Second * 12
return c
},
},
}
for i, test := range tests {
c := caddy.NewTestController("http", test.config)
mc, err := parse(c)
if test.expectErr != nil {
assert.Nil(t, mc, "Index %d", i)
assert.EqualError(t, err, test.expectErr.Error(), "Index %d with config:\n%s", i, test.config)
continue
}
assert.NoError(t, err, "Index %d", i)
assert.Exactly(t, test.expectConf(), mc, "Index %d", i)
}
}