Skip to content

Commit

Permalink
Tests passing for ActiveRecord and MongoMapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Dec 21, 2009
1 parent 947de72 commit 35a8d13
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 39 deletions.
14 changes: 11 additions & 3 deletions Rakefile
Expand Up @@ -5,10 +5,18 @@ require 'rake/testtask'
require 'rake/rdoctask'
require File.join(File.dirname(__FILE__), 'lib', 'devise', 'version')

desc 'Default: run unit tests.'
task :default => :test
desc 'Default: run tests for all ORMs.'
task :default => :pre_commit

desc 'Test Devise.'
desc 'Run Devise tests for all ORMs.'
task :pre_commit do
Dir[File.join(File.dirname(__FILE__), 'test', 'orm', '*.rb')].each do |file|
orm = File.basename(file).split(".").first
system "rake test DEVISE_ORM=#{orm}"
end
end

desc 'Run Devise unit tests.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
Expand Down
24 changes: 15 additions & 9 deletions lib/devise/models.rb
Expand Up @@ -102,7 +102,7 @@ def devise_modules
@devise_modules ||= []
end

# Find an initialize a record setting an error if it can't be found
# Find an initialize a record setting an error if it can't be found.
def find_or_initialize_with_error_by(attribute, value, error=:invalid)
if value.present?
conditions = { attribute => value }
Expand All @@ -114,19 +114,25 @@ def find_or_initialize_with_error_by(attribute, value, error=:invalid)

if value.present?
record.send(:"#{attribute}=", value)
options = { :default => error.to_s.gsub("_", " ") }
else
error, options = :blank, {}
error, skip_default = :blank, true
end

begin
record.errors.add(attribute, error, options)
rescue ArgumentError
record.errors.add(attribute, error.to_s.gsub("_", " "))
end
add_error_on(record, attribute, error, !skip_default)
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
end
2 changes: 1 addition & 1 deletion lib/devise/models/authenticatable.rb
Expand Up @@ -64,7 +64,7 @@ def update_with_password(params={})
if valid_password?(params[:old_password])
update_attributes(params)
else
errors.add(:old_password, :invalid)
self.class.add_error_on(self, :old_password, :invalid, false)
false
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/devise/models/confirmable.rb
Expand Up @@ -127,7 +127,7 @@ def unless_confirmed
unless confirmed?
yield
else
errors.add(:email, :already_confirmed, :default => 'already confirmed')
self.class.add_error_on(self, :email, :already_confirmed)
false
end
end
Expand Down
5 changes: 3 additions & 2 deletions test/integration/confirmable_test.rb
Expand Up @@ -27,7 +27,7 @@ def visit_user_confirmation_with_token(confirmation_token)
assert_response :success
assert_template 'confirmations/new'
assert_have_selector '#errorExplanation'
assert_contain 'Confirmation token is invalid'
assert_contain /Confirmation token(.*)invalid/
end

test 'user with valid confirmation token should be able to confirm an account' do
Expand All @@ -44,7 +44,8 @@ def visit_user_confirmation_with_token(confirmation_token)

test 'user already confirmed user should not be able to confirm the account again' do
user = create_user(:confirm => false)
user.update_attribute(:confirmed_at, Time.now)
user.confirmed_at = Time.now
user.save
visit_user_confirmation_with_token(user.confirmation_token)

assert_template 'confirmations/new'
Expand Down
2 changes: 1 addition & 1 deletion test/integration/recoverable_test.rb
Expand Up @@ -78,7 +78,7 @@ def reset_password(options={}, &block)
assert_response :success
assert_template 'passwords/edit'
assert_have_selector '#errorExplanation'
assert_contain 'Reset password token is invalid'
assert_contain /Reset password token(.*)invalid/
assert_not user.reload.valid_password?('987654321')
end

