Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HaveSecureTokenMatcher: optional argument to ignore db index check #1278

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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