golang-lib/mailer/test.go

128 lines
2.4 KiB
Go
Raw Permalink Normal View History

2021-06-23 20:48:08 +02:00
package mailer
import (
"errors"
"fmt"
"net"
"net/textproto"
2021-09-29 13:08:43 +02:00
"go.uber.org/zap"
2021-06-23 20:48:08 +02:00
)
var defaultStartupPort = 12025
2021-06-23 20:48:08 +02:00
type fakeServer struct {
2021-09-29 13:08:43 +02:00
log *zap.Logger
s *Service
l net.Listener
Mails chan *TestingMail
}
// TestingMail a mail in format from test server
type TestingMail struct {
Header textproto.MIMEHeader
Body string
2021-06-23 20:48:08 +02:00
}
// NewFakeServer - to get mocked Service for mail-service
2021-09-29 13:08:43 +02:00
func NewFakeServer(log *zap.Logger) (*fakeServer, *Service) {
2021-06-23 20:48:08 +02:00
s := &Service{
SMTPHost: "127.0.0.1",
SMTPPort: defaultStartupPort,
2021-06-23 20:48:08 +02:00
SMTPUsername: "user",
SMTPPassword: "password",
SMTPSSL: false,
From: "golang-lib@example.org",
2021-06-23 20:48:08 +02:00
}
defaultStartupPort++
2021-09-29 13:08:43 +02:00
return newFakeServer(s, log)
2021-06-23 20:48:08 +02:00
}
2021-09-29 13:08:43 +02:00
func newFakeServer(s *Service, log *zap.Logger) (*fakeServer, *Service) {
2021-06-23 20:48:08 +02:00
fs := &fakeServer{
2021-09-29 13:08:43 +02:00
log: log,
s: s,
Mails: make(chan *TestingMail),
2021-06-23 20:48:08 +02:00
}
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", fs.s.SMTPHost, fs.s.SMTPPort))
if err != nil {
2021-09-29 13:08:43 +02:00
log.Panic("error listing", zap.Error(err))
2021-06-23 20:48:08 +02:00
return nil, nil
}
fs.l = l
go fs.run()
return fs, s
}
func (fs *fakeServer) Close() {
fs.l.Close()
}
func (fs *fakeServer) run() {
for {
conn, err := fs.l.Accept()
if err != nil {
if errors.Is(err, net.ErrClosed) {
return
}
2021-09-29 13:08:43 +02:00
fs.log.Panic("Error accepting", zap.Error(err))
2021-06-23 20:48:08 +02:00
}
go fs.handle(conn)
}
}
func (fs *fakeServer) handle(conn net.Conn) {
defer conn.Close()
c := textproto.NewConn(conn)
defer c.Close()
c.Cmd("220 localhost.fake ESMTP Postfix")
s, _ := c.ReadLine()
if len(s) < 6 || s[:4] != "EHLO" {
c.Cmd("221 Bye")
return
}
c.Cmd("250-Hello %s", s[5:])
c.Cmd("250-PIPELINIG")
c.Cmd("250 AUTH PLAIN")
s, _ = c.ReadLine()
if s == "AUTH PLAIN AHVzZXIAcGFzc3dvcmQ=" {
c.Cmd("235 Authentication successful")
} else {
c.Cmd("535 Authentication failed")
c.Cmd("221 Bye")
return
}
for {
s, _ = c.ReadLine()
switch s {
case "QUIT":
c.Cmd("221 Bye")
return
case "DATA":
c.Cmd("354 End data with <CR><LF>.<CR><LF>")
head, _ := c.ReadMIMEHeader()
data := ""
handleMsgData:
for {
s, _ := c.ReadLine()
switch s {
case ".":
break handleMsgData
default:
data = fmt.Sprintf("%s%s\n", data, s)
c.Cmd("250 Ok")
}
}
fs.Mails <- &TestingMail{
Header: head,
Body: data,
}
default:
// fmt.Println(s)
// TODO : MAIL FROM: and RCPT TO:
c.Cmd("250 Ok")
2021-06-23 20:48:08 +02:00
}
}
}