HaveSecureTokenMatcher: qualifier to ignore db index check

This commit is contained in:
Nikolaos Michas 2020-02-21 09:56:28 +02:00 committed by Elliot Winkler
parent 321b87e76d
commit 81b9ec0469
2 changed files with 17 additions and 1 deletions

View File

@ -41,6 +41,7 @@ module Shoulda
def initialize(token_attribute)
@token_attribute = token_attribute
@options = { ignore_check_for_db_index: false }
end
def description
@ -65,6 +66,11 @@ module Shoulda
@errors.empty?
end
def ignoring_check_for_db_index
@options[:ignore_check_for_db_index] = true
self
end
private
def run_checks
@ -75,7 +81,7 @@ module Shoulda
if !has_expected_db_column?
@errors << "missing correct column #{token_attribute}:string"
end
if !has_expected_db_index?
if !@options[:ignore_check_for_db_index] && !has_expected_db_index?
@errors << "missing unique index for #{table_and_column}"
end
@errors

View File

@ -58,6 +58,16 @@ describe Shoulda::Matchers::ActiveRecord::HaveSecureTokenMatcher,
end
end
it 'matches when called with ignoring_check_for_db_index without db index' 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.ignoring_check_for_db_index
end
it 'does not match when missing a token column' do
create_table(:users)
invalid_model = define_model_class(:User) { has_secure_token }