]> git.openstreetmap.org Git - rails.git/blob - test/controllers/confirmations_controller_test.rb
Merge remote-tracking branch 'upstream/pull/4753'
[rails.git] / test / controllers / confirmations_controller_test.rb
1 require "test_helper"
2
3 class UsersControllerTest < ActionDispatch::IntegrationTest
4   ##
5   # test all routes which lead to this controller
6   def test_routes
7     assert_routing(
8       { :path => "/user/username/confirm", :method => :get },
9       { :controller => "confirmations", :action => "confirm", :display_name => "username" }
10     )
11     assert_routing(
12       { :path => "/user/username/confirm", :method => :post },
13       { :controller => "confirmations", :action => "confirm", :display_name => "username" }
14     )
15     assert_routing(
16       { :path => "/user/username/confirm/resend", :method => :get },
17       { :controller => "confirmations", :action => "confirm_resend", :display_name => "username" }
18     )
19
20     assert_routing(
21       { :path => "/user/confirm", :method => :get },
22       { :controller => "confirmations", :action => "confirm" }
23     )
24     assert_routing(
25       { :path => "/user/confirm", :method => :post },
26       { :controller => "confirmations", :action => "confirm" }
27     )
28     assert_routing(
29       { :path => "/user/confirm-email", :method => :get },
30       { :controller => "confirmations", :action => "confirm_email" }
31     )
32     assert_routing(
33       { :path => "/user/confirm-email", :method => :post },
34       { :controller => "confirmations", :action => "confirm_email" }
35     )
36   end
37
38   def test_confirm_get
39     user = build(:user, :pending)
40     post user_new_path, :params => { :user => user.attributes }
41     post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
42     confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
43
44     get user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
45     assert_response :success
46     assert_template :confirm
47   end
48
49   def test_confirm_get_already_confirmed
50     user = build(:user, :pending)
51     stub_gravatar_request(user.email)
52     post user_new_path, :params => { :user => user.attributes }
53     post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
54     confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
55
56     # Get the confirmation page
57     get user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
58     assert_response :success
59     assert_template :confirm
60
61     # Confirm the user
62     post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
63     assert_redirected_to welcome_path
64
65     # Now try to get the confirmation page again
66     get user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
67     assert_redirected_to root_path
68   end
69
70   def test_confirm_success_no_token_no_referer
71     user = build(:user, :pending)
72     stub_gravatar_request(user.email)
73     post user_new_path, :params => { :user => user.attributes }
74     post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
75     confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
76
77     post logout_path
78
79     post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
80     assert_redirected_to login_path
81     assert_match(/Confirmed your account/, flash[:notice])
82   end
83
84   def test_confirm_success_good_token_no_referer
85     user = build(:user, :pending)
86     stub_gravatar_request(user.email)
87     post user_new_path, :params => { :user => user.attributes }
88     post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
89     confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
90
91     post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
92     assert_redirected_to welcome_path
93   end
94
95   def test_confirm_success_bad_token_no_referer
96     user = build(:user, :pending)
97     stub_gravatar_request(user.email)
98     post user_new_path, :params => { :user => user.attributes }
99     post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
100     confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
101
102     post logout_path
103     session_for(create(:user))
104
105     post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
106     assert_redirected_to login_path
107     assert_match(/Confirmed your account/, flash[:notice])
108   end
109
110   def test_confirm_success_no_token_with_referer
111     user = build(:user, :pending)
112     stub_gravatar_request(user.email)
113     post user_new_path, :params => { :user => user.attributes }
114     post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
115     confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
116
117     post logout_path
118
119     post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string, :referer => new_diary_entry_path }
120     assert_redirected_to login_path(:referer => new_diary_entry_path)
121     assert_match(/Confirmed your account/, flash[:notice])
122   end
123
124   def test_confirm_success_good_token_with_referer
125     user = build(:user, :pending)
126     stub_gravatar_request(user.email)
127     post user_new_path, :params => { :user => user.attributes }
128     post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
129     confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
130
131     post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string, :referer => new_diary_entry_path }
132     assert_redirected_to new_diary_entry_path
133   end
134
135   def test_confirm_success_bad_token_with_referer
136     user = build(:user, :pending)
137     stub_gravatar_request(user.email)
138     post user_new_path, :params => { :user => user.attributes }
139     post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
140     confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
141
142     post logout_path
143     session_for(create(:user))
144
145     post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string, :referer => new_diary_entry_path }
146     assert_redirected_to login_path(:referer => new_diary_entry_path)
147     assert_match(/Confirmed your account/, flash[:notice])
148   end
149
150   def test_confirm_expired_token
151     user = build(:user, :pending)
152     stub_gravatar_request(user.email)
153     post user_new_path, :params => { :user => user.attributes }
154     post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
155     confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
156
157     travel 2.weeks do
158       post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
159     end
160     assert_redirected_to :action => "confirm"
161     assert_match(/confirmation code has expired/, flash[:error])
162   end
163
164   def test_confirm_already_confirmed
165     user = build(:user, :pending)
166     stub_gravatar_request(user.email)
167     post user_new_path, :params => { :user => user.attributes }
168     post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
169     confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
170
171     post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string, :referer => new_diary_entry_path }
172     assert_redirected_to new_diary_entry_path
173
174     post logout_path
175
176     confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
177     post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string, :referer => new_diary_entry_path }
178     assert_redirected_to login_path
179     assert_match(/already been confirmed/, flash[:error])
180   end
181
182   def test_confirm_deleted
183     user = build(:user, :pending)
184     stub_gravatar_request(user.email)
185     post user_new_path, :params => { :user => user.attributes }
186     post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
187     confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
188
189     User.find_by(:display_name => user.display_name).hide!
190
191     # Get the confirmation page
192     get user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
193     assert_redirected_to root_path
194
195     # Confirm the user
196     post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
197     assert_response :not_found
198     assert_template :no_such_user
199   end
200
201   def test_confirm_resend_success
202     user = build(:user, :pending)
203     post user_new_path, :params => { :user => user.attributes }
204     post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
205
206     assert_difference "ActionMailer::Base.deliveries.size", 1 do
207       perform_enqueued_jobs do
208         get user_confirm_resend_path(user)
209       end
210     end
211
212     assert_redirected_to login_path
213     assert_equal("confirmations/resend_success_flash", flash[:notice][:partial])
214     assert_equal({ :email => user.email, :sender => Settings.email_from }, flash[:notice][:locals])
215
216     email = ActionMailer::Base.deliveries.last
217
218     assert_equal user.email, email.to.first
219
220     ActionMailer::Base.deliveries.clear
221   end
222
223   def test_confirm_resend_no_token
224     user = build(:user, :pending)
225     # only complete first half of registration
226     post user_new_path, :params => { :user => user.attributes }
227
228     assert_no_difference "ActionMailer::Base.deliveries.size" do
229       perform_enqueued_jobs do
230         get user_confirm_resend_path(user)
231       end
232     end
233
234     assert_redirected_to login_path
235     assert_match "User #{user.display_name} not found.", flash[:error]
236   end
237
238   def test_confirm_resend_deleted
239     user = build(:user, :pending)
240     post user_new_path, :params => { :user => user.attributes }
241     post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
242
243     User.find_by(:display_name => user.display_name).hide!
244
245     assert_no_difference "ActionMailer::Base.deliveries.size" do
246       perform_enqueued_jobs do
247         get user_confirm_resend_path(user)
248       end
249     end
250
251     assert_redirected_to login_path
252     assert_match "User #{user.display_name} not found.", flash[:error]
253   end
254
255   def test_confirm_resend_unknown_user
256     assert_no_difference "ActionMailer::Base.deliveries.size" do
257       perform_enqueued_jobs do
258         get user_confirm_resend_path(:display_name => "No Such User")
259       end
260     end
261
262     assert_redirected_to login_path
263     assert_match "User No Such User not found.", flash[:error]
264   end
265
266   def test_confirm_email_get
267     user = create(:user)
268     confirm_string = user.generate_token_for(:new_email)
269
270     get user_confirm_email_path, :params => { :confirm_string => confirm_string }
271     assert_response :success
272     assert_template :confirm_email
273   end
274
275   def test_confirm_email_success
276     user = create(:user, :new_email => "test-new@example.com")
277     stub_gravatar_request(user.new_email)
278     confirm_string = user.generate_token_for(:new_email)
279
280     post user_confirm_email_path, :params => { :confirm_string => confirm_string }
281     assert_redirected_to edit_account_path
282     assert_match(/Confirmed your change of email address/, flash[:notice])
283   end
284
285   def test_confirm_email_already_confirmed
286     user = create(:user)
287     confirm_string = user.generate_token_for(:new_email)
288
289     post user_confirm_email_path, :params => { :confirm_string => confirm_string }
290     assert_redirected_to edit_account_path
291     assert_match(/already been confirmed/, flash[:error])
292   end
293
294   def test_confirm_email_bad_token
295     post user_confirm_email_path, :params => { :confirm_string => "XXXXX" }
296     assert_redirected_to edit_account_path
297     assert_match(/confirmation code has expired or does not exist/, flash[:error])
298   end
299
300   ##
301   # test if testing for a gravatar works
302   # this happens when the email is actually changed
303   # which is triggered by the confirmation mail
304   def test_gravatar_auto_enable
305     # switch to email that has a gravatar
306     user = create(:user, :new_email => "test-new@example.com")
307     stub_gravatar_request(user.new_email, 200)
308     confirm_string = user.generate_token_for(:new_email)
309     # precondition gravatar should be turned off
310     assert_not user.image_use_gravatar
311     post user_confirm_email_path, :params => { :confirm_string => confirm_string }
312     assert_redirected_to edit_account_path
313     assert_match(/Confirmed your change of email address/, flash[:notice])
314     # gravatar use should now be enabled
315     assert User.find(user.id).image_use_gravatar
316   end
317
318   def test_gravatar_auto_disable
319     # switch to email without a gravatar
320     user = create(:user, :new_email => "test-new@example.com", :image_use_gravatar => true)
321     stub_gravatar_request(user.new_email, 404)
322     confirm_string = user.generate_token_for(:new_email)
323     # precondition gravatar should be turned on
324     assert user.image_use_gravatar
325     post user_confirm_email_path, :params => { :confirm_string => confirm_string }
326     assert_redirected_to edit_account_path
327     assert_match(/Confirmed your change of email address/, flash[:notice])
328     # gravatar use should now be disabled
329     assert_not User.find(user.id).image_use_gravatar
330   end
331 end