Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion app/controllers/users/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def send_password_reset_mail
end

def send_password_reset_sms
# Never text a reset link to a number the requester typed: only the account's stored number qualifies.
return unless submitted_phone_matches_account?

# for case where user enters ONLY a phone number, generate a new reset token to use;
# otherwise, use the same reset token as sent by devise mailer
@reset_token ||= @resource.generate_password_reset_token
Expand All @@ -62,11 +65,16 @@ def send_password_reset_sms
sms_params = {
From: @resource.casa_org.twilio_phone_number,
Body: password_reset_msg(@resource.display_name, @short_io_service.short_url),
To: @phone_number
To: @resource.phone_number
}
twilio_service.send_sms(sms_params)
end

def submitted_phone_matches_account?
stored = @resource.phone_number.to_s.gsub(/\D/, "")
stored.present? && stored == @phone_number.to_s.gsub(/\D/, "")
end

def valid_params?(email, phone_number)
return empty_fields_error if params_not_present(email, phone_number)

Expand Down
21 changes: 21 additions & 0 deletions spec/requests/users/passwords_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,27 @@
end
end

context "when the email belongs to one account and the phone number is someone else's" do
let(:params) { {user: {email: user.email, phone_number: "+13333333333"}} }

it "does not text anyone a reset link, but still emails the account" do
expect_any_instance_of(User).to receive(:send_reset_password_instructions).once
request
expect(twillio_service_double).not_to have_received(:send_sms)
end
end

context "when the submitted number is formatted differently from the stored one" do
let(:params) { {user: {email: user.email, phone_number: "+1 (222) 222-2222"}} }

it "texts the stored number, never the submitted string" do
request
expect(twillio_service_double).to have_received(:send_sms).once.with(
{From: org.twilio_phone_number, Body: a_string_matching("reset_url"), To: user.phone_number}
)
end
end

context "with invalid parameters" do
let(:params) { {user: {email: "", phone_number: ""}} }

Expand Down
Loading