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_redirected_to root_path
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)
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])
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)
91 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
92 assert_redirected_to welcome_path
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)
103 session_for(create(:user))
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])
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)
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])
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)
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
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)
143 session_for(create(:user))
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])
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)
158 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
160 assert_redirected_to :action => "confirm"
161 assert_match(/confirmation code has expired/, flash[:error])
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)
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
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])
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)
189 User.find_by(:display_name => user.display_name).hide!
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
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
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 }
206 assert_difference "ActionMailer::Base.deliveries.size", 1 do
207 perform_enqueued_jobs do
208 get user_confirm_resend_path(user)
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])
216 email = ActionMailer::Base.deliveries.last
218 assert_equal user.email, email.to.first
220 ActionMailer::Base.deliveries.clear
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 }
228 assert_no_difference "ActionMailer::Base.deliveries.size" do
229 perform_enqueued_jobs do
230 get user_confirm_resend_path(user)
234 assert_redirected_to login_path
235 assert_match "User #{user.display_name} not found.", flash[:error]
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 }
243 User.find_by(:display_name => user.display_name).hide!
245 assert_no_difference "ActionMailer::Base.deliveries.size" do
246 perform_enqueued_jobs do
247 get user_confirm_resend_path(user)
251 assert_redirected_to login_path
252 assert_match "User #{user.display_name} not found.", flash[:error]
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")
262 assert_redirected_to login_path
263 assert_match "User No Such User not found.", flash[:error]
266 def test_confirm_email_get
268 confirm_string = user.generate_token_for(:new_email)
270 get user_confirm_email_path, :params => { :confirm_string => confirm_string }
271 assert_response :success
272 assert_template :confirm_email
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)
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])
285 def test_confirm_email_already_confirmed
287 confirm_string = user.generate_token_for(:new_email)
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])
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])
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
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