Skip to content

Instantly share code, notes, and snippets.

@buurzx
Created April 18, 2016 08:00
Show Gist options
  • Save buurzx/b904e77caf111e15e95188b8a1cc0d05 to your computer and use it in GitHub Desktop.
Save buurzx/b904e77caf111e15e95188b8a1cc0d05 to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe Account::PhoneVerificationService do
let(:verification_code) { SecureRandom.hex(5) }
let!(:account) { create(:account, phone_verification_code: verification_code, phone_verification_sent_at: 2.hours.ago) }
let(:service) { Account::PhoneVerificationService.new(account) }
describe '#verify' do
describe 'with valid code' do
it 'should be success' do
expect(service.verify(verification_code)).to eq(true)
end
it 'should mark phone as verificated' do
expect { service.verify(verification_code) }.to change(account, :phone_verified?).to(true)
end
it 'should not add errors' do
service.verify(verification_code)
expect(service.errors).to be_blank
end
end
describe 'with invalid code' do
it 'should not be success' do
expect(service.verify('1')).not_to be_truthy
end
it 'should not mark phone as verificated' do
expect { service.verify('1') }.not_to change(account, :phone_verified?)
end
it 'should add errors' do
service.verify('1')
expect(service.errors).not_to be_empty
end
it 'should count incorrect retries' do
expect { service.verify('1') }.to change(account, :phone_verification_attempts)
end
end
describe 'with expired code' do
it 'should show error' do
Timecop.travel(25.hours.from_now) do
expect(service.verify(verification_code)).to be_falsey
expect(service.errors).not_to be_empty
end
end
end
describe 'with failed attempts exceeded' do
let(:invalid_code) { '1' }
before { 2.times { service.verify(invalid_code) } }
before { allow(service).to receive(:resend_message) { true } }
it 'should be falsey' do
expect(service.verify(invalid_code)).to be_falsey
end
it 'should show error' do
service.verify(invalid_code)
expect(service.errors).not_to be_empty
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment