-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RUBY-2658] Classes and specs for unlock_instructions and reset_passw…
…ord notify emails
- Loading branch information
Showing
8 changed files
with
170 additions
and
37 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
25 changes: 25 additions & 0 deletions
25
app/services/waste_carriers_engine/notify/reset_password_instructions_email_service.rb
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
module WasteCarriersEngine | ||
module Notify | ||
class ResetPasswordInstructionsEmailService < DeviseSender | ||
private | ||
def reset_url(token) | ||
Rails.application.routes.url_helpers.edit_user_password_url( | ||
host: Rails.configuration.wcrs_back_office_url, | ||
reset_password_token: token | ||
) | ||
end | ||
|
||
def notify_options(record, token) | ||
{ | ||
email_address: record.email, | ||
template_id: "bfe66f5e-29ed-4f78-82e1-8baf5548f97a", | ||
personalisation: { | ||
reset_password_link: reset_url(token) | ||
} | ||
} | ||
end | ||
end | ||
end | ||
end |
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
43 changes: 43 additions & 0 deletions
43
spec/services/waste_carriers_engine/notify/devise_sender_spec.rb
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require "rails_helper" | ||
|
||
module WasteCarriersEngine | ||
module Notify | ||
RSpec.describe DeviseSender do | ||
let(:user_with_email) { create(:user, email: "[email protected]") } | ||
let(:user_without_email) { create(:user, email: nil) } | ||
let(:token) { "example_token" } | ||
|
||
let(:notifications_client) { instance_double(Notifications::Client) } | ||
|
||
before do | ||
allow(Notifications::Client).to receive(:new).and_return(notifications_client) | ||
allow(notifications_client).to receive(:send_email) | ||
end | ||
|
||
describe "#run" do | ||
context "with :reset_password_instructions template" do | ||
it "uses ResetPasswordInstructionsEmailService" do | ||
expect(ResetPasswordInstructionsEmailService).to receive(:new).and_call_original | ||
described_class.new.run(template: :reset_password_instructions, record: user_with_email, opts: { token: token }) | ||
end | ||
end | ||
|
||
context "with :unlock_instructions template" do | ||
it "uses UnlockInstructionsEmailService" do | ||
expect(UnlockInstructionsEmailService).to receive(:new).and_call_original | ||
described_class.new.run(template: :unlock_instructions, record: user_with_email, opts: { token: token }) | ||
end | ||
end | ||
|
||
context "with unknown template" do | ||
it "raises an error" do | ||
expect { | ||
described_class.new.run(template: :unknown_template, record: user_with_email, opts: { token: token }) | ||
}.to raise_error(ArgumentError, "Unknown email template: unknown_template") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
54 changes: 54 additions & 0 deletions
54
spec/services/waste_carriers_engine/notify/reset_password_instructions_email_service_spec.rb
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
module WasteCarriersEngine | ||
module Notify | ||
RSpec.describe ResetPasswordInstructionsEmailService do | ||
describe ".send_email" do | ||
let(:template_id) { "bfe66f5e-29ed-4f78-82e1-8baf5548f97a" } | ||
let(:user) { create(:user, email: "[email protected]") } | ||
let(:token) { "example_token" } | ||
|
||
let(:expected_notify_options) do | ||
{ | ||
email_address: user.email, | ||
template_id: template_id, | ||
personalisation: { | ||
reset_password_link: Rails.application.routes.url_helpers.edit_user_password_url( | ||
host: Rails.configuration.wcrs_back_office_url, | ||
reset_password_token: token | ||
) | ||
} | ||
} | ||
end | ||
|
||
let(:notifications_client) { instance_double(Notifications::Client) } | ||
|
||
before do | ||
allow(Notifications::Client).to receive(:new).and_return(notifications_client) | ||
allow(notifications_client).to receive(:send_email) | ||
|
||
described_class.new.send_email(user, token) | ||
end | ||
|
||
context "with an email" do | ||
|
||
it "sends an email" do | ||
expect(notifications_client).to have_received(:send_email).with(expected_notify_options) | ||
end | ||
end | ||
|
||
context "with no email" do | ||
before { user.email = nil } | ||
|
||
it "sends an email" do | ||
expect(notifications_client).not_to have_received(:send_email).with(expected_notify_options) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
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 |
---|---|---|
|
@@ -5,50 +5,45 @@ | |
module WasteCarriersEngine | ||
module Notify | ||
RSpec.describe UnlockInstructionsEmailService do | ||
describe ".run" do | ||
describe ".send_email" do | ||
let(:template_id) { "a3295516-26a6-4c01-9e3a-d5000f1a86c6" } | ||
let(:user) { create(:user, email: "[email protected]") } # Assuming you have a factory for User | ||
let(:user) { create(:user, email: "[email protected]") } | ||
let(:token) { "example_token" } | ||
|
||
let(:expected_notify_options) do | ||
{ | ||
email_address: user.email, | ||
template_id: template_id, | ||
personalisation: { | ||
unlock_link: Rails.application.routes.url_helpers.unlock_url( | ||
user, | ||
unlock_link: Rails.application.routes.url_helpers.user_unlock_url( | ||
host: Rails.configuration.wcrs_back_office_url, | ||
unlock_token: token | ||
) | ||
} | ||
} | ||
end | ||
|
||
context "with an email" do | ||
before do | ||
allow_any_instance_of(Notifications::Client) | ||
.to receive(:send_email) | ||
.with(expected_notify_options) | ||
.and_call_original | ||
end | ||
let(:notifications_client) { instance_double(Notifications::Client) } | ||
|
||
subject(:run_service) do | ||
described_class.new.run(template: template_id, record: user, opts: { token: token }) | ||
end | ||
before do | ||
allow(Notifications::Client).to receive(:new).and_return(notifications_client) | ||
allow(notifications_client).to receive(:send_email) | ||
|
||
described_class.new.send_email(user, token) | ||
end | ||
|
||
context "with an email" do | ||
|
||
it "sends an email" do | ||
expect(run_service).to be_a(Notifications::Client::ResponseNotification) | ||
expect(run_service.template["id"]).to eq(template_id) | ||
expect(notifications_client).to have_received(:send_email).with(expected_notify_options) | ||
end | ||
end | ||
|
||
context "with no email" do | ||
before { user.email = nil } | ||
|
||
it "does not attempt to send an email" do | ||
expect_any_instance_of(Notifications::Client).not_to receive(:send_email) | ||
|
||
described_class.new.run(template: template_id, record: user, opts: { token: token }) | ||
it "sends an email" do | ||
expect(notifications_client).not_to have_received(:send_email).with(expected_notify_options) | ||
end | ||
end | ||
end | ||
|