Created
September 5, 2022 18:26
-
-
Save danielwestendorf/29122618d0841a4b6d2578c6d4f66170 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class TwilioVerification | |
include Singleton | |
class << self | |
delegate :create, :verify, to: :instance | |
end | |
def initialize | |
client = Twilio::REST::Client.new(ENV["TWILIO_ACCOUNT_SID"], ENV["TWILIO_AUTH_TOKEN"]) | |
@services = client.verify.services(ENV.fetch("TWILIO_VERIFY_SERVICE_SID", "1234")) | |
end | |
def create(to:, channel:) | |
@services | |
.verifications | |
.create(to: to, channel: channel) | |
.status == "pending" | |
end | |
def verify(to:, code:) | |
@services | |
.verification_checks | |
.create(to: to, code: code.strip) | |
.status == "approved" | |
end | |
end |
This file contains hidden or 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
require "rails_helper" | |
require "twilio_verification" | |
RSpec.describe TwilioVerification do | |
let(:client_dbl) { instance_double(Twilio::REST::Client) } | |
let(:verify_dbl) { instance_double(Twilio::REST::Verify) } | |
let(:services_dbl) { instance_double(Twilio::REST::Verify::V2::ServiceContext) } | |
before do | |
Singleton.__init__(described_class) | |
expect(Twilio::REST::Client).to receive(:new) | |
.and_return(client_dbl) | |
expect(client_dbl).to receive(:verify) | |
.and_return(verify_dbl) | |
expect(verify_dbl).to receive(:services) | |
.and_return(services_dbl) | |
end | |
describe "#create" do | |
subject { described_class.create(to: to, channel: channel) } | |
let(:to) { "foo" } | |
let(:channel) { "bar" } | |
let(:verifications_dbl) { instance_double(Twilio::REST::Verify::V2::ServiceContext::VerificationList) } | |
let(:create_dbl) { instance_double(Twilio::REST::Verify::V2::ServiceContext::VerificationItem) } | |
before do | |
expect(services_dbl).to receive(:verifications) | |
.and_return(verifications_dbl) | |
expect(verifications_dbl).to receive(:create) | |
.with(to: to, channel: channel) | |
.and_return(OpenStruct.new(status: status)) | |
end | |
context "pending" do | |
let(:status) { "pending" } | |
it { is_expected.to eq(true) } | |
end | |
context "not pending" do | |
let(:status) { "abc" } | |
it { is_expected.to eq(false) } | |
end | |
end | |
describe "#verify" do | |
subject { described_class.verify(to: to, code: code) } | |
let(:to) { "foo" } | |
let(:code) { "bar" } | |
let(:verification_checks_dbl) { instance_double(Twilio::REST::Verify::V2::ServiceContext::VerificationCheckList) } | |
let(:create_dbl) { instance_double(Twilio::REST::Verify::V2::ServiceContext::VerificationCheckItem) } | |
before do | |
expect(services_dbl).to receive(:verification_checks) | |
.and_return(verification_checks_dbl) | |
expect(verification_checks_dbl).to receive(:create) | |
.with(to: to, code: code) | |
.and_return(OpenStruct.new(status: status)) | |
end | |
context "approved" do | |
let(:status) { "approved" } | |
it { is_expected.to eq(true) } | |
end | |
context "not approved" do | |
let(:status) { "abc" } | |
it { is_expected.to eq(false) } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment