Skip to content

Commit 801b762

Browse files
Merge pull request #5 from matthewsullivan/feature/add-password-strength
Feature/add password strength
2 parents 67b9c64 + f04fd01 commit 801b762

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

app/models/user.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ class User < ApplicationRecord
99
}, presence: true, uniqueness: true
1010
validates :first_name, presence: true
1111
validates :last_name, presence: true
12+
validates :password, length: { minimum: 6 }
1213
end

test/graph/mutations/register_test.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,15 @@ def perform(args = {})
9090
parameters = build_parameters(@user)
9191
result = perform(parameters)
9292

93-
assert_equal("Invalid input: Password can't be blank", result['errors'][0]['message'])
93+
assert_equal("Invalid input: Password can't be blank, Password is too short (minimum is 6 characters)", result['errors'][0]['message'])
94+
end
95+
96+
test 'should not register with short password' do
97+
@user[:password] = '(a1)'
98+
parameters = build_parameters(@user)
99+
result = perform(parameters)
100+
101+
assert_equal("Invalid input: Password is too short (minimum is 6 characters)", result['errors'][0]['message'])
94102
end
95103

96104
test 'should not register with duplicate email' do

test/graph/mutations/update_user_test.rb

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
module Mutations
66
class UpdateUserTest < ActionDispatch::IntegrationTest
77
def setup
8+
@password = '!a1B2c3D4e5F6g!'
89
@token = login_as(users(:john))[:token]
910
end
1011

@@ -45,7 +46,7 @@ def perform(args = {})
4546
firstName: 'Jonathan',
4647
lastName: 'D.',
4748
48-
password: '!a1B2c3D4e5F6g!'
49+
password: @password
4950
}
5051
}
5152
}
@@ -61,7 +62,8 @@ def perform(args = {})
6162
parameters = {
6263
input: {
6364
arguments: {
64-
firstName: ''
65+
firstName: '',
66+
password: @password
6567
}
6668
}
6769
}
@@ -74,7 +76,8 @@ def perform(args = {})
7476
parameters = {
7577
input: {
7678
arguments: {
77-
lastName: ''
79+
lastName: '',
80+
password: @password
7881
}
7982
}
8083
}
@@ -87,7 +90,8 @@ def perform(args = {})
8790
parameters = {
8891
input: {
8992
arguments: {
90-
email: ''
93+
email: '',
94+
password: @password
9195
}
9296
}
9397
}
@@ -100,7 +104,8 @@ def perform(args = {})
100104
parameters = {
101105
input: {
102106
arguments: {
103-
107+
108+
password: @password
104109
}
105110
}
106111
}
@@ -113,13 +118,40 @@ def perform(args = {})
113118
parameters = {
114119
input: {
115120
arguments: {
116-
email: 'janedoelocalhost'
121+
email: 'janedoelocalhost',
122+
password: @password
117123
}
118124
}
119125
}
120126
result = perform(parameters)
121127

122128
assert_equal('Invalid input: Email invalid format', result['errors'][0]['message'])
123129
end
130+
131+
test 'should not update without password' do
132+
parameters = {
133+
input: {
134+
arguments: {
135+
password: ''
136+
}
137+
}
138+
}
139+
result = perform(parameters)
140+
141+
assert_equal("Invalid input: Password is too short (minimum is 6 characters)", result['errors'][0]['message'])
142+
end
143+
144+
test 'should not update with short password' do
145+
parameters = {
146+
input: {
147+
arguments: {
148+
password: '(a1)'
149+
}
150+
}
151+
}
152+
result = perform(parameters)
153+
154+
assert_equal("Invalid input: Password is too short (minimum is 6 characters)", result['errors'][0]['message'])
155+
end
124156
end
125157
end

test/models/user_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,12 @@ def setup
5555
refute(user.valid?)
5656
assert_not_nil(user.errors[:password], 'no password present')
5757
end
58+
59+
test 'should not create with short password' do
60+
@user[:password] = '(a1)'
61+
user = User.create(@user)
62+
63+
refute(user.valid?)
64+
assert_equal(user.errors[:password][0], 'is too short (minimum is 6 characters)')
65+
end
5866
end

0 commit comments

Comments
 (0)