Skip to content

Commit

Permalink
Got all tests in test/models and failure app ones passing. 369 tests,…
Browse files Browse the repository at this point in the history
… 805 assertions, 13 failures, 2 errors.
  • Loading branch information
josevalim committed Feb 16, 2010
1 parent d466849 commit e6e6648
Show file tree
Hide file tree
Showing 19 changed files with 78 additions and 81 deletions.
6 changes: 6 additions & 0 deletions config/locales/en.yml
@@ -1,4 +1,10 @@
en:
errors:
messages:
not_found: "not found"
already_confirmed: "was already confirmed"
not_locked: "was not locked"

devise:
sessions:
link: 'Sign in'
Expand Down
15 changes: 2 additions & 13 deletions lib/devise/models.rb
Expand Up @@ -89,24 +89,13 @@ def find_or_initialize_with_error_by(attribute, value, error=:invalid)
if value.present?
record.send(:"#{attribute}=", value)
else
error, skip_default = :blank, true
error = :blank
end

add_error_on(record, attribute, error, !skip_default)
record.errors.add(attribute, error)
end

record
end

# Wraps add error logic in a method that works for different frameworks.
def add_error_on(record, attribute, error, add_default=true)
options = add_default ? { :default => error.to_s.gsub("_", " ") } : {}

begin
record.errors.add(attribute, error, options)
rescue ArgumentError
record.errors.add(attribute, error.to_s.gsub("_", " "))
end
end
end
end
3 changes: 1 addition & 2 deletions lib/devise/models/authenticatable.rb
Expand Up @@ -80,8 +80,7 @@ def update_with_password(params={})
result = if valid_password?(current_password)
update_attributes(params)
else
message = current_password.blank? ? :blank : :invalid
self.class.add_error_on(self, :current_password, message, false)
self.errors.add(:current_password, current_password.blank? ? :blank : :invalid)
self.attributes = params
false
end
Expand Down
2 changes: 1 addition & 1 deletion lib/devise/models/confirmable.rb
Expand Up @@ -131,7 +131,7 @@ def unless_confirmed
unless confirmed?
yield
else
self.class.add_error_on(self, :email, :already_confirmed)
self.errors.add(:email, :already_confirmed)
false
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/devise/models/lockable.rb
Expand Up @@ -122,7 +122,7 @@ def if_locked
if locked?
yield
else
self.class.add_error_on(self, :email, :not_locked)
self.errors.add(:email, :not_locked)
false
end
end
Expand Down
12 changes: 2 additions & 10 deletions lib/devise/strategies/http_authenticatable.rb
Expand Up @@ -5,7 +5,7 @@ module Strategies
# Sign in an user using HTTP authentication.
class HttpAuthenticatable < Base
def valid?
http_authentication? && mapping.to.respond_to?(:authenticate_with_http)
request.authorization && mapping.to.respond_to?(:authenticate_with_http)
end

def authenticate!
Expand All @@ -24,16 +24,8 @@ def username_and_password
decode_credentials(request).split(/:/, 2)
end

def http_authentication
request.env['HTTP_AUTHORIZATION'] ||
request.env['X-HTTP_AUTHORIZATION'] ||
request.env['X_HTTP_AUTHORIZATION'] ||
request.env['REDIRECT_X_HTTP_AUTHORIZATION']
end
alias :http_authentication? :http_authentication

def decode_credentials(request)
ActiveSupport::Base64.decode64(http_authentication.split(' ', 2).last || '')
ActiveSupport::Base64.decode64(request.authorization.split(' ', 2).last || '')
end

def custom_headers
Expand Down
5 changes: 1 addition & 4 deletions lib/devise/test_helpers.rb
Expand Up @@ -45,10 +45,7 @@ def catch_with_redirect(&block)

# We need to setup the environment variables and the response in the controller.
def setup_controller_for_warden #:nodoc:
@request.env['action_controller.rescue.request'] = @request
@request.env['action_controller.rescue.response'] = @response
@request.env['rack.session'] = session
@controller.response = @response
@request.env['action_controller.instance'] = @controller
end

# Quick access to Warden::Proxy.
Expand Down
7 changes: 6 additions & 1 deletion test/failure_app_test.rb
Expand Up @@ -4,7 +4,12 @@
class FailureTest < ActiveSupport::TestCase

