Skip to content

Commit

Permalink
Refactor the mobile form specs
Browse files Browse the repository at this point in the history
Cleans up the scenarios so all the failure contexts are grouped together
  • Loading branch information
rjlynch committed Oct 16, 2024
1 parent bcac317 commit 672ae78
Showing 1 changed file with 49 additions and 50 deletions.
99 changes: 49 additions & 50 deletions spec/forms/mobile_number_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,21 @@
end
end

context "when notify response is successful" do
before do
allow(OneTimePassword::Generator).to receive(:new).and_return(
instance_double(OneTimePassword::Generator, code: "111111")
)

allow(NotifySmsMessage).to receive(:new).and_return(notify_double)

form.save
end
before do
allow(OneTimePassword::Generator).to receive(:new).and_return(
instance_double(OneTimePassword::Generator, code: "111111")
)

let(:notify_double) do
instance_double(NotifySmsMessage, deliver!: notify_response)
end
allow(NotifySmsMessage).to receive(:new).and_return(notify_double)
end

context "when notify response is successful" do
let(:mobile_number) { "07123456789" }

context "when notify is successful" do
let(:notify_response) do
Notifications::Client::ResponseNotification.new(
let(:notify_double) do
instance_double(
NotifySmsMessage,
deliver!: Notifications::Client::ResponseNotification.new(
{
id: "123",
reference: "456",
Expand All @@ -96,39 +91,49 @@
uri: "uri"
}
)
end
)
end

it "stores the mobile number" do
expect(journey_session.reload.answers.mobile_number).to eq(mobile_number)
end
before { form.save }

it "resets dependent attributes" do
expect(journey_session.reload.answers.mobile_verified).to be_nil
end
it "stores the mobile number" do
expect(journey_session.reload.answers.mobile_number).to eq(mobile_number)
end

it "sends a text message" do
expect(NotifySmsMessage).to have_received(:new).with(
phone_number: mobile_number,
template_id: NotifySmsMessage::OTP_PROMPT_TEMPLATE_ID,
personalisation: {
otp: "111111"
}
)
it "resets dependent attributes" do
expect(journey_session.reload.answers.mobile_verified).to be_nil
end

expect(notify_double).to have_received(:deliver!)
end
it "sends a text message" do
expect(NotifySmsMessage).to have_received(:new).with(
phone_number: mobile_number,
template_id: NotifySmsMessage::OTP_PROMPT_TEMPLATE_ID,
personalisation: {
otp: "111111"
}
)

it "sets sent_one_time_password_at to the current time" do
expect(journey_session.reload.answers.sent_one_time_password_at).to(
eq(DateTime.new(2024, 1, 1, 12, 0, 0))
)
end
expect(notify_double).to have_received(:deliver!)
end

it "sets sent_one_time_password_at to the current time" do
expect(journey_session.reload.answers.sent_one_time_password_at).to(
eq(DateTime.new(2024, 1, 1, 12, 0, 0))
)
end
end

context "when notify response is not successful" do
context "when the notify response is nil" do
let(:mobile_number) { "07123456789" }

context "when notify is unsuccessful" do
# Not sure how this could be nil rather than a bad response but that's
# what the existing code checks for
let(:notify_response) { nil }
let(:notify_double) do
instance_double(NotifySmsMessage, deliver!: nil)
end

before { form.save }

it "stores the mobile number" do
expect(journey_session.reload.answers.mobile_number).to eq(mobile_number)
Expand All @@ -154,21 +159,17 @@
expect(journey_session.reload.answers.sent_one_time_password_at).to be_nil
end
end
end

context "when notify response is not successful" do
context "when the error is an invalid phone number" do
let(:mobile_number) { "07123456789" }

before do
notify_double = instance_double(NotifySmsMessage)
let(:notify_double) { instance_double(NotifySmsMessage) }

before do
allow(notify_double).to receive(:deliver!).and_raise(
NotifySmsMessage::NotifySmsError,
"ValidationError: phone_number Number is not valid – double check the phone number you entered"
)

allow(NotifySmsMessage).to receive(:new).and_return(notify_double)
end

it "adds a validation error" do
Expand All @@ -185,15 +186,13 @@
context "when some other error" do
let(:mobile_number) { "07123456789" }

before do
notify_double = instance_double(NotifySmsMessage)
let(:notify_double) { instance_double(NotifySmsMessage) }

before do
allow(notify_double).to receive(:deliver!).and_raise(
NotifySmsMessage::NotifySmsError,
"Something went wrong with the SMS service. Please try again later."
)

allow(NotifySmsMessage).to receive(:new).and_return(notify_double)
end

it "raises the error" do
Expand Down

0 comments on commit 672ae78

Please sign in to comment.