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).tokens.create.token
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).tokens.create.token
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).tokens.create.token
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).tokens.create.token
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).tokens.create.token
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).tokens.create(:referer => new_diary_entry_path).token
120 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
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).tokens.create(:referer => new_diary_entry_path).token
132 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
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).tokens.create(:referer => new_diary_entry_path).token
144 session_for(create(:user))
146 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
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).tokens.create(:expiry => 1.day.ago).token
158 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
159 assert_redirected_to :action => "confirm"
160 assert_match(/confirmation code has expired/, flash[:error])
163 def test_confirm_already_confirmed
164 user = build(:user, :pending)
165 stub_gravatar_request(user.email)
166 post user_new_path, :params => { :user => user.attributes }
167 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
168 confirm_string = User.find_by(:email => user.email).tokens.create(:referer => new_diary_entry_path).token
170 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
171 assert_redirected_to new_diary_entry_path
175 confirm_string = User.find_by(:email => user.email).tokens.create(:referer => new_diary_entry_path).token
176 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
177 assert_redirected_to login_path
178 assert_match(/already been confirmed/, flash[:error])
181 def test_confirm_deleted
182 user = build(:user, :pending)
183 stub_gravatar_request(user.email)
184 post user_new_path, :params => { :user => user.attributes }
185 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
186 confirm_string = User.find_by(:email => user.email).tokens.create.token
188 User.find_by(:display_name => user.display_name).update(:status => "deleted")
190 # Get the confirmation page
191 get user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
192 assert_response :redirect
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_response :redirect
213 assert_redirected_to login_path
214 assert_equal("confirmations/resend_success_flash", flash[:notice][:partial])
215 assert_equal({ :email => user.email, :sender => Settings.email_from }, flash[:notice][:locals])
217 email = ActionMailer::Base.deliveries.last
219 assert_equal user.email, email.to.first
221 ActionMailer::Base.deliveries.clear
224 def test_confirm_resend_no_token
225 user = build(:user, :pending)
226 # only complete first half of registration
227 post user_new_path, :params => { :user => user.attributes }
229 assert_no_difference "ActionMailer::Base.deliveries.size" do
230 perform_enqueued_jobs do
231 get user_confirm_resend_path(user)
235 assert_response :redirect
236 assert_redirected_to login_path
237 assert_match "User #{user.display_name} not found.", flash[:error]
240 def test_confirm_resend_deleted
241 user = build(:user, :pending)
242 post user_new_path, :params => { :user => user.attributes }
243 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
245 User.find_by(:display_name => user.display_name).update(:status => "deleted")
247 assert_no_difference "ActionMailer::Base.deliveries.size" do
248 perform_enqueued_jobs do
249 get user_confirm_resend_path(user)
253 assert_response :redirect
254 assert_redirected_to login_path
255 assert_match "User #{user.display_name} not found.", flash[:error]
258 def test_confirm_resend_unknown_user
259 assert_no_difference "ActionMailer::Base.deliveries.size" do
260 perform_enqueued_jobs do
261 get user_confirm_resend_path(:display_name => "No Such User")
265 assert_response :redirect
266 assert_redirected_to login_path
267 assert_match "User No Such User not found.", flash[:error]
270 def test_confirm_email_get
272 confirm_string = user.tokens.create.token
274 get user_confirm_email_path, :params => { :confirm_string => confirm_string }
275 assert_response :success
276 assert_template :confirm_email
279 def test_confirm_email_success
280 user = create(:user, :new_email => "test-new@example.com")
281 stub_gravatar_request(user.new_email)
282 confirm_string = user.tokens.create.token
284 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
285 assert_response :redirect
286 assert_redirected_to edit_account_path
287 assert_match(/Confirmed your change of email address/, flash[:notice])
290 def test_confirm_email_already_confirmed
292 confirm_string = user.tokens.create.token
294 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
295 assert_response :redirect
296 assert_redirected_to edit_account_path
297 assert_match(/already been confirmed/, flash[:error])
300 def test_confirm_email_bad_token
301 post user_confirm_email_path, :params => { :confirm_string => "XXXXX" }
302 assert_response :success
303 assert_template :confirm_email
304 assert_match(/confirmation code has expired or does not exist/, flash[:error])
308 # test if testing for a gravatar works
309 # this happens when the email is actually changed
310 # which is triggered by the confirmation mail
311 def test_gravatar_auto_enable
312 # switch to email that has a gravatar
313 user = create(:user, :new_email => "test-new@example.com")
314 stub_gravatar_request(user.new_email, 200)
315 confirm_string = user.tokens.create.token
316 # precondition gravatar should be turned off
317 assert_not user.image_use_gravatar
318 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
319 assert_response :redirect
320 assert_redirected_to edit_account_path
321 assert_match(/Confirmed your change of email address/, flash[:notice])
322 # gravatar use should now be enabled
323 assert User.find(user.id).image_use_gravatar
326 def test_gravatar_auto_disable
327 # switch to email without a gravatar
328 user = create(:user, :new_email => "test-new@example.com", :image_use_gravatar => true)
329 stub_gravatar_request(user.new_email, 404)
330 confirm_string = user.tokens.create.token
331 # precondition gravatar should be turned on
332 assert user.image_use_gravatar
333 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
334 assert_response :redirect
335 assert_redirected_to edit_account_path
336 assert_match(/Confirmed your change of email address/, flash[:notice])
337 # gravatar use should now be disabled
338 assert_not User.find(user.id).image_use_gravatar