forked from mcuadros/ofelia
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmail_test.go
79 lines (60 loc) · 1.25 KB
/
mail_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
package middlewares
import (
"net"
"strconv"
"strings"
"sync"
"github.com/bradfitz/go-smtpd/smtpd"
. "gopkg.in/check.v1"
)
type MailSuite struct {
BaseSuite
l net.Listener
smtpd *smtpd.Server
smtpdHost string
smtpdPort int
}
var _ = Suite(&MailSuite{})
func (s *MailSuite) SetUpTest(c *C) {
s.BaseSuite.SetUpTest(c)
s.smtpd = &smtpd.Server{
Addr: ":0",
}
ln, err := net.Listen("tcp", "127.0.0.1:0")
c.Assert(err, IsNil)
s.l = ln
go func() {
err := s.smtpd.Serve(ln)
c.Assert(err, IsNil)
}()
p := strings.Split(s.l.Addr().String(), ":")
s.smtpdHost = p[0]
s.smtpdPort, _ = strconv.Atoi(p[1])
}
func (s *MailSuite) TearDownTest(c *C) {
s.l.Close()
}
func (s *MailSuite) TestNewSlackEmpty(c *C) {
c.Assert(NewMail(&MailConfig{}), IsNil)
}
func (s *MailSuite) TestRunSuccess(c *C) {
s.ctx.Start()
s.ctx.Stop(nil)
m := NewMail(&MailConfig{
SMTPHost: s.smtpdHost,
SMTPPort: s.smtpdPort,
EmailTo: "[email protected]",
EmailFrom: "[email protected]",
})
var wg sync.WaitGroup
s.smtpd.OnNewMail = func(_ smtpd.Connection, from smtpd.MailAddress) (smtpd.Envelope, error) {
c.Assert(from.Email(), Equals, "[email protected]")
wg.Done()
return nil, nil
}
wg.Add(1)
go func() {
c.Assert(m.Run(s.ctx), IsNil)
}()
wg.Wait()
}