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