active_model-password 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 196a3392ca7a52ab350848ddaca5810b9d9acf14
4
- data.tar.gz: d06691a64b9b4a6faec580c9f9278bea29db192c
3
+ metadata.gz: 7032cfaaa97d36b5a31b521501b89cba1f53e2a3
4
+ data.tar.gz: 9d77c84a05dd700d7d1782c892ad5f8a246cfe30
5
5
  SHA512:
6
- metadata.gz: c9beff8f2e9b5b791b1645d3a020babc5f1eaaee3c408774459e7187ea9908c4643f82d8550b3c085a07a6968152396312be44f7a508797a0d0b2665ad69d118
7
- data.tar.gz: 5929107214bd6c1833feee6f015d66291effc6af0015b6ef370b6af3136f546c8f90faa9c5e9f122dcd322d4f9388ce848f112979699c5ee7b2f511a8762f5fc
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
 
@@ -1,5 +1,5 @@
1
1
  module ActiveModel
2
2
  class Password
3
- VERSION = "1.0.0"
3
+ VERSION = "1.0.1"
4
4
  end
5
5
  end
@@ -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.present? and user.persisted?
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
@@ -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
- password == password_confirmation
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?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_model-password
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kuba Kuźma