Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix return value of #add_provider_to_user on User instance #224

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions lib/sorcery/model/submodules/external.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,13 @@ module InstanceMethods
def add_provider_to_user(provider, uid)
authentications = sorcery_config.authentications_class.name.demodulize.underscore.pluralize
# first check to see if user has a particular authentication already
if sorcery_adapter.find_authentication_by_oauth_credentials(authentications, provider, uid).nil?
user = send(authentications).build(sorcery_config.provider_uid_attribute_name => uid,
sorcery_config.provider_attribute_name => provider)
user.sorcery_adapter.save(validate: false)
else
user = false
end
return false if sorcery_adapter.find_authentication_by_oauth_credentials(authentications, provider, uid)

user
authentication = send(authentications).build(sorcery_config.provider_uid_attribute_name => uid,
sorcery_config.provider_attribute_name => provider)
authentication.sorcery_adapter.save(validate: false)

authentication
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/rails_app/app/controllers/sorcery_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,10 @@ def test_add_second_provider

return unless logged_in?

if (@user = add_provider_to_user(provider))
redirect_to 'bla', notice: 'Success!'
if (@authentication = add_provider_to_user(provider))
redirect_to "bla", notice: "Success!"
else
redirect_to 'blu', alert: 'Failed!'
redirect_to "blu", alert: "Failed!"
end
end

Expand Down
23 changes: 23 additions & 0 deletions spec/shared_examples/user_oauth_shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,28 @@
external_user
expect(User.load_from_provider(:twitter, 980_342)).to be_nil
end

describe "#add_provider_to_user" do
let!(:user) { create_new_user }

subject { user.add_provider_to_user(:twitter, 123) }

context "when a provider is successfully added" do
it "returns an instance of authentication" do
expect {
expect(subject).to be_kind_of(Authentication)
}.to change(Authentication, :count).by(1)
end
end

context "when a provider already exists" do
let(:user) { create_new_external_user :twitter }

it "does not create a new authentication and returns false" do
expect { subject }.not_to change(Authentication, :count)
expect(subject).to be false
end
end
end
end
end