Permalink
Browse files

Only mix in SMTP to instance config

  • Loading branch information...
brycekahle committed Oct 10, 2017
1 parent b8d3edd commit 310d1ed6855f46767cd66beeafbb8afa0865be68
Showing with 21 additions and 28 deletions.
  1. +1 −1 api/api_test.go
  2. +3 −3 api/middleware.go
  3. +3 −3 api/payments_test.go
  4. +1 −1 api/utils_test.go
  5. +1 −1 cmd/serve_cmd.go
  6. +7 −10 conf/configuration.go
  7. +2 −6 mailer/mailer.go
  8. +3 −3 mailer/mailer_test.go
View
@@ -20,7 +20,7 @@ func TestTraceWrapper(t *testing.T) {
config.Payment.Stripe.Enabled = true
config.Payment.Stripe.SecretKey = "secret"
ctx, err := WithInstanceConfig(context.Background(), globalConfig, config, "")
ctx, err := WithInstanceConfig(context.Background(), globalConfig.SMTP, config, "")
require.NoError(t, err)
api := NewAPIWithVersion(ctx, globalConfig, nil, "")
View
@@ -100,20 +100,20 @@ func (api *API) loadInstanceConfig(w http.ResponseWriter, r *http.Request) (cont
}
logEntrySetField(r, "site_url", config.SiteURL)
ctx, err = WithInstanceConfig(ctx, api.config, config, instanceID)
ctx, err = WithInstanceConfig(ctx, api.config.SMTP, config, instanceID)
if err != nil {
return nil, internalServerError("Error loading instance config").WithInternalError(err)
}
return ctx, nil
}
func WithInstanceConfig(ctx context.Context, global *conf.GlobalConfiguration, config *conf.Configuration, instanceID string) (context.Context, error) {
func WithInstanceConfig(ctx context.Context, smtp conf.SMTPConfiguration, config *conf.Configuration, instanceID string) (context.Context, error) {
ctx = gcontext.WithInstanceID(ctx, instanceID)
ctx = gcontext.WithConfig(ctx, config)
ctx = gcontext.WithCoupons(ctx, config)
mailer := mailer.NewMailer(&global.SMTP, config)
mailer := mailer.NewMailer(smtp, config)
ctx = gcontext.WithMailer(ctx, mailer)
store, err := assetstores.NewStore(config)
View
@@ -234,7 +234,7 @@ func TestPaymentsRefund(t *testing.T) {
globalConfig := new(conf.GlobalConfiguration)
provider := &memProvider{name: payments.StripeProvider}
ctx, err := WithInstanceConfig(context.Background(), globalConfig, test.Config, "")
ctx, err := WithInstanceConfig(context.Background(), globalConfig.SMTP, test.Config, "")
require.NoError(t, err)
ctx = gcontext.WithPaymentProviders(ctx, map[string]payments.Provider{payments.StripeProvider: provider})
@@ -465,7 +465,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, test.Config, "")
ctx, err := WithInstanceConfig(context.Background(), globalConfig.SMTP, test.Config, "")
require.NoError(t, err)
NewAPIWithVersion(ctx, test.GlobalConfig, test.DB, "").handler.ServeHTTP(recorder, req)
@@ -506,7 +506,7 @@ func TestPaymentPreauthorize(t *testing.T) {
req.Header.Set("Content-Type", "application/json")
globalConfig := new(conf.GlobalConfiguration)
ctx, err := WithInstanceConfig(context.Background(), globalConfig, test.Config, "")
ctx, err := WithInstanceConfig(context.Background(), globalConfig.SMTP, test.Config, "")
require.NoError(t, err)
NewAPIWithVersion(ctx, test.GlobalConfig, test.DB, "").handler.ServeHTTP(recorder, req)
View
@@ -349,7 +349,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, r.Config, "")
ctx, err := WithInstanceConfig(context.Background(), globalConfig.SMTP, r.Config, "")
require.NoError(r.T, err)
NewAPIWithVersion(ctx, r.GlobalConfig, r.DB, "").handler.ServeHTTP(recorder, req)
View
@@ -32,7 +32,7 @@ func serve(globalConfig *conf.GlobalConfiguration, config *conf.Configuration) {
}
defer bgDB.Close()
ctx, err := api.WithInstanceConfig(context.Background(), globalConfig, config, "")
ctx, err := api.WithInstanceConfig(context.Background(), globalConfig.SMTP, config, "")
if err != nil {
logrus.Fatalf("Error loading instance config: %+v", err)
}
View
@@ -2,7 +2,6 @@ package conf
import (
"os"
"time"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
@@ -25,12 +24,11 @@ type JWTConfiguration struct {
}
type SMTPConfiguration struct {
MaxFrequency time.Duration `json:"max_frequency" split_words:"true"`
Host string `json:"host"`
Port int `json:"port" default:"587"`
User string `json:"user"`
Pass string `json:"pass"`
AdminEmail string `json:"admin_email" split_words:"true"`
Host string `json:"host"`
Port int `json:"port" default:"587"`
User string `json:"user"`
Pass string `json:"pass"`
AdminEmail string `json:"admin_email" split_words:"true"`
}
// GlobalConfiguration holds all the global configuration for gocommerce
@@ -61,9 +59,8 @@ type Configuration struct {
SMTP SMTPConfiguration `json:"smtp"`
Mailer struct {
AdminEmail string `json:"admin_email" split_words:"true"`
Subjects EmailContentConfiguration `json:"subjects"`
Templates EmailContentConfiguration `json:"templates"`
Subjects EmailContentConfiguration `json:"subjects"`
Templates EmailContentConfiguration `json:"templates"`
} `json:"mailer"`
Payment struct {
View
@@ -28,7 +28,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) Mailer {
if smtp.Host == "" && instanceConfig.SMTP.Host == "" {
return newNoopMailer()
}
@@ -53,10 +53,6 @@ func NewMailer(smtp *conf.SMTPConfiguration, instanceConfig *conf.Configuration)
if smtpAdminEmail == "" {
smtpAdminEmail = smtp.AdminEmail
}
smtpMaxFrequency := instanceConfig.SMTP.MaxFrequency
if smtpMaxFrequency == 0 {
smtpMaxFrequency = smtp.MaxFrequency
}
return &mailer{
Config: instanceConfig,
@@ -140,7 +136,7 @@ const defaultReceivedTemplate = `<h2>Order Received From {{ .Order.Email }}</h2>
// OrderReceivedMail sends a notification to the shop admin
func (m *mailer) OrderReceivedMail(transaction *models.Transaction) error {
return m.TemplateMailer.Mail(
m.Config.Mailer.AdminEmail,
m.TemplateMailer.From,
withDefault(m.Config.Mailer.Subjects.OrderReceived, "Order Received From {{ .Order.Email }}"),
m.Config.Mailer.Templates.OrderReceived,
defaultReceivedTemplate,
View
@@ -8,19 +8,19 @@ import (
)
func TestNoopMailer(t *testing.T) {
smtp := &conf.SMTPConfiguration{}
smtp := conf.SMTPConfiguration{}
conf := &conf.Configuration{}
m := NewMailer(smtp, conf)
assert.IsType(t, &noopMailer{}, m)
}
func TestTemplateMailer(t *testing.T) {
smtp := &conf.SMTPConfiguration{
smtp := conf.SMTPConfiguration{
Host: "localhost",
Port: 25,
}
conf := &conf.Configuration{}
conf.Mailer.AdminEmail = "test@example.com"
conf.SMTP.AdminEmail = "test@example.com"
m := NewMailer(smtp, conf)
assert.IsType(t, &mailer{}, m)
}

0 comments on commit 310d1ed

Please sign in to comment.