-
-
Notifications
You must be signed in to change notification settings - Fork 910
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support custom has_secure_password attributes
Starting with version 6, Rails accepts custom attributes for has_secure_password macro. Without one it defaults to `password`, so everything should still work without breaking for older versions of Rails as well.
- Loading branch information
1 parent
4ab77e8
commit 4868da7
Showing
3 changed files
with
78 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 31 additions & 10 deletions
41
spec/unit/shoulda/matchers/active_model/have_secure_password_matcher_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,39 @@ | ||
require 'unit_spec_helper' | ||
|
||
describe Shoulda::Matchers::ActiveModel::HaveSecurePasswordMatcher, type: :model do | ||
it 'matches when the subject configures has_secure_password with default options' do | ||
working_model = define_model(:example, password_digest: :string) { has_secure_password } | ||
expect(working_model.new).to have_secure_password | ||
end | ||
context "with no arguments passed to has_secure_password" do | ||
it 'matches when the subject configures has_secure_password with default options' do | ||
working_model = define_model(:example, password_digest: :string) { has_secure_password } | ||
expect(working_model.new).to have_secure_password | ||
end | ||
|
||
it 'does not match when the subject does not authenticate a password' do | ||
no_secure_password = define_model(:example) | ||
expect(no_secure_password.new).not_to have_secure_password | ||
end | ||
|
||
it 'does not match when the subject does not authenticate a password' do | ||
no_secure_password = define_model(:example) | ||
expect(no_secure_password.new).not_to have_secure_password | ||
it 'does not match when the subject is missing the password_digest attribute' do | ||
no_digest_column = define_model(:example) { has_secure_password } | ||
expect(no_digest_column.new).not_to have_secure_password | ||
end | ||
end | ||
|
||
it 'does not match when the subject is missing the password_digest attribute' do | ||
no_digest_column = define_model(:example) { has_secure_password } | ||
expect(no_digest_column.new).not_to have_secure_password | ||
if active_model_supports_custom_has_secure_password_attribute? | ||
context "when custom attribute is given to has_secure_password" do | ||
it 'matches when the subject configures has_secure_password with correct options' do | ||
working_model = define_model(:example, reset_password_digest: :string) { has_secure_password :reset_password } | ||
expect(working_model.new).to have_secure_password :reset_password | ||
end | ||
|
||
it 'does not match when the subject does not authenticate a password' do | ||
no_secure_password = define_model(:example) | ||
expect(no_secure_password.new).not_to have_secure_password :reset_password | ||
end | ||
|
||
it 'does not match when the subject is missing the custom digest attribute' do | ||
no_digest_column = define_model(:example) { has_secure_password :reset_password } | ||
expect(no_digest_column.new).not_to have_secure_password :reset_password | ||
end | ||
end | ||
end | ||
end |