From d0b4c751b7bf923ee4255d1953bd6a40bc061732 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 26 Aug 2013 17:36:20 +1000 Subject: [PATCH] fix facebook authenticator --- lib/auth/facebook_authenticator.rb | 6 +- .../auth/facebook_authenticator_spec.rb | 57 +++++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 spec/components/auth/facebook_authenticator_spec.rb diff --git a/lib/auth/facebook_authenticator.rb b/lib/auth/facebook_authenticator.rb index d1e16208c66..bb5d18969af 100644 --- a/lib/auth/facebook_authenticator.rb +++ b/lib/auth/facebook_authenticator.rb @@ -14,12 +14,12 @@ class Auth::FacebookAuthenticator < Auth::Authenticator result.email = email = session_info[:email] result.name = name = facebook_hash[:name] - result.extra_info = facebook_hash + result.extra_data = facebook_hash user_info = FacebookUserInfo.where(facebook_user_id: facebook_hash[:facebook_user_id]).first - if result.user = lookup_user(user_info, email) && !user_info - user.create_facebook_user_info! facebook_hash + if !user_info && result.user = lookup_user(user_info, email) + FacebookUserInfo.create({user_id: result.user.id}.merge(facebook_hash)) end result diff --git a/spec/components/auth/facebook_authenticator_spec.rb b/spec/components/auth/facebook_authenticator_spec.rb new file mode 100644 index 00000000000..8ace4391005 --- /dev/null +++ b/spec/components/auth/facebook_authenticator_spec.rb @@ -0,0 +1,57 @@ +require 'spec_helper' + +# In the ghetto ... getting the spec to run in autospec +# thing is we need to load up all auth really early pre-fork +# it means that the require is not going to get a new copy +Auth.send(:remove_const, :FacebookAuthenticator) +load 'auth/facebook_authenticator.rb' + +describe Auth::FacebookAuthenticator do + + context 'after_authenticate' do + it 'can authenticate and create a user record for already existing users' do + authenticator = Auth::FacebookAuthenticator.new + user = Fabricate(:user) + + hash = { + "extra" => { + "raw_info" => { + "username" => "bob" + } + }, + "info" => { + :email => user.email + }, + "uid" => "100" + } + + result = authenticator.after_authenticate(hash) + + result.user.id.should == user.id + end + + it 'can create a proper result for non existing users' do + + hash = { + "extra" => { + "raw_info" => { + "username" => "bob", + "name" => "bob bob" + } + }, + "info" => { + :email => "bob@bob.com" + }, + "uid" => "100" + } + + authenticator = Auth::FacebookAuthenticator.new + + result = authenticator.after_authenticate(hash) + + result.user.should be_nil + result.extra_data[:name].should == "bob bob" + end + end + +end