Skip to content

Instantly share code, notes, and snippets.

@jesster2k10
Created April 26, 2020 15:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesster2k10/4a547c05df38d07a66b10f0b83546838 to your computer and use it in GitHub Desktop.
Save jesster2k10/4a547c05df38d07a66b10f0b83546838 to your computer and use it in GitHub Desktop.
Social Login Specs
require 'rails_helper'
RSpec.describe External::Provider::Base do
let(:subject) { described_class.new SecureRandom.hex }
describe '#for' do
it 'returns Facebook provider' do
class_instance = described_class.for :facebook
expect(class_instance).to eq(External::Provider::Facebook)
end
end
describe '#info' do
it 'raises unimplemented method error' do
assert_raises_unimplemented_error
end
end
describe '#retrive_user_info' do
it 'raises unimplemented method error' do
assert_raises_unimplemented_error
end
end
describe '#retrieve_client' do
it 'raises unimplemented method error' do
assert_raises_unimplemented_error
end
end
def assert_raises_unimplemented_error
expect do
subject.public_send(method)
end.to raise_error(StandardError)
end
end
require 'rails_helper'
require 'faker'
RSpec.describe External::Provider::Facebook do
let(:access_token) { SecureRandom.hex }
let(:refresh_token) { SecureRandom.hex }
let(:subject) { described_class.new access_token, refresh_token }
let(:stubbed_raw_info) do
{
id: SecureRandom.hex,
email: Faker::Internet.email,
birthday: '02/22/2002',
first_name: Faker::Name.first_name,
last_name: Faker::Name.last_name
}
end
before { allow(subject).to receive(:raw_info) { stubbed_raw_info } }
describe '#credentials' do
it 'returns a ProviderCredentials object' do
expect(subject.credentials).to be_a(External::ProviderCredentials)
end
it 'includes the access and refresh token' do
expect(subject.credentials.access_token).to eq(access_token)
end
it 'includes the access and refresh token' do
expect(subject.credentials.refresh_token).to eq(refresh_token)
end
end
describe '#name' do
it { expect(described_class.name).to eq(:facebook) }
end
describe '#info' do
it 'assigns the uid' do
expect(subject.info.uid).to eq(stubbed_raw_info[:id])
end
it 'assigns the email' do
expect(subject.info.email).to eq(stubbed_raw_info[:email])
end
it 'assigns the birthday' do
expect(subject.info.birthday).to eq(Date.new(2002, 2, 22))
end
it 'assigns the profile first_name' do
expect(subject.info.first_name).to eq(stubbed_raw_info[:first_name])
end
it 'assigns the profile last_name' do
expect(subject.info.last_name).to eq(stubbed_raw_info[:last_name])
end
it 'assigns the profile name' do
expect(subject.info.name).to eq("#{stubbed_raw_info[:first_name]} #{stubbed_raw_info[:last_name]}")
end
end
end
require 'rails_helper'
RSpec.describe External::ProviderCredentials do
describe '#to_h' do
let(:subject) do
described_class.new do |c|
c.access_token = SecureRandom.hex
c.refresh_token = SecureRandom.hex
c.expires = [true, false].sample
c.expires_at = Time.now + 2.days
end
end
let(:hash) { subject.to_h }
it 'includes the access_token' do
expect(hash).to include(:access_token)
expect(hash[:access_token]).to eq(subject.access_token)
end
it 'includes the refresh_token' do
expect(hash).to include(:refresh_token)
expect(hash[:refresh_token]).to eq(subject.refresh_token)
end
it 'includes the expires' do
expect(hash).to include(:expires)
expect(hash[:expires]).to eq(subject.expires)
end
it 'includes the expires_at' do
expect(hash).to include(:expires_at)
expect(hash[:expires_at]).to eq(subject.expires_at)
end
end
end
require 'rails_helper'
require 'faker'
RSpec.describe External::ProviderInfo do
let(:subject) do
described_class.new do |c|
c.uid = SecureRandom.hex
c.email = Faker::Internet.email
c.username = Faker::Internet.username
c.birthday = Faker::Date.birthday.to_s
c.first_name = Faker::Name.first_name
c.last_name = Faker::Name.last_name
end
end
describe '#birthday' do
it 'should return a date object' do
expect(subject.birthday).to be_a(Date)
end
it 'should parse the date in format DD/MM/YYYY' do
date_str = '27/04/2002'
expected_date = Date.new 2002, 4, 27
subject.birthday = date_str
expect(subject.birthday).to eq(expected_date)
end
it 'should parse the date as a timestamp' do
expected_date = Date.new(2001, 11, 2)
date = expected_date.to_time.to_i
subject.birthday = date
expect(subject.birthday).to eq(expected_date)
end
it 'should parse the date given a format' do
subject.date_format = '%m/%d/%Y'
subject.birthday = '11/23/2000'
expected_date = Date.new(2000, 11, 23)
expect(subject.birthday).to eq(expected_date)
end
it 'should return the date if given a date object' do
birthday = Faker::Date.birthday
subject.birthday = birthday
expect(subject.birthday).to eq(birthday)
end
end
describe '#first_name' do
it 'returns the first name if given' do
first_name = Faker::Name.first_name
subject.first_name = first_name
subject.name = "#{Faker::Name.first_name} #{Faker::Name.last_name}"
expect(subject.first_name).to eq(first_name)
end
it 'splits the name when first_name is not present' do
first_name = Faker::Name.first_name
subject.first_name = nil
subject.name = "#{first_name} #{Faker::Name.last_name}"
expect(subject.first_name).to eq(first_name)
end
it 'splits the name with a middle name when first_name is not present' do
first_name = Faker::Name.first_name
middle_name = Faker::Name.middle_name
subject.first_name = nil
subject.name = "#{first_name} #{middle_name} #{Faker::Name.last_name}"
expect(subject.first_name).to eq("#{first_name} #{middle_name}")
end
it 'splits the first name without a last name' do
first_name = Faker::Name.first_name
subject.first_name = nil
subject.name = first_name.to_s
expect(subject.first_name).to eq(first_name)
end
end
describe '#last_name' do
it 'returns the last name if given' do
last_name = Faker::Name.last_name
subject.last_name = last_name
subject.name = "#{Faker::Name.first_name} #{Faker::Name.last_name}"
expect(subject.last_name).to eq(last_name)
end
it 'splits the name if not given' do
last_name = Faker::Name.last_name
subject.last_name = last_name
subject.name = "#{Faker::Name.first_name} #{last_name}"
expect(subject.last_name).to eq(last_name)
end
it 'splits the name with a middle name if not given' do
last_name = Faker::Name.last_name
subject.last_name = last_name
subject.name = "#{Faker::Name.first_name} #{Faker::Name.middle_name} #{last_name}"
expect(subject.last_name).to eq(last_name)
end
it 'returns nothing if the name only has the first name' do
subject.last_name = nil
subject.name = Faker::Name.first_name
expect(subject.last_name).to be_falsey
end
end
describe '#to_h' do
let(:hash) { subject.to_h }
it 'includes the uid' do
expect(hash).to include(:uid)
expect(hash[:uid]).to eq(subject.uid)
end
it 'includes the email' do
expect(hash).to include(:email)
expect(hash[:email]).to eq(subject.email)
end
it 'includes the username' do
expect(hash).to include(:username)
expect(hash[:username]).to eq(subject.username)
end
it 'includes the birthday' do
expect(hash).to include(:birthday)
expect(hash[:birthday]).to eq(subject.birthday)
end
it 'includes the first_name' do
expect(hash).to include(:first_name)
expect(hash[:first_name]).to eq(subject.first_name)
end
it 'includes the first_name' do
expect(hash).to include(:last_name)
expect(hash[:last_name]).to eq(subject.last_name)
end
it 'includes the name' do
expect(hash).to include(:name)
expect(hash[:name]).to eq(subject.name)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment