From 1e1571a2c30a841ee3f74db4006275f8b86020f0 Mon Sep 17 00:00:00 2001 From: genofire Date: Tue, 28 Sep 2021 04:23:20 +0200 Subject: [PATCH] web/webtest: setup mailserver only on request --- web/webtest/main.go | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/web/webtest/main.go b/web/webtest/main.go index 4d70059..45ba79c 100644 --- a/web/webtest/main.go +++ b/web/webtest/main.go @@ -24,6 +24,7 @@ var ( // Option to configure TestServer type Option struct { ReRun bool + Mailer bool DBSetup func(db *database.Database) ModuleLoader web.ModuleRegisterFunc } @@ -87,31 +88,32 @@ func NewWithOption(option Option) (*TestServer, error) { gin.EnableJsonDecoderDisallowUnknownFields() gin.SetMode(gin.TestMode) - mock, mail := mailer.NewFakeServer() - - err = mail.Setup() - if err != nil { - return nil, err - } - ws := &web.Service{ - DB: dbConfig.DB, - Mailer: mail, + DB: dbConfig.DB, + } + ts := &TestServer{ + DB: &dbConfig, + WS: ws, } ws.ModuleRegister(option.ModuleLoader) ws.Session.Name = "mysession" ws.Session.Secret = "hidden" + if option.Mailer { + mock, mail := mailer.NewFakeServer() + if err := mail.Setup(); err != nil { + return nil, err + } + ws.Mailer = mail + ts.Mails = mock.Mails + ts.Close = mock.Close + } + r := gin.Default() ws.LoadSession(r) ws.Bind(r) - return &TestServer{ - DB: &dbConfig, - Mails: mock.Mails, - Close: mock.Close, - gin: r, - WS: ws, - }, nil + ts.gin = r + return ts, nil } // DatabaseForget to run a test without a database