def call_failure(env_params={})
env = {'warden.options' => { :scope => :user }}.merge!(env_params)
env = {
'warden.options' => { :scope => :user },
'REQUEST_URI' => 'http://test.host/',
'REQUEST_METHOD' => 'GET',
'rack.session' => {}
}.merge!(env_params)
Devise::FailureApp.call(env)
end

Expand Down
11 changes: 2 additions & 9 deletions test/mapping_test.rb
Expand Up @@ -67,7 +67,7 @@ class MappingTest < ActiveSupport::TestCase
assert_equal 'sign_out', mapping.path_names[:sign_out]
assert_equal 'password', mapping.path_names[:password]
assert_equal 'confirmation', mapping.path_names[:confirmation]
assert_equal 'sign_up', mapping.path_names[:sign_up]
assert_equal 'sign_up', mapping.path_names[:sign_up]
assert_equal 'unlock', mapping.path_names[:unlock]
end

Expand Down Expand Up @@ -129,20 +129,13 @@ class MappingTest < ActiveSupport::TestCase
end
end

test 'should have default route options' do
assert_equal({}, Devise.mappings[:user].route_options)
end

test 'should allow passing route options to devise routes' do
assert_equal({ :requirements => { :extra => 'value' } }, Devise.mappings[:manager].route_options)
end

test 'magic predicates' do
mapping = Devise.mappings[:user]
assert mapping.authenticatable?
assert mapping.confirmable?
assert mapping.recoverable?
assert mapping.rememberable?
assert mapping.registerable?

