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

Add a more generic build_from method to external #596

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: 14 additions & 0 deletions lib/sorcery/controller/submodules/external.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ def create_and_validate_from(provider_name)
return user
end

def build_from(provider_name)
sorcery_fetch_user_hash provider_name
config = user_class.sorcery_config

attrs = user_attrs(@provider.user_info_mapping, @user_hash)

@user = user_class.new
attrs.each do |k,v|
@user.send(:"#{k}=", v)
end
@user.send(config.authentications_class.to_s.downcase.pluralize).build(config.provider_uid_attribute_name => @user_hash[:uid], config.provider_attribute_name => provider_name)
@user
end

# this method automatically creates a new user from the data in the external user hash.
# The mappings from user hash fields to user db fields are set at controller config.
# If the hash field you would like to map is nested, use slashes. For example, Given a hash like:
Expand Down
11 changes: 11 additions & 0 deletions spec/rails_app/app/controllers/sorcery_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ def test_create_from_provider
redirect_to 'blu', alert: 'Failed!'
end
end

def test_build_from_provider
provider = params[:provider]
login_from(provider)
@user = build_from(provider)
if @user.save
redirect_to 'bla', notice: 'Success!'
else
redirect_to 'blu', alert: 'Failed!'
end
end

def test_add_second_provider
provider = params[:provider]
Expand Down
1 change: 1 addition & 0 deletions spec/rails_app/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
get :test_logout_with_remember
get :test_should_be_logged_in
get :test_create_from_provider
get :test_build_from_provider
get :test_add_second_provider
get :test_return_to_with_external
get :test_login_from
Expand Down
16 changes: 16 additions & 0 deletions spec/shared_examples/controller_oauth2_shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,20 @@

end
end

describe 'using build_from' do
before(:each) do
stub_all_oauth2_requests!
User.delete_all
Authentication.delete_all
end

it 'builds and saves a new user' do
sorcery_model_property_set(:authentications_class, Authentication)
sorcery_controller_external_property_set(:facebook, :user_info_mapping, { username: 'name' })

expect { get :test_build_from_provider, provider: 'facebook' }.to change { User.count }.by 1
expect(User.first.username).to eq 'Noam Ben Ari'
end
end
end