Permalink
Browse files

oauth plaugin testing

  • Loading branch information...
1 parent db548dd commit 6e15b8a4bd4072c27b40ac552c46262be9e9621e Aashish Manchanda committed Jul 13, 2009
View
2 app/models/addusersoauthfields.rb
@@ -0,0 +1,2 @@
+class Addusersoauthfields < ActiveRecord::Base
+end
View
21 db/migrate/20090713171702_create_addusersoauthfields.rb
@@ -0,0 +1,21 @@
+class CreateAddusersoauthfields < ActiveRecord::Migration
+ ddef self.up
+ add_column :users, :oauth_token, :string
+ add_column :users, :oauth_secret, :string
+ add_index :users, :oauth_token
+
+ change_column :users, :login, :string, :default => nil, :null => true
+ change_column :users, :crypted_password, :string, :default => nil, :null => true
+ change_column :users, :password_salt, :string, :default => nil, :null => true
+ end
+
+ def self.down
+ remove_column :users, :oauth_token
+ remove_column :users, :oauth_secret
+
+ [:login, :crypted_password, :password_salt].each do |field|
+ User.all(:conditions => "#{field} is NULL").each { |user| user.update_attribute(field, "") if user.send(field).nil? }
+ change_column :users, field, :string, :default => "", :null => false
+ end
+ end
+ end
View
7 test/fixtures/addusersoauthfields.yml
@@ -0,0 +1,7 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+# one:
+# column: value
+#
+# two:
+# column: value
View
8 test/unit/addusersoauthfields_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class AddusersoauthfieldsTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
View
23 vendor/plugins/authlogic_oauth/CHANGELOG.rdoc
@@ -0,0 +1,23 @@
+== 1.0.6 released 2009-6-29
+
+* Any attributes set on the User model before saving, will now be maintained after the user
+ returns from authenticating with the oauth server.
+
+== 1.0.4 released 2009-6-27
+
+* Bug fix
+
+== 1.0.2 released 2009-6-27
+
+* Using oauth's callback_url parameter to control where the oauth server returns the user to the application.
+ The callback_url parameter was temporarily disabled on major oauth sites due to security concerns, but has been resolved.
+
+* Removed the need to add specific oauth routes and an oauth_controller (YAY!). This makes using the plugin much easier.
+
+== 1.0.1 released 2009-6-4
+
+* Adding helpers for the login/register buttons to be used in conjunction with authlogic_oauth
+
+== 1.0.0 released 2009-5-31
+
+* Initial release.
View
20 vendor/plugins/authlogic_oauth/MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 John Allison (johnallison.me)
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
20 vendor/plugins/authlogic_oauth/Manifest.txt
@@ -0,0 +1,20 @@
+CHANGELOG.rdoc
+MIT-LICENSE
+Manifest.txt
+README.rdoc
+Rakefile
+init.rb
+lib/authlogic_oauth.rb
+lib/authlogic_oauth/acts_as_authentic.rb
+lib/authlogic_oauth/helper.rb
+lib/authlogic_oauth/oauth_process.rb
+lib/authlogic_oauth/session.rb
+lib/authlogic_oauth/version.rb
+lib/oauth_callback_filter.rb
+rails/init.rb
+test/acts_as_authentic_test.rb
+test/fixtures/users.yml
+test/lib/user.rb
+test/lib/user_session.rb
+test/session_test.rb
+test/test_helper.rb
View
100 vendor/plugins/authlogic_oauth/README.rdoc
@@ -0,0 +1,100 @@
+= Authlogic OAuth
+
+Authlogic OAuth is an extension of the Authlogic library to add OAuth support. One use case for authentication with OAuth is allowing users to log in with their Twitter credentials.
+
+== Helpful links
+
+* <b>Authlogic:</b> http://github.com/binarylogic/authlogic
+* <b>OAuth Example Project:</b> http://github.com/jrallison/authlogic_example/tree/with-oauth
+* <b>Live example with Twitter:</b> http://authlogic-oauth.heroku.com
+
+== Install and use
+
+=== 1. Install Authlogic and setup your application
+
+* <b>Authlogic:</b> http://github.com/binarylogic/authlogic
+* <b>Authlogic Example:</b> http://github.com/binarylogic/authlogic_example
+
+=== 2. Install OAuth and Authlogic_Oauth
+
+ $ sudo gem install oauth
+ $ sudo gem install authlogic-oauth
+
+Now add the gem dependencies in your config:
+
+ config.gem "oauth"
+ config.gem "authlogic-oauth", :lib => "authlogic_oauth"
+
+Or for older version of rails, install it as a plugin:
+
+ $ script/plugin install git://github.com/jrallison/authlogic_oauth.git
+
+=== 3. Make some simple changes to your database:
+
+ class AddUsersOauthFields < ActiveRecord::Migration
+ def self.up
+ add_column :users, :oauth_token, :string
+ add_column :users, :oauth_secret, :string
+ add_index :users, :oauth_token
+
+ change_column :users, :login, :string, :default => nil, :null => true
+ change_column :users, :crypted_password, :string, :default => nil, :null => true
+ change_column :users, :password_salt, :string, :default => nil, :null => true
+ end
+
+ def self.down
+ remove_column :users, :oauth_token
+ remove_column :users, :oauth_secret
+
+ [:login, :crypted_password, :password_salt].each do |field|
+ User.all(:conditions => "#{field} is NULL").each { |user| user.update_attribute(field, "") if user.send(field).nil? }
+ change_column :users, field, :string, :default => "", :null => false
+ end
+ end
+ end
+
+=== 4. Make sure you save your objects properly
+
+You only need to save your objects this way if you want the user to authenticate with their OAuth provider.
+
+That being said, you probably want to do this in your controllers. You should do this for BOTH your User objects and UserSession objects (assuming you are authenticating users). It should look something like this:
+
+ @user_session.save do |result|
+ if result
+ flash[:notice] = "Login successful!"
+ redirect_back_or_default account_url
+ else
+ render :action => :new
+ end
+ end
+
+You should save your @user objects this way as well, because you also want the user to authenticate with OAuth.
+
+Notice we are saving with a block. Why? Because we need to redirect the user to their OAuth provider so that they can authenticate. When we do this, we don't want to execute that block of code, because if we do, we will get a DoubleRender error. This lets us skip that entire block and send the user along their way without any problems.
+
+=== 5. Define the oauth_consumer class method on your UserSession model
+
+The oauth_consumer should return an OAuth::Consumer which is configured for your OAuth provider. Here's an example for Twitter:
+
+ class UserSession < Authlogic::Session::Base
+
+ def self.oauth_consumer
+ OAuth::Consumer.new("TOKEN", "SECRET",
+ { :site=>"http://twitter.com",
+ :authorize_url => "http://twitter.com/oauth/authenticate" })
+ end
+
+ end
+
+=== 6. Add login and register buttons to your views
+
+ <%= oauth_register_button :value => "Register with Twitter" %>
+ <%= oauth_login_button :value => "Login with Twitter" %>
+
+That's it! The rest is taken care of for you.
+
+= Here are some next steps for the plugin.
+
+1. Safe OAuth error handling.
+2. Remove oauth request from the Rails request cycle.
+
View
1 vendor/plugins/authlogic_oauth/init.rb
@@ -0,0 +1 @@
+File.dirname(__FILE__) + "/rails/init.rb"
View
9 vendor/plugins/authlogic_oauth/lib/authlogic_oauth.rb
@@ -0,0 +1,9 @@
+require File.dirname(__FILE__) + "/authlogic_oauth/version"
+require File.dirname(__FILE__) + "/authlogic_oauth/oauth_process"
+require File.dirname(__FILE__) + "/authlogic_oauth/acts_as_authentic"
+require File.dirname(__FILE__) + "/authlogic_oauth/session"
+require File.dirname(__FILE__) + "/authlogic_oauth/helper"
+
+ActiveRecord::Base.send(:include, AuthlogicOauth::ActsAsAuthentic)
+Authlogic::Session::Base.send(:include, AuthlogicOauth::Session)
+ActionController::Base.helper AuthlogicOauth::Helper
View
120 vendor/plugins/authlogic_oauth/lib/authlogic_oauth/acts_as_authentic.rb
@@ -0,0 +1,120 @@
+module AuthlogicOauth
+ module ActsAsAuthentic
+ def self.included(klass)
+ klass.class_eval do
+ extend Config
+ add_acts_as_authentic_module(Methods, :prepend)
+ end
+ end
+
+ module Config
+ # The name of the oauth token field in the database.
+ #
+ # * <tt>Default:</tt> :oauth_token
+ # * <tt>Accepts:</tt> Symbol
+ def oauth_token_field(value = nil)
+ rw_config(:oauth_token_field, value, :oauth_token)
+ end
+ alias_method :oauth_token_field=, :oauth_token_field
+
+ # The name of the oauth token secret field in the database.
+ #
+ # * <tt>Default:</tt> :oauth_secret
+ # * <tt>Accepts:</tt> Symbol
+ def oauth_secret_field(value = nil)
+ rw_config(:oauth_secret_field, value, :oauth_secret)
+ end
+ alias_method :oauth_secret_field=, :oauth_secret_field
+ end
+
+ module Methods
+ include OauthProcess
+
+ # Set up some simple validations
+ def self.included(klass)
+ klass.class_eval do
+ alias_method "#{oauth_token_field.to_s}=".to_sym, :oauth_token=
+ alias_method "#{oauth_secret_field.to_s}=".to_sym, :oauth_secret=
+ end
+
+ return if !klass.column_names.include?(klass.oauth_token_field.to_s)
+
+ klass.class_eval do
+ validate :validate_by_oauth, :if => :authenticating_with_oauth?
+
+ validates_uniqueness_of klass.oauth_token_field, :scope => validations_scope, :if => :using_oauth?
+ validates_presence_of klass.oauth_secret_field, :scope => validations_scope, :if => :using_oauth?
+
+ validates_length_of_password_field_options validates_length_of_password_field_options.merge(:if => :validate_password_with_oauth?)
+ validates_confirmation_of_password_field_options validates_confirmation_of_password_field_options.merge(:if => :validate_password_with_oauth?)
+ validates_length_of_password_confirmation_field_options validates_length_of_password_confirmation_field_options.merge(:if => :validate_password_with_oauth?)
+ validates_length_of_login_field_options validates_length_of_login_field_options.merge(:if => :validate_password_with_oauth?)
+ validates_format_of_login_field_options validates_format_of_login_field_options.merge(:if => :validate_password_with_oauth?)
+ end
+
+ # email needs to be optional for oauth
+ klass.validate_email_field = false
+ end
+
+ def save(perform_validation = true, &block)
+ if perform_validation && block_given? && redirecting_to_oauth_server?
+ # Save attributes so they aren't lost during the authentication with the oauth server
+ session_class.controller.session[:authlogic_oauth_attributes] = attributes.reject!{|k, v| v.blank?}
+ redirect_to_oauth
+ return false
+ end
+
+ result = super
+ yield(result) if block_given?
+ result
+ end
+
+ # Set the oauth fields
+ def oauth_token=(value)
+ write_attribute(oauth_token_field, value.blank? ? nil : value)
+ end
+
+ def oauth_secret=(value)
+ write_attribute(oauth_secret_field, value.blank? ? nil : value)
+ end
+
+ private
+
+ def authenticating_with_oauth?
+ (session_class.controller.params && !session_class.controller.params[:register_with_oauth].blank?) || oauth_response
+ end
+
+ def authenticate_with_oauth
+ # Restore any attributes which were saved before redirecting to the oauth server
+ self.attributes = session_class.controller.session.delete(:authlogic_oauth_attributes)
+ access_token = generate_access_token
+
+ self.oauth_token = access_token.token
+ self.oauth_secret = access_token.secret
+ end
+
+ def access_token
+ OAuth::AccessToken.new(oauth,
+ read_attribute(oauth_token_field),
+ read_attribute(oauth_secret_field))
+ end
+
+ def using_oauth?
+ respond_to?(oauth_token_field) && !oauth_token.blank?
+ end
+
+ def validate_password_with_oauth?
+ !using_oauth? && require_password?
+ end
+
+ def oauth_token_field
+ self.class.oauth_token_field
+ end
+
+ def oauth_secret_field
+ self.class.oauth_secret_field
+ end
+
+ end
+ end
+end
View
16 vendor/plugins/authlogic_oauth/lib/authlogic_oauth/helper.rb
@@ -0,0 +1,16 @@
+module AuthlogicOauth
+ module Helper
+ def oauth_register_button(options = {})
+ oauth_button('register_with_oauth', options)
+ end
+
+ def oauth_login_button(options = {})
+ oauth_button('login_with_oauth', options)
+ end
+
+ private
+ def oauth_button(name, options = {})
+ "<input type='submit' value='#{options[:value]}' name='#{name}' id='user_submit' class='#{options[:class]}'/>"
+ end
+ end
+end
View
62 vendor/plugins/authlogic_oauth/lib/authlogic_oauth/oauth_process.rb
@@ -0,0 +1,62 @@
+module AuthlogicOauth
+ module OauthProcess
+
+ private
+
+ def validate_by_oauth
+ validate_email_field = false
+
+ if oauth_response.blank?
+ redirect_to_oauth
+ else
+ authenticate_with_oauth
+ end
+ end
+
+ def redirecting_to_oauth_server?
+ authenticating_with_oauth? && oauth_response.blank?
+ end
+
+ def redirect_to_oauth
+ request = oauth.get_request_token :oauth_callback => build_callback_url
+ oauth_controller.session[:oauth_request_token] = request.token
+ oauth_controller.session[:oauth_request_token_secret] = request.secret
+
+ # Tell our rack callback filter what method the current request is using
+ oauth_controller.session[:oauth_callback_method] = oauth_controller.request.method
+
+ oauth_controller.redirect_to request.authorize_url
+ end
+
+ def build_callback_url
+ oauth_controller.url_for :controller => oauth_controller.controller_name, :action => oauth_controller.action_name
+ end
+
+ def request_token
+ OAuth::RequestToken.new(oauth,
+ oauth_controller.session[:oauth_request_token],
+ oauth_controller.session[:oauth_request_token_secret])
+ end
+
+ def generate_access_token
+ request_token.get_access_token(:oauth_verifier => oauth_controller.params[:oauth_verifier])
+ end
+
+ def oauth_response
+ oauth_controller.params && oauth_controller.params[:oauth_token]
+ end
+
+ def oauth_controller
+ is_auth_session? ? controller : session_class.controller
+ end
+
+ def oauth
+ is_auth_session? ? self.class.oauth_consumer : session_class.oauth_consumer
+ end
+
+ def is_auth_session?
+ self.is_a?(Authlogic::Session::Base)
+ end
+
+ end
+end
View
73 vendor/plugins/authlogic_oauth/lib/authlogic_oauth/session.rb
@@ -0,0 +1,73 @@
+module AuthlogicOauth
+ # This module is responsible for adding oauth
+ # to the Authlogic::Session::Base class.
+ module Session
+ def self.included(klass)
+ klass.class_eval do
+ extend Config
+ include Methods
+ end
+ end
+
+ module Config
+ # * <tt>Default:</tt> :find_by_oauth_token
+ # * <tt>Accepts:</tt> Symbol
+ def find_by_oauth_method(value = nil)
+ rw_config(:find_by_oauth_method, value, :find_by_oauth_token)
+ end
+ alias_method :find_by_oauth_method=, :find_by_oauth_method
+ end
+
+ module Methods
+ include OauthProcess
+
+ def self.included(klass)
+ klass.class_eval do
+ validate :validate_by_oauth, :if => :authenticating_with_oauth?
+ end
+ end
+
+ # Hooks into credentials so that you can pass a user who has already has an oauth access token.
+ def credentials=(value)
+ super
+ values = value.is_a?(Array) ? value : [value]
+ hash = values.first.is_a?(Hash) ? values.first.with_indifferent_access : nil
+ self.record = hash[:priority_record] if !hash.nil? && hash.key?(:priority_record)
+ end
+
+ def record=(record)
+ @record = record
+ end
+
+ # Clears out the block if we are authenticating with oauth,
+ # so that we can redirect without a DoubleRender error.
+ def save(&block)
+ block = nil if redirecting_to_oauth_server?
+ super(&block)
+ end
+
+ private
+
+ def authenticating_with_oauth?
+ (controller.params && !controller.params[:login_with_oauth].blank?) || oauth_response
+ end
+
+ def authenticate_with_oauth
+ if @record
+ self.attempted_record = record
+ else
+ self.attempted_record = search_for_record(find_by_oauth_method, generate_access_token.token)
+ #errors.add_to_base("Unable to authenticate with Twitter.")
+ end
+
+ if !attempted_record
+ errors.add_to_base("Could not find user in our database, have you registered with your oauth account?")
+ end
+ end
+
+ def find_by_oauth_method
+ self.class.find_by_oauth_method
+ end
+ end
+ end
+end
View
51 vendor/plugins/authlogic_oauth/lib/authlogic_oauth/version.rb
@@ -0,0 +1,51 @@
+module AuthlogicOauth
+ # A class for describing the current version of a library. The version
+ # consists of three parts: the +major+ number, the +minor+ number, and the
+ # +tiny+ (or +patch+) number.
+ class Version
+ include Comparable
+
+ # A convenience method for instantiating a new Version instance with the
+ # given +major+, +minor+, and +tiny+ components.
+ def self.[](major, minor, tiny)
+ new(major, minor, tiny)
+ end
+
+ attr_reader :major, :minor, :tiny
+
+ # Create a new Version object with the given components.
+ def initialize(major, minor, tiny)
+ @major, @minor, @tiny = major, minor, tiny
+ end
+
+ # Compare this version to the given +version+ object.
+ def <=>(version)
+ to_i <=> version.to_i
+ end
+
+ # Converts this version object to a string, where each of the three
+ # version components are joined by the '.' character. E.g., 2.0.0.
+ def to_s
+ @to_s ||= [@major, @minor, @tiny].join(".")
+ end
+
+ # Converts this version to a canonical integer that may be compared
+ # against other version objects.
+ def to_i
+ @to_i ||= @major * 1_000_000 + @minor * 1_000 + @tiny
+ end
+
+ def to_a
+ [@major, @minor, @tiny]
+ end
+
+ MAJOR = 1
+ MINOR = 0
+ TINY = 6
+
+ # The current version as a Version instance
+ CURRENT = new(MAJOR, MINOR, TINY)
+ # The current version as a String
+ STRING = CURRENT.to_s
+ end
+end
View
12 vendor/plugins/authlogic_oauth/lib/oauth_callback_filter.rb
@@ -0,0 +1,12 @@
+class OauthCallbackFilter
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ unless env["rack.session"][:oauth_callback_method].blank?
+ env["REQUEST_METHOD"] = env["rack.session"].delete(:oauth_callback_method).to_s.upcase
+ end
+ @app.call(env)
+ end
+end
View
10 vendor/plugins/authlogic_oauth/rails/init.rb
@@ -0,0 +1,10 @@
+require "authlogic_oauth"
+require "oauth_callback_filter"
+
+# Throw callback rack app into the middleware stack
+ActionController::Dispatcher.middleware = ActionController::MiddlewareStack.new do |m|
+ ActionController::Dispatcher.middleware.each do |klass|
+ m.use klass
+ end
+ m.use OauthCallbackFilter
+end
View
100 vendor/plugins/authlogic_oauth/test/acts_as_authentic_test.rb
@@ -0,0 +1,100 @@
+require File.dirname(__FILE__) + '/test_helper.rb'
+
+class ActsAsAuthenticTest < ActiveSupport::TestCase
+ def test_included
+ assert User.send(:acts_as_authentic_modules).include?(AuthlogicOauth::ActsAsAuthentic::Methods)
+ assert_equal :validate_password_with_oauth?, User.validates_length_of_password_field_options[:if]
+ assert_equal :validate_password_with_oauth?, User.validates_confirmation_of_password_field_options[:if]
+ assert_equal :validate_password_with_oauth?, User.validates_length_of_password_confirmation_field_options[:if]
+ assert_equal :validate_password_with_oauth?, User.validates_length_of_login_field_options[:if]
+ assert_equal :validate_password_with_oauth?, User.validates_format_of_login_field_options[:if]
+ end
+
+ def test_required_fields_on_create
+ user = User.new
+ assert !user.save
+ assert user.errors.on(:login)
+ assert user.errors.on(:password)
+ assert user.errors.on(:password_confirmation)
+ end
+
+ def test_fields_not_required_on_create_if_using_oauth
+ user = User.new
+ user.oauth_token = "SLDKJSD"
+ assert !user.save {} # because we are redirecting, the user was NOT saved
+ #assert redirecting_to_oauth?
+ end
+
+ def test_login_not_required_on_update_if_using_oauth
+ john = users(:john)
+ assert_nil john.login
+ assert john.save
+ end
+
+ def test_password_not_required_on_update_if_using_oauth
+ john = users(:john)
+ assert_nil john.crypted_password
+ assert john.save
+ end
+
+ def test_email_not_required_on_update_if_using_oauth
+ john = users(:john)
+ assert_nil john.email
+ assert john.save
+ end
+
+ def test_fields_required_on_update_if_not_using_oauth
+ john = users(:john)
+ john.oauth_token = nil
+ assert_nil john.login
+ assert_nil john.crypted_password
+ assert_nil john.email
+ assert !john.save
+
+ assert john.errors.on(:login)
+ assert john.errors.on(:password)
+ assert john.errors.on(:password_confirmation)
+ end
+
+ def test_validates_uniqueness_of_oauth_token
+ u = User.new(:oauth_token => "johns_token")
+ assert !u.valid?
+ assert u.errors.on(:oauth_token)
+ end
+
+ def test_blank_oauth_token_gets_set_to_nil
+ u = User.new(:oauth_token => "")
+ assert_nil u.oauth_token
+ end
+
+ def test_blank_oauth_secret_gets_set_to_nil
+ u = User.new(:oauth_secret => "")
+ assert_nil u.oauth_secret
+ end
+
+ def test_updating_without_oauth
+ john = users(:john)
+ john.oauth_token = nil
+ john.login = "john"
+ john.email = "john@example.com"
+ john.password = "test"
+ john.password_confirmation = "test"
+ assert john.save
+ #assert !redirecting_to_yahoo?
+ end
+
+ def test_updating_without_validation
+ john = users(:john)
+ john.oauth_token = "SDSDGSDGSD"
+ assert john.save(false)
+ #assert !redirecting_to_yahoo?
+ end
+
+ def test_updating_without_a_block
+ john = users(:john)
+ john.oauth_token = "SDSDGSDGSD"
+ assert john.save
+ john.reload
+ assert_equal "SDSDGSDGSD", john.oauth_token
+ end
+end
View
6 vendor/plugins/authlogic_oauth/test/fixtures/users.yml
@@ -0,0 +1,6 @@
+john:
+ persistence_token: 6cde0674657a8a313ce952df979de2830309aa4c11ca65805dd00bfdc65dbcc2f5e36718660a1d2e68c1a08c276d996763985d2f06fd3d076eb7bc4d97b1e317
+ single_access_token: <%= Authlogic::Random.friendly_token %>
+ perishable_token: <%= Authlogic::Random.friendly_token %>
+ oauth_token: johns_token
+ oauth_secret: johns_token_secret
View
3 vendor/plugins/authlogic_oauth/test/lib/user.rb
@@ -0,0 +1,3 @@
+class User < ActiveRecord::Base
+ acts_as_authentic
+end
View
7 vendor/plugins/authlogic_oauth/test/lib/user_session.rb
@@ -0,0 +1,7 @@
+class UserSession < Authlogic::Session::Base
+
+ def self.oauth_consumer
+ OAuth::Consumer.new("CONSUMER_TOKEN", "CONSUMER_SECRET", { :site=>"http://example.com" })
+ end
+
+end
View
23 vendor/plugins/authlogic_oauth/test/session_test.rb
@@ -0,0 +1,23 @@
+require File.dirname(__FILE__) + '/test_helper.rb'
+
+class SessionTest < ActiveSupport::TestCase
+ def test_authenticate_by_record
+ session = UserSession.new
+ assert session.respond_to?(:record)
+ session.record = users(:john)
+ assert_equal users(:john), session.record
+ end
+
+ def test_validate_by_nil_oauth_token
+ session = UserSession.new
+ assert !session.save
+ assert !redirecting_to_oauth?
+ end
+
+ def test_validate_by_oauth
+ controller.stubs(:params).returns({ :login_with_oauth => true })
+ session = UserSession.new
+ assert !session.save
+ assert redirecting_to_oauth?
+ end
+end
View
74 vendor/plugins/authlogic_oauth/test/test_helper.rb
@@ -0,0 +1,74 @@
+require "test/unit"
+require "rubygems"
+require "ruby-debug"
+require "active_record"
+
+ActiveRecord::Schema.verbose = false
+ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
+ActiveRecord::Base.configurations = true
+ActiveRecord::Schema.define(:version => 1) do
+ create_table :users do |t|
+ t.datetime :created_at
+ t.datetime :updated_at
+ t.integer :lock_version, :default => 0
+ 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 :oauth_token
+ t.string :oauth_secret
+ t.string :email
+ 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
+ end
+end
+
+require "active_record/fixtures"
+require "action_controller"
+require "oauth"
+Rails = true # to trick authlogic into loading the rails adapter
+require File.dirname(__FILE__) + "/../../authlogic/lib/authlogic"
+require File.dirname(__FILE__) + "/../../authlogic/lib/authlogic/test_case"
+require File.dirname(__FILE__) + '/../lib/authlogic_oauth' unless defined?(AuthlogicOauth)
+require File.dirname(__FILE__) + '/lib/user'
+require File.dirname(__FILE__) + '/lib/user_session'
+
+class ActionController::Base
+ def redirecting_to
+ @redirect_to
+ end
+
+ def redirect_to(*args)
+ @redirect_to = args
+ end
+end
+
+class ActiveSupport::TestCase
+ include ActiveRecord::TestFixtures
+ self.fixture_path = File.dirname(__FILE__) + "/fixtures"
+ self.use_transactional_fixtures = false
+ self.use_instantiated_fixtures = false
+ self.pre_loaded_fixtures = false
+ fixtures :all
+ setup :activate_authlogic
+
+
+ def activate_authlogic
+ Authlogic::Session::Base.controller = controller
+ end
+
+ def controller
+ @controller ||= Authlogic::ControllerAdapters::RailsAdapter.new(ActionController::Base.new)
+ end
+
+ def redirecting_to_oauth?
+ controller.redirecting_to.to_s =~ /^http:\/\/example.com/
+ end
+end

0 comments on commit 6e15b8a

Please sign in to comment.