merge v0.23.0-rc changes
This commit is contained in:
@@ -1,76 +0,0 @@
|
||||
package mails
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/mail"
|
||||
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/mails/templates"
|
||||
"github.com/pocketbase/pocketbase/models"
|
||||
"github.com/pocketbase/pocketbase/tokens"
|
||||
"github.com/pocketbase/pocketbase/tools/mailer"
|
||||
"github.com/pocketbase/pocketbase/tools/rest"
|
||||
)
|
||||
|
||||
// SendAdminPasswordReset sends a password reset request email to the specified admin.
|
||||
func SendAdminPasswordReset(app core.App, admin *models.Admin) error {
|
||||
token, tokenErr := tokens.NewAdminResetPasswordToken(app, admin)
|
||||
if tokenErr != nil {
|
||||
return tokenErr
|
||||
}
|
||||
|
||||
actionUrl, urlErr := rest.NormalizeUrl(fmt.Sprintf(
|
||||
"%s/_/#/confirm-password-reset/%s",
|
||||
app.Settings().Meta.AppUrl,
|
||||
token,
|
||||
))
|
||||
if urlErr != nil {
|
||||
return urlErr
|
||||
}
|
||||
|
||||
params := struct {
|
||||
AppName string
|
||||
AppUrl string
|
||||
Admin *models.Admin
|
||||
Token string
|
||||
ActionUrl string
|
||||
}{
|
||||
AppName: app.Settings().Meta.AppName,
|
||||
AppUrl: app.Settings().Meta.AppUrl,
|
||||
Admin: admin,
|
||||
Token: token,
|
||||
ActionUrl: actionUrl,
|
||||
}
|
||||
|
||||
mailClient := app.NewMailClient()
|
||||
|
||||
// resolve body template
|
||||
body, renderErr := resolveTemplateContent(params, templates.Layout, templates.AdminPasswordResetBody)
|
||||
if renderErr != nil {
|
||||
return renderErr
|
||||
}
|
||||
|
||||
message := &mailer.Message{
|
||||
From: mail.Address{
|
||||
Name: app.Settings().Meta.SenderName,
|
||||
Address: app.Settings().Meta.SenderAddress,
|
||||
},
|
||||
To: []mail.Address{{Address: admin.Email}},
|
||||
Subject: "Reset admin password",
|
||||
HTML: body,
|
||||
}
|
||||
|
||||
event := new(core.MailerAdminEvent)
|
||||
event.MailClient = mailClient
|
||||
event.Message = message
|
||||
event.Admin = admin
|
||||
event.Meta = map[string]any{"token": token}
|
||||
|
||||
return app.OnMailerBeforeAdminResetPasswordSend().Trigger(event, func(e *core.MailerAdminEvent) error {
|
||||
if err := e.MailClient.Send(e.Message); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return app.OnMailerAfterAdminResetPasswordSend().Trigger(e)
|
||||
})
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package mails_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/pocketbase/pocketbase/mails"
|
||||
"github.com/pocketbase/pocketbase/tests"
|
||||
)
|
||||
|
||||
func TestSendAdminPasswordReset(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testApp, _ := tests.NewTestApp()
|
||||
defer testApp.Cleanup()
|
||||
|
||||
// ensure that action url normalization will be applied
|
||||
testApp.Settings().Meta.AppUrl = "http://localhost:8090////"
|
||||
|
||||
admin, _ := testApp.Dao().FindAdminByEmail("test@example.com")
|
||||
|
||||
err := mails.SendAdminPasswordReset(testApp, admin)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if testApp.TestMailer.TotalSend != 1 {
|
||||
t.Fatalf("Expected one email to be sent, got %d", testApp.TestMailer.TotalSend)
|
||||
}
|
||||
|
||||
expectedParts := []string{
|
||||
"http://localhost:8090/_/#/confirm-password-reset/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.",
|
||||
}
|
||||
for _, part := range expectedParts {
|
||||
if !strings.Contains(testApp.TestMailer.LastMessage.HTML, part) {
|
||||
t.Fatalf("Couldn't find %s \nin\n %s", part, testApp.TestMailer.LastMessage.HTML)
|
||||
}
|
||||
}
|
||||
}
|
||||
+142
-100
@@ -6,59 +6,14 @@ import (
|
||||
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/mails/templates"
|
||||
"github.com/pocketbase/pocketbase/models"
|
||||
"github.com/pocketbase/pocketbase/models/settings"
|
||||
"github.com/pocketbase/pocketbase/tokens"
|
||||
"github.com/pocketbase/pocketbase/tools/mailer"
|
||||
)
|
||||
|
||||
// @todo remove after the refactoring
|
||||
//
|
||||
// SendRecordPasswordLoginAlert sends a OAuth2 password login alert to the specified auth record.
|
||||
func SendRecordPasswordLoginAlert(app core.App, authRecord *models.Record, providerNames ...string) error {
|
||||
params := struct {
|
||||
AppName string
|
||||
AppUrl string
|
||||
Record *models.Record
|
||||
ProviderNames []string
|
||||
}{
|
||||
AppName: app.Settings().Meta.AppName,
|
||||
AppUrl: app.Settings().Meta.AppUrl,
|
||||
Record: authRecord,
|
||||
ProviderNames: providerNames,
|
||||
}
|
||||
|
||||
// SendRecordAuthAlert sends a new device login alert to the specified auth record.
|
||||
func SendRecordAuthAlert(app core.App, authRecord *core.Record) error {
|
||||
mailClient := app.NewMailClient()
|
||||
|
||||
// resolve body template
|
||||
body, renderErr := resolveTemplateContent(params, templates.Layout, templates.PasswordLoginAlertBody)
|
||||
if renderErr != nil {
|
||||
return renderErr
|
||||
}
|
||||
|
||||
message := &mailer.Message{
|
||||
From: mail.Address{
|
||||
Name: app.Settings().Meta.SenderName,
|
||||
Address: app.Settings().Meta.SenderAddress,
|
||||
},
|
||||
To: []mail.Address{{Address: authRecord.Email()}},
|
||||
Subject: "Password login alert",
|
||||
HTML: body,
|
||||
}
|
||||
|
||||
return mailClient.Send(message)
|
||||
}
|
||||
|
||||
// SendRecordPasswordReset sends a password reset request email to the specified user.
|
||||
func SendRecordPasswordReset(app core.App, authRecord *models.Record) error {
|
||||
token, tokenErr := tokens.NewRecordResetPasswordToken(app, authRecord)
|
||||
if tokenErr != nil {
|
||||
return tokenErr
|
||||
}
|
||||
|
||||
mailClient := app.NewMailClient()
|
||||
|
||||
subject, body, err := resolveEmailTemplate(app, token, app.Settings().Meta.ResetPasswordTemplate)
|
||||
subject, body, err := resolveEmailTemplate(app, authRecord, authRecord.Collection().AuthAlert.EmailTemplate, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -74,31 +29,24 @@ func SendRecordPasswordReset(app core.App, authRecord *models.Record) error {
|
||||
}
|
||||
|
||||
event := new(core.MailerRecordEvent)
|
||||
event.MailClient = mailClient
|
||||
event.App = app
|
||||
event.Mailer = mailClient
|
||||
event.Message = message
|
||||
event.Collection = authRecord.Collection()
|
||||
event.Record = authRecord
|
||||
event.Meta = map[string]any{"token": token}
|
||||
|
||||
return app.OnMailerBeforeRecordResetPasswordSend().Trigger(event, func(e *core.MailerRecordEvent) error {
|
||||
if err := e.MailClient.Send(e.Message); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return app.OnMailerAfterRecordResetPasswordSend().Trigger(e)
|
||||
return app.OnMailerRecordAuthAlertSend().Trigger(event, func(e *core.MailerRecordEvent) error {
|
||||
return e.Mailer.Send(e.Message)
|
||||
})
|
||||
}
|
||||
|
||||
// SendRecordVerification sends a verification request email to the specified user.
|
||||
func SendRecordVerification(app core.App, authRecord *models.Record) error {
|
||||
token, tokenErr := tokens.NewRecordVerifyToken(app, authRecord)
|
||||
if tokenErr != nil {
|
||||
return tokenErr
|
||||
}
|
||||
|
||||
// SendRecordOTP sends OTP email to the specified auth record.
|
||||
func SendRecordOTP(app core.App, authRecord *core.Record, otpId string, pass string) error {
|
||||
mailClient := app.NewMailClient()
|
||||
|
||||
subject, body, err := resolveEmailTemplate(app, token, app.Settings().Meta.VerificationTemplate)
|
||||
subject, body, err := resolveEmailTemplate(app, authRecord, authRecord.Collection().OTP.EmailTemplate, map[string]any{
|
||||
core.EmailPlaceholderOTPId: otpId,
|
||||
core.EmailPlaceholderOTP: pass,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -114,31 +62,108 @@ func SendRecordVerification(app core.App, authRecord *models.Record) error {
|
||||
}
|
||||
|
||||
event := new(core.MailerRecordEvent)
|
||||
event.MailClient = mailClient
|
||||
event.App = app
|
||||
event.Mailer = mailClient
|
||||
event.Message = message
|
||||
event.Collection = authRecord.Collection()
|
||||
event.Record = authRecord
|
||||
event.Meta = map[string]any{"token": token}
|
||||
event.Meta = map[string]any{
|
||||
"otpId": otpId,
|
||||
"password": pass,
|
||||
}
|
||||
|
||||
return app.OnMailerBeforeRecordVerificationSend().Trigger(event, func(e *core.MailerRecordEvent) error {
|
||||
if err := e.MailClient.Send(e.Message); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return app.OnMailerAfterRecordVerificationSend().Trigger(e)
|
||||
return app.OnMailerRecordOTPSend().Trigger(event, func(e *core.MailerRecordEvent) error {
|
||||
return e.Mailer.Send(e.Message)
|
||||
})
|
||||
}
|
||||
|
||||
// SendRecordChangeEmail sends a change email confirmation email to the specified user.
|
||||
func SendRecordChangeEmail(app core.App, record *models.Record, newEmail string) error {
|
||||
token, tokenErr := tokens.NewRecordChangeEmailToken(app, record, newEmail)
|
||||
// SendRecordPasswordReset sends a password reset request email to the specified auth record.
|
||||
func SendRecordPasswordReset(app core.App, authRecord *core.Record) error {
|
||||
token, tokenErr := authRecord.NewPasswordResetToken()
|
||||
if tokenErr != nil {
|
||||
return tokenErr
|
||||
}
|
||||
|
||||
mailClient := app.NewMailClient()
|
||||
|
||||
subject, body, err := resolveEmailTemplate(app, token, app.Settings().Meta.ConfirmEmailChangeTemplate)
|
||||
subject, body, err := resolveEmailTemplate(app, authRecord, authRecord.Collection().ResetPasswordTemplate, map[string]any{
|
||||
core.EmailPlaceholderToken: token,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
message := &mailer.Message{
|
||||
From: mail.Address{
|
||||
Name: app.Settings().Meta.SenderName,
|
||||
Address: app.Settings().Meta.SenderAddress,
|
||||
},
|
||||
To: []mail.Address{{Address: authRecord.Email()}},
|
||||
Subject: subject,
|
||||
HTML: body,
|
||||
}
|
||||
|
||||
event := new(core.MailerRecordEvent)
|
||||
event.App = app
|
||||
event.Mailer = mailClient
|
||||
event.Message = message
|
||||
event.Record = authRecord
|
||||
event.Meta = map[string]any{"token": token}
|
||||
|
||||
return app.OnMailerRecordPasswordResetSend().Trigger(event, func(e *core.MailerRecordEvent) error {
|
||||
return e.Mailer.Send(e.Message)
|
||||
})
|
||||
}
|
||||
|
||||
// SendRecordVerification sends a verification request email to the specified auth record.
|
||||
func SendRecordVerification(app core.App, authRecord *core.Record) error {
|
||||
token, tokenErr := authRecord.NewVerificationToken()
|
||||
if tokenErr != nil {
|
||||
return tokenErr
|
||||
}
|
||||
|
||||
mailClient := app.NewMailClient()
|
||||
|
||||
subject, body, err := resolveEmailTemplate(app, authRecord, authRecord.Collection().VerificationTemplate, map[string]any{
|
||||
core.EmailPlaceholderToken: token,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
message := &mailer.Message{
|
||||
From: mail.Address{
|
||||
Name: app.Settings().Meta.SenderName,
|
||||
Address: app.Settings().Meta.SenderAddress,
|
||||
},
|
||||
To: []mail.Address{{Address: authRecord.Email()}},
|
||||
Subject: subject,
|
||||
HTML: body,
|
||||
}
|
||||
|
||||
event := new(core.MailerRecordEvent)
|
||||
event.App = app
|
||||
event.Mailer = mailClient
|
||||
event.Message = message
|
||||
event.Record = authRecord
|
||||
event.Meta = map[string]any{"token": token}
|
||||
|
||||
return app.OnMailerRecordVerificationSend().Trigger(event, func(e *core.MailerRecordEvent) error {
|
||||
return e.Mailer.Send(e.Message)
|
||||
})
|
||||
}
|
||||
|
||||
// SendRecordChangeEmail sends a change email confirmation email to the specified auth record.
|
||||
func SendRecordChangeEmail(app core.App, authRecord *core.Record, newEmail string) error {
|
||||
token, tokenErr := authRecord.NewEmailChangeToken(newEmail)
|
||||
if tokenErr != nil {
|
||||
return tokenErr
|
||||
}
|
||||
|
||||
mailClient := app.NewMailClient()
|
||||
|
||||
subject, body, err := resolveEmailTemplate(app, authRecord, authRecord.Collection().ConfirmEmailChangeTemplate, map[string]any{
|
||||
core.EmailPlaceholderToken: token,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -154,42 +179,59 @@ func SendRecordChangeEmail(app core.App, record *models.Record, newEmail string)
|
||||
}
|
||||
|
||||
event := new(core.MailerRecordEvent)
|
||||
event.MailClient = mailClient
|
||||
event.App = app
|
||||
event.Mailer = mailClient
|
||||
event.Message = message
|
||||
event.Collection = record.Collection()
|
||||
event.Record = record
|
||||
event.Record = authRecord
|
||||
event.Meta = map[string]any{
|
||||
"token": token,
|
||||
"newEmail": newEmail,
|
||||
}
|
||||
|
||||
return app.OnMailerBeforeRecordChangeEmailSend().Trigger(event, func(e *core.MailerRecordEvent) error {
|
||||
if err := e.MailClient.Send(e.Message); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return app.OnMailerAfterRecordChangeEmailSend().Trigger(e)
|
||||
return app.OnMailerRecordEmailChangeSend().Trigger(event, func(e *core.MailerRecordEvent) error {
|
||||
return e.Mailer.Send(e.Message)
|
||||
})
|
||||
}
|
||||
|
||||
func resolveEmailTemplate(
|
||||
app core.App,
|
||||
token string,
|
||||
emailTemplate settings.EmailTemplate,
|
||||
authRecord *core.Record,
|
||||
emailTemplate core.EmailTemplate,
|
||||
placeholders map[string]any,
|
||||
) (subject string, body string, err error) {
|
||||
subject, rawBody, _ := emailTemplate.Resolve(
|
||||
app.Settings().Meta.AppName,
|
||||
app.Settings().Meta.AppUrl,
|
||||
token,
|
||||
)
|
||||
|
||||
params := struct {
|
||||
HtmlContent template.HTML
|
||||
}{
|
||||
HtmlContent: template.HTML(rawBody),
|
||||
if placeholders == nil {
|
||||
placeholders = map[string]any{}
|
||||
}
|
||||
|
||||
body, err = resolveTemplateContent(params, templates.Layout, templates.HtmlBody)
|
||||
// register default system placeholders
|
||||
if _, ok := placeholders[core.EmailPlaceholderAppName]; !ok {
|
||||
placeholders[core.EmailPlaceholderAppName] = app.Settings().Meta.AppName
|
||||
}
|
||||
if _, ok := placeholders[core.EmailPlaceholderAppURL]; !ok {
|
||||
placeholders[core.EmailPlaceholderAppURL] = app.Settings().Meta.AppURL
|
||||
}
|
||||
|
||||
// register default auth record placeholders
|
||||
for _, field := range authRecord.Collection().Fields {
|
||||
if field.GetHidden() {
|
||||
continue
|
||||
}
|
||||
|
||||
fieldPlacehodler := "{RECORD:" + field.GetName() + "}"
|
||||
if _, ok := placeholders[fieldPlacehodler]; !ok {
|
||||
placeholders[fieldPlacehodler] = authRecord.Get(field.GetName())
|
||||
}
|
||||
}
|
||||
|
||||
subject, rawBody := emailTemplate.Resolve(placeholders)
|
||||
|
||||
params := struct {
|
||||
HTMLContent template.HTML
|
||||
}{
|
||||
HTMLContent: template.HTML(rawBody),
|
||||
}
|
||||
|
||||
body, err = resolveTemplateContent(params, templates.Layout, templates.HTMLBody)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
+60
-30
@@ -8,31 +8,32 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tests"
|
||||
)
|
||||
|
||||
func TestSendRecordPasswordLoginAlert(t *testing.T) {
|
||||
func TestSendRecordAuthAlert(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testApp, _ := tests.NewTestApp()
|
||||
defer testApp.Cleanup()
|
||||
|
||||
// ensure that action url normalization will be applied
|
||||
testApp.Settings().Meta.AppUrl = "http://localhost:8090////"
|
||||
user, _ := testApp.FindFirstRecordByData("users", "email", "test@example.com")
|
||||
|
||||
user, _ := testApp.Dao().FindFirstRecordByData("users", "email", "test@example.com")
|
||||
|
||||
err := mails.SendRecordPasswordLoginAlert(testApp, user, "test1", "test2")
|
||||
err := mails.SendRecordAuthAlert(testApp, user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if testApp.TestMailer.TotalSend != 1 {
|
||||
t.Fatalf("Expected one email to be sent, got %d", testApp.TestMailer.TotalSend)
|
||||
if testApp.TestMailer.TotalSend() != 1 {
|
||||
t.Fatalf("Expected one email to be sent, got %d", testApp.TestMailer.TotalSend())
|
||||
}
|
||||
|
||||
expectedParts := []string{"using a password", "OAuth2", "test1", "test2", "auth linked"}
|
||||
|
||||
expectedParts := []string{
|
||||
user.GetString("name") + "{RECORD:tokenKey}", // public and private record placeholder checks
|
||||
"login to your " + testApp.Settings().Meta.AppName + " account from a new location",
|
||||
"If this was you",
|
||||
"If this wasn't you",
|
||||
}
|
||||
for _, part := range expectedParts {
|
||||
if !strings.Contains(testApp.TestMailer.LastMessage.HTML, part) {
|
||||
t.Fatalf("Couldn't find %s\n in\n %s", part, testApp.TestMailer.LastMessage.HTML)
|
||||
if !strings.Contains(testApp.TestMailer.LastMessage().HTML, part) {
|
||||
t.Fatalf("Couldn't find %s \nin\n %s", part, testApp.TestMailer.LastMessage().HTML)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,26 +44,24 @@ func TestSendRecordPasswordReset(t *testing.T) {
|
||||
testApp, _ := tests.NewTestApp()
|
||||
defer testApp.Cleanup()
|
||||
|
||||
// ensure that action url normalization will be applied
|
||||
testApp.Settings().Meta.AppUrl = "http://localhost:8090////"
|
||||
|
||||
user, _ := testApp.Dao().FindFirstRecordByData("users", "email", "test@example.com")
|
||||
user, _ := testApp.FindFirstRecordByData("users", "email", "test@example.com")
|
||||
|
||||
err := mails.SendRecordPasswordReset(testApp, user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if testApp.TestMailer.TotalSend != 1 {
|
||||
t.Fatalf("Expected one email to be sent, got %d", testApp.TestMailer.TotalSend)
|
||||
if testApp.TestMailer.TotalSend() != 1 {
|
||||
t.Fatalf("Expected one email to be sent, got %d", testApp.TestMailer.TotalSend())
|
||||
}
|
||||
|
||||
expectedParts := []string{
|
||||
user.GetString("name") + "{RECORD:tokenKey}", // the record name as {RECORD:name}
|
||||
"http://localhost:8090/_/#/auth/confirm-password-reset/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.",
|
||||
}
|
||||
for _, part := range expectedParts {
|
||||
if !strings.Contains(testApp.TestMailer.LastMessage.HTML, part) {
|
||||
t.Fatalf("Couldn't find %s \nin\n %s", part, testApp.TestMailer.LastMessage.HTML)
|
||||
if !strings.Contains(testApp.TestMailer.LastMessage().HTML, part) {
|
||||
t.Fatalf("Couldn't find %s \nin\n %s", part, testApp.TestMailer.LastMessage().HTML)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,23 +72,24 @@ func TestSendRecordVerification(t *testing.T) {
|
||||
testApp, _ := tests.NewTestApp()
|
||||
defer testApp.Cleanup()
|
||||
|
||||
user, _ := testApp.Dao().FindFirstRecordByData("users", "email", "test@example.com")
|
||||
user, _ := testApp.FindFirstRecordByData("users", "email", "test@example.com")
|
||||
|
||||
err := mails.SendRecordVerification(testApp, user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if testApp.TestMailer.TotalSend != 1 {
|
||||
t.Fatalf("Expected one email to be sent, got %d", testApp.TestMailer.TotalSend)
|
||||
if testApp.TestMailer.TotalSend() != 1 {
|
||||
t.Fatalf("Expected one email to be sent, got %d", testApp.TestMailer.TotalSend())
|
||||
}
|
||||
|
||||
expectedParts := []string{
|
||||
user.GetString("name") + "{RECORD:tokenKey}", // the record name as {RECORD:name}
|
||||
"http://localhost:8090/_/#/auth/confirm-verification/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.",
|
||||
}
|
||||
for _, part := range expectedParts {
|
||||
if !strings.Contains(testApp.TestMailer.LastMessage.HTML, part) {
|
||||
t.Fatalf("Couldn't find %s \nin\n %s", part, testApp.TestMailer.LastMessage.HTML)
|
||||
if !strings.Contains(testApp.TestMailer.LastMessage().HTML, part) {
|
||||
t.Fatalf("Couldn't find %s \nin\n %s", part, testApp.TestMailer.LastMessage().HTML)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -100,23 +100,53 @@ func TestSendRecordChangeEmail(t *testing.T) {
|
||||
testApp, _ := tests.NewTestApp()
|
||||
defer testApp.Cleanup()
|
||||
|
||||
user, _ := testApp.Dao().FindFirstRecordByData("users", "email", "test@example.com")
|
||||
user, _ := testApp.FindFirstRecordByData("users", "email", "test@example.com")
|
||||
|
||||
err := mails.SendRecordChangeEmail(testApp, user, "new_test@example.com")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if testApp.TestMailer.TotalSend != 1 {
|
||||
t.Fatalf("Expected one email to be sent, got %d", testApp.TestMailer.TotalSend)
|
||||
if testApp.TestMailer.TotalSend() != 1 {
|
||||
t.Fatalf("Expected one email to be sent, got %d", testApp.TestMailer.TotalSend())
|
||||
}
|
||||
|
||||
expectedParts := []string{
|
||||
user.GetString("name") + "{RECORD:tokenKey}", // the record name as {RECORD:name}
|
||||
"http://localhost:8090/_/#/auth/confirm-email-change/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.",
|
||||
}
|
||||
for _, part := range expectedParts {
|
||||
if !strings.Contains(testApp.TestMailer.LastMessage.HTML, part) {
|
||||
t.Fatalf("Couldn't find %s \nin\n %s", part, testApp.TestMailer.LastMessage.HTML)
|
||||
if !strings.Contains(testApp.TestMailer.LastMessage().HTML, part) {
|
||||
t.Fatalf("Couldn't find %s \nin\n %s", part, testApp.TestMailer.LastMessage().HTML)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendRecordOTP(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testApp, _ := tests.NewTestApp()
|
||||
defer testApp.Cleanup()
|
||||
|
||||
user, _ := testApp.FindFirstRecordByData("users", "email", "test@example.com")
|
||||
|
||||
err := mails.SendRecordOTP(testApp, user, "test_otp_id", "test_otp_code")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if testApp.TestMailer.TotalSend() != 1 {
|
||||
t.Fatalf("Expected one email to be sent, got %d", testApp.TestMailer.TotalSend())
|
||||
}
|
||||
|
||||
expectedParts := []string{
|
||||
user.GetString("name") + "{RECORD:tokenKey}", // the record name as {RECORD:name}
|
||||
"one-time password",
|
||||
"test_otp_code",
|
||||
}
|
||||
for _, part := range expectedParts {
|
||||
if !strings.Contains(testApp.TestMailer.LastMessage().HTML, part) {
|
||||
t.Fatalf("Couldn't find %s \nin\n %s", part, testApp.TestMailer.LastMessage().HTML)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
package templates
|
||||
|
||||
// Available variables:
|
||||
//
|
||||
// ```
|
||||
// Admin *models.Admin
|
||||
// AppName string
|
||||
// AppUrl string
|
||||
// Token string
|
||||
// ActionUrl string
|
||||
// ```
|
||||
const AdminPasswordResetBody = `
|
||||
{{define "content"}}
|
||||
<p>Hello,</p>
|
||||
<p>Follow this link to reset your admin password for {{.AppName}}.</p>
|
||||
<p>
|
||||
<a class="btn" href="{{.ActionUrl}}" target="_blank" rel="noopener">Reset password</a>
|
||||
</p>
|
||||
<p><i>If you did not request to reset your password, please ignore this email and the link will expire on its own.</i></p>
|
||||
{{end}}
|
||||
`
|
||||
@@ -3,6 +3,6 @@ package templates
|
||||
// Available variables:
|
||||
//
|
||||
// ```
|
||||
// HtmlContent template.HTML
|
||||
// HTMLContent template.HTML
|
||||
// ```
|
||||
const HtmlBody = `{{define "content"}}{{.HtmlContent}}{{end}}`
|
||||
const HTMLBody = `{{define "content"}}{{.HTMLContent}}{{end}}`
|
||||
|
||||
@@ -53,7 +53,7 @@ const Layout = `
|
||||
.btn {
|
||||
display: inline-block;
|
||||
vertical-align: top;
|
||||
border: 1px solid #e5e5e5;
|
||||
border: 0;
|
||||
cursor: pointer;
|
||||
color: #fff !important;
|
||||
background: #16161a !important;
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
package templates
|
||||
|
||||
// Available variables:
|
||||
//
|
||||
// ```
|
||||
// Record *models.Record
|
||||
// AppName string
|
||||
// AppUrl string
|
||||
// ProviderNames []string
|
||||
// ```
|
||||
const PasswordLoginAlertBody = `
|
||||
{{define "content"}}
|
||||
<p>Hello,</p>
|
||||
<p>
|
||||
Just to let you know that someone has logged in to your {{.AppName}} account using a password while you already have
|
||||
OAuth2
|
||||
{{range $index, $provider := .ProviderNames }}
|
||||
{{if $index}}|{{end}}
|
||||
{{ $provider }}
|
||||
{{ end }}
|
||||
auth linked.
|
||||
</p>
|
||||
<p>If you have recently signed in with a password, you may disregard this email.</p>
|
||||
<p><strong>If you don't recognize the above action, you should immediately change your {{.AppName}} account password.</strong></p>
|
||||
<p>
|
||||
Thanks,<br/>
|
||||
{{.AppName}} team
|
||||
</p>
|
||||
{{end}}
|
||||
`
|
||||
Reference in New Issue
Block a user