Skip to content

Commit

Permalink
Split new scope validation to separate test user. CR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
xhocquet committed Aug 12, 2020
1 parent 361efa2 commit bd4580f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
4 changes: 1 addition & 3 deletions test/integration/registerable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ def user_sign_up
# 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
user.update_attribute(:username, nil)

create_user
get new_user_registration_path

fill_in 'email', with: '[email protected]'
Expand Down
12 changes: 3 additions & 9 deletions test/models/validatable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,9 @@ class ValidatableTest < ActiveSupport::TestCase
assert user.valid?
end

test 'should allow duplicate email when email_scope attribute does not match' do
existing_user = create_user

user = new_user(email: existing_user.email)
user.username = nil
assert user.valid?
assert_no_match(/taken/, user.errors[:email].join)

user.save(validate: false)
test 'should allow non-unique emails using email_scope attribute' do
existing_user = create_user_with_scope
user = new_user_with_scope(email: existing_user.email, username: "New username")
assert user.valid?
end

Expand Down
10 changes: 10 additions & 0 deletions test/rails_app/app/active_record/user_with_scope.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require 'shared_user_with_scope'

class UserWithScope < ActiveRecord::Base
self.table_name = 'users'
include Shim
include SharedUserWithScope
end

2 changes: 1 addition & 1 deletion test/rails_app/lib/shared_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module SharedUser
devise :database_authenticatable, :confirmable, :lockable, :recoverable,
:registerable, :rememberable, :timeoutable,
:trackable, :validatable, :omniauthable, password_length: 7..72,
reconfirmable: false, email_scope: [:username]
reconfirmable: false

attr_accessor :other_key

Expand Down
11 changes: 11 additions & 0 deletions test/rails_app/lib/shared_user_with_scope.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module SharedUserWithScope
extend ActiveSupport::Concern

included do
devise :database_authenticatable, :lockable, :recoverable,
:registerable, :rememberable, :timeoutable,
:trackable, :validatable, email_scope: [:username]
end
end
8 changes: 8 additions & 0 deletions test/support/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def new_user(attributes={})
User.new(valid_attributes(attributes))
end

def new_user_with_scope(attributes={})
UserWithScope.new(valid_attributes(attributes))
end

def create_user(attributes={})
User.create!(valid_attributes(attributes))
end
Expand All @@ -56,6 +60,10 @@ def create_user_with_validations(attributes={})
UserWithValidations.create!(valid_attributes(attributes))
end

def create_user_with_scope(attributes={})
UserWithScope.create!(valid_attributes(attributes))
end

# Execute the block setting the given values and restoring old values after
# the block is executed.
def swap(object, new_values)
Expand Down

0 comments on commit bd4580f

Please sign in to comment.