mapping = Devise.mappings[:admin]
assert mapping.authenticatable?
Expand Down
6 changes: 3 additions & 3 deletions test/models/authenticatable_test.rb
Expand Up @@ -100,7 +100,7 @@ def encrypt_password(user, pepper=User.pepper, stretches=User.stretches, encrypt

test 'should authenticate a valid user with email and password and return it' do
user = create_user
User.any_instance.stubs(:confirmed?).returns(true)
user.confirm!
authenticated_user = User.authenticate(:email => user.email, :password => user.password)
assert_equal authenticated_user, user
end
Expand Down Expand Up @@ -146,15 +146,15 @@ def encrypt_password(user, pepper=User.pepper, stretches=User.stretches, encrypt
assert_not user.update_with_password(:current_password => 'other',
:password => 'pass321', :password_confirmation => 'pass321')
assert user.reload.valid_password?('123456')
assert_match /invalid/, user.errors[:current_password]
assert_match "is invalid", user.errors[:current_password].join
end

test 'should add an error to current password when it is blank' do
user = create_user
assert_not user.update_with_password(:password => 'pass321',
:password_confirmation => 'pass321')
assert user.reload.valid_password?('123456')
assert_match /blank/, user.errors[:current_password]
assert_match "can't be blank", user.errors[:current_password].join
end

test 'should ignore password and its confirmation if they are blank' do
Expand Down
14 changes: 7 additions & 7 deletions test/models/confirmable_test.rb
Expand Up @@ -54,10 +54,10 @@ def setup
test 'should not confirm a user already confirmed' do
user = create_user
assert user.confirm!
assert_nil user.errors[:email]
assert_blank user.errors[:email]

assert_not user.confirm!
assert_match /already confirmed/, user.errors[:email]
assert_equal "was already confirmed", user.errors[:email].join
end

test 'should find and confirm an user automatically' do
Expand All @@ -70,13 +70,13 @@ def setup
test 'should return a new record with errors when a invalid token is given' do
confirmed_user = User.confirm!(:confirmation_token => 'invalid_confirmation_token')
assert confirmed_user.new_record?
assert_match /invalid/, confirmed_user.errors[:confirmation_token]
assert_equal "is invalid", confirmed_user.errors[:confirmation_token].join
end

test 'should return a new record with errors when a blank token is given' do
confirmed_user = User.confirm!(:confirmation_token => '')
assert confirmed_user.new_record?
assert_match /blank/, confirmed_user.errors[:confirmation_token]
assert_equal "can't be blank", confirmed_user.errors[:confirmation_token].join
end

test 'should generate errors for a user email if user is already confirmed' do
Expand All @@ -85,7 +85,7 @@ def setup
user.save
confirmed_user = User.confirm!(:confirmation_token => user.confirmation_token)
assert confirmed_user.confirmed?
assert confirmed_user.errors[:email]
assert_equal "was already confirmed", confirmed_user.errors[:email].join
end

test 'should authenticate a confirmed user' do
Expand Down Expand Up @@ -134,7 +134,7 @@ def setup
test 'should add error to new user email if no email was found' do
confirmation_user = User.send_confirmation_instructions(:email => "invalid@email.com")
assert confirmation_user.errors[:email]
assert_equal 'not found', confirmation_user.errors[:email]
assert_equal "not found", confirmation_user.errors[:email].join
end

test 'should generate a confirmation token before send the confirmation instructions email' do
Expand Down Expand Up @@ -175,7 +175,7 @@ def setup
user.confirm!
assert_not user.resend_confirmation!
assert user.confirmed?
assert_equal 'already confirmed', user.errors[:email]
assert_equal 'was already confirmed', user.errors[:email].join
end

test 'confirm time should fallback to devise confirm in default configuration' do
Expand Down
19 changes: 19 additions & 0 deletions test/models/http_authenticatable_test.rb
@@ -0,0 +1,19 @@
require 'test/test_helper'

class HttpAuthenticatableTest < ActiveSupport::TestCase
test 'should authenticate a valid user with email and password and return it' do
user = create_user
user.confirm!

authenticated_user = User.authenticate_with_http(user.email, user.password)
assert_equal authenticated_user, user
end

test 'should return nil when authenticating an invalid user by email' do
user = create_user
user.confirm!

authenticated_user = User.authenticate_with_http('another.email@email.com', user.password)
assert_nil authenticated_user
end
end
13 changes: 7 additions & 6 deletions test/models/lockable_test.rb
Expand Up @@ -9,6 +9,7 @@ def setup
test "should increment failed attempts on unsuccessful authentication" do
user = create_user
assert_equal 0, user.failed_attempts

authenticated_user = User.authenticate(:email => user.email, :password => "anotherpassword")
assert_equal 1, user.reload.failed_attempts
end
Expand Down Expand Up @@ -56,6 +57,7 @@ def setup
user.lock!
assert_not_nil user.reload.locked_at
assert_not_nil user.reload.unlock_token

user.unlock!
assert_nil user.reload.locked_at
assert_nil user.reload.unlock_token
Expand All @@ -65,7 +67,7 @@ def setup
test 'should not unlock an unlocked user' do
user = create_user
assert_not user.unlock!
assert_match /not locked/, user.errors[:email]
assert_match "was not locked", user.errors[:email].join
end

test "new user should not be locked and should have zero failed_attempts" do
Expand Down Expand Up @@ -157,13 +159,13 @@ def setup
test 'should return a new record with errors when a invalid token is given' do
locked_user = User.unlock!(:unlock_token => 'invalid_token')
assert locked_user.new_record?
assert_match /invalid/, locked_user.errors[:unlock_token]
assert_equal "is invalid", locked_user.errors[:unlock_token].join
end

test 'should return a new record with errors when a blank token is given' do
locked_user = User.unlock!(:unlock_token => '')
assert locked_user.new_record?
assert_match /blank/, locked_user.errors[:unlock_token]
assert_equal "can't be blank", locked_user.errors[:unlock_token].join
end

test 'should authenticate a unlocked user' do
Expand All @@ -188,15 +190,14 @@ def setup

test 'should add error to new user email if no email was found' do
unlock_user = User.send_unlock_instructions(:email => "invalid@email.com")
assert unlock_user.errors[:email]
assert_equal 'not found', unlock_user.errors[:email]
assert_equal 'not found', unlock_user.errors[:email].join
end

test 'should not be able to send instructions if the user is not locked' do
user = create_user
assert_not user.resend_unlock!
assert_not user.locked?
assert_equal 'not locked', user.errors[:email]
assert_equal 'was not locked', user.errors[:email].join
end

end
6 changes: 3 additions & 3 deletions test/models/recoverable_test.rb
Expand Up @@ -83,7 +83,7 @@ def setup
test 'should return a new record with errors if user was not found by e-mail' do
reset_password_user = User.send_reset_password_instructions(:email => "invalid@email.com")
assert reset_password_user.new_record?
assert_match /not found/, reset_password_user.errors[:email]
assert_equal "not found", reset_password_user.errors[:email].join
end

test 'should reset reset_password_token before send the reset instructions email' do
Expand Down Expand Up @@ -111,13 +111,13 @@ def setup
test 'should a new record with errors if no reset_password_token is found' do
reset_password_user = User.reset_password!(:reset_password_token => 'invalid_token')
assert reset_password_user.new_record?
assert_match /invalid/, reset_password_user.errors[:reset_password_token]
assert_equal "is invalid", reset_password_user.errors[:reset_password_token].join
end

test 'should a new record with errors if reset_password_token is blank' do
reset_password_user = User.reset_password!(:reset_password_token => '')
assert reset_password_user.new_record?
assert_match /blank/, reset_password_user.errors[:reset_password_token]
assert_match "can't be blank", reset_password_user.errors[:reset_password_token].join
end

test 'should reset successfully user password given the new password and confirmation' do
Expand Down
4 changes: 0 additions & 4 deletions test/rails_app/app/controllers/users_controller.rb
Expand Up @@ -9,8 +9,4 @@ def expire
user_session['last_request_at'] = 31.minutes.ago.utc
render :text => 'User will be expired on next request'
end

def show
render :text => current_user.id.to_s
end
end
2 changes: 1 addition & 1 deletion test/rails_app/app/views/users/index.html.erb
@@ -1 +1 @@
Welcome User!
Welcome User #<%= current_user.id %>!
2 changes: 1 addition & 1 deletion test/rails_app/config/routes.rb
Expand Up @@ -6,7 +6,7 @@
devise_for :users
devise_for :admin, :as => 'admin_area'
devise_for :accounts, :scope => 'manager', :path_prefix => ':locale',
:class_name => "User", :requirements => { :extra => 'value' }, :path_names => {
:class_name => "User", :path_names => {
:sign_in => 'login', :sign_out => 'logout',
:password => 'secret', :confirmation => 'verification',
:unlock => 'unblock', :sign_up => 'register'
Expand Down
12 changes: 6 additions & 6 deletions test/routes_test.rb
Expand Up @@ -85,26 +85,26 @@ class MapRoutingTest < ActionController::TestCase
end

test 'map account with custom path name for session sign in' do
assert_recognizes({:controller => 'sessions', :action => 'new', :locale => 'en', :extra => 'value'}, '/en/accounts/login')
assert_recognizes({:controller => 'sessions', :action => 'new', :locale => 'en'}, '/en/accounts/login')
end

test 'map account with custom path name for session sign out' do
assert_recognizes({:controller => 'sessions', :action => 'destroy', :locale => 'en', :extra => 'value'}, '/en/accounts/logout')
assert_recognizes({:controller => 'sessions', :action => 'destroy', :locale => 'en'}, '/en/accounts/logout')
end

test 'map account with custom path name for password' do
assert_recognizes({:controller => 'passwords', :action => 'new', :locale => 'en', :extra => 'value'}, '/en/accounts/secret/new')
assert_recognizes({:controller => 'passwords', :action => 'new', :locale => 'en'}, '/en/accounts/secret/new')
end

test 'map account with custom path name for confirmation' do
assert_recognizes({:controller => 'confirmations', :action => 'new', :locale => 'en', :extra => 'value'}, '/en/accounts/verification/new')
assert_recognizes({:controller => 'confirmations', :action => 'new', :locale => 'en'}, '/en/accounts/verification/new')
end

test 'map account with custom path name for unlock' do
assert_recognizes({:controller => 'unlocks', :action => 'new', :locale => 'en', :extra => 'value'}, '/en/accounts/unblock/new')
assert_recognizes({:controller => 'unlocks', :action => 'new', :locale => 'en'}, '/en/accounts/unblock/new')
end

test 'map account with custom path name for registration' do
assert_recognizes({:controller => 'registrations', :action => 'new', :locale => 'en', :extra => 'value'}, '/en/accounts/register')
assert_recognizes({:controller => 'registrations', :action => 'new', :locale => 'en'}, '/en/accounts/register')
end
end

0 comments on commit e6e6648

Please sign in to comment.