add support of Mailtesting in web/webtest
continuous-integration/drone the build failed Details

This commit is contained in:
Geno 2021-06-25 12:22:19 +02:00
parent 8c87415917
commit 979f5fef7d
10 changed files with 35 additions and 12 deletions

View File

@ -36,7 +36,7 @@ func TestSend(t *testing.T) {
assert.NoError(err)
m := mail.NewMessage()
m.SetHeader("From", "alex@example.com")
m.SetHeader("From", s.From)
m.SetHeader("To", "bob@example.com", "cora@example.com")
m.SetAddressHeader("Cc", "dan@example.com", "Dan")
m.SetHeader("Subject", "Hello!")
@ -46,9 +46,9 @@ func TestSend(t *testing.T) {
err = s.Dailer.DialAndSend(m)
assert.NoError(err)
msg := <-mock.MSGS
msg := <-mock.Mails
mock.Close()
assert.Equal("alex@example.com", msg.Header["From"][0])
assert.Equal(s.From, msg.Header["From"][0])
assert.Contains(msg.Body, "Bob and Cora!")
}

View File

@ -9,13 +9,16 @@ import (
"github.com/bdlm/log"
)
var defaultStartupPort = 12025
type fakeServer struct {
s *Service
l net.Listener
MSGS chan msg
Mails chan *TestingMail
}
type msg struct {
// TestingMail a mail in format from test server
type TestingMail struct {
Header textproto.MIMEHeader
Body string
}
@ -24,18 +27,20 @@ type msg struct {
func NewFakeServer() (*fakeServer, *Service) {
s := &Service{
SMTPHost: "127.0.0.1",
SMTPPort: 12025,
SMTPPort: defaultStartupPort,
SMTPUsername: "user",
SMTPPassword: "password",
SMTPSSL: false,
From: "golang-lib@example.org",
}
defaultStartupPort++
return newFakeServer(s)
}
func newFakeServer(s *Service) (*fakeServer, *Service) {
fs := &fakeServer{
s: s,
MSGS: make(chan msg),
Mails: make(chan *TestingMail),
}
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", fs.s.SMTPHost, fs.s.SMTPPort))
if err != nil {
@ -107,7 +112,7 @@ func (fs *fakeServer) handle(conn net.Conn) {
}
}
fs.MSGS <- msg{
fs.Mails <- &TestingMail{
Header: head,
Body: data,
}

View File

@ -12,6 +12,7 @@ import (
func TestAPIStatus(t *testing.T) {
assert := assert.New(t)
s := webtest.New(assert)
defer s.Close()
assert.NotNil(s)
obj := Status{}

View File

@ -13,6 +13,7 @@ import (
func TestAPILogin(t *testing.T) {
assert := assert.New(t)
s := webtest.New(assert)
defer s.Close()
assert.NotNil(s)
SetupMigration(s.DB)
s.DB.MigrateTestdata()

View File

@ -14,6 +14,7 @@ import (
func TestAPIPasswordCode(t *testing.T) {
assert := assert.New(t)
s := webtest.New(assert)
defer s.Close()
assert.NotNil(s)
SetupMigration(s.DB)
s.DB.MigrateTestdata()

View File

@ -13,6 +13,7 @@ import (
func TestAPIPassword(t *testing.T) {
assert := assert.New(t)
s := webtest.New(assert)
defer s.Close()
assert.NotNil(s)
SetupMigration(s.DB)
s.DB.MigrateTestdata()

View File

@ -16,6 +16,7 @@ import (
// @Failure 401 {object} web.HTTPError
// @Failure 500 {object} web.HTTPError
// @Router /api/v1/auth/status [get]
// @Security ApiKeyAuth
func init() {
web.ModuleRegister(func(r *gin.Engine, ws *web.Service) {
r.GET("/api/v1/auth/status", MiddlewareLogin(ws), func(c *gin.Context) {

View File

@ -13,6 +13,7 @@ import (
func TestAPIStatus(t *testing.T) {
assert := assert.New(t)
s := webtest.New(assert)
defer s.Close()
assert.NotNil(s)
SetupMigration(s.DB)
s.DB.MigrateTestdata()

View File

@ -12,6 +12,7 @@ import (
func TestMetricsLoaded(t *testing.T) {
assert := assert.New(t)
s := webtest.New(assert)
defer s.Close()
assert.NotNil(s)
// GET

View File

@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/assert"
"dev.sum7.eu/genofire/golang-lib/database"
"dev.sum7.eu/genofire/golang-lib/mailer"
"dev.sum7.eu/genofire/golang-lib/web"
)
@ -23,6 +24,8 @@ var (
type testServer struct {
DB *database.Database
Mails chan *mailer.TestingMail
Close func()
gin *gin.Engine
ws *web.Service
assert *assert.Assertions
@ -55,8 +58,14 @@ func New(assert *assert.Assertions) *testServer {
gin.EnableJsonDecoderDisallowUnknownFields()
gin.SetMode(gin.TestMode)
mock, mail := mailer.NewFakeServer()
err = mail.Setup()
assert.Nil(err)
ws := &web.Service{
DB: dbConfig.DB,
Mailer: mail,
}
ws.Session.Name = "mysession"
ws.Session.Secret = "hidden"
@ -66,6 +75,8 @@ func New(assert *assert.Assertions) *testServer {
ws.Bind(r)
return &testServer{
DB: &dbConfig,
Mails: mock.Mails,
Close: mock.Close,
gin: r,
ws: ws,
assert: assert,