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

fix(config): diff email template content with remote #2873

Merged
merged 3 commits into from
Nov 13, 2024
Merged
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
4 changes: 2 additions & 2 deletions internal/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,10 @@ EOF
id+filepath.Ext(tmpl.ContentPath),
))
}
if len(tmpl.Subject) > 0 {
if tmpl.Subject != nil {
env = append(env, fmt.Sprintf("GOTRUE_MAILER_SUBJECTS_%s=%s",
strings.ToUpper(id),
tmpl.Subject,
*tmpl.Subject,
))
}
}
Expand Down
23 changes: 8 additions & 15 deletions pkg/config/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestApiToUpdatePostgrestConfigBody(t *testing.T) {
})
}

func TestApiDiffWithRemote(t *testing.T) {
func TestApiDiff(t *testing.T) {
t.Run("detects differences", func(t *testing.T) {
api := &api{
Enabled: true,
Expand All @@ -49,14 +49,9 @@ func TestApiDiffWithRemote(t *testing.T) {
}

diff, err := api.DiffWithRemote(remoteConfig)
assert.NoError(t, err, string(diff))

assert.Contains(t, string(diff), "-schemas = [\"public\"]")
assert.Contains(t, string(diff), "+schemas = [\"public\", \"private\"]")
assert.Contains(t, string(diff), "-extra_search_path = [\"public\"]")
assert.Contains(t, string(diff), "+extra_search_path = [\"extensions\", \"public\"]")
assert.Contains(t, string(diff), "-max_rows = 500")
assert.Contains(t, string(diff), "+max_rows = 1000")
assert.NoError(t, err)

assertSnapshotEqual(t, diff)
})

t.Run("handles no differences", func(t *testing.T) {
Expand Down Expand Up @@ -114,10 +109,9 @@ func TestApiDiffWithRemote(t *testing.T) {
}

diff, err := api.DiffWithRemote(remoteConfig)
assert.NoError(t, err, string(diff))
assert.NoError(t, err)

assert.Contains(t, string(diff), "-enabled = false")
assert.Contains(t, string(diff), "+enabled = true")
assertSnapshotEqual(t, diff)
})

t.Run("handles api disabled on local side", func(t *testing.T) {
Expand All @@ -135,9 +129,8 @@ func TestApiDiffWithRemote(t *testing.T) {
}

diff, err := api.DiffWithRemote(remoteConfig)
assert.NoError(t, err, string(diff))
assert.NoError(t, err)

assert.Contains(t, string(diff), "-enabled = true")
assert.Contains(t, string(diff), "+enabled = false")
assertSnapshotEqual(t, diff)
})
}
84 changes: 83 additions & 1 deletion pkg/config/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ type (
}

emailTemplate struct {
Subject string `toml:"subject"`
Subject *string `toml:"subject"`
Content *string `toml:"content"`
// Only content path is accepted in config.toml
ContentPath string `toml:"content_path"`
}

Expand Down Expand Up @@ -326,6 +328,28 @@ func (e email) toAuthConfigBody(body *v1API.UpdateAuthConfigBody) {
// Setting a single empty string disables SMTP
body.SmtpHost = cast.Ptr("")
}
if len(e.Template) == 0 {
return
}
var tmpl *emailTemplate
tmpl = cast.Ptr(e.Template["invite"])
body.MailerSubjectsInvite = tmpl.Subject
body.MailerTemplatesInviteContent = tmpl.Content
tmpl = cast.Ptr(e.Template["confirmation"])
body.MailerSubjectsConfirmation = tmpl.Subject
body.MailerTemplatesConfirmationContent = tmpl.Content
tmpl = cast.Ptr(e.Template["recovery"])
body.MailerSubjectsRecovery = tmpl.Subject
body.MailerTemplatesRecoveryContent = tmpl.Content
tmpl = cast.Ptr(e.Template["magic_link"])
body.MailerSubjectsMagicLink = tmpl.Subject
body.MailerTemplatesMagicLinkContent = tmpl.Content
tmpl = cast.Ptr(e.Template["email_change"])
body.MailerSubjectsEmailChange = tmpl.Subject
body.MailerTemplatesEmailChangeContent = tmpl.Content
tmpl = cast.Ptr(e.Template["reauthentication"])
body.MailerSubjectsReauthentication = tmpl.Subject
body.MailerTemplatesReauthenticationContent = tmpl.Content
}

func (e *email) fromAuthConfig(remoteConfig v1API.AuthConfigResponse) {
Expand All @@ -352,6 +376,64 @@ func (e *email) fromAuthConfig(remoteConfig v1API.AuthConfigResponse) {
} else {
e.Smtp = nil
}
if len(e.Template) == 0 {
return
}
var tmpl emailTemplate
// When local config is not set, we assume platform defaults should not change
tmpl = e.Template["invite"]
if tmpl.Subject != nil {
tmpl.Subject = remoteConfig.MailerSubjectsInvite
}
if tmpl.Content != nil {
tmpl.Content = remoteConfig.MailerTemplatesInviteContent
}
e.Template["invite"] = tmpl

tmpl = e.Template["confirmation"]
if tmpl.Subject != nil {
tmpl.Subject = remoteConfig.MailerSubjectsConfirmation
}
if tmpl.Content != nil {
tmpl.Content = remoteConfig.MailerTemplatesConfirmationContent
}
e.Template["confirmation"] = tmpl

tmpl = e.Template["recovery"]
if tmpl.Subject != nil {
tmpl.Subject = remoteConfig.MailerSubjectsRecovery
}
if tmpl.Content != nil {
tmpl.Content = remoteConfig.MailerTemplatesRecoveryContent
}
e.Template["recovery"] = tmpl

tmpl = e.Template["magic_link"]
if tmpl.Subject != nil {
tmpl.Subject = remoteConfig.MailerSubjectsMagicLink
}
if tmpl.Content != nil {
tmpl.Content = remoteConfig.MailerTemplatesMagicLinkContent
}
e.Template["magic_link"] = tmpl

tmpl = e.Template["email_change"]
if tmpl.Subject != nil {
tmpl.Subject = remoteConfig.MailerSubjectsEmailChange
}
if tmpl.Content != nil {
tmpl.Content = remoteConfig.MailerTemplatesEmailChangeContent
}
e.Template["email_change"] = tmpl

tmpl = e.Template["reauthentication"]
if tmpl.Subject != nil {
tmpl.Subject = remoteConfig.MailerSubjectsReauthentication
}
if tmpl.Content != nil {
tmpl.Content = remoteConfig.MailerTemplatesReauthenticationContent
}
e.Template["reauthentication"] = tmpl
}

func (s sms) toAuthConfigBody(body *v1API.UpdateAuthConfigBody) {
Expand Down
Loading