Permalink
Browse files

Created railtie, added Rails3 support and updated all tests to reflect

new test suite and Rails3 changes.

Signed-off-by: James McCarthy <james2mccarthy@gmail.com>
  • Loading branch information...
1 parent 07324f8 commit feb783f35032d82d052bb10821ac199dd2f98b10 @james2m committed Nov 28, 2011
View
15 lib/authlogic_facebook_shim.rb
@@ -1,14 +1,11 @@
+require 'authlogic_facebook_shim/version'
require 'yaml'
require 'ostruct'
require 'singleton'
-if ActiveRecord::Base.respond_to?(:add_acts_as_authentic_module)
- require 'authlogic_facebook_shim/acts_as_authentic'
- require 'authlogic_facebook_shim/session/config'
- require 'authlogic_facebook_shim/session/adapter'
- require 'authlogic_facebook_shim/session/facebook'
- require 'authlogic_facebook_shim/session'
-
- Authlogic::Session::Base.send :include, AuthlogicFacebookShim::Session
- ActiveRecord::Base.send :include, AuthlogicFacebookShim::ActsAsAuthentic
+module AuthlogicFacebookShim
+ autoload :ActsAsAuthentic, 'authlogic_facebook_shim/acts_as_authentic'
+ autoload :Session, 'authlogic_facebook_shim/session'
end
+
+require 'authlogic_facebook_shim/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
View
19 lib/authlogic_facebook_shim/railtie.rb
@@ -0,0 +1,19 @@
+require 'authlogic_facebook_shim'
+require 'rails'
+
+module AuthlogicFacebookShim
+ class Railtie < Rails::Railtie
+
+ initializer "authlogic_facebook_shim.active_record" do |app|
+ ActiveSupport.on_load :active_record do
+
+ if respond_to?(:add_acts_as_authentic_module)
+ Authlogic::Session::Base.send :include, AuthlogicFacebookShim::Session
+ include AuthlogicFacebookShim::ActsAsAuthentic
+ end
+
+ end
+ end
+
+ end
+end
View
4 lib/authlogic_facebook_shim/session.rb
@@ -1,5 +1,9 @@
module AuthlogicFacebookShim
module Session
+
+ autoload :Config, 'authlogic_facebook_shim/session/config'
+ autoload :Adapter, 'authlogic_facebook_shim/session/adapter'
+ autoload :Facebook, 'authlogic_facebook_shim/session/facebook'
def self.included(klass)
klass.extend Session::Config
View
2 lib/authlogic_facebook_shim/session/config.rb
@@ -63,7 +63,7 @@ def facebook_uid_field(value=nil)
# * <tt>Default:</tt> :find_by_#{facebook_uid_field}
# * <tt>Accepts:</tt> Symbol or String
def facebook_finder(value=nil)
- rw_config(:facebook_finder, value, nil)
+ rw_config(:facebook_finder, value, false)
end
alias_method :facebook_finder=, :facebook_finder
View
4 lib/authlogic_facebook_shim/session/facebook.rb
@@ -19,7 +19,7 @@ def logged_in_with_facebook?
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?
+ !skip_facebook_authentication && !authenticating_with_unauthorized_record? && attempted_record.nil? && facebook_session?
end
private
@@ -38,7 +38,7 @@ def validate_by_facebook
end
@logged_in_with_facebook = true
- return self.attempted_record.save(false)
+ return self.attempted_record.save(:validate => false)
end
end
View
302 test/units/adapters/koala_adapter_test.rb
@@ -1,182 +1,192 @@
-require File.expand_path( '../../test_helper.rb', File.dirname(__FILE__) )
+require File.expand_path('../../../test_helper', __FILE__)
+require 'koala'
-class AuthlogicFacebookShim::Adapters::KoalaAdapterTest < ActiveSupport::TestCase
+describe AuthlogicFacebookShim::Adapters::KoalaAdapter do
- setup :activate_authlogic
+ before do
+ activate_authlogic
+
+ @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'
+ }
+
+ override controller, :cookies => @mock_cookies
+
+ @session = UserSession.new
+ override @session, :facebook_app_id => 'mockappid'
+ override @session, :facebook_api_key => 'mockapikey'
+ override @session, :facebook_secret_key => 'mocksecret'
+ end
- context "Adapters::KoalaAdapter" 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')
+ describe "setup - for my own sanity" do
- @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
+ it "should set the controller" do
+ @session.send(:controller).must_equal controller
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
+
+ it "should set the cookies" do
+ @session.send(:controller).cookies.must_equal @mock_cookies
+ end
+
+ it "should set the facebook_app_id" do
+ @session.facebook_app_id.must_equal 'mockappid'
+ end
+
+ it "should set the facebook_secret_key" do
+ @session.facebook_secret_key.must_equal 'mocksecret'
+ end
+
+ it "should set the facebook_api_key" do
+ @session.facebook_api_key.must_equal 'mockapikey'
+ end
+
+ end
+
+ describe "facebook_session" do
+
+ describe "with a valid facebook cookie" do
- should "set the facebook_secret_key" do
- assert_equal 'mocksecret', @session.facebook_secret_key
- end
+ describe "and koala support for get_user_info_from_cookie" do
+
+ it "should return a session_key" do
+ @session.facebook_session.session_key.must_equal 'mocksessionkey'
+ end
+
+ it "should return a uid" do
+ @session.facebook_session.uid.must_equal 'mockuid'
+ end
+
+ it "should return a secret" do
+ @session.facebook_session.secret.must_equal 'mocksecret'
+ end
+
+ it "should return a sig" do
+ @session.facebook_session.sig.must_equal 'cbd80b97f124bf392f76e2ee61168990'
+ end
+
+ it "should return an access_token" do
+ @session.facebook_session.access_token.must_equal 'mockaccesstoken'
+ 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
+
+ describe "with previous koala api" do
- end
-
- context "with previous koala api" do
+ it "should get user info with the get_user_from_cookie method" do
+ @oauth = MiniTest::Mock.new
- 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
+ override Koala::Facebook::OAuth, :new => @oauth
+
+ @oauth.expect :respond_to?, false, [:get_user_info_from_cookie]
+ @oauth.expect :get_user_from_cookie, @user_info, [@mock_cookies]
+ @session.facebook_session.session_key.must_equal 'mocksessionkey'
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
+
+ describe "with no valid facebook cookie" do
+
+ it "should return nil" do
+ def @session.facebook_app_id; nil end
+
+ @session.facebook_session.must_be_nil
end
end
- context "facebook_session?" do
-
- context "with a valid facebook session" do
-
- should "be true" do
- assert @session.facebook_session?
- end
-
+ end
+
+ describe "facebook_session?" do
+
+ describe "with a valid facebook session" do
+
+ it "should be true" do
+ @session.facebook_session?.must_equal true
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
+
+ describe "without a valid facebook session" do
+
+ it "should be false" do
+ override @session, :facebook_app_id => nil
+ @session.facebook_session?.must_equal false
end
end
-
- context "facebook_user" do
+
+ end
+
+ describe "facebook_user" do
+
+ describe "with a valid facebook session" do
- context "with a valid facebook session" do
+ before do
+ @user = {
+ "id" => "mockid",
+ "name" => "Full name",
+ "first_name" => "First name",
+ "last_name" => "Last name"
+ }
- 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
+ override @session, :facebook_session? => true
+
+ @graph_api = MiniTest::Mock.new
+ @graph_api.expect :get_object, @user, ['me']
- 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
+ override Koala::Facebook::GraphAPI, :new => @graph_api
+ end
+
+ it "should initialize the graph api" do
+ facebook_session = MiniTest::Mock.new
+ access_token = MiniTest::Mock.new
+ facebook_session.expect :access_token, access_token
- should "return the facebook id as uid" do
- assert_equal 'mockid', @session.facebook_user.uid
- end
+ override @session, :facebook_session => facebook_session
+ expect Koala::Facebook::GraphAPI, :new, :with => [access_token], :return => @graph_api
+ @session.facebook_user
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
+
+ it "should return an OpenStruct" do
+ @session.facebook_user.must_be_instance_of OpenStruct
+ end
+
+ it "should return the user details" do
+ @session.facebook_user.name.must_equal 'Full name'
+ @session.facebook_user.first_name.must_equal 'First name'
+ @session.facebook_user.last_name.must_equal 'Last name'
+ end
+
+ it "should return the facebook id as uid" do
+ @session.facebook_user.uid.must_equal 'mockid'
+ end
+
+ end
+
+ describe "with no valid facebook session" do
+
+ it "should return nil" do
+ override @session, :facebook_session? => false
+ @session.facebook_user.must_be_nil
end
end
end
-
+
end
View
218 test/units/session/config_test.rb
@@ -1,145 +1,147 @@
require File.expand_path( '../../test_helper.rb', File.dirname(__FILE__) )
-class AuthlogicFacebookShim::Session::ConfigTest < ActiveSupport::TestCase
-
- context "Session::Config" do
+describe AuthlogicFacebookShim::Session::Config do
- setup do
- @session_class = Class.new(Authlogic::Session::Base)
- end
+ before do
+ @session_class = Class.new(Authlogic::Session::Base)
+ end
- context "facebook_config_file" do
+ describe "facebook_config_file" do
- should "have a default 'facebook.yml'" do
- assert_equal 'facebook.yml', @session_class.facebook_config_file
- end
-
- should "have a setter method" do
- fb_config = 'fbconf.yml'
- @session_class.facebook_config_file = fb_config
- assert_equal fb_config, @session_class.facebook_config_file
- end
-
+ it "should have a default 'facebook.yml'" do
+ @session_class.facebook_config_file.must_equal 'facebook.yml'
+ end
+
+ it "should have a setter method" do
+ fb_config = 'fbconf.yml'
+ @session_class.facebook_config_file = fb_config
+ @session_class.facebook_config_file.must_equal fb_config
end
- context "facebook_app_id" do
+ end
+
+ describe "facebook_app_id" do
- should "default to default_config.app_id" do
- default_from_config = 'defaultappid'
- flexmock(@session_class).should_receive('default_config.app_id').and_return(default_from_config).once
- assert_equal default_from_config, @session_class.facebook_app_id
- end
+ it "should default to default_config.app_id" do
+ default_from_config = 'defaultappid'
- 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
- end
+ expect @session_class.default_config, 'app_id', :with => [], :return => default_from_config
+ @session_class.facebook_app_id.must_equal default_from_config
+ end
+
+ it "should have a setter method" do
+ fb_app_id = '234234234'
+ @session_class.facebook_app_id = fb_app_id
+ @session_class.facebook_app_id.must_equal fb_app_id
end
+
+ end
- context "facebook_secret_key" do
+ describe "facebook_secret_key" do
- should "default to default_config.secret_key" do
- default_from_config = 'defaultsecretkey'
- flexmock(@session_class).should_receive('default_config.secret_key').and_return(default_from_config).once
- assert_equal default_from_config, @session_class.facebook_secret_key
- end
+ it "should default to default_config.secret_key" do
+ default_from_config = 'defaultsecretkey'
- 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
- end
+ expect @session_class.default_config, 'secret_key', :with => [], :return => default_from_config
+ @session_class.facebook_secret_key.must_equal default_from_config
end
- context "facebook_api_key" do
-
- should "default to default_config.api_key" do
- default_from_config = 'defaultapikey'
- flexmock(@session_class).should_receive('default_config.api_key').and_return(default_from_config).once
- assert_equal default_from_config, @session_class.facebook_api_key
- 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
- end
-
+ it "should have a setter method" do
+ fb_secret = '553246736447566b583138525a716e693950736'
+ @session_class.facebook_secret_key = fb_secret
+ @session_class.facebook_secret_key.must_equal fb_secret
end
- context "facebook_uid_field" do
+ end
+
+ describe "facebook_api_key" do
- should "have a default of :facebook_uid" do
- assert_equal :facebook_uid, @session_class.facebook_uid_field
- end
+ it "should default to default_config.api_key" do
+ default_from_config = 'defaultapikey'
- 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
- end
+ expect @session_class.default_config, 'api_key', :with => [], :return => default_from_config
+ @session_class.facebook_api_key.must_equal default_from_config
end
- context "facebook_finder" do
+ it "should have a setter method" do
+ fb_api_key = '25a366a46366451636933676978776a45585734'
+ @session_class.facebook_api_key = fb_api_key
+ @session_class.facebook_api_key.must_equal fb_api_key
+ end
+
+ end
+
+ describe "facebook_uid_field" do
- should 'have a default nil' do
- assert_nil @session_class.facebook_finder
- 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
-
+ it "should have a default of :facebook_uid" do
+ @session_class.facebook_uid_field.must_equal :facebook_uid
end
- context "facebook_auto_register" do
+ it "should have a setter method" do
+ fb_uid_field = 'fb_uid'
+ @session_class.facebook_uid_field = fb_uid_field
+ @session_class.facebook_uid_field.must_equal fb_uid_field
+ end
+
+ end
+
+ describe "facebook_finder" do
- should 'have a default false' do
- assert_false @session_class.facebook_auto_register
- end
-
- should "have a setter method" do
- fb_auto_reg = true
- @session_class.facebook_auto_register = fb_auto_reg
- assert_equal fb_auto_reg, @session_class.facebook_auto_register
- end
-
+ it 'should default to false' do
+ @session_class.facebook_finder.must_equal false
end
+
+ it "should have a setter method" do
+ fb_finder = 'find_by_fb_uid'
+ @session_class.facebook_finder = fb_finder
+ @session_class.facebook_finder.must_equal fb_finder
+ end
+
+ end
+
+ describe "facebook_auto_register" do
- context "default_config" do
+ it 'should have a default false' do
+ @session_class.facebook_auto_register.must_equal false
+ end
+
+ it "should have a setter method" do
+ fb_auto_reg = true
+ @session_class.facebook_auto_register = fb_auto_reg
+ @session_class.facebook_auto_register.must_equal fb_auto_reg
+ end
+
+ end
- should "be a class method" do
- assert @session_class.respond_to?(:default_config)
- end
-
- should "return an OpenStruct" do
- assert @session_class.default_config.is_a?(OpenStruct)
- end
-
- should "return the app_id from the default config file" do
- assert_equal 'appidfromfile', @session_class.default_config.app_id
- end
-
- should "return the api_key from the default config file" do
- assert_equal 'apikeyfromfile', @session_class.default_config.api_key
- end
-
- should "return the secret_key from the default config file" do
- assert_equal 'secretkeyfromfile', @session_class.default_config.secret_key
- end
+ describe "default_config" do
- should "return an empty OpenStruct if the file isn't found" do
- flexmock(@session_class).should_receive(:facebook_config_file).and_return('notthere.yml').once
- assert_equal OpenStruct.new({}), @session_class.default_config
- end
+ it "should be a class method" do
+ @session_class.must_respond_to :default_config
end
+ it "should return an OpenStruct" do
+ @session_class.default_config.must_be_instance_of OpenStruct
+ end
+
+ it "should return the app_id from the default config file" do
+ @session_class.default_config.app_id.must_equal 'appidfromfile'
+ end
+
+ it "should return the api_key from the default config file" do
+ @session_class.default_config.api_key.must_equal 'apikeyfromfile'
+ end
+
+ it "should return the secret_key from the default config file" do
+ @session_class.default_config.secret_key.must_equal 'secretkeyfromfile'
+ end
+
+ it "should return an empty OpenStruct if the file isn't found" do
+ expect @session_class, :facebook_config_file, :with => [], :return => 'notthere.yml'
+ @session_class.default_config.must_equal OpenStruct.new({})
+ end
end
end
View
338 test/units/session/facebook_test.rb
@@ -1,218 +1,246 @@
require File.expand_path( '../../test_helper.rb', File.dirname(__FILE__) )
+require 'koala'
-class AuthlogicFacebookShim::Session::FacebookTest < ActiveSupport::TestCase
-
- setup :activate_authlogic
+describe AuthlogicFacebookShim::Session::Facebook do
+
+ before do
+ activate_authlogic
- context "Session::Facebook" do
+ @mock_cookies = MockCookieJar.new
+ @mock_cookies['fbs_mockappid'] = {:value => 'access_token=mockaccesstoken&expires=0&secret=mocksecret&session_key=mocksessionkey&sig=cbd80b97f124bf392f76e2ee61168990&uid=mockuid'}
- setup do
- @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
+ override controller, :cookies => @mock_cookies
+ end
- @session = flexmock(UserSession.new)
- @session.should_receive(:controller).and_return(controller).by_default
+ describe "setup - for my own sanity" do
+
+ before do
+ @session = UserSession.new
+ override @session, :controller => controller
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
-
+ it "should set the controller" do
+ @session.controller.must_equal controller
end
- context "config accessors" 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 "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)
+ it "should set the cookies" do
+ @session.controller.cookies.must_equal @mock_cookies
+ end
+
+ end
+
+ describe "config accessors" do
+
+ before do
+ @session = UserSession.new
+ override @session, :controller => controller
+ end
+
+ after do
+ Object.instance_eval do
+ remove_const :User
+ remove_const :UserSession
+ GC.start
+
+ load(File.join(Rails.root, 'app', 'models', 'user.rb'))
+ load(File.join(Rails.root, 'app', 'models', 'user_session.rb'))
end
+ end
- 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
+ it "should return facebook_app_id" do
+ mockappid = 'mockappid'
+ override UserSession, :facebook_app_id => mockappid
+ @session.send(:facebook_app_id).must_equal mockappid
+ end
- 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
+ it "should return facebook_api_key" do
+ mockapikey = 'mockapikey'
+ override UserSession, :facebook_api_key => mockapikey
- context "facebook_finder" do
+ @session.send(:facebook_api_key).must_equal mockapikey
+ end
- 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
+ it "should return facebook_secret_key" do
+ mocksecret = 'mocksecret'
+ override UserSession, :facebook_secret_key => mocksecret
+
+ @session.send(:facebook_secret_key).must_equal mocksecret
+ 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
+ it "should return facebook_uid_field" do
+ mockuidfield = 'mock_uidfield'
+ override UserSession, :facebook_uid_field => mockuidfield
+
+ @session.send(:facebook_uid_field).must_equal mockuidfield
+ end
+
+ describe "facebook_finder" do
+ it "should delegate to the class" do
+ mockfinder = 'mockfinder'
+ override UserSession, :facebook_finder => mockfinder
+
+ @session.send(:facebook_finder).must_equal mockfinder
end
- should "return facebook_auto_register?" do
- flexmock(UserSession).should_receive(:facebook_auto_register).and_return(true).once
- assert @session.send(:facebook_auto_register?)
+ it "should default if the class returns nil" do
+ override UserSession, :facebook_finder => false
+ override @session, :facebook_uid_field => 'mock_uidfield'
+
+ @session.send(:facebook_finder).must_equal "find_by_mock_uidfield"
end
end
+
+ it "should return facebook_auto_register?" do
+ override UserSession, :facebook_auto_register => true
+
+ @session.send(:facebook_auto_register?).must_equal true
+ end
+
+ end
+
+ describe "validating with facebook" do
- context "validating with facebook" do
+ before do
+ @session = UserSession.new
+ override @session, :controller => controller
+ end
+
+ describe "with a valid facebook session" do
+
+ before do
+ @facebook_session = OpenStruct.new(:uid => 'mockuid')
+
+ override @session, :facebook_session? => true
+ override @session, :facebook_session => @facebook_session
+ end
+
+ describe "with an existing facebook uid" do
- context "with a valid facebook session" do
+ before do
+ override @session, :facebook_finder => 'finder_method'
- setup do
- @facebook_session = flexmock('facebook session', :uid => 'mockuid')
- @session.should_receive(:facebook_session).and_return(@facebook_session).by_default
+ @user = User.create
+ override User, :finder_method => @user
+
+ @session.save
+ end
+
+ it "should return true for logged_in_with_facebook?" do
+ @session.logged_in_with_facebook?.must_equal true
end
+
+ it "should set attempted_record" do
+ @session.attempted_record.must_equal @user
+ end
+
+ end
- context "with an existing facebook uid" do
+ describe "without an existing facebook uid" do
- setup do
- @session.should_receive(:facebook_finder).and_return('finder_method').by_default
+ before do
+ override @session, :facebook_finder => 'finder_method'
+ override User, :finder_method => nil
- @user = User.create
- flexmock(User).should_receive('finder_method').with('mockuid').and_return(@user).by_default
+ @user = User.new
+ override User, :new => @user
+ end
+
+ describe "and facebook_auto_register? true" do
+ before do
+ override @session, :facebook_auto_register? => true
+ end
+
+ it "should build a new user on attempted_record" do
+ override User, :new => @user
+
@session.save
+ @session.attempted_record.must_equal @user
end
- should "return true for logged_in_with_facebook?" do
- assert @session.logged_in_with_facebook?
+ it "should attempt to call before_connect on the new user" do
+ expect @user, :before_connect, :with => [@facebook_session], :return => true
+ assert @session.save
end
- should "set attempted_record" do
- assert_equal @user, @session.attempted_record
+ it "should save the new user" do
+ expect @user, :save, :with => [:validate => false], :return => true
+ assert @session.save
end
end
+
+ describe "and facebook_auto_register? false" do
- 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
+ it "should return false for logged_in_with_facebook?" do
+ override @session, :facebook_auto_register? => false
+ @session.save
- @user = flexmock(User.new)
- flexmock(User).should_receive(:new).and_return(@user).by_default
+ @session.logged_in_with_facebook?.must_equal false
end
- context "and facebook_auto_register? true" do
+ it "should not set attempted record" do
+ override @session, :facebook_auto_register? => false
- 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
- assert @session.save
- end
-
- should "save the new user" do
- @user.should_receive(:save).with(false).and_return(true).at_least.once
- assert @session.save
- end
-
- end
-
- context "and facebook_auto_register? false" do
-
- should "return false for logged_in_with_facebook?" do
- @session.should_receive(:facebook_auto_register?).and_return(false).once
- @session.save
-
- assert_equal false, @session.logged_in_with_facebook?
- end
-
- should "not set attempted record" do
- @session.should_receive(:facebook_auto_register?).and_return(false).once
- @session.save
+ @session.save
- assert_nil @session.attempted_record
- end
-
+ @session.attempted_record.must_be_nil
end
end
-
+
end
- context "when skip_facebook_authentication is true" do
+ end
+
+ describe "when skip_facebook_authentication is true" do
- should "not attempt to validate with facebook" do
- @session.should_receive(:skip_facebook_authentication).and_return(true).once
- @session.should_receive(:validate_by_facebook).never
-
- assert_equal false, @session.save
- end
+ it "should not attempt to validate with facebook" do
+ override @session, :skip_facebook_authentication => true
+ override @session, :validate_by_facebook => lambda { raise Override::ExpectationError.new('to not be called', 'called') }
- should "return false for logged_in_with_facebook?" do
- @session.should_receive(:skip_facebook_authentication).and_return(true).once
-
- assert_equal false, @session.save
- assert_nil @session.logged_in_with_facebook?
- end
+ @session.save.must_equal false
+ end
+
+ it "should return false for logged_in_with_facebook?" do
+ override @session, :skip_facebook_authentication => true
- should "not set attempted record" do
- @session.should_receive(:skip_facebook_authentication).and_return(true).once
-
- assert_equal false, @session.save
- assert_nil @session.attempted_record
- end
+ @session.save.must_equal false
+ @session.logged_in_with_facebook?.must_be_nil
end
- context "when authenticating_with_unauthorized_record? is false" do
+ it "should not set attempted record" do
+ override @session, :skip_facebook_authentication => true
- should "not attempt to validate with facebook" do
- @session.should_receive(:authenticating_with_unauthorized_record?).and_return(false).at_least.once
- @session.should_receive(:validate_by_facebook).never
-
- assert_equal false, @session.save
- end
+ @session.save.must_equal false
+ @session.attempted_record.must_be_nil
+ end
+ end
+
+ describe "when authenticating_with_unauthorized_record? is false" do
- should "return false for logged_in_with_facebook?" do
- @session.should_receive(:authenticating_with_unauthorized_record?).and_return(true).at_least.once
-
- assert_equal false, @session.save
- assert_nil @session.logged_in_with_facebook?
- end
+ it "should not attempt to validate with facebook" do
+ override @session, :authenticating_with_unauthorized_record? => false
+ override @session, :validate_by_facebook => lambda { raise Override::ExpectationError.new('to not be called', 'called') }
- should "not set attempted record" do
- @session.should_receive(:authenticating_with_unauthorized_record?).and_return(true).at_least.once
+ @session.save.must_equal false
+ end
- assert_equal false, @session.save
- assert_nil @session.attempted_record
- end
+ it "should return false for logged_in_with_facebook?" do
+ override @session, :authenticating_with_unauthorized_record? => true
+ @session.save.must_equal false
+ @session.logged_in_with_facebook?.must_be_nil
end
+ it "should not set attempted record" do
+ override @session, :authenticating_with_unauthorized_record? => true
+
+ @session.save.must_equal false
+ @session.attempted_record.must_be_nil
+ end
end

0 comments on commit feb783f

Please sign in to comment.