Skip to content

Instantly share code, notes, and snippets.

@jancel
Forked from r00k/gist:906356
Created November 15, 2011 17:01
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save jancel/1367606 to your computer and use it in GitHub Desktop.
Save jancel/1367606 to your computer and use it in GitHub Desktop.
Custom devise strategy
# 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