From 72fa8a81133fe247101a5f6d8c534c191e8c5d0e Mon Sep 17 00:00:00 2001 From: Geno Date: Wed, 23 Jun 2021 20:48:08 +0200 Subject: [PATCH] test(mailer): improve --- go.mod | 1 + go.sum | 3 +- mailer/main_test.go | 24 ++++++++++++ mailer/test.go | 89 +++++++++++++++++++++++++++++++++++++++++++++ mailer/test_test.go | 25 +++++++++++++ web/main_test.go | 2 +- 6 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 mailer/test.go create mode 100644 mailer/test_test.go diff --git a/go.mod b/go.mod index 8f61289..ea8bad1 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/stretchr/testify v1.7.0 golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba + golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/mail.v2 v2.3.1 // indirect gorm.io/driver/postgres v1.1.0 diff --git a/go.sum b/go.sum index af40ed2..f6bfeb5 100644 --- a/go.sum +++ b/go.sum @@ -578,8 +578,9 @@ golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= diff --git a/mailer/main_test.go b/mailer/main_test.go index 1c651e7..374bb39 100644 --- a/mailer/main_test.go +++ b/mailer/main_test.go @@ -1 +1,25 @@ package mailer + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSetupAndPing(t *testing.T) { + assert := assert.New(t) + + mock, s := NewFakeServer() + assert.NotNil(mock) + // correct setup + err := s.Setup() + assert.NoError(err) + mock.Close() + + s.SMTPPassword = "wrong" + mock, s = newFakeServer(s) + // wrong password + err = s.Setup() + assert.Error(err) + mock.Close() +} diff --git a/mailer/test.go b/mailer/test.go new file mode 100644 index 0000000..60bc881 --- /dev/null +++ b/mailer/test.go @@ -0,0 +1,89 @@ +package mailer + +import ( + "errors" + "fmt" + "net" + "net/textproto" + + "github.com/bdlm/log" +) + +type fakeServer struct { + s *Service + l net.Listener +} + +// NewFakeServer - to get mocked Service for mail-service +func NewFakeServer() (*fakeServer, *Service) { + s := &Service{ + SMTPHost: "127.0.0.1", + SMTPPort: 12025, + SMTPUsername: "user", + SMTPPassword: "password", + SMTPSSL: false, + } + return newFakeServer(s) +} + +func newFakeServer(s *Service) (*fakeServer, *Service) { + fs := &fakeServer{ + s: s, + } + l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", fs.s.SMTPHost, fs.s.SMTPPort)) + if err != nil { + log.Panicf("Error listing: %s", err) + 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 + } + log.Panicf("Error accepting: %s", err) + } + 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 + } + } +} diff --git a/mailer/test_test.go b/mailer/test_test.go new file mode 100644 index 0000000..1771349 --- /dev/null +++ b/mailer/test_test.go @@ -0,0 +1,25 @@ +package mailer + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFakeServer(t *testing.T) { + assert := assert.New(t) + + s := &Service{ + SMTPHost: "127.0.0.1", + SMTPPort: -2, + SMTPUsername: "user", + SMTPPassword: "password", + SMTPSSL: false, + } + + // Port + assert.Panics(func() { + mock, _ := newFakeServer(s) + mock.Close() + }) +} diff --git a/web/main_test.go b/web/main_test.go index e1871c0..7b207af 100644 --- a/web/main_test.go +++ b/web/main_test.go @@ -13,7 +13,7 @@ func TestRun(t *testing.T) { ModuleRegister(func(_ *gin.Engine, _ *Service) { }) - s := &Service{AccessLog: true, Listen: ":80"} + s := &Service{AccessLog: true, Listen: "8.8.8.8:80"} // HTTP - failed err := s.Run() assert.Error(err)