|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class Shopkeeper::NotificationMailerTest < ActionMailer::TestCase |
| 4 | + setup do |
| 5 | + @shopkeeper = shopkeepers(:one) |
| 6 | + @shopkeeper.create_default_account |
| 7 | + @account = @shopkeeper.accounts.first |
| 8 | + @invitation = AccountsInvitation.create!( |
| 9 | + account: @account, |
| 10 | + name: "Invited User", |
| 11 | + email: "invited@example.com", |
| 12 | + member: true, |
| 13 | + invited_by: @shopkeeper |
| 14 | + ) |
| 15 | + end |
| 16 | + |
| 17 | + test "invited renders the expected subject, recipient, and body" do |
| 18 | + mail = Shopkeeper::NotificationMailer.with(accounts_invitation: @invitation).invited |
| 19 | + |
| 20 | + expected_subject = I18n.t( |
| 21 | + "shopkeeper.notification_mailer.invited.subject", |
| 22 | + inviter: @shopkeeper.name, |
| 23 | + account: @account.name |
| 24 | + ) |
| 25 | + assert_equal expected_subject, mail.subject |
| 26 | + assert_equal [@invitation.email], mail.to |
| 27 | + assert_equal [ConfigSettings.email.default_from], mail.from |
| 28 | + assert_match @invitation.token, mail.body.encoded |
| 29 | + assert_match @account.name, mail.body.encoded |
| 30 | + end |
| 31 | + |
| 32 | + test "confirmation_instructions renders the expected subject, recipient, and body" do |
| 33 | + mail = Shopkeeper::NotificationMailer.with( |
| 34 | + resource: @shopkeeper, |
| 35 | + token: "confirm-token-123", |
| 36 | + opts: {redirect_url: "http://example.com/confirm", client_config: "default"} |
| 37 | + ).confirmation_instructions |
| 38 | + |
| 39 | + assert_equal I18n.t("shopkeeper.notification_mailer.confirmation_instructions.subject"), mail.subject |
| 40 | + assert_equal [@shopkeeper.email], mail.to |
| 41 | + assert_match "confirm-token-123", mail.body.encoded |
| 42 | + assert_match @shopkeeper.email, mail.body.encoded |
| 43 | + end |
| 44 | + |
| 45 | + test "confirmation_instructions uses opts[:to] when provided" do |
| 46 | + mail = Shopkeeper::NotificationMailer.with( |
| 47 | + resource: @shopkeeper, |
| 48 | + token: "tok", |
| 49 | + opts: {to: "override@example.com", redirect_url: "http://example.com/confirm", client_config: "default"} |
| 50 | + ).confirmation_instructions |
| 51 | + |
| 52 | + assert_equal ["override@example.com"], mail.to |
| 53 | + assert_match "override@example.com", mail.body.encoded |
| 54 | + end |
| 55 | + |
| 56 | + test "reset_password_instructions renders the expected subject, recipient, and body" do |
| 57 | + mail = Shopkeeper::NotificationMailer.with( |
| 58 | + resource: @shopkeeper, |
| 59 | + token: "reset-token-xyz", |
| 60 | + opts: {redirect_url: "http://example.com/reset", client_config: "default"} |
| 61 | + ).reset_password_instructions |
| 62 | + |
| 63 | + assert_equal I18n.t("shopkeeper.notification_mailer.reset_password_instructions.subject"), mail.subject |
| 64 | + assert_equal [@shopkeeper.email], mail.to |
| 65 | + assert_match "reset-token-xyz", mail.body.encoded |
| 66 | + end |
| 67 | +end |
0 commit comments