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

Add #ignoring_check_for_db_column to HaveSecureTokenMatcher #1437

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 26 additions & 1 deletion lib/shoulda/matchers/active_record/have_secure_token_matcher.rb
Expand Up @@ -23,6 +23,25 @@ module ActiveRecord
#
# #### Qualifiers
#
# ##### ignoring_check_for_db_column
#
# By default, this matcher tests that a token column is present.
# Use `ignoring_check_for_db_column` if this is not the case.
#
# class User < ActiveRecord
# has_secure_token :auth_token
# end
#
# # RSpec
# RSpec.describe User, type: :model do
# it { should have_secure_token(:auth_token).ignoring_check_for_db_column }
# end
#
# # Minitest (Shoulda)
# class UserTest < ActiveSupport::TestCase
# should have_secure_token(:auth_token).ignoring_check_for_db_column
# end
#
# ##### ignoring_check_for_db_index
#
# By default, this matcher tests that an index is defined on your token
Expand Down Expand Up @@ -81,6 +100,12 @@ def matches?(subject)
@errors.empty?
end

def ignoring_check_for_db_column
@options[:ignore_check_for_db_column] = true
ignoring_check_for_db_index
self
end

def ignoring_check_for_db_index
@options[:ignore_check_for_db_index] = true
self
Expand All @@ -93,7 +118,7 @@ def run_checks
if !has_expected_instance_methods?
@errors << 'missing expected class and instance methods'
end
if !has_expected_db_column?
if !@options[:ignore_check_for_db_column] && !has_expected_db_column?
@errors << "missing correct column #{token_attribute}:string"
end
if !@options[:ignore_check_for_db_index] && !has_expected_db_index?
Expand Down
Expand Up @@ -54,6 +54,18 @@
to fail_with_message(expected_message)
end
end

it 'matches when called with ignoring_check_for_db_column without db column' do
create_table(:users) do |t|
end

valid_model = define_model_class(:User) do
attr_accessor :token
has_secure_token
end
expect(valid_model.new).
to have_secure_token.ignoring_check_for_db_column
end

it 'matches when called with ignoring_check_for_db_index without db index' do
create_table(:users) do |t|
Expand Down