3 class UsersControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/user/username/confirm", :method => :get },
9 { :controller => "confirmations", :action => "confirm", :display_name => "username" }
12 { :path => "/user/username/confirm", :method => :post },
13 { :controller => "confirmations", :action => "confirm", :display_name => "username" }
16 { :path => "/user/username/confirm/resend", :method => :get },
17 { :controller => "confirmations", :action => "confirm_resend", :display_name => "username" }
21 { :path => "/user/confirm", :method => :get },
22 { :controller => "confirmations", :action => "confirm" }
25 { :path => "/user/confirm", :method => :post },
26 { :controller => "confirmations", :action => "confirm" }
29 { :path => "/user/confirm-email", :method => :get },
30 { :controller => "confirmations", :action => "confirm_email" }
33 { :path => "/user/confirm-email", :method => :post },
34 { :controller => "confirmations", :action => "confirm_email" }
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)
44 get user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
45 assert_response :success
46 assert_template :confirm
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)
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
62 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
63 assert_redirected_to welcome_path
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_response :redirect
68 assert_redirected_to root_path
71 def test_confirm_success_no_token_no_referer
72 user = build(:user, :pending)
73 stub_gravatar_request(user.email)
74 post user_new_path, :params => { :user => user.attributes }
75 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
76 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
80 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
81 assert_redirected_to login_path
82 assert_match(/Confirmed your account/, flash[:notice])
85 def test_confirm_success_good_token_no_referer
86 user = build(:user, :pending)
87 stub_gravatar_request(user.email)
88 post user_new_path, :params => { :user => user.attributes }
89 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
90 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
92 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
93 assert_redirected_to welcome_path
96 def test_confirm_success_bad_token_no_referer
97 user = build(:user, :pending)
98 stub_gravatar_request(user.email)
99 post user_new_path, :params => { :user => user.attributes }
100 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
101 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
104 session_for(create(:user))
106 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
107 assert_redirected_to login_path
108 assert_match(/Confirmed your account/, flash[:notice])
111 def test_confirm_success_no_token_with_referer
112 user = build(:user, :pending)
113 stub_gravatar_request(user.email)
114 post user_new_path, :params => { :user => user.attributes }
115 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
116 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
120 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string, :referer => new_diary_entry_path }
121 assert_redirected_to login_path(:referer => new_diary_entry_path)
122 assert_match(/Confirmed your account/, flash[:notice])
125 def test_confirm_success_good_token_with_referer
126 user = build(:user, :pending)
127 stub_gravatar_request(user.email)
128 post user_new_path, :params => { :user => user.attributes }
129 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
130 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
132 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string, :referer => new_diary_entry_path }
133 assert_redirected_to new_diary_entry_path
136 def test_confirm_success_bad_token_with_referer
137 user = build(:user, :pending)
138 stub_gravatar_request(user.email)
139 post user_new_path, :params => { :user => user.attributes }
140 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
141 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
144 session_for(create(:user))
146 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string, :referer => new_diary_entry_path }
147 assert_redirected_to login_path(:referer => new_diary_entry_path)
148 assert_match(/Confirmed your account/, flash[:notice])
151 def test_confirm_expired_token
152 user = build(:user, :pending)
153 stub_gravatar_request(user.email)
154 post user_new_path, :params => { :user => user.attributes }
155 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
156 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
159 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
161 assert_redirected_to :action => "confirm"
162 assert_match(/confirmation code has expired/, flash[:error])
165 def test_confirm_already_confirmed
166 user = build(:user, :pending)
167 stub_gravatar_request(user.email)
168 post user_new_path, :params => { :user => user.attributes }
169 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
170 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
172 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string, :referer => new_diary_entry_path }
173 assert_redirected_to new_diary_entry_path
177 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
178 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string, :referer => new_diary_entry_path }
179 assert_redirected_to login_path
180 assert_match(/already been confirmed/, flash[:error])
183 def test_confirm_deleted
184 user = build(:user, :pending)
185 stub_gravatar_request(user.email)
186 post user_new_path, :params => { :user => user.attributes }
187 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
188 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
190 User.find_by(:display_name => user.display_name).hide!
192 # Get the confirmation page
193 get user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
194 assert_response :redirect
195 assert_redirected_to root_path
198 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
199 assert_response :not_found
200 assert_template :no_such_user
203 def test_confirm_resend_success
204 user = build(:user, :pending)
205 post user_new_path, :params => { :user => user.attributes }
206 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
208 assert_difference "ActionMailer::Base.deliveries.size", 1 do
209 perform_enqueued_jobs do
210 get user_confirm_resend_path(user)
214 assert_response :redirect
215 assert_redirected_to login_path
216 assert_equal("confirmations/resend_success_flash", flash[:notice][:partial])
217 assert_equal({ :email => user.email, :sender => Settings.email_from }, flash[:notice][:locals])
219 email = ActionMailer::Base.deliveries.last
221 assert_equal user.email, email.to.first
223 ActionMailer::Base.deliveries.clear
226 def test_confirm_resend_no_token
227 user = build(:user, :pending)
228 # only complete first half of registration
229 post user_new_path, :params => { :user => user.attributes }
231 assert_no_difference "ActionMailer::Base.deliveries.size" do
232 perform_enqueued_jobs do
233 get user_confirm_resend_path(user)
237 assert_response :redirect
238 assert_redirected_to login_path
239 assert_match "User #{user.display_name} not found.", flash[:error]
242 def test_confirm_resend_deleted
243 user = build(:user, :pending)
244 post user_new_path, :params => { :user => user.attributes }
245 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
247 User.find_by(:display_name => user.display_name).hide!
249 assert_no_difference "ActionMailer::Base.deliveries.size" do
250 perform_enqueued_jobs do
251 get user_confirm_resend_path(user)
255 assert_response :redirect
256 assert_redirected_to login_path
257 assert_match "User #{user.display_name} not found.", flash[:error]
260 def test_confirm_resend_unknown_user
261 assert_no_difference "ActionMailer::Base.deliveries.size" do
262 perform_enqueued_jobs do
263 get user_confirm_resend_path(:display_name => "No Such User")
267 assert_response :redirect
268 assert_redirected_to login_path
269 assert_match "User No Such User not found.", flash[:error]
272 def test_confirm_email_get
274 confirm_string = user.generate_token_for(:new_email)
276 get user_confirm_email_path, :params => { :confirm_string => confirm_string }
277 assert_response :success
278 assert_template :confirm_email
281 def test_confirm_email_success
282 user = create(:user, :new_email => "test-new@example.com")
283 stub_gravatar_request(user.new_email)
284 confirm_string = user.generate_token_for(:new_email)
286 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
287 assert_response :redirect
288 assert_redirected_to edit_account_path
289 assert_match(/Confirmed your change of email address/, flash[:notice])
292 def test_confirm_email_already_confirmed
294 confirm_string = user.generate_token_for(:new_email)
296 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
297 assert_response :redirect
298 assert_redirected_to edit_account_path
299 assert_match(/already been confirmed/, flash[:error])
302 def test_confirm_email_bad_token
303 post user_confirm_email_path, :params => { :confirm_string => "XXXXX" }
304 assert_response :redirect
305 assert_redirected_to edit_account_path
306 assert_match(/confirmation code has expired or does not exist/, flash[:error])
310 # test if testing for a gravatar works
311 # this happens when the email is actually changed
312 # which is triggered by the confirmation mail
313 def test_gravatar_auto_enable
314 # switch to email that has a gravatar
315 user = create(:user, :new_email => "test-new@example.com")
316 stub_gravatar_request(user.new_email, 200)
317 confirm_string = user.generate_token_for(:new_email)
318 # precondition gravatar should be turned off
319 assert_not user.image_use_gravatar
320 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
321 assert_response :redirect
322 assert_redirected_to edit_account_path
323 assert_match(/Confirmed your change of email address/, flash[:notice])
324 # gravatar use should now be enabled
325 assert User.find(user.id).image_use_gravatar
328 def test_gravatar_auto_disable
329 # switch to email without a gravatar
330 user = create(:user, :new_email => "test-new@example.com", :image_use_gravatar => true)
331 stub_gravatar_request(user.new_email, 404)
332 confirm_string = user.generate_token_for(:new_email)
333 # precondition gravatar should be turned on
334 assert user.image_use_gravatar
335 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
336 assert_response :redirect
337 assert_redirected_to edit_account_path
338 assert_match(/Confirmed your change of email address/, flash[:notice])
339 # gravatar use should now be disabled
340 assert_not User.find(user.id).image_use_gravatar