From 3e5f7f8990dc208cb8f3809e48bc515221c5ad75 Mon Sep 17 00:00:00 2001 From: Vivian Brown Date: Thu, 16 Apr 2020 13:47:22 -0700 Subject: [PATCH] Pass request logger to mailer --- api/api_test.go | 2 +- api/middleware.go | 7 ++++--- api/payments_test.go | 6 +++--- api/utils_test.go | 2 +- cmd/serve_cmd.go | 2 +- mailer/mailer.go | 4 ++-- mailer/mailer_test.go | 5 +++-- 7 files changed, 15 insertions(+), 13 deletions(-) diff --git a/api/api_test.go b/api/api_test.go index 619aad6..f7b1bcd 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -24,7 +24,7 @@ func TestTraceWrapper(t *testing.T) { config.Payment.Stripe.Enabled = true config.Payment.Stripe.SecretKey = "secret" - ctx, err := WithInstanceConfig(context.Background(), globalConfig.SMTP, config, "") + ctx, err := WithInstanceConfig(context.Background(), globalConfig.SMTP, config, "", logrus.New()) require.NoError(t, err) api := NewAPIWithVersion(ctx, globalConfig, logrus.StandardLogger(), nil, "") diff --git a/api/middleware.go b/api/middleware.go index 64205e7..909ffaf 100644 --- a/api/middleware.go +++ b/api/middleware.go @@ -14,6 +14,7 @@ import ( "github.com/netlify/gocommerce/mailer" "github.com/netlify/gocommerce/models" "github.com/pkg/errors" + "github.com/sirupsen/logrus" ) const ( @@ -100,7 +101,7 @@ func (api *API) loadInstanceConfig(w http.ResponseWriter, r *http.Request) (cont } logEntrySetField(r, "site_url", config.SiteURL) - ctx, err = WithInstanceConfig(ctx, api.config.SMTP, config, instanceID) + ctx, err = WithInstanceConfig(ctx, api.config.SMTP, config, instanceID, getLogEntry(r)) if err != nil { return nil, internalServerError("Error loading instance config").WithInternalError(err) } @@ -108,7 +109,7 @@ func (api *API) loadInstanceConfig(w http.ResponseWriter, r *http.Request) (cont return ctx, nil } -func WithInstanceConfig(ctx context.Context, smtp conf.SMTPConfiguration, config *conf.Configuration, instanceID string) (context.Context, error) { +func WithInstanceConfig(ctx context.Context, smtp conf.SMTPConfiguration, config *conf.Configuration, instanceID string, log logrus.FieldLogger) (context.Context, error) { ctx = gcontext.WithInstanceID(ctx, instanceID) ctx = gcontext.WithConfig(ctx, config) ctx, err := gcontext.WithCoupons(ctx, config) @@ -116,7 +117,7 @@ func WithInstanceConfig(ctx context.Context, smtp conf.SMTPConfiguration, config return nil, err } - mailer := mailer.NewMailer(smtp, config) + mailer := mailer.NewMailer(smtp, config, log) ctx = gcontext.WithMailer(ctx, mailer) store, err := assetstores.NewStore(config) diff --git a/api/payments_test.go b/api/payments_test.go index 140fbc6..8ff6bfd 100644 --- a/api/payments_test.go +++ b/api/payments_test.go @@ -237,7 +237,7 @@ func TestPaymentsRefund(t *testing.T) { globalConfig := new(conf.GlobalConfiguration) provider := &memProvider{name: payments.StripeProvider} - ctx, err := WithInstanceConfig(context.Background(), globalConfig.SMTP, test.Config, "") + ctx, err := WithInstanceConfig(context.Background(), globalConfig.SMTP, test.Config, "", logrus.New()) require.NoError(t, err) ctx = gcontext.WithPaymentProviders(ctx, map[string]payments.Provider{payments.StripeProvider: provider}) @@ -628,7 +628,7 @@ func TestPaymentPreauthorize(t *testing.T) { req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=utf-8") globalConfig := new(conf.GlobalConfiguration) - ctx, err := WithInstanceConfig(context.Background(), globalConfig.SMTP, test.Config, "") + ctx, err := WithInstanceConfig(context.Background(), globalConfig.SMTP, test.Config, "", logrus.New()) require.NoError(t, err) NewAPIWithVersion(ctx, test.GlobalConfig, logrus.StandardLogger(), test.DB, "").handler.ServeHTTP(recorder, req) @@ -669,7 +669,7 @@ func TestPaymentPreauthorize(t *testing.T) { req.Header.Set("Content-Type", "application/json") globalConfig := new(conf.GlobalConfiguration) - ctx, err := WithInstanceConfig(context.Background(), globalConfig.SMTP, test.Config, "") + ctx, err := WithInstanceConfig(context.Background(), globalConfig.SMTP, test.Config, "", logrus.New()) require.NoError(t, err) NewAPIWithVersion(ctx, test.GlobalConfig, logrus.StandardLogger(), test.DB, "").handler.ServeHTTP(recorder, req) diff --git a/api/utils_test.go b/api/utils_test.go index fa5318d..215b716 100644 --- a/api/utils_test.go +++ b/api/utils_test.go @@ -384,7 +384,7 @@ func (r *RouteTest) TestEndpoint(method string, url string, body io.Reader, toke require.NoError(r.T, signHTTPRequest(req, token, r.Config.JWT.Secret)) } globalConfig := new(conf.GlobalConfiguration) - ctx, err := WithInstanceConfig(context.Background(), globalConfig.SMTP, r.Config, "") + ctx, err := WithInstanceConfig(context.Background(), globalConfig.SMTP, r.Config, "", logrus.New()) require.NoError(r.T, err) NewAPIWithVersion(ctx, r.GlobalConfig, logrus.StandardLogger(), r.DB, "").handler.ServeHTTP(recorder, req) diff --git a/cmd/serve_cmd.go b/cmd/serve_cmd.go index 0f34592..9f9ae17 100644 --- a/cmd/serve_cmd.go +++ b/cmd/serve_cmd.go @@ -32,7 +32,7 @@ func serve(globalConfig *conf.GlobalConfiguration, log logrus.FieldLogger, confi } defer bgDB.Close() - ctx, err := api.WithInstanceConfig(context.Background(), globalConfig.SMTP, config, "") + ctx, err := api.WithInstanceConfig(context.Background(), globalConfig.SMTP, config, "", log) if err != nil { log.Fatalf("Error loading instance config: %+v", err) } diff --git a/mailer/mailer.go b/mailer/mailer.go index 7e1c7b3..2ba514b 100644 --- a/mailer/mailer.go +++ b/mailer/mailer.go @@ -29,7 +29,7 @@ type MailSubjects struct { } // NewMailer returns a new authlify mailer -func NewMailer(smtp conf.SMTPConfiguration, instanceConfig *conf.Configuration) Mailer { +func NewMailer(smtp conf.SMTPConfiguration, instanceConfig *conf.Configuration, log logrus.FieldLogger) Mailer { if smtp.Host == "" && instanceConfig.SMTP.Host == "" { return newNoopMailer() } @@ -69,7 +69,7 @@ func NewMailer(smtp conf.SMTPConfiguration, instanceConfig *conf.Configuration) "price": price, "hasProductType": hasProductType, }, - Logger: logrus.New(), + Logger: log, }, } } diff --git a/mailer/mailer_test.go b/mailer/mailer_test.go index 6a09d24..fb55b5a 100644 --- a/mailer/mailer_test.go +++ b/mailer/mailer_test.go @@ -4,13 +4,14 @@ import ( "testing" "github.com/netlify/gocommerce/conf" + "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" ) func TestNoopMailer(t *testing.T) { smtp := conf.SMTPConfiguration{} conf := &conf.Configuration{} - m := NewMailer(smtp, conf) + m := NewMailer(smtp, conf, logrus.New()) assert.IsType(t, &noopMailer{}, m) } @@ -21,6 +22,6 @@ func TestTemplateMailer(t *testing.T) { } conf := &conf.Configuration{} conf.SMTP.AdminEmail = "test@example.com" - m := NewMailer(smtp, conf) + m := NewMailer(smtp, conf, logrus.New()) assert.IsType(t, &mailer{}, m) }