Skip to content

Commit 3aefbaa

Browse files
committed
🐛 Fix return value of #add_provider_to_user on User instance
* Before, the method would return an instance of Authentication, instead of the original intention to return an instance of the current user. * Arguably the instance of the current user is not very useful, as it's already known 🤷‍♂, but at least it matches the current examples.
1 parent c30cefa commit 3aefbaa

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

lib/sorcery/model/submodules/external.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ module External
1717
def self.included(base)
1818
base.sorcery_config.class_eval do
1919
attr_accessor :authentications_class,
20-
:authentications_user_id_attribute_name,
21-
:provider_attribute_name,
22-
:provider_uid_attribute_name
20+
:authentications_user_id_attribute_name,
21+
:provider_attribute_name,
22+
:provider_uid_attribute_name
2323
end
2424

2525
base.sorcery_config.instance_eval do
26-
@defaults.merge!(:@authentications_class => nil,
26+
@defaults.merge!(:@authentications_class => nil,
2727
:@authentications_user_id_attribute_name => :user_id,
28-
:@provider_attribute_name => :provider,
29-
:@provider_uid_attribute_name => :uid)
28+
:@provider_attribute_name => :provider,
29+
:@provider_uid_attribute_name => :uid)
3030

3131
reset!
3232
end
@@ -96,14 +96,14 @@ def add_provider_to_user(provider, uid)
9696
authentications = sorcery_config.authentications_class.name.demodulize.underscore.pluralize
9797
# first check to see if user has a particular authentication already
9898
if sorcery_adapter.find_authentication_by_oauth_credentials(authentications, provider, uid).nil?
99-
user = send(authentications).build(sorcery_config.provider_uid_attribute_name => uid,
100-
sorcery_config.provider_attribute_name => provider)
101-
user.sorcery_adapter.save(validate: false)
99+
authentication = send(authentications).build(sorcery_config.provider_uid_attribute_name => uid,
100+
sorcery_config.provider_attribute_name => provider)
101+
authentication.sorcery_adapter.save(validate: false)
102102
else
103-
user = false
103+
return false
104104
end
105105

106-
user
106+
self
107107
end
108108
end
109109
end

spec/shared_examples/user_oauth_shared_examples.rb

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
shared_examples_for 'rails_3_oauth_model' do
1+
shared_examples_for "rails_3_oauth_model" do
22
# ----------------- PLUGIN CONFIGURATION -----------------------
33

44
let(:external_user) { create_new_external_user :twitter }
55

6-
describe 'loaded plugin configuration' do
6+
describe "loaded plugin configuration" do
77
before(:all) do
88
Authentication.sorcery_adapter.delete_all
99
User.sorcery_adapter.delete_all
1010

1111
sorcery_reload!([:external])
1212
sorcery_controller_property_set(:external_providers, [:twitter])
1313
sorcery_model_property_set(:authentications_class, Authentication)
14-
sorcery_controller_external_property_set(:twitter, :key, 'eYVNBjBDi33aa9GkA3w')
15-
sorcery_controller_external_property_set(:twitter, :secret, 'XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8')
16-
sorcery_controller_external_property_set(:twitter, :callback_url, 'http://blabla.com')
14+
sorcery_controller_external_property_set(:twitter, :key, "eYVNBjBDi33aa9GkA3w")
15+
sorcery_controller_external_property_set(:twitter, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
16+
sorcery_controller_external_property_set(:twitter, :callback_url, "http://blabla.com")
1717
end
1818

1919
it "responds to 'load_from_provider'" do
@@ -29,5 +29,28 @@
2929
external_user
3030
expect(User.load_from_provider(:twitter, 980_342)).to be_nil
3131
end
32+
33+
describe "#add_provider_to_user" do
34+
let!(:user) { create_new_user }
35+
36+
subject { user.add_provider_to_user(:twitter, 123) }
37+
38+
context "when a provider is successfully added" do
39+
it "returns an instance of user" do
40+
user = subject
41+
expect(user).to be_kind_of(User)
42+
expect(user.authentications.count).to eq 1
43+
end
44+
end
45+
46+
context "when a provider already exists" do
47+
let(:user) { create_new_external_user :twitter }
48+
49+
it "does not create a new authentication and returns false" do
50+
expect { subject }.not_to change(Authentication, :count)
51+
expect(subject).to be false
52+
end
53+
end
54+
end
3255
end
3356
end

0 commit comments

Comments
 (0)