]> git.openstreetmap.org Git - rails.git/blob - test/controllers/passwords_controller_test.rb
Merge remote-tracking branch 'upstream/pull/4747'
[rails.git] / test / controllers / passwords_controller_test.rb
1 require "test_helper"
2
3 class PasswordsControllerTest < ActionDispatch::IntegrationTest
4   ##
5   # test all routes which lead to this controller
6   def test_routes
7     assert_routing(
8       { :path => "/user/forgot-password", :method => :get },
9       { :controller => "passwords", :action => "new" }
10     )
11     assert_routing(
12       { :path => "/user/forgot-password", :method => :post },
13       { :controller => "passwords", :action => "create" }
14     )
15     assert_routing(
16       { :path => "/user/reset-password", :method => :get },
17       { :controller => "passwords", :action => "edit" }
18     )
19     assert_routing(
20       { :path => "/user/reset-password", :method => :post },
21       { :controller => "passwords", :action => "update" }
22     )
23   end
24
25   def test_lost_password
26     # Test fetching the lost password page
27     get user_forgot_password_path
28     assert_response :success
29     assert_template :new
30     assert_select "div#notice", false
31
32     # Test resetting using the address as recorded for a user that has an
33     # address which is duplicated in a different case by another user
34     user = create(:user)
35     uppercase_user = build(:user, :email => user.email.upcase).tap { |u| u.save(:validate => false) }
36
37     # Resetting with GET should fail
38     assert_no_difference "ActionMailer::Base.deliveries.size" do
39       perform_enqueued_jobs do
40         get user_forgot_password_path, :params => { :email => user.email }
41       end
42     end
43     assert_response :success
44     assert_template :new
45
46     # Resetting with POST should work
47     assert_difference "ActionMailer::Base.deliveries.size", 1 do
48       perform_enqueued_jobs do
49         post user_forgot_password_path, :params => { :email => user.email }
50       end
51     end
52     assert_redirected_to login_path
53     assert_match(/^If your email address exists/, flash[:notice])
54     email = ActionMailer::Base.deliveries.first
55     assert_equal 1, email.to.count
56     assert_equal user.email, email.to.first
57     ActionMailer::Base.deliveries.clear
58
59     # Test resetting using an address that does not exist
60     assert_no_difference "ActionMailer::Base.deliveries.size" do
61       perform_enqueued_jobs do
62         post user_forgot_password_path, :params => { :email => "nobody@example.com" }
63       end
64     end
65     # Be paranoid about revealing there was no match
66     assert_redirected_to login_path
67     assert_match(/^If your email address exists/, flash[:notice])
68
69     # Test resetting using an address that matches a different user
70     # that has the same address in a different case
71     assert_difference "ActionMailer::Base.deliveries.size", 1 do
72       perform_enqueued_jobs do
73         post user_forgot_password_path, :params => { :email => user.email.upcase }
74       end
75     end
76     assert_redirected_to login_path
77     assert_match(/^If your email address exists/, flash[:notice])
78     email = ActionMailer::Base.deliveries.first
79     assert_equal 1, email.to.count
80     assert_equal uppercase_user.email, email.to.first
81     ActionMailer::Base.deliveries.clear
82
83     # Test resetting using an address that is a case insensitive match
84     # for more than one user but not an exact match for either
85     assert_no_difference "ActionMailer::Base.deliveries.size" do
86       perform_enqueued_jobs do
87         post user_forgot_password_path, :params => { :email => user.email.titlecase }
88       end
89     end
90     # Be paranoid about revealing there was no match
91     assert_redirected_to login_path
92     assert_match(/^If your email address exists/, flash[:notice])
93
94     # Test resetting using the address as recorded for a user that has an
95     # address which is case insensitively unique
96     third_user = create(:user)
97     assert_difference "ActionMailer::Base.deliveries.size", 1 do
98       perform_enqueued_jobs do
99         post user_forgot_password_path, :params => { :email => third_user.email }
100       end
101     end
102     assert_redirected_to login_path
103     assert_match(/^If your email address exists/, flash[:notice])
104     email = ActionMailer::Base.deliveries.first
105     assert_equal 1, email.to.count
106     assert_equal third_user.email, email.to.first
107     ActionMailer::Base.deliveries.clear
108
109     # Test resetting using an address that matches a user that has the
110     # same (case insensitively unique) address in a different case
111     assert_difference "ActionMailer::Base.deliveries.size", 1 do
112       perform_enqueued_jobs do
113         post user_forgot_password_path, :params => { :email => third_user.email.upcase }
114       end
115     end
116     assert_redirected_to login_path
117     assert_match(/^If your email address exists/, flash[:notice])
118     email = ActionMailer::Base.deliveries.first
119     assert_equal 1, email.to.count
120     assert_equal third_user.email, email.to.first
121     ActionMailer::Base.deliveries.clear
122   end
123
124   def test_reset_password
125     user = create(:user, :pending)
126     # Test a request with no token
127     get user_reset_password_path
128     assert_response :bad_request
129
130     # Test a request with a bogus token
131     get user_reset_password_path, :params => { :token => "made_up_token" }
132     assert_redirected_to :action => :new
133
134     # Create a valid token for a user
135     token = user.generate_token_for(:password_reset)
136
137     # Test a request with a valid token
138     get user_reset_password_path, :params => { :token => token }
139     assert_response :success
140     assert_template :edit
141
142     # Test that errors are reported for erroneous submissions
143     post user_reset_password_path, :params => { :token => token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "different_password" } }
144     assert_response :success
145     assert_template :edit
146     assert_select "div.invalid-feedback"
147
148     # Test setting a new password
149     post user_reset_password_path, :params => { :token => token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" } }
150     assert_redirected_to root_path
151     assert_equal user.id, session[:user]
152     user.reload
153     assert_equal "active", user.status
154     assert user.email_valid
155     assert_equal user, User.authenticate(:username => user.email, :password => "new_password")
156   end
157 end