-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Introduce config to allow for password complexity #5727
Open
kykyi
wants to merge
6
commits into
heartcombo:main
Choose a base branch
from
kykyi:feautre/provide-config-options-for-password-complexity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+143
−3
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a6301cc
Introduce config to allow for password complexity
kykyi ea11968
Merge branch 'main' into feautre/provide-config-options-for-password-…
kykyi d463f9a
Update password complexity to be a hash instead of individual keys
kykyi a57aac3
Update docs on Devise::Models
kykyi 776a657
Update from PR review
kykyi 44dca10
Remove require_special to make the presence of special_characters imp…
kykyi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,10 +1,9 @@ | ||||||
# encoding: UTF-8 | ||||||
# frozen_string_literal: true | ||||||
|
||||||
require 'test_helper' | ||||||
require "test_helper" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||||||
|
||||||
class ValidatableTest < ActiveSupport::TestCase | ||||||
test 'should require email to be set' do | ||||||
test 'should require email to be set' do | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
remove redundant space |
||||||
user = new_user(email: nil) | ||||||
assert user.invalid? | ||||||
assert user.errors[:email] | ||||||
|
@@ -121,4 +120,80 @@ class ValidatableTest < ActiveSupport::TestCase | |||||
test 'required_fields should be an empty array' do | ||||||
assert_equal [], Devise::Models::Validatable.required_fields(User) | ||||||
end | ||||||
|
||||||
|
||||||
test "password must require a lower case letter if password_requires_lowercase_letter is true" do | ||||||
with_password_requirement(:password_requires_lowercase, false) do | ||||||
user = new_user(password: 'PASSWORD', password_confirmation: 'PASSWORD') | ||||||
assert user.valid? | ||||||
end | ||||||
|
||||||
with_password_requirement(:password_requires_lowercase, true) do | ||||||
user = new_user(password: 'PASSWORD', password_confirmation: 'PASSWORD') | ||||||
assert user.invalid? | ||||||
assert_equal 'must include at least one lowercase letter', user.errors[:password].join | ||||||
end | ||||||
end | ||||||
|
||||||
test "password must require an upper case letter if password_requires_uppercase_letter is true" do | ||||||
with_password_requirement(:password_requires_uppercase, false) do | ||||||
user = new_user(password: 'password', password_confirmation: 'password') | ||||||
assert user.valid? | ||||||
end | ||||||
|
||||||
with_password_requirement(:password_requires_uppercase, true) do | ||||||
user = new_user(password: 'password', password_confirmation: 'password') | ||||||
assert user.invalid? | ||||||
assert_equal 'must include at least one uppercase letter', user.errors[:password].join | ||||||
end | ||||||
end | ||||||
|
||||||
test "password must require an upper case letter if password_requires_number is true" do | ||||||
with_password_requirement(:password_requires_number, false) do | ||||||
user = new_user(password: 'password', password_confirmation: 'password') | ||||||
assert user.valid? | ||||||
end | ||||||
|
||||||
with_password_requirement(:password_requires_number, true) do | ||||||
user = new_user(password: 'password', password_confirmation: 'password') | ||||||
assert user.invalid? | ||||||
assert_equal 'must include at least one number', user.errors[:password].join | ||||||
end | ||||||
end | ||||||
|
||||||
test "password must require special character if password_requires_special_character is true" do | ||||||
with_password_requirement(:password_requires_special_character, false) do | ||||||
user = new_user(password: 'password', password_confirmation: 'password') | ||||||
assert user.valid? | ||||||
end | ||||||
|
||||||
with_password_requirement(:password_requires_special_character, true) do | ||||||
user = new_user(password: 'password', password_confirmation: 'password') | ||||||
assert user.invalid? | ||||||
assert_equal 'must include at least one special character', user.errors[:password].join | ||||||
end | ||||||
end | ||||||
|
||||||
|
||||||
test "special character must be within defined special character set if it is custom" do | ||||||
with_password_requirement(:password_requires_special_character, true) do | ||||||
with_password_requirement(:password_special_characters, '!') do | ||||||
user = new_user(password: 'password!', password_confirmation: 'password!') | ||||||
assert user.valid? | ||||||
|
||||||
user = new_user(password: 'password?', password_confirmation: 'password?') | ||||||
assert user.invalid? | ||||||
assert_equal 'must include at least one special character', user.errors[:password].join | ||||||
end | ||||||
end | ||||||
end | ||||||
|
||||||
def with_password_requirement(requirement, value) | ||||||
# Change the password requirement and restore it after the block is executed | ||||||
original_value = User.public_send(requirement) | ||||||
User.public_send("#{requirement}=", value) | ||||||
yield | ||||||
ensure | ||||||
User.public_send("#{requirement}=", original_value) | ||||||
end | ||||||
end |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if there could be some more general config like:
Which if
true
would require all of these individual pieces?So the validations could become: