Skip to content
Browse files

Adds an adapter for koala, isolating all koala dependent code

  • Loading branch information...
1 parent cb334d7 commit 980fb75cbfe361746a82041b8650fb793b47773d @james2m committed
Showing with 217 additions and 0 deletions.
  1. +35 −0 lib/authlogic_facebook_koala/adapter.rb
  2. +182 −0 test/units/adapter_test.rb
View
35 lib/authlogic_facebook_koala/adapter.rb
@@ -0,0 +1,35 @@
+module AuthlogicFacebookKoala
+
+ module Adapter
+
+ def facebook_session
+ @facebook_session ||= begin
+ if controller.cookies.has_key?("fbs_#{facebook_app_id}")
+ oauth = Koala::Facebook::OAuth.new(facebook_app_id, facebook_secret_key)
+ if oauth.respond_to?(:get_user_info_from_cookie)
+ user_info = oauth.get_user_info_from_cookie(controller.cookies)
+ else
+ user_info = oauth.get_user_from_cookie(controller.cookies)
+ end
+ OpenStruct.new( user_info )
+ end
+ end
+ end
+
+ def facebook_session?
+ !facebook_session.nil?
+ end
+
+ def facebook_user
+ @facebook_user ||= begin
+ facebook_graph = Koala::Facebook::GraphAPI.new(facebook_session.access_token)
+ user = facebook_graph.get_object('me')
+ user[:uid] = user.delete('id')
+ OpenStruct.new( user )
+ end if facebook_session?
+ end
+
+ end
+
+end
+
View
182 test/units/adapter_test.rb
@@ -0,0 +1,182 @@
+require File.expand_path( '../test_helper.rb', File.dirname(__FILE__) )
+
+class AdapterTest < ActiveSupport::TestCase
+
+ setup :activate_authlogic
+
+ context "Adapter" do
+ setup do
+ @user_info = {
+ 'session_key' => 'mocksessionkey',
+ 'expires' => '0',
+ 'uid' => 'mockuid',
+ 'sig' => 'cbd80b97f124bf392f76e2ee61168990',
+ 'secret' => 'mocksecret',
+ 'access_token' => 'mockaccesstoken'
+ }
+ @mock_cookies = MockCookieJar.new
+ @mock_cookies['fbs_mockappid'] = {:value => 'access_token=mockaccesstoken&expires=0&secret=mocksecret&session_key=mocksessionkey&sig=cbd80b97f124bf392f76e2ee61168990&uid=mockuid'}
+ @session = flexmock(UserSession.new)
+ @controller = flexmock('Controller')
+
+ @session.should_receive(:facebook_app_id).and_return('mockappid').by_default
+ @session.should_receive(:facebook_api_key).and_return('mockapikey').by_default
+ @session.should_receive(:facebook_secret_key).and_return('mocksecret').by_default
+ @session.should_receive(:controller).and_return(@controller).by_default
+ @controller.should_receive(:cookies).and_return(@mock_cookies).by_default
+ end
+
+ context "setup - for my own sanity" do
+
+ should "set the controller" do
+ assert_equal @controller, @session.controller
+ end
+
+ should "set the cookies" do
+ assert_equal @mock_cookies, @session.controller.cookies
+ end
+
+ should "set the facebook_app_id" do
+ assert_equal 'mockappid', @session.facebook_app_id
+ end
+
+ should "set the facebook_secret_key" do
+ assert_equal 'mocksecret', @session.facebook_secret_key
+ end
+
+ should "set the facebook_api_key" do
+ assert_equal 'mockapikey', @session.facebook_api_key
+ end
+
+ end
+
+ context "facebook_session" do
+
+ context "with a valid facebook cookie" do
+
+ context "and koala support for get_user_info_from_cookie" do
+
+ should "return a session_key" do
+ assert_equal 'mocksessionkey', @session.facebook_session.session_key
+ end
+
+ should "return a uid" do
+ assert_equal 'mockuid', @session.facebook_session.uid
+ end
+
+ should "return a secret" do
+ assert_equal 'mocksecret', @session.facebook_session.secret
+ end
+
+ should "return a sig" do
+ assert_equal 'cbd80b97f124bf392f76e2ee61168990', @session.facebook_session.sig
+ end
+
+ should "return an access_token" do
+ assert_equal 'mockaccesstoken', @session.facebook_session.access_token
+ end
+
+ end
+
+ context "with previous koala api" do
+
+ should "get user info with the get_user_from_cookie method" do
+ @oauth = flexmock('oauth')
+ flexmock(Koala::Facebook::OAuth).should_receive(:new).and_return(@oauth).once
+ @oauth.should_receive(:respond_to?).with(:get_user_info_from_cookie).and_return(false).once
+ @oauth.should_receive(:get_user_from_cookie).with(@mock_cookies).and_return(@user_info).once
+
+ assert_equal 'mocksessionkey', @session.facebook_session.session_key
+ end
+
+ end
+
+ end
+
+ context "with no valid facebook cookie" do
+
+ should "return nil" do
+ @session.should_receive('facebook_app_id').and_return(nil).once
+ assert_nil @session.facebook_session
+ end
+
+ end
+
+ end
+
+ context "facebook_session?" do
+
+ context "with a valid facebook session" do
+
+ should "be true" do
+ assert @session.facebook_session?
+ end
+
+ end
+
+ context "without a valid facebook session" do
+
+ should "return nil" do
+ @session.should_receive('facebook_app_id').and_return(nil).once
+ assert_equal false, @session.facebook_session?
+ end
+
+ end
+
+ end
+
+ context "facebook_user" do
+
+ context "with a valid facebook session" do
+
+ setup do
+ @user = {
+ "id" => "mockid",
+ "name" => "Full name",
+ "first_name" => "First name",
+ "last_name" => "Last name"
+ }
+
+ @access_token = flexmock('access token')
+ @session.should_receive('facebook_session.access_token').and_return(@access_token).by_default
+ @session.should_receive('facebook_session?').and_return(true).by_default
+
+ @graph_api = flexmock('graph api', :get_object => @user)
+ flexmock(Koala::Facebook::GraphAPI).should_receive(:new).and_return(@graph_api).by_default
+ end
+
+ should "initialize the graph api" do
+ flexmock(Koala::Facebook::GraphAPI).should_receive(:new).with(@access_token).and_return(@graph_api).once
+ @session.facebook_user
+ end
+
+ should "return an OpenStruct" do
+ assert @session.facebook_user.is_a?(OpenStruct)
+ end
+
+ should "return the user details" do
+ assert_equal 'Full name', @session.facebook_user.name
+ assert_equal 'First name', @session.facebook_user.first_name
+ assert_equal 'Last name', @session.facebook_user.last_name
+ end
+
+ should "return the facebook id as uid" do
+ assert_equal 'mockid', @session.facebook_user.uid
+ end
+
+ end
+
+ context "with no valid facebook session" do
+
+ should "return nil" do
+ @session.should_receive('facebook_session?').and_return(false).once
+ assert_nil @session.facebook_user
+ end
+
+ end
+
+ end
+
+ end
+
+end

0 comments on commit 980fb75

Please sign in to comment.
Something went wrong with that request. Please try again.