Add scheduled email feature for reminders

This commit is contained in:
2020-02-12 14:19:27 +01:00
parent e4a317111c
commit 9ee37b42d8
13 changed files with 127 additions and 4 deletions

View File

@ -0,0 +1,13 @@
require 'rails_helper'
RSpec.describe "ResetPassword", type: :feature do
it "sends an email upon a password reset request" do
user = create(:user)
visit new_user_session_path
click_link 'password'
fill_in 'Email', with: user.email
click_button 'reset password'
expect(page).to have_content 'You will receive an email with instructions on how to reset your password in a few minutes.'
expect(last_email).to have_content user.email
end
end

View File

@ -0,0 +1,7 @@
# Preview all emails at http://localhost:3000/rails/mailers/reminder_mailer
class ReminderMailerPreview < ActionMailer::Preview
# Preview this email at http://localhost:3000/rails/mailers/reminder_mailer/schedule_email
def schedule_email
ReminderMailer.schedule_email(FactoryBot.build(:reminder), 'to@example.com')
end
end

View File

@ -0,0 +1,29 @@
require 'rails_helper'
RSpec.describe ReminderMailer, type: :mailer do
describe 'Email delivery' do
let(:reminder) { build :reminder }
let(:mail) { ReminderMailer.schedule_email reminder, 'to@example.org' }
it 'renders the headers' do
expect(mail.subject).to eq 'Reminder'
expect(mail.to).to eq ['to@example.org']
expect(mail.from).to eq ['from@example.com']
end
it 'renders the body' do
expect(mail.body.encoded).to have_content reminder.title
expect(mail.body.encoded).to have_content reminder.body
end
end
describe 'Scheduled email' do
before { clear_enqueued_jobs }
let(:reminder) { create :reminder }
it 'queues the reminder' do
expect { ReminderMailer.schedule_email(reminder, 'to@example.org').deliver_later(wait_until: reminder.date) }.to have_enqueued_job.on_queue('mailers')
expect { ReminderMailer.schedule_email(reminder, 'to@example.org').deliver_later(wait_until: reminder.date) }.to have_enqueued_job.at(reminder.date)
end
end
end

View File

@ -35,7 +35,7 @@ rescue ActiveRecord::PendingMigrationError => e
end
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
@ -63,5 +63,7 @@ RSpec.configure do |config|
# config.filter_gems_from_backtrace("gem name")
config.include FactoryBot::Syntax::Methods
config.include(MailerMacros)
config.before(:each) { reset_email }
config.include Devise::Test::IntegrationHelpers, type: :request
end

View File

@ -0,0 +1,9 @@
module MailerMacros
def last_email
ActionMailer::Base.deliveries.last
end
def reset_email
ActionMailer::Base.deliveries = []
end
end