Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass request logger to mailer #208

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")

Expand Down
7 changes: 4 additions & 3 deletions api/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/netlify/gocommerce/mailer"
"github.com/netlify/gocommerce/models"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

const (
Expand Down Expand Up @@ -100,23 +101,23 @@ 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)
}

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)
if err != nil {
return nil, err
}

mailer := mailer.NewMailer(smtp, config)
mailer := mailer.NewMailer(smtp, config, log)
ctx = gcontext.WithMailer(ctx, mailer)

store, err := assetstores.NewStore(config)
Expand Down
6 changes: 3 additions & 3 deletions api/payments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion api/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion cmd/serve_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions mailer/mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -69,7 +69,7 @@ func NewMailer(smtp conf.SMTPConfiguration, instanceConfig *conf.Configuration)
"price": price,
"hasProductType": hasProductType,
},
Logger: logrus.New(),
Logger: log,
},
}
}
Expand Down
5 changes: 3 additions & 2 deletions mailer/mailer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -21,6 +22,6 @@ func TestTemplateMailer(t *testing.T) {
}
conf := &conf.Configuration{}
conf.SMTP.AdminEmail = "[email protected]"
m := NewMailer(smtp, conf)
m := NewMailer(smtp, conf, logrus.New())
assert.IsType(t, &mailer{}, m)
}