Skip to content
Browse files

Tests passing for ActiveRecord and MongoMapper.

  • Loading branch information...
1 parent 947de72 commit 35a8d13369b30dd9c67111dc621ea47fb3b5d826 @josevalim josevalim committed Dec 21, 2009
View
14 Rakefile
@@ -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'
View
24 lib/devise/models.rb
@@ -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 }
@@ -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
View
2 lib/devise/models/authenticatable.rb
@@ -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
View
2 lib/devise/models/confirmable.rb
@@ -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
View
5 test/integration/confirmable_test.rb
@@ -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
@@ -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'
View
2 test/integration/recoverable_test.rb
@@ -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
View
2 test/models/authenticatable_test.rb
@@ -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
View
12 test/models/confirmable_test.rb
@@ -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
@@ -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]
@@ -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
View
6 test/models/recoverable_test.rb
@@ -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
@@ -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
View
16 test/models/rememberable_test.rb
@@ -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
@@ -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
@@ -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
@@ -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
View
2 test/support/integration_tests_helper.rb
@@ -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
View
1 test/support/model_tests_helper.rb
@@ -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
View
2 test/test_helper.rb
@@ -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'
View
8 test/test_helpers_test.rb
@@ -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.
Something went wrong with that request. Please try again.