Skip to content

Commit

Permalink
[Feature] Allow custom phone number for SSO (#96)
Browse files Browse the repository at this point in the history
* add custom phone number ability; with spec test

* version bump

* using factory may yield fake numbers for user; mock out twilio
  • Loading branch information
matt-taylor authored Sep 22, 2024
1 parent f03d25a commit 122e475
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
rails_base (0.81.1)
rails_base (0.82.0)
allow_numeric
bootstrap (~> 4.6.0)
browser
Expand Down
12 changes: 8 additions & 4 deletions app/services/rails_base/authentication/single_sign_on_send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def message(url:, full_name:)
# This method is expected to be overridden by the main app
# This is expected default behavior if SSO is available
def sso_decision_type
if user.phone_number.present?
if phone.present?
SSO_DECISION_TWILIO
elsif user.email_validated
SSO_DECISION_EMAIL
Expand All @@ -69,15 +69,19 @@ def sso_decision_type
end

def send_to_twilio!(message:)
TwilioJob.perform_later(message: message, to: user.phone_number)
log(level: :info, msg: "Sent twilio message to #{user.phone_number}")
TwilioJob.perform_later(message: message, to: phone)
log(level: :info, msg: "Sent twilio message to #{phone}")
rescue StandardError => e
log(level: :error, msg: "Error caught #{e.class.name}")
log(level: :error, msg: "Error caught #{e.message}")
log(level: :error, msg: "Failed to send sms to #{user.phone_number}")
log(level: :error, msg: "Failed to send sms to #{phone}")
context.fail!(message: "Failed to send sms to user. Try again.")
end

def phone
context.phone_number || user.phone_number
end

def send_to_email!(message:)
RailsBase::EventMailer.send_sso(user: user, message: message).deliver_me
rescue StandardError => e
Expand Down
4 changes: 2 additions & 2 deletions lib/rails_base/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module RailsBase
MAJOR = "0"
MINOR = "81"
PATCH = "1"
MINOR = "82"
PATCH = "0"
VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}"

def self.print_version
Expand Down
18 changes: 16 additions & 2 deletions spec/app/services/authentication/single_sign_on_send_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
subject(:call) { described_class.call(params) }

let(:instance) { described_class.new(params) }
let(:user) { User.first }
let(:user) { create(:user) }
let(:token_length) { 32 }
let(:token_type) { :alphanumeric }
let(:uses) { nil }
Expand All @@ -30,6 +30,7 @@
end

describe '#call' do
before { allow(TwilioHelper).to receive(:send_sms) }
context 'when sso_decision_type is invalid' do
before do
allow(user).to receive(:phone_number).and_return(nil)
Expand All @@ -54,10 +55,23 @@
end
end

context "with custom phone" do
let(:phone_number) { "4158675309" }
let(:params) { super().merge(phone_number: phone_number) }
it 'sends to twilio' do
expect(TwilioHelper).to receive(:send_sms).with(
message: /Hello #{user.full_name}. This is your SSO link/,
to: phone_number,
)

call
end
end

it 'sends to twilio' do
expect(TwilioHelper).to receive(:send_sms).with(
message: /Hello #{user.full_name}. This is your SSO link/,
to: user.phone_number
to: user.phone_number,
)

call
Expand Down

0 comments on commit 122e475

Please sign in to comment.