Skip to content

Commit

Permalink
HaveSecureTokenMatcher: qualifier to ignore db index check
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosmichas authored and mcmire committed Mar 20, 2020
1 parent 321b87e commit 81b9ec0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Expand Up @@ -41,6 +41,7 @@ class HaveSecureTokenMatcher

def initialize(token_attribute)
@token_attribute = token_attribute
@options = { ignore_check_for_db_index: false }
end

def description
Expand All @@ -65,6 +66,11 @@ def matches?(subject)
@errors.empty?
end

def ignoring_check_for_db_index
@options[:ignore_check_for_db_index] = true
self
end

private

def run_checks
Expand All @@ -75,7 +81,7 @@ def run_checks
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
Expand Down
Expand Up @@ -58,6 +58,16 @@
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 }
Expand Down

0 comments on commit 81b9ec0

Please sign in to comment.