Skip to content

Commit

Permalink
add tests to the user class
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiedeziel committed Apr 23, 2016
1 parent e4e9ead commit 7f7a270
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ group :development, :test do
gem "pry"
gem "rspec-rails"
gem "shoulda-matchers"
gem "rspec-its"
gem "rubocop", require: false
gem "sqlite3"
gem "timecop"
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ GEM
rspec-expectations (3.4.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-its (1.2.0)
rspec-core (>= 3.0.0)
rspec-expectations (>= 3.0.0)
rspec-mocks (3.4.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
Expand Down Expand Up @@ -392,6 +395,7 @@ DEPENDENCIES
rails_12factor
redcarpet
reverse_markdown
rspec-its
rspec-rails
rubocop
sass-rails
Expand Down
8 changes: 4 additions & 4 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ def active_for_authentication?

private

def find_with_auth_provider(auth)
def self.find_with_auth_provider(auth)
find_by(provider: auth.provider, uid: auth.uid)
end

def find_existing_with_auth_email(auth)
def self.find_existing_with_auth_email(auth)
find_by(email: auth.info.email).tap do |user|
user.update_attributes(provider: auth.provider, uid: auth.uid)
user.update_attributes(provider: auth.provider, uid: auth.uid) if user
end
end

def create_with_auth(auth)
def self.create_with_auth(auth)
create(
email: auth.info.email,
password: Devise.friendly_token[0, 20],
Expand Down
46 changes: 46 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require "rails_helper"

RSpec.describe User, type: :model do
it { is_expected.to have_one :member }

describe ".from_omniauth" do
let(:auth) do
double(:auth, {
provider: 'Github',
uid: 'aksjdf',
info: double(:info, { email: 'an_email@provider.com' })
})
end

subject { described_class.from_omniauth(auth) }

context "when the user exists with auth uid" do
let!(:user) { create(:user, provider: auth.provider, uid: auth.uid) }

it { expect { subject }.to_not change(User, :count) }

its(:id) { is_expected.to eq user.id }
end

context "when the user exists with only email" do
let!(:user) { create(:user, provider: nil, uid: nil, email: auth.info.email) }

it { expect { subject }.to_not change(User, :count) }

its(:id) { is_expected.to eq user.id }
its(:uid) { is_expected.to eq auth.uid }
its(:provider) { is_expected.to eq auth.provider }
end

context "when the user does not exist" do

it { expect { subject }.to change(User, :count) }

it { is_expected.to be_a User }
it { is_expected.to be_persisted }

its(:uid) { is_expected.to eq auth.uid }
its(:provider) { is_expected.to eq auth.provider }
end
end
end

0 comments on commit 7f7a270

Please sign in to comment.