Skip to content

Commit

Permalink
Merge pull request #96 from rjurado01/refactor/action-mailer-paramete…
Browse files Browse the repository at this point in the history
…rized

Refactor mailer to use ActionMailer::Parameterized.
  • Loading branch information
rjurado01 authored Jun 15, 2020
2 parents 20f6eff + bde7260 commit 040bedb
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 145 deletions.
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby 2.6.6
42 changes: 17 additions & 25 deletions app/mailers/rails_jwt_auth/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,67 @@
class RailsJwtAuth::Mailer < ApplicationMailer
default from: RailsJwtAuth.mailer_sender

def confirmation_instructions(user)
before_action do
@user = RailsJwtAuth.model.find(params[:user_id])
@subject = I18n.t("rails_jwt_auth.mailer.#{action_name}.subject")
end

def confirmation_instructions
raise RailsJwtAuth::NotConfirmationsUrl unless RailsJwtAuth.confirmations_url.present?
@user = user

@confirmations_url = add_param_to_url(
RailsJwtAuth.confirmations_url,
'confirmation_token',
@user.confirmation_token
)

subject = I18n.t('rails_jwt_auth.mailer.confirmation_instructions.subject')
mail(to: @user.unconfirmed_email || @user[RailsJwtAuth.email_field_name], subject: subject)
mail(to: @user.unconfirmed_email || @user[RailsJwtAuth.email_field_name], subject: @subject)
end

def email_changed(user)
@user = user
subject = I18n.t('rails_jwt_auth.mailer.email_changed.subject')
mail(to: @user[RailsJwtAuth.email_field_name!], subject: subject)
def email_changed
mail(to: @user[RailsJwtAuth.email_field_name!], subject: @subject)
end

def reset_password_instructions(user)
def reset_password_instructions
raise RailsJwtAuth::NotResetPasswordsUrl unless RailsJwtAuth.reset_passwords_url.present?
@user = user

@reset_passwords_url = add_param_to_url(
RailsJwtAuth.reset_passwords_url,
'reset_password_token',
@user.reset_password_token
)

subject = I18n.t('rails_jwt_auth.mailer.reset_password_instructions.subject')
mail(to: @user[RailsJwtAuth.email_field_name], subject: subject)
mail(to: @user[RailsJwtAuth.email_field_name], subject: @subject)
end

def set_password_instructions(user)
def set_password_instructions
raise RailsJwtAuth::NotSetPasswordsUrl unless RailsJwtAuth.set_passwords_url.present?
@user = user

@reset_passwords_url = add_param_to_url(
RailsJwtAuth.set_passwords_url,
'reset_password_token',
@user.reset_password_token
)

subject = I18n.t('rails_jwt_auth.mailer.set_password_instructions.subject')
mail(to: @user[RailsJwtAuth.email_field_name], subject: subject)
mail(to: @user[RailsJwtAuth.email_field_name], subject: @subject)
end

def send_invitation(user)
def send_invitation
raise RailsJwtAuth::NotInvitationsUrl unless RailsJwtAuth.invitations_url.present?
@user = user

@invitations_url = add_param_to_url(
RailsJwtAuth.invitations_url,
'invitation_token',
@user.invitation_token
)

subject = I18n.t('rails_jwt_auth.mailer.send_invitation.subject')
mail(to: @user[RailsJwtAuth.email_field_name], subject: subject)
mail(to: @user[RailsJwtAuth.email_field_name], subject: @subject)
end

def send_unlock_instructions(user)
@user = user
subject = I18n.t('rails_jwt_auth.mailer.send_unlock_instructions.subject')

def send_unlock_instructions
@unlock_url = add_param_to_url(RailsJwtAuth.unlock_url, 'unlock_token', @user.unlock_token)

mail(to: @user[RailsJwtAuth.email_field_name], subject: subject)
mail(to: @user[RailsJwtAuth.email_field_name], subject: @subject)
end

protected
Expand Down
9 changes: 3 additions & 6 deletions app/models/concerns/rails_jwt_auth/confirmable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ def self.included(base)
self.confirmation_token = SecureRandom.base58(24)
self.confirmation_sent_at = Time.current

mailer = Mailer.confirmation_instructions(self)
RailsJwtAuth.deliver_later ? mailer.deliver_later : mailer.deliver
RailsJwtAuth.send_email(:confirmation_instructions, self)

if RailsJwtAuth.send_email_changed_notification
mailer = Mailer.email_changed(self)
RailsJwtAuth.deliver_later ? mailer.deliver_later : mailer.deliver
RailsJwtAuth.send_email(:email_changed, self)
end
end
end
Expand All @@ -58,8 +56,7 @@ def send_confirmation_instructions
self.confirmation_sent_at = Time.current
return false unless save

mailer = Mailer.confirmation_instructions(self)
RailsJwtAuth.deliver_later ? mailer.deliver_later : mailer.deliver
RailsJwtAuth.send_email(:confirmation_instructions, self)
true
end

Expand Down
5 changes: 2 additions & 3 deletions app/models/concerns/rails_jwt_auth/invitable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,8 @@ def generate_invitation_token
end

def send_invitation_mail
RailsJwtAuth.email_field_name! # ensure email field es valid
mailer = Mailer.send_invitation(self)
RailsJwtAuth.deliver_later ? mailer.deliver_later : mailer.deliver
RailsJwtAuth.email_field_name! # ensure email field is valid
RailsJwtAuth.send_email(:send_invitation, self)
end

def invitation_period_valid?
Expand Down
3 changes: 1 addition & 2 deletions app/models/concerns/rails_jwt_auth/lockable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ def send_unlock_instructions
self.unlock_token = SecureRandom.base58(24)
save(validate: false)

mailer = Mailer.send_unlock_instructions(self)
RailsJwtAuth.deliver_later ? mailer.deliver_later : mailer.deliver
RailsJwtAuth.send_email(:send_unlock_instructions, self)
end

def access_locked?
Expand Down
6 changes: 2 additions & 4 deletions app/models/concerns/rails_jwt_auth/recoverable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ def send_reset_password_instructions
self.reset_password_sent_at = Time.current
return false unless save

mailer = Mailer.reset_password_instructions(self)
RailsJwtAuth.deliver_later ? mailer.deliver_later : mailer.deliver
RailsJwtAuth.send_email(:reset_password_instructions, self)
end

def set_and_send_password_instructions
Expand All @@ -56,8 +55,7 @@ def set_and_send_password_instructions
self.reset_password_sent_at = Time.current
return false unless save

mailer = Mailer.set_password_instructions(self)
RailsJwtAuth.deliver_later ? mailer.deliver_later : mailer.deliver
RailsJwtAuth.send_email(:set_password_instructions, self)
true
end

Expand Down
5 changes: 5 additions & 0 deletions lib/rails_jwt_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,9 @@ def self.email_field_name!

field_name
end

def self.send_email(method, user)
mailer = RailsJwtAuth::Mailer.with(user_id: user.id.to_s).public_send(method)
RailsJwtAuth.deliver_later ? mailer.deliver_later : mailer.deliver
end
end
10 changes: 2 additions & 8 deletions spec/controllers/confirmations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,9 @@
end

it 'sends new confirmation email with new token' do
class Mock
def deliver
end
end

expect(RailsJwtAuth::Mailer).to receive(:confirmation_instructions)
.with(user).and_return(Mock.new)

old_token = user.confirmation_token
expect(RailsJwtAuth).to receive(:send_email).with(:confirmation_instructions, anything)

post :create, params: {confirmation: {email: user.email}}
expect(user.reload.confirmation_token).not_to eq(old_token)
end
Expand Down
9 changes: 2 additions & 7 deletions spec/controllers/passwords_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,8 @@
end

it 'sends new reset_password email with new token' do
class Mock
def deliver
end
end

expect(RailsJwtAuth::Mailer).to receive(:reset_password_instructions)
.with(user).and_return(Mock.new)
expect(RailsJwtAuth).to receive(:send_email)
.with(:reset_password_instructions, anything)

old_token = user.reset_password_token
post :create, params: {password: {email: user.email}}
Expand Down
41 changes: 41 additions & 0 deletions spec/lib/rails_jwt_auth_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'rails_helper'

describe RailsJwtAuth do
before(:all) { RailsJwtAuth.model_name = "ActiveRecordUser" }

describe '#send_email' do
let(:unconfirmed_user) { FactoryBot.create(:active_record_unconfirmed_user) }

after { RailsJwtAuth.deliver_later = false }

context 'when deliver_later options is false' do
before { RailsJwtAuth.deliver_later = false }

it 'uses deliver method' do
mock2 = OpenStruct.new(deliver: true)
mock = OpenStruct.new(confirmation_instructions: mock2)

expect(RailsJwtAuth::Mailer).to receive(:with).with(user_id: unconfirmed_user.id.to_s)
.and_return(mock)
expect(mock2).to receive(:deliver)

RailsJwtAuth.send_email(:confirmation_instructions, unconfirmed_user)
end
end

context 'when deliver_later options is false' do
before { RailsJwtAuth.deliver_later = true }

it 'uses deliver method' do
mock2 = OpenStruct.new(deliver_later: true)
mock = OpenStruct.new(confirmation_instructions: mock2)

expect(RailsJwtAuth::Mailer).to receive(:with).with(user_id: unconfirmed_user.id.to_s)
.and_return(mock)
expect(mock2).to receive(:deliver_later)

RailsJwtAuth.send_email(:confirmation_instructions, unconfirmed_user)
end
end
end
end
14 changes: 9 additions & 5 deletions spec/mailers/mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
require 'rails_helper'

RSpec.describe RailsJwtAuth::Mailer, type: :mailer do
before(:all) { RailsJwtAuth.model_name = 'ActiveRecordUser' }

let(:mail_params) { {user_id: user.id.to_s } }

describe 'confirmation_instructions' do
let(:user) do
FactoryBot.create(:active_record_unconfirmed_user,
confirmation_token: 'abcd', confirmation_sent_at: Time.current)
end

let(:mail) { described_class.confirmation_instructions(user).deliver_now }
let(:mail) { described_class.with(mail_params).confirmation_instructions.deliver_now }
let(:url) { "#{RailsJwtAuth.confirmations_url}?confirmation_token=#{user.confirmation_token}" }

it 'sends email with correct info' do
Expand Down Expand Up @@ -46,7 +50,7 @@
reset_password_sent_at: Time.current)
end

let(:mail) { described_class.reset_password_instructions(user).deliver_now }
let(:mail) { described_class.with(mail_params).reset_password_instructions.deliver_now }
let(:url) { "#{RailsJwtAuth.reset_passwords_url}?reset_password_token=#{user.reset_password_token}" }

it 'sends email with correct info' do
Expand Down Expand Up @@ -75,7 +79,7 @@
reset_password_sent_at: Time.current)
end

let(:mail) { described_class.set_password_instructions(user).deliver_now }
let(:mail) { described_class.with(mail_params).set_password_instructions.deliver_now }
let(:url) { "#{RailsJwtAuth.set_passwords_url}?reset_password_token=#{user.reset_password_token}" }

it 'sends email with correct info' do
Expand Down Expand Up @@ -104,7 +108,7 @@
invitation_created_at: Time.current)
end

let(:mail) { described_class.send_invitation(user).deliver_now }
let(:mail) { described_class.with(mail_params).send_invitation.deliver_now }
let(:url) { "#{RailsJwtAuth.invitations_url}?invitation_token=#{user.invitation_token}" }

it 'sends email with correct info' do
Expand Down Expand Up @@ -136,7 +140,7 @@
)
end

let(:mail) { described_class.send_unlock_instructions(user).deliver_now }
let(:mail) { described_class.with(mail_params).send_unlock_instructions.deliver_now }
let(:url) { "#{RailsJwtAuth.unlock_url}?unlock_token=#{user.unlock_token}" }

it 'sends email with correct info' do
Expand Down
46 changes: 8 additions & 38 deletions spec/models/concerns/confirmable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,45 +71,18 @@
end

describe '#send_confirmation_instructions' do
before :all do
class Mock
def deliver
end

def deliver_later
end
end
end

it 'fills confirmation fields' do
mock = Mock.new
allow(RailsJwtAuth::Mailer).to receive(:confirmation_instructions).and_return(mock)
unconfirmed_user.send_confirmation_instructions
expect(unconfirmed_user.confirmation_token).not_to be_nil
expect(unconfirmed_user.confirmation_sent_at).not_to be_nil
end

it 'sends confirmation email' do
mock = Mock.new
new_user = FactoryBot.build("#{orm.underscore}_unconfirmed_user")
allow(RailsJwtAuth::Mailer).to receive(:confirmation_instructions).and_return(mock)
expect(mock).to receive(:deliver)
expect(RailsJwtAuth).to receive(:send_email).with(:confirmation_instructions, anything)
new_user.send_confirmation_instructions
end

context 'when use deliver_later option' do
before { RailsJwtAuth.deliver_later = true }
after { RailsJwtAuth.deliver_later = false }

it 'uses deliver_later method to send email' do
mock = Mock.new
new_user = FactoryBot.build("#{orm.underscore}_unconfirmed_user")
allow(RailsJwtAuth::Mailer).to receive(:confirmation_instructions).and_return(mock)
expect(mock).to receive(:deliver_later)
new_user.send_confirmation_instructions
end
end

context 'when user is confirmed' do
it 'returns false' do
expect(user.send_confirmation_instructions).to eq(false)
Expand All @@ -121,9 +94,9 @@ def deliver_later
end

it 'does not send confirmation email' do
mock = Mock.new
allow(RailsJwtAuth::Mailer).to receive(:confirmation_instructions).and_return(mock)
expect(mock).not_to receive(:deliver)
expect(RailsJwtAuth).not_to receive(:send_email)
.with(:confirmation_instructions, anything)

user.send_confirmation_instructions
end

Expand Down Expand Up @@ -174,22 +147,19 @@ def deliver_later
context 'when send_email_changed_notification option is false' do
it 'sends only confirmation email' do
allow(RailsJwtAuth).to receive(:send_email_changed_notification).and_return(false)
expect(RailsJwtAuth).to receive(:send_email).with(:confirmation_instructions, user)
expect(RailsJwtAuth).not_to receive(:send_email).with(:email_changed, user)
user.update(email: '[email protected]')
expect(ActionMailer::Base.deliveries.count).to eq(1)
expect(ActionMailer::Base.deliveries.first.subject).to eq('Confirmation instructions')
expect(ActionMailer::Base.deliveries.first.to).to eq(['[email protected]'])
end
end

context 'when send_email_changed_notification option is true' do
it 'sends confirmation and nofication email' do
allow(RailsJwtAuth).to receive(:send_email_changed_notification).and_return(true)
expect(RailsJwtAuth).to receive(:send_email).with(:email_changed, user)
expect(RailsJwtAuth).to receive(:send_email).with(:confirmation_instructions, user)
old_email = user.email
user.update(email: '[email protected]')
expect(ActionMailer::Base.deliveries.first.subject).to eq('Confirmation instructions')
expect(ActionMailer::Base.deliveries.first.to).to eq(['[email protected]'])
expect(ActionMailer::Base.deliveries.last.subject).to eq('Email changed')
expect(ActionMailer::Base.deliveries.last.to).to eq([old_email])
end
end
end
Expand Down
Loading

0 comments on commit 040bedb

Please sign in to comment.