Expand Down
2 changes: 1 addition & 1 deletion test/models/authenticatable_test.rb
Expand Up @@ -168,8 +168,8 @@ def encrypt_password(user, pepper=User.pepper, stretches=User.stretches, encrypt
user = create_user
assert_not user.update_with_password(:old_password => 'other',
:password => 'pass321', :password_confirmation => 'pass321')
assert_equal 'is invalid', user.errors[:old_password]
assert user.reload.valid_password?('123456')
assert_match /invalid/, user.errors[:old_password]
end

test 'should not update password with invalid confirmation' do
Expand Down
12 changes: 7 additions & 5 deletions test/models/confirmable_test.rb
Expand Up @@ -57,7 +57,7 @@ def setup
assert_nil user.errors[:email]

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

test 'should find and confirm an user automatically' do
Expand All @@ -70,18 +70,19 @@ 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_equal "is invalid", confirmed_user.errors[:confirmation_token]
assert_match /invalid/, confirmed_user.errors[:confirmation_token]
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_equal "can't be blank", confirmed_user.errors[:confirmation_token]
assert_match /blank/, confirmed_user.errors[:confirmation_token]
end

test 'should generate errors for a user email if user is already confirmed' do
user = create_user
user.update_attribute(:confirmed_at, Time.now)
user.confirmed_at = Time.now
user.save
confirmed_user = User.confirm!(:confirmation_token => user.confirmation_token)
assert confirmed_user.confirmed?
assert confirmed_user.errors[:email]
Expand Down Expand Up @@ -220,7 +221,8 @@ def setup

test 'should not be active without confirmation' do
user = create_user
user.update_attribute(:confirmation_sent_at, nil)
user.confirmation_sent_at = nil
user.save
assert_not user.reload.active?
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_equal 'not found', reset_password_user.errors[:email]
assert_match /not found/, reset_password_user.errors[:email]
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_equal 'is invalid', reset_password_user.errors[:reset_password_token]
assert_match /invalid/, reset_password_user.errors[:reset_password_token]
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_equal "can't be blank", reset_password_user.errors[:reset_password_token]
assert_match /blank/, reset_password_user.errors[:reset_password_token]
end

test 'should reset successfully user password given the new password and confirmation' do
Expand Down
16 changes: 11 additions & 5 deletions test/models/rememberable_test.rb
Expand Up @@ -56,7 +56,8 @@ def setup
test 'valid remember token should also verify if remember is not expired' do
user = create_user
user.remember_me!
user.update_attributes(:remember_created_at => 3.days.ago)
user.remember_created_at = 3.days.ago
user.save
assert_not user.valid_remember_token?(user.remember_token)
end

Expand All @@ -72,8 +73,11 @@ def setup
assert_equal user, User.serialize_from_cookie("#{user.id}::#{user.remember_token}")
end

test 'serialize should return nil if no user is found' do
assert_nil User.serialize_from_cookie('0::123')
# MongoMapper cries if an invalid ID is given, so this does not need to be tested
unless DEVISE_ORM == :mongo_mapper
test 'serialize should return nil if no user is found' do
assert_nil User.serialize_from_cookie('0::123')
end
end

test 'remember me return nil if is a valid user with invalid token' do
Expand Down Expand Up @@ -113,7 +117,8 @@ def setup
swap Devise, :remember_for => 1.day do
user = create_user
user.remember_me!
user.update_attribute(:remember_created_at, 2.days.ago)
user.remember_created_at = 2.days.ago
user.save
assert user.remember_expired?
end
end
Expand All @@ -122,7 +127,8 @@ def setup
swap Devise, :remember_for => 30.days do
user = create_user
user.remember_me!
user.update_attribute(:remember_created_at, 30.days.ago + 2.minutes)
user.remember_created_at = (30.days.ago + 2.minutes)
user.save
assert_not user.remember_expired?
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/support/integration_tests_helper.rb
Expand Up @@ -7,7 +7,7 @@ def warden
def create_user(options={})
@user ||= begin
user = User.create!(
:email => 'user@test.com', :password => '123456', :password_confirmation => '123456'
:email => 'user@test.com', :password => '123456', :password_confirmation => '123456', :created_at => Time.now.utc
)
user.confirm! unless options[:confirm] == false
user
Expand Down
1 change: 0 additions & 1 deletion test/support/model_tests_helper.rb
Expand Up @@ -22,7 +22,6 @@ def generate_unique_email

def valid_attributes(attributes={})
{ :email => generate_unique_email,
:created_at => Time.now.utc,
:password => '123456',
:password_confirmation => '123456' }.update(attributes)
end
Expand Down
2 changes: 2 additions & 0 deletions test/test_helper.rb
Expand Up @@ -2,6 +2,8 @@

ENV["RAILS_ENV"] = "test"
DEVISE_ORM = (ENV["DEVISE_ORM"] || :active_record).to_sym

puts "\n==> Devise.orm = #{DEVISE_ORM.inspect}"
require File.join(File.dirname(__FILE__), 'orm', DEVISE_ORM.to_s)

require 'webrat'
Expand Down
8 changes: 2 additions & 6 deletions test/test_helpers_test.rb
Expand Up @@ -39,23 +39,19 @@ class TestHelpersTest < ActionController::TestCase
end

test "allows to sign in with different users" do
first_user = create_user(1)
first_user = create_user
first_user.confirm!

sign_in first_user
get :show
assert_equal first_user.id.to_s, @response.body
sign_out first_user

second_user = create_user(2)
second_user = create_user
second_user.confirm!

sign_in second_user
get :show
assert_equal second_user.id.to_s, @response.body
end

def create_user(i=nil)
super(:email => "jose.valim#{i}@plataformatec.com")
end
end

0 comments on commit 35a8d13

Please sign in to comment.