From 55af9f8a40b52274e95c00334d85f7897cf48187 Mon Sep 17 00:00:00 2001 From: Ches Martin Date: Fri, 5 Aug 2011 01:38:42 +0700 Subject: [PATCH] Fix failing Mongoid tests - attr_accessible not set for test user model, making Serializable tests inaccurate - Mongoid does not `include_root_in_json` by default, so enable this for consistency with AR tests - Mark tests pending for Mongoid < 2.1 that fail there due to known bugs - Add `:mongoid` key for i18n model labels - Remove outdated shim of `update_attribute` that caused mass assignment security to be applied (ugh, that took awhile to find) --- Gemfile | 2 +- Gemfile.lock | 14 ++++++-------- test/helpers/devise_helper_test.rb | 12 ++++++++++-- test/integration/registerable_test.rb | 8 ++++++++ test/orm/mongoid.rb | 3 +++ test/rails_app/app/active_record/user.rb | 2 -- test/rails_app/app/mongoid/shim.rb | 5 ----- test/rails_app/lib/shared_user.rb | 1 + 8 files changed, 29 insertions(+), 18 deletions(-) diff --git a/Gemfile b/Gemfile index 2f774306..18e6e1c9 100644 --- a/Gemfile +++ b/Gemfile @@ -28,7 +28,7 @@ platforms :ruby do group :mongoid do gem "mongo", "~> 1.3.0" - gem "mongoid", "2.0.1" + gem "mongoid", "~> 2.0" gem "bson_ext", "~> 1.3.0" end end diff --git a/Gemfile.lock b/Gemfile.lock index cab13426..5d8a77cc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -43,7 +43,7 @@ GEM addressable (2.2.4) arel (2.0.9) bcrypt-ruby (2.1.4) - bson (1.3.0) + bson (1.3.1) bson_ext (1.3.0) builder (2.1.2) columnize (0.3.2) @@ -63,13 +63,12 @@ GEM treetop (~> 1.4.8) mime-types (1.16) mocha (0.9.12) - mongo (1.3.0) - bson (>= 1.3.0) - mongoid (2.0.1) + mongo (1.3.1) + bson (>= 1.3.1) + mongoid (2.1.4) activemodel (~> 3.0) mongo (~> 1.3) tzinfo (~> 0.3.22) - will_paginate (~> 3.0.pre) multi_json (0.0.5) multipart-post (1.1.0) nokogiri (1.4.3.1) @@ -130,7 +129,7 @@ GEM thor (0.14.6) treetop (1.4.9) polyglot (>= 0.3.1) - tzinfo (0.3.27) + tzinfo (0.3.29) warden (1.0.4) rack (>= 1.0) weakling (0.0.4-java) @@ -138,7 +137,6 @@ GEM nokogiri (>= 1.2.0) rack (>= 1.0) rack-test (>= 0.5.3) - will_paginate (3.0.pre2) PLATFORMS java @@ -150,7 +148,7 @@ DEPENDENCIES devise! mocha mongo (~> 1.3.0) - mongoid (= 2.0.1) + mongoid (~> 2.0) oa-oauth (~> 0.2.0) oa-openid (~> 0.2.0) rails (~> 3.0.7) diff --git a/test/helpers/devise_helper_test.rb b/test/helpers/devise_helper_test.rb index 6b3a6af3..61ad1f7b 100644 --- a/test/helpers/devise_helper_test.rb +++ b/test/helpers/devise_helper_test.rb @@ -2,13 +2,16 @@ require 'test_helper' class DeviseHelperTest < ActionController::IntegrationTest setup do + model_labels = { :models => { :user => "utilisateur" } } + I18n.backend.store_translations :fr, { :errors => { :messages => { :not_saved => { :one => "Erreur lors de l'enregistrement de '%{resource}': 1 erreur.", :other => "Erreur lors de l'enregistrement de '%{resource}': %{count} erreurs." } } }, - :activerecord => { :models => { :user => "utilisateur" } } + :activerecord => model_labels, + :mongoid => model_labels } I18n.locale = 'fr' @@ -30,6 +33,10 @@ class DeviseHelperTest < ActionController::IntegrationTest end test 'test errors.messages.not_saved with multiple errors from i18n' do + # Dirty tracking behavior prevents email validations from being applied: + # https://github.com/mongoid/mongoid/issues/756 + (pending "Fails on Mongoid < 2.1"; break) if defined?(Mongoid) && Mongoid::VERSION.to_f < 2.1 + get new_user_registration_path fill_in 'email', :with => 'invalid_email' @@ -40,4 +47,5 @@ class DeviseHelperTest < ActionController::IntegrationTest assert_have_selector '#error_explanation' assert_contain "Erreur lors de l'enregistrement de 'utilisateur': 2 erreurs" end -end \ No newline at end of file +end + diff --git a/test/integration/registerable_test.rb b/test/integration/registerable_test.rb index 846bac47..5a116a55 100644 --- a/test/integration/registerable_test.rb +++ b/test/integration/registerable_test.rb @@ -69,6 +69,10 @@ class RegistrationTest < ActionController::IntegrationTest end test 'a guest user cannot sign up with invalid information' do + # Dirty tracking behavior prevents email validations from being applied: + # https://github.com/mongoid/mongoid/issues/756 + (pending "Fails on Mongoid < 2.1"; break) if defined?(Mongoid) && Mongoid::VERSION.to_f < 2.1 + get new_user_registration_path fill_in 'email', :with => 'invalid_email' @@ -87,6 +91,10 @@ class RegistrationTest < ActionController::IntegrationTest end test 'a guest should not sign up with email/password that already exists' do + # Dirty tracking behavior prevents email validations from being applied: + # https://github.com/mongoid/mongoid/issues/756 + (pending "Fails on Mongoid < 2.1"; break) if defined?(Mongoid) && Mongoid::VERSION.to_f < 2.1 + user = create_user get new_user_registration_path diff --git a/test/orm/mongoid.rb b/test/orm/mongoid.rb index 315530e9..b28a796c 100644 --- a/test/orm/mongoid.rb +++ b/test/orm/mongoid.rb @@ -1,6 +1,9 @@ +require 'mongoid/version' + Mongoid.configure do |config| config.master = Mongo::Connection.new('127.0.0.1', 27017).db("devise-test-suite") config.use_utc = true + config.include_root_in_json = true end class ActiveSupport::TestCase diff --git a/test/rails_app/app/active_record/user.rb b/test/rails_app/app/active_record/user.rb index ccb119e2..eefe2dd9 100644 --- a/test/rails_app/app/active_record/user.rb +++ b/test/rails_app/app/active_record/user.rb @@ -3,6 +3,4 @@ require 'shared_user' class User < ActiveRecord::Base include Shim include SharedUser - - attr_accessible :username, :email, :password, :password_confirmation, :remember_me end diff --git a/test/rails_app/app/mongoid/shim.rb b/test/rails_app/app/mongoid/shim.rb index 87317e98..0556561e 100644 --- a/test/rails_app/app/mongoid/shim.rb +++ b/test/rails_app/app/mongoid/shim.rb @@ -21,9 +21,4 @@ module Shim def ==(other) other.is_a?(self.class) && _id == other._id end - - # Mongoid does not have this method in the current beta version (2.0.0.beta.20) - def update_attribute(attribute, value) - update_attributes(attribute => value) - end end diff --git a/test/rails_app/lib/shared_user.rb b/test/rails_app/lib/shared_user.rb index e4bd8712..6f5ba230 100644 --- a/test/rails_app/lib/shared_user.rb +++ b/test/rails_app/lib/shared_user.rb @@ -7,6 +7,7 @@ module SharedUser :trackable, :validatable, :omniauthable attr_accessor :other_key + attr_accessible :username, :email, :password, :password_confirmation, :remember_me # They need to be included after Devise is called. extend ExtendMethods