Skip to content

Commit

Permalink
HaveSecureTokenMatcher: optional argument to ignore db index check
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosmichas committed Feb 21, 2020
1 parent 321b87e commit da25e61
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
Expand Up @@ -30,17 +30,18 @@ module ActiveRecord
#

# rubocop:disable Style/PredicateName
def have_secure_token(token_attribute = :token)
HaveSecureTokenMatcher.new(token_attribute)
def have_secure_token(token_attribute = :token, options = {})
HaveSecureTokenMatcher.new(token_attribute, options)
end
# rubocop:enable Style/PredicateName

# @private
class HaveSecureTokenMatcher
attr_reader :token_attribute

def initialize(token_attribute)
def initialize(token_attribute, options = {})
@token_attribute = token_attribute
@ignore_check_for_db_index = options[:ignore_check_for_db_index]
end

def description
Expand Down Expand Up @@ -75,7 +76,7 @@ def run_checks
if !has_expected_db_column?
@errors << "missing correct column #{token_attribute}:string"
end
if !has_expected_db_index?
if !@ignore_check_for_db_index && !has_expected_db_index?
@errors << "missing unique index for #{table_and_column}"
end
@errors
Expand Down
Expand Up @@ -58,6 +58,16 @@
end
end

it 'matches when missing a db index but ignore_check_for_db_index is true' do
create_table(:users) do |t|
t.string :token
end

valid_model = define_model_class(:User) { has_secure_token }
expect(valid_model.new).
to have_secure_token(:token, ignore_check_for_db_index: true)
end

it 'does not match when missing a token column' do
create_table(:users)
invalid_model = define_model_class(:User) { has_secure_token }
Expand Down

0 comments on commit da25e61

Please sign in to comment.