-
-
Notifications
You must be signed in to change notification settings - Fork 391
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add admin email notifications on new comments
- Loading branch information
Showing
6 changed files
with
151 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,15 +100,17 @@ func TestEmailSendErrors(t *testing.T) { | |
|
||
e.verifyTmpl, err = template.New("test").Parse("{{.Test}}") | ||
assert.NoError(t, err) | ||
assert.EqualError(t, e.Send(context.Background(), Request{Email: "[email protected]", Verification: VerificationMetadata{Token: "some"}}), | ||
"error executing template to build verification message: template: test:1:2: executing \"test\" at <.Test>: can't evaluate field Test in type notify.verifyTmplData") | ||
err = e.Send(context.Background(), Request{Email: "[email protected]", Verification: VerificationMetadata{Token: "some"}}) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "error executing template to build verification message: template: test:1:2: executing \"test\" at <.Test>: can't evaluate field Test in type notify.verifyTmplData") | ||
e.verifyTmpl, err = template.New("test").Parse(defaultEmailVerificationTemplate) | ||
assert.NoError(t, err) | ||
|
||
e.msgTmpl, err = template.New("test").Parse("{{.Test}}") | ||
assert.NoError(t, err) | ||
assert.EqualError(t, e.Send(context.Background(), Request{Comment: store.Comment{ID: "999"}, parent: store.Comment{User: store.User{ID: "test"}}, Email: "[email protected]"}), | ||
"error executing template to build comment reply message: template: test:1:2: executing \"test\" at <.Test>: can't evaluate field Test in type notify.msgTmplData") | ||
err = e.Send(context.Background(), Request{Comment: store.Comment{ID: "999"}, parent: store.Comment{User: store.User{ID: "test"}}, Email: "[email protected]"}) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "error executing template to build comment reply message: template: test:1:2: executing \"test\" at <.Test>: can't evaluate field Test in type notify.msgTmplData") | ||
e.msgTmpl, err = template.New("test").Parse(defaultEmailTemplate) | ||
assert.NoError(t, err) | ||
|
||
|
@@ -118,8 +120,9 @@ func TestEmailSendErrors(t *testing.T) { | |
"sending message to \"[email protected]\" aborted due to canceled context") | ||
|
||
e.smtp = &fakeTestSMTP{} | ||
assert.EqualError(t, e.Send(context.Background(), Request{Comment: store.Comment{ID: "999"}, parent: store.Comment{User: store.User{ID: "error"}}, Email: "[email protected]"}), | ||
"error creating token for unsubscribe link: token generation error") | ||
err = e.Send(context.Background(), Request{Comment: store.Comment{ID: "999"}, parent: store.Comment{User: store.User{ID: "error"}}, Email: "[email protected]"}) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "error creating token for unsubscribe link: token generation error") | ||
e.msgTmpl, err = template.New("test").Parse(defaultEmailTemplate) | ||
assert.NoError(t, err) | ||
} | ||
|
@@ -187,7 +190,7 @@ func TestEmail_Send(t *testing.T) { | |
email.TokenGenFn = TokenGenFn | ||
email.UnsubscribeURL = "https://remark42.com/api/v1/email/unsubscribe" | ||
req := Request{ | ||
Comment: store.Comment{ID: "999", User: store.User{ID: "1", Name: "test_user"}, PostTitle: "test_title"}, | ||
Comment: store.Comment{ID: "999", User: store.User{ID: "1", Name: "test_user"}, ParentID: "1", PostTitle: "test_title"}, | ||
parent: store.Comment{ID: "1", User: store.User{ID: "999", Name: "parent_user"}}, | ||
Email: "[email protected]", | ||
} | ||
|
@@ -196,7 +199,7 @@ func TestEmail_Send(t *testing.T) { | |
assert.Equal(t, 1, fakeSmtp.readQuitCount()) | ||
assert.Equal(t, "[email protected]", fakeSmtp.readRcpt()) | ||
// test buildMessageFromRequest separately for message text | ||
res, err := email.buildMessageFromRequest(req) | ||
res, err := email.buildMessageFromRequest(req, false) | ||
assert.NoError(t, err) | ||
assert.Contains(t, res, `From: [email protected] | ||
To: [email protected] | ||
|
@@ -209,6 +212,47 @@ List-Unsubscribe: <https://remark42.com/api/v1/email/unsubscribe?site=&tkn=token | |
Date: `) | ||
} | ||
|
||
func TestEmail_SendAdmin(t *testing.T) { | ||
email, err := NewEmail(EmailParams{From: "[email protected]", AdminEmail: "[email protected]"}, SmtpParams{}) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, email) | ||
fakeSmtp := fakeTestSMTP{} | ||
email.smtp = &fakeSmtp | ||
email.TokenGenFn = TokenGenFn | ||
req := Request{ | ||
Comment: store.Comment{ID: "999", User: store.User{ID: "1", Name: "test_user"}, ParentID: "1", PostTitle: "test_title"}, | ||
parent: store.Comment{ID: "1", User: store.User{ID: "999", Name: "parent_user"}}, | ||
} | ||
assert.NoError(t, email.Send(context.TODO(), req)) | ||
assert.Equal(t, "[email protected]", fakeSmtp.readMail()) | ||
assert.Equal(t, 1, fakeSmtp.readQuitCount()) | ||
assert.Equal(t, "[email protected]", fakeSmtp.readRcpt()) | ||
// test buildMessageFromRequest separately for message text | ||
res, err := email.buildMessageFromRequest(req, true) | ||
assert.NoError(t, err) | ||
assert.Contains(t, res, `From: [email protected] | ||
To: [email protected] | ||
Subject: New comment to your site for "test_title" | ||
Content-Transfer-Encoding: quoted-printable | ||
MIME-version: 1.0 | ||
Content-Type: text/html; charset="UTF-8" | ||
Date: `) | ||
// send email to admin without parent set | ||
req = Request{ | ||
Comment: store.Comment{ID: "999", User: store.User{ID: "1", Name: "test_user"}, PostTitle: "test_title"}, | ||
} | ||
assert.NoError(t, email.Send(context.TODO(), req)) | ||
res, err = email.buildMessageFromRequest(req, true) | ||
assert.NoError(t, err) | ||
assert.Contains(t, res, `From: [email protected] | ||
To: [email protected] | ||
Subject: New comment to your site for "test_title" | ||
Content-Transfer-Encoding: quoted-printable | ||
MIME-version: 1.0 | ||
Content-Type: text/html; charset="UTF-8" | ||
Date: `) | ||
} | ||
|
||
func TestEmail_SendVerification(t *testing.T) { | ||
email, err := NewEmail(EmailParams{From: "[email protected]"}, SmtpParams{}) | ||
assert.NoError(t, err) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters