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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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