Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Partially re-written session.rb and tested.

  • Loading branch information...
commit 9316184bc8931131a31b8489e3957376d83db19b 1 parent 980fb75
@james2m authored
View
166 lib/authlogic_facebook_koala/session.rb
@@ -1,134 +1,68 @@
module AuthlogicFacebookKoala
module Session
+
def self.included(klass)
+
klass.class_eval do
- extend Config
- include Methods
- end
- end
-
- module Config
- # REQUIRED
- #
- # Specify your app_id.
- #
- # * <tt>Default:</tt> nil
- # * <tt>Accepts:</tt> String
- def facebook_app_id(value=nil)
- rw_config(:facebook_app_id, value, nil)
- end
- alias_method :facebook_app_id=, :facebook_app_id
-
- # REQUIRED
- #
- # Specify your secret_key.
- #
- # * <tt>Default:</tt> nil
- # * <tt>Accepts:</tt> String
- def facebook_secret_key(value=nil)
- rw_config(:facebook_secret_key, value, nil)
- end
- alias_method :facebook_secret_key=, :facebook_secret_key
-
- # Specify your api_key.
- #
- # * <tt>Default:</tt> nil
- # * <tt>Accepts:</tt> String
- def facebook_api_key(value=nil)
- rw_config(:facebook_api_key, value, nil)
- end
- alias_method :facebook_api_key=, :facebook_api_key
-
- # What user field should be used for the facebook UID?
- #
- # * <tt>Default:</tt> :facebook_uid
- # * <tt>Accepts:</tt> Symbol
- def facebook_uid_field(value=nil)
- rw_config(:facebook_uid_field, value, :facebook_uid)
- end
- alias_method :facebook_uid_field=, :facebook_uid_field
-
- # What method should be used to find the facebook account?
- #
- # * <tt>Default:</tt> :find_by_#{facebook_uid_field}
- # * <tt>Accepts:</tt> Symbol or String
- def facebook_finder(value=nil)
- rw_config(:facebook_finder, value, 'find_by_#{facebook_uid_field}')
- end
- alias_method :facebook_finder=, :facebook_finder
-
- # Should a new user be automatically created if there is no user with
- # given facebook uid?
- #
- # * <tt>Default:</tt> false
- # * <tt>Accepts:</tt> Boolean
- def facebook_auto_register(value=nil)
- rw_config(:facebook_auto_register, value, false)
- end
- alias_method :facebook_auto_register=, :facebook_auto_register
- end
-
- module Methods
- def self.included(klass)
- klass.class_eval do
- validate :validate_by_facebook, :if => :authenticating_with_facebook?
- end
+ attr_accessor :skip_facebook_authentication
+ validate :validate_by_facebook, :if => :authenticating_with_facebook?
end
- def logged_in_with_facebook?
- @logged_in_with_facebook
- end
-
- protected
- # Override this if you want only some requests to use facebook
- def authenticating_with_facebook?
- if controller.respond_to?(:controller) && controller.controller.respond_to?(:set_facebook_session)
- controller.set_facebook_session
- !authenticating_with_unauthorized_record? && controller.facebook_session?
- end
- end
-
- private
+ end
- def validate_by_facebook
- puts "validating with facebook"
- facebook_uid = facebook_session.uid
- self.attempted_record = klass.send(facebook_finder, facebook_uid)
+ def logged_in_with_facebook?
+ @logged_in_with_facebook
+ end
- if self.attempted_record || !facebook_auto_register?
- return @logged_in_with_facebook = !!self.attempted_record
- else
- self.attempted_record = klass.new
- self.attempted_record.send(:"#{facebook_uid_field}=", facebook_uid)
- if self.attempted_record.respond_to?(:before_connect)
- self.attempted_record.send(:before_connect, facebook_session)
- end
+ protected
+ # Override this if you want only some requests to use facebook
+ def authenticating_with_facebook?
+ !skip_facebook_authentication && !authenticating_with_unauthorized_record? && facebook_session?
+ end
- @logged_in_with_facebook = true
- return self.attempted_record.save(false)
+ private
+
+ def validate_by_facebook
+ facebook_uid = facebook_session.uid
+ self.attempted_record = klass.send(facebook_finder, facebook_uid)
+
+ if self.attempted_record || !facebook_auto_register?
+ return @logged_in_with_facebook = !!self.attempted_record
+ else
+ self.attempted_record = klass.new
+ self.attempted_record.send(:"#{facebook_uid_field}=", facebook_uid)
+ if self.attempted_record.respond_to?(:before_connect)
+ self.attempted_record.send(:before_connect, facebook_session)
end
- end
-
- def facebook_user
- controller.facebook_user
- end
-
- def facebook_session
- controller.facebook_session
- end
- def facebook_auto_register?
- self.class.facebook_auto_register
+ @logged_in_with_facebook = true
+ return self.attempted_record.save(false)
end
+ end
+
+ def facebook_app_id
+ self.class.facebook_app_id
+ end
+
+ def facebook_api_key
+ self.class.facebook_api_key
+ end
+
+ def facebook_secret_key
+ self.class.facebook_secret_key
+ end
- def facebook_uid_field
- self.class.facebook_uid_field
- end
+ def facebook_auto_register?
+ self.class.facebook_auto_register
+ end
- def facebook_finder
- instance_eval(self.class.facebook_finder)
- end
+ def facebook_uid_field
+ self.class.facebook_uid_field
+ end
+ def facebook_finder
+ self.class.facebook_finder || "find_by_#{facebook_uid_field}"
end
+
end
end
View
4 test/rails_root/app/models/user.rb
@@ -1,3 +1,7 @@
class User < ActiveRecord::Base
acts_as_authentic
+
+ def before_connect(facebook_session)
+ true
+ end
end
View
30 test/rails_root/db/migrate/20101217000008_create_users.rb
@@ -1,7 +1,37 @@
class CreateUsers < ActiveRecord::Migration
+
def self.up
+ create_table :users do |t|
+ t.datetime :created_at
+ t.datetime :updated_at
+ t.integer :lock_version, :default => 0
+ t.integer :company_id
+ t.string :login
+ t.string :crypted_password
+ t.string :password_salt
+ t.string :persistence_token
+ t.string :single_access_token
+ t.string :perishable_token
+ t.string :email
+ t.string :first_name
+ t.string :last_name
+ t.integer :login_count, :default => 0, :null => false
+ t.integer :failed_login_count, :default => 0, :null => false
+ t.datetime :last_request_at
+ t.datetime :current_login_at
+ t.datetime :last_login_at
+ t.string :current_login_ip
+ t.string :last_login_ip
+ t.boolean :active, :default => true
+ t.boolean :approved, :default => true
+ t.boolean :confirmed, :default => true
+ t.string :facebook_uid
+ end
+
end
def self.down
+ drop_table :users
end
+
end
View
205 test/units/session_test.rb
@@ -1,84 +1,191 @@
require File.expand_path( '../test_helper.rb', File.dirname(__FILE__) )
class SessionTest < ActiveSupport::TestCase
+
+ setup :activate_authlogic
- context "Config" do
+ context "Session" do
setup do
- @session_class = Class.new(Authlogic::Session::Base)
- end
+ @mock_cookies = MockCookieJar.new
+ @mock_cookies['fbs_mockappid'] = {:value => 'access_token=mockaccesstoken&expires=0&secret=mocksecret&session_key=mocksessionkey&sig=cbd80b97f124bf392f76e2ee61168990&uid=mockuid'}
+
+ flexmock(controller).should_receive(:cookies).and_return(@mock_cookies).by_default
- context "facebook_app_id" do
+ @session = flexmock(UserSession.new)
+ @session.should_receive(:controller).and_return(controller).by_default
+ end
- should "have a default nil" do
- assert_nil @session_class.facebook_app_id
+ context "setup - for my own sanity" do
+
+ should "set the controller" do
+ assert_equal controller, @session.controller
end
- should "have a setter method" do
- fb_app_id = '234234234'
- @session_class.facebook_app_id = fb_app_id
- assert_equal fb_app_id, @session_class.facebook_app_id
+ should "set the cookies" do
+ assert_equal @mock_cookies, @session.controller.cookies
end
-
+
end
+
+ context "config accessors" do
- context "facebook_secret_key" do
+ should "return facebook_app_id" do
+ mockappid = 'mockappid'
+ flexmock(UserSession).should_receive(:facebook_app_id).and_return(mockappid).once
+ assert_equal mockappid, @session.send(:facebook_app_id)
+ end
- should "have a default nil" do
- assert_nil @session_class.facebook_secret_key
+ should "return facebook_api_key" do
+ mockapikey = 'mockapikey'
+ flexmock(UserSession).should_receive(:facebook_api_key).and_return(mockapikey).once
+ assert_equal mockapikey, @session.send(:facebook_api_key)
end
-
- should "have a setter method" do
- fb_secret = '553246736447566b583138525a716e693950736'
- @session_class.facebook_secret_key = fb_secret
- assert_equal fb_secret, @session_class.facebook_secret_key
+
+ should "return facebook_secret_key" do
+ mocksecret = 'mocksecret'
+ flexmock(UserSession).should_receive(:facebook_secret_key).and_return(mocksecret).once
+ assert_equal mocksecret, @session.send(:facebook_secret_key)
end
-
- end
-
- context "facebook_api_key" do
- should "have a default nil" do
- assert_nil @session_class.facebook_api_key
+ should "return facebook_uid_field" do
+ mockuidfield = 'mockuidfield'
+ flexmock(UserSession).should_receive(:facebook_uid_field).and_return(mockuidfield).once
+ assert_equal mockuidfield, @session.send(:facebook_uid_field)
end
- should "have a setter method" do
- fb_api_key = '25a366a46366451636933676978776a45585734'
- @session_class.facebook_api_key = fb_api_key
- assert_equal fb_api_key, @session_class.facebook_api_key
+ context "facebook_finder" do
+
+ should "delegate to the class" do
+ mockfinder = 'mockfinder'
+ flexmock(UserSession).should_receive(:facebook_finder).and_return(mockfinder).once
+ assert_equal mockfinder, @session.send(:facebook_finder)
+ end
+
+ should "default if the class returns nil" do
+ flexmock(UserSession).should_receive(:facebook_finder).and_return(nil).once
+ @session.should_receive(:facebook_uid_field).and_return('mockuidfield').once
+ assert_equal "find_by_mockuidfield", @session.send(:facebook_finder)
+ end
+
end
-
+
+ should "return facebook_auto_register?" do
+ flexmock(UserSession).should_receive(:facebook_auto_register).and_return(true).once
+ assert @session.send(:facebook_auto_register?)
+ end
+
end
- context "facebook_uid_field" do
+ context "validating with facebook" do
+
+ context "with a valid facebook session" do
+
+ setup do
+ @facebook_session = flexmock('facebook session', :uid => 'mockuid')
+ @session.should_receive(:facebook_session).and_return(@facebook_session).by_default
+ end
+
+ context "with an existing facebook uid" do
+
+ setup do
+ @session.should_receive(:facebook_finder).and_return('finder_method').by_default
+
+ @user = User.create
+ flexmock(User).should_receive('finder_method').with('mockuid').and_return(@user).by_default
+
+ @session.save
+ end
+
+ should "return true for logged_in_with_facebook?" do
+ assert @session.logged_in_with_facebook?
+ end
+
+ should "set attempted_record" do
+ assert_equal @user, @session.attempted_record
+ end
+
+ end
+
+ context "without an existing facebook uid" do
+
+ setup do
+ @session.should_receive(:facebook_finder).and_return('finder_method').by_default
+ flexmock(User).should_receive('finder_method').with('mockuid').and_return(nil).by_default
+
+ @user = flexmock(User.new)
+ flexmock(User).should_receive(:new).and_return(@user).by_default
+ end
+
+ context "and facebook_auto_register? true" do
+
+ setup do
+ @session.should_receive(:facebook_auto_register?).and_return(true).by_default
+ end
+
+ should "build a new user on attempted_record" do
+ flexmock(User).should_receive(:new).and_return(@user).once
+ @session.save
+ assert_equal @user, @session.attempted_record
+ end
+
+ should "attempt to call before_connect on the new user" do
+ # TODO this is a bit flakey because I can't get flexmock to mock with(@facebook_session)
+ @user.should_receive(:before_connect).with(any).and_return(true).once
+ @session.save
+ end
+
+ should "save the new user" do
+ @user.should_receive(:save).with(false).and_return(true).at_least.once
+ @session.save
+ end
+
+ end
+
+ context "and facebook_auto_register? false" do
+
+ should "return false for logged_in_with_facebook?" do
+
+ end
+
+ should "not set attempted record" do
+
+ end
+
+ end
+
+ end
- should "have a default of :facebook_uid" do
- assert_equal :facebook_uid, @session_class.facebook_uid_field
end
- should "have a setter method" do
- fb_uid_field = 'fb_uid'
- @session_class.facebook_uid_field = fb_uid_field
- assert_equal fb_uid_field, @session_class.facebook_uid_field
+
+ context "when skip_facebook_authentication is true" do
+
+ should "not attempt to validate with facebook" do
+
+ end
+
+ should "return false for logged_in_with_facebook?" do
+
+ end
+
end
- end
-
- context "facebook_finder" do
+ context "when authenticating_with_unauthorized_record? is false" do
- should 'have a default find_by_#{facebook_uid_field}' do
- assert_equal 'find_by_#{facebook_uid_field}', @session_class.facebook_finder
+ should "not attempt to validate with facebook" do
+
+ end
+
+ should "return false for logged_in_with_facebook?" do
+
+ end
+
end
- should "have a setter method" do
- fb_finder = 'find_by_fb_uid'
- @session_class.facebook_finder = fb_finder
- assert_equal fb_finder, @session_class.facebook_finder
- end
end
-
end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.