Skip to content

Instantly share code, notes, and snippets.

@gigorok
Forked from jancel/gist:1367606
Created June 12, 2013 07:58
Show Gist options
  • Save gigorok/5763545 to your computer and use it in GitHub Desktop.
Save gigorok/5763545 to your computer and use it in GitHub Desktop.
# In config/initializers/local_override.rb:
require 'devise/strategies/authenticatable'
module Devise
module Strategies
class LocalOverride < Authenticatable
def valid?
true
end
def authenticate!
if params[:user]
user = User.find_by_email(params[:user][:email])
if user && user.encrypted_password == params[:user][:password]
success!(user)
else
fail
end
else
fail
end
end
end
end
end
Warden::Strategies.add(:local_override, Devise::Strategies::LocalOverride)
# In config/initializers/devise.rb
config.warden do |manager|
manager.default_strategies(:scope => :user).unshift :local_override
end
# In spec/path_to_test_spec.rb
# I do this in before block or right before test executes
@request = mock(:request)
@strategy = Devise::Strategies::AndroidAuthenticatable.new(nil)
@request.should_receive(:headers).and_return({#hash of the headers you are testing})
@strategy.should_receive(:params).at_least(:once).and_return({#hash of the params})
@strategy.should_receive(:request).and_return(@request)
# Break these up as needed to test failing and successful
# strategies for your application
lambda {
@strategy.should be_valid
@strategy.authenticate!.should eql :success
}.should_not raise_error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment