active_model-password 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -6
- data/lib/active_model/password/version.rb +1 -1
- data/lib/active_model/password.rb +16 -2
- data/test/password_test.rb +28 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7032cfaaa97d36b5a31b521501b89cba1f53e2a3
|
4
|
+
data.tar.gz: 9d77c84a05dd700d7d1782c892ad5f8a246cfe30
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e081ff3687ed785744c68b03f80f0dcc468b59982fe8f8434afda4a3fcb9ee2a1df50d3c7184cb5c8342156e2f8698a5b0b8633fca1c968b0d51974fad62e16
|
7
|
+
data.tar.gz: 33ab627139e2ae5ebf22e1a7b7cbb431165aa9094af9ca7b81dfcbe74dd5385d10203a635a029fa150c7c040be4693de63aa64b3fa98520504b2db95a0528a84
|
data/README.md
CHANGED
@@ -45,12 +45,7 @@ The most popular workflow is:
|
|
45
45
|
end
|
46
46
|
|
47
47
|
If you don't like the default behavior, you can always inherit the
|
48
|
-
password model and override some defaults
|
49
|
-
|
50
|
-
class Password < ActiveModel::Password
|
51
|
-
validates :password, format: {with: /\A(?=.*[a-z])(?=.*[A-Z])(?=.*\d)./}, length: {in: 8..255}, if: -> { password.present? }
|
52
|
-
end
|
53
|
-
|
48
|
+
password model and override some defaults.
|
54
49
|
|
55
50
|
## Copyright
|
56
51
|
|
@@ -9,16 +9,30 @@ module ActiveModel
|
|
9
9
|
|
10
10
|
validates :user, presence: true
|
11
11
|
validates :password, presence: true, confirmation: true
|
12
|
+
validate :user_password, if: :user?
|
13
|
+
|
14
|
+
def user?
|
15
|
+
user.present?
|
16
|
+
end
|
12
17
|
|
13
18
|
def persisted?
|
14
|
-
user
|
19
|
+
user? and user.persisted?
|
15
20
|
end
|
16
21
|
|
17
22
|
def save
|
18
23
|
return false unless valid?
|
19
24
|
user.password = password
|
20
|
-
user.password_confirmation = password_confirmation
|
21
25
|
user.save
|
22
26
|
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def user_password
|
31
|
+
old_password = user.password
|
32
|
+
user.password = password
|
33
|
+
errors.add(:password, user.errors[:password]) if user.invalid? and user.errors[:password].present?
|
34
|
+
ensure
|
35
|
+
user.password = old_password
|
36
|
+
end
|
23
37
|
end
|
24
38
|
end
|
data/test/password_test.rb
CHANGED
@@ -1,10 +1,13 @@
|
|
1
1
|
require "test_helper"
|
2
2
|
|
3
3
|
class User
|
4
|
+
include ActiveModel::Model
|
5
|
+
|
4
6
|
attr_accessor :password, :password_confirmation, :persisted
|
7
|
+
validates :password, format: {with: /\A(?=.*[a-z])(?=.*[A-Z])(?=.*\d)./}, length: {in: 8..255}, if: -> { password.present? }
|
5
8
|
|
6
9
|
def save
|
7
|
-
|
10
|
+
valid?
|
8
11
|
end
|
9
12
|
|
10
13
|
def persisted?
|
@@ -36,6 +39,30 @@ class PasswordTest < Test::Unit::TestCase
|
|
36
39
|
assert @password.errors[:password].present?
|
37
40
|
end
|
38
41
|
|
42
|
+
test "is invalid with too short password" do
|
43
|
+
@password.password = "Secret1"
|
44
|
+
assert @password.invalid?
|
45
|
+
assert @password.errors[:password].present?
|
46
|
+
end
|
47
|
+
|
48
|
+
test "is invalid with password without uppercase letter" do
|
49
|
+
@password.password = "secretsecret1"
|
50
|
+
assert @password.invalid?
|
51
|
+
assert @password.errors[:password].present?
|
52
|
+
end
|
53
|
+
|
54
|
+
test "is invalid with password without lowercase letter" do
|
55
|
+
@password.password = "SECRETSECRET1"
|
56
|
+
assert @password.invalid?
|
57
|
+
assert @password.errors[:password].present?
|
58
|
+
end
|
59
|
+
|
60
|
+
test "is invalid with password without number" do
|
61
|
+
@password.password = "SecretSecret"
|
62
|
+
assert @password.invalid?
|
63
|
+
assert @password.errors[:password].present?
|
64
|
+
end
|
65
|
+
|
39
66
|
test "is invalid without password confirmation" do
|
40
67
|
@password.password_confirmation = ""
|
41
68
|
assert @password.invalid?
|