test(mailer): improve
continuous-integration/drone the build failed Details

This commit is contained in:
Geno 2021-06-23 20:48:08 +02:00
parent 87ebc96f80
commit 72fa8a8113
6 changed files with 142 additions and 2 deletions

1
go.mod
View File

@ -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

3
go.sum
View File

@ -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=

View File

@ -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()
}

89
mailer/test.go Normal file
View File

@ -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
}
}
}

25
mailer/test_test.go Normal file
View File

@ -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()
})
}

View File

@ -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)