Skip to content

Commit

Permalink
Ensure Devise isn't performing model validations
Browse files Browse the repository at this point in the history
  • Loading branch information
tegon committed Mar 12, 2018
1 parent c0ede63 commit 3dd45e9
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 1 deletion.
6 changes: 6 additions & 0 deletions test/integration/authenticatable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
require 'test_helper'

class AuthenticationSanityTest < Devise::IntegrationTest
test 'sign in should not run model validations' do
sign_in_as_user

refute User.validations_performed
end

test 'home should be accessible without sign in' do
visit '/'
assert_response :success
Expand Down
6 changes: 6 additions & 0 deletions test/integration/http_authenticatable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
require 'test_helper'

class HttpAuthenticationTest < Devise::IntegrationTest
test 'sign in with HTTP should not run model validations' do
sign_in_as_new_user_with_http

refute User.validations_performed
end

test 'handles unverified requests gets rid of caches but continues signed in' do
swap ApplicationController, allow_forgery_protection: true do
create_user
Expand Down
11 changes: 11 additions & 0 deletions test/integration/omniauthable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ def stub_action!(name)
end
end

test "omniauth sign in should not run model validations" do
stub_action!(:sign_in_facebook) do
create_user
visit "/users/sign_in"
click_link "Sign in with FaceBook"
assert warden.authenticated?(:user)

refute User.validations_performed
end
end

test "can access omniauth.auth in the env hash" do
visit "/users/sign_in"
click_link "Sign in with FaceBook"
Expand Down
6 changes: 6 additions & 0 deletions test/integration/trackable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
require 'test_helper'

class TrackableHooksTest < Devise::IntegrationTest
test "sign in with HTTP should not run model validations" do
create_user
sign_in_as_user

refute User.validations_performed
end

test "current and last sign in timestamps are updated on each sign in" do
user = create_user
Expand Down
12 changes: 11 additions & 1 deletion test/models/trackable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,22 @@ class TrackableTest < ActiveSupport::TestCase
assert_equal 0, user.sign_in_count
end

test 'update_tracked_fields should run model validations' do
test "update_tracked_fields! should not persist invalid records" do
user = UserWithValidations.new
request = mock
request.stubs(:remote_ip).returns("127.0.0.1")

assert_not user.update_tracked_fields!(request)
assert_not user.persisted?
end

test "update_tracked_fields! should not run model validations" do
user = User.new
request = mock
request.stubs(:remote_ip).returns("127.0.0.1")

user.expects(:after_validation_callback).never

assert_not user.update_tracked_fields!(request)
end
end
8 changes: 8 additions & 0 deletions test/rails_app/app/active_record/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ class User < ActiveRecord::Base
include ActiveModel::Serializers::Xml if Devise::Test.rails5?

validates :sign_in_count, presence: true

cattr_accessor :validations_performed
after_validation :after_validation_callback

def after_validation_callback
# used to check in our test if the validations were called
@@validations_performed = true
end
end
9 changes: 9 additions & 0 deletions test/rails_app/app/mongoid/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,13 @@ class User
field :failed_attempts, type: Integer, default: 0 # Only if lock strategy is :failed_attempts
field :unlock_token, type: String # Only if unlock strategy is :email or :both
field :locked_at, type: Time

cattr_accessor :validations_performed

after_validation :after_validation_callback

def after_validation_callback
# used to check in our test if the validations were called
@@validations_performed = true
end
end
1 change: 1 addition & 0 deletions test/support/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def create_user(options={})
user.update_attribute(:confirmation_sent_at, options[:confirmation_sent_at]) if options[:confirmation_sent_at]
user.confirm unless options[:confirm] == false
user.lock_access! if options[:locked] == true
User.validations_performed = false
user
end
end
Expand Down

0 comments on commit 3dd45e9

Please sign in to comment.