1
1
require "test_helper"
2
2
3
- class PasswordResetsTest < ActionDispatch ::IntegrationTest
3
+ class PasswordResets < ActionDispatch ::IntegrationTest
4
4
5
5
def setup
6
6
ActionMailer ::Base . deliveries . clear
7
7
@user = users ( :michael )
8
8
end
9
+ end
9
10
10
- test "password resets" do
11
+ class PasswordResetsTest < PasswordResets
12
+
13
+ test "password reset path" do
11
14
get new_password_reset_path
12
15
assert_template 'password_resets/new'
13
16
assert_select 'input[name=?]' , 'password_reset[email]'
14
- # Invalid email
17
+ end
18
+
19
+ test "reset path with invalid email" do
15
20
post password_resets_path , params : { password_reset : { email : "" } }
16
21
assert_not flash . empty?
17
22
assert_template 'password_resets/new'
18
- # Valid email
23
+ end
24
+ end
25
+
26
+ class PasswordForm < PasswordResets
27
+
28
+ def setup
29
+ super
19
30
post password_resets_path ,
20
31
params : { password_reset : { email : @user . email } }
32
+ @actual_user = assigns ( :user )
33
+ end
34
+ end
35
+
36
+ class PasswordFormTest < PasswordForm
37
+
38
+ test "reset with valid email" do
21
39
assert_not_equal @user . reset_digest , @user . reload . reset_digest
22
40
assert_equal 1 , ActionMailer ::Base . deliveries . size
23
41
assert_not flash . empty?
24
42
assert_redirected_to root_url
25
- # Password reset form
26
- user = assigns ( :user )
27
- # Wrong email
28
- get edit_password_reset_path ( user . reset_token , email : "" )
43
+ end
44
+
45
+ test "reset with wrong email" do
46
+ get edit_password_reset_path ( @actual_user . reset_token , email : "" )
29
47
assert_redirected_to root_url
30
- # Inactive user
31
- user . toggle! ( :activated )
32
- get edit_password_reset_path ( user . reset_token , email : user . email )
48
+ end
49
+
50
+ test "reset with inactive user" do
51
+ @actual_user . toggle! ( :activated )
52
+ get edit_password_reset_path ( @actual_user . reset_token ,
53
+ email : @actual_user . email )
33
54
assert_redirected_to root_url
34
- user . toggle! ( :activated )
35
- # Right email, wrong token
36
- get edit_password_reset_path ( 'wrong token' , email : user . email )
55
+ end
56
+
57
+ test "reset with right email but wrong token" do
58
+ get edit_password_reset_path ( 'wrong token' , email : @actual_user . email )
37
59
assert_redirected_to root_url
38
- # Right email, right token
39
- get edit_password_reset_path ( user . reset_token , email : user . email )
60
+ end
61
+
62
+ test "reset with right email and right token" do
63
+ get edit_password_reset_path ( @actual_user . reset_token ,
64
+ email : @actual_user . email )
40
65
assert_template 'password_resets/edit'
41
- assert_select "input[name=email][type=hidden][value=?]" , user . email
42
- # Invalid password & confirmation
43
- patch password_reset_path ( user . reset_token ) ,
44
- params : { email : user . email ,
66
+ assert_select "input[name=email][type=hidden][value=?]" , @actual_user . email
67
+ end
68
+ end
69
+
70
+ class PasswordUpdateTest < PasswordForm
71
+
72
+ test "update with invalid password and confirmation" do
73
+ patch password_reset_path ( @actual_user . reset_token ) ,
74
+ params : { email : @actual_user . email ,
45
75
user : { password : "foobaz" ,
46
76
password_confirmation : "barquux" } }
47
77
assert_select 'div#error_explanation'
48
- # Empty password
49
- patch password_reset_path ( user . reset_token ) ,
50
- params : { email : user . email ,
78
+ end
79
+
80
+ test "update with empty password" do
81
+ patch password_reset_path ( @actual_user . reset_token ) ,
82
+ params : { email : @actual_user . email ,
51
83
user : { password : "" ,
52
84
password_confirmation : "" } }
53
85
assert_select 'div#error_explanation'
54
- # Valid password & confirmation
55
- patch password_reset_path ( user . reset_token ) ,
56
- params : { email : user . email ,
86
+ end
87
+
88
+ test "update with valid password and confirmation" do
89
+ patch password_reset_path ( @actual_user . reset_token ) ,
90
+ params : { email : @actual_user . email ,
57
91
user : { password : "foobaz" ,
58
92
password_confirmation : "foobaz" } }
59
93
assert is_logged_in?
60
94
assert_not flash . empty?
61
- assert_redirected_to user
95
+ assert_redirected_to @actual_user
62
96
end
63
- end
97
+ end
0 commit comments