diff --git a/mailer/main_test.go b/mailer/main_test.go index d5ad93c..d23e20e 100644 --- a/mailer/main_test.go +++ b/mailer/main_test.go @@ -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!") } diff --git a/mailer/test.go b/mailer/test.go index 51fe70b..c7901c1 100644 --- a/mailer/test.go +++ b/mailer/test.go @@ -9,13 +9,16 @@ import ( "github.com/bdlm/log" ) +var defaultStartupPort = 12025 + type fakeServer struct { - s *Service - l net.Listener - MSGS chan msg + s *Service + l net.Listener + 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), + s: s, + 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, } diff --git a/web/api/status/main_test.go b/web/api/status/main_test.go index 3a85129..668c60e 100644 --- a/web/api/status/main_test.go +++ b/web/api/status/main_test.go @@ -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{} diff --git a/web/auth/api_login_test.go b/web/auth/api_login_test.go index 11d1714..8061e0d 100644 --- a/web/auth/api_login_test.go +++ b/web/auth/api_login_test.go @@ -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() diff --git a/web/auth/api_password_code_test.go b/web/auth/api_password_code_test.go index a47032f..dc4215d 100644 --- a/web/auth/api_password_code_test.go +++ b/web/auth/api_password_code_test.go @@ -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() diff --git a/web/auth/api_password_test.go b/web/auth/api_password_test.go index 4af6681..dd298ae 100644 --- a/web/auth/api_password_test.go +++ b/web/auth/api_password_test.go @@ -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() diff --git a/web/auth/api_status.go b/web/auth/api_status.go index fed239b..6706b91 100644 --- a/web/auth/api_status.go +++ b/web/auth/api_status.go @@ -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) { diff --git a/web/auth/api_status_test.go b/web/auth/api_status_test.go index 52ba659..cc595e3 100644 --- a/web/auth/api_status_test.go +++ b/web/auth/api_status_test.go @@ -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() diff --git a/web/metrics/main_test.go b/web/metrics/main_test.go index 8b656a2..c053927 100644 --- a/web/metrics/main_test.go +++ b/web/metrics/main_test.go @@ -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 diff --git a/web/webtest/main.go b/web/webtest/main.go index ae9afdc..1346843 100644 --- a/web/webtest/main.go +++ b/web/webtest/main.go @@ -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, + 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,