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 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
43 get user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
44 assert_response :success
45 assert_template :confirm
48 def test_confirm_get_already_confirmed
49 user = build(:user, :pending)
50 stub_gravatar_request(user.email)
51 post user_new_path, :params => { :user => user.attributes }
52 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
54 # Get the confirmation page
55 get user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
56 assert_response :success
57 assert_template :confirm
60 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
61 assert_redirected_to welcome_path
63 # Now try to get the confirmation page again
64 get user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
65 assert_redirected_to root_path
68 def test_confirm_success_no_token_no_referer
69 user = build(:user, :pending)
70 stub_gravatar_request(user.email)
71 post user_new_path, :params => { :user => user.attributes }
72 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
76 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
77 assert_redirected_to login_path
78 assert_match(/Confirmed your account/, flash[:notice])
81 def test_confirm_success_good_token_no_referer
82 user = build(:user, :pending)
83 stub_gravatar_request(user.email)
84 post user_new_path, :params => { :user => user.attributes }
85 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
87 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
88 assert_redirected_to welcome_path
91 def test_confirm_success_bad_token_no_referer
92 user = build(:user, :pending)
93 stub_gravatar_request(user.email)
94 post user_new_path, :params => { :user => user.attributes }
95 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
98 session_for(create(:user))
100 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
101 assert_redirected_to login_path
102 assert_match(/Confirmed your account/, flash[:notice])
105 def test_confirm_success_no_token_with_referer
106 user = build(:user, :pending)
107 stub_gravatar_request(user.email)
108 post user_new_path, :params => { :user => user.attributes }
109 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
113 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string, :referer => new_diary_entry_path }
114 assert_redirected_to login_path(:referer => new_diary_entry_path)
115 assert_match(/Confirmed your account/, flash[:notice])
118 def test_confirm_success_good_token_with_referer
119 user = build(:user, :pending)
120 stub_gravatar_request(user.email)
121 post user_new_path, :params => { :user => user.attributes }
122 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
124 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string, :referer => new_diary_entry_path }
125 assert_redirected_to new_diary_entry_path
128 def test_confirm_success_bad_token_with_referer
129 user = build(:user, :pending)
130 stub_gravatar_request(user.email)
131 post user_new_path, :params => { :user => user.attributes }
132 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
135 session_for(create(:user))
137 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string, :referer => new_diary_entry_path }
138 assert_redirected_to login_path(:referer => new_diary_entry_path)
139 assert_match(/Confirmed your account/, flash[:notice])
142 def test_confirm_expired_token
143 user = build(:user, :pending)
144 stub_gravatar_request(user.email)
145 post user_new_path, :params => { :user => user.attributes }
146 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
149 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
151 assert_redirected_to :action => "confirm"
152 assert_match(/confirmation code has expired/, flash[:error])
155 def test_confirm_already_confirmed
156 user = build(:user, :pending)
157 stub_gravatar_request(user.email)
158 post user_new_path, :params => { :user => user.attributes }
159 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
161 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string, :referer => new_diary_entry_path }
162 assert_redirected_to new_diary_entry_path
166 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
167 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string, :referer => new_diary_entry_path }
168 assert_redirected_to login_path
169 assert_match(/already been confirmed/, flash[:error])
172 def test_confirm_deleted
173 user = build(:user, :pending)
174 stub_gravatar_request(user.email)
175 post user_new_path, :params => { :user => user.attributes }
176 confirm_string = User.find_by(:email => user.email).generate_token_for(:new_user)
178 User.find_by(:display_name => user.display_name).hide!
180 # Get the confirmation page
181 get user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
182 assert_redirected_to root_path
185 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
186 assert_response :not_found
187 assert_template :no_such_user
190 def test_confirm_resend_success
191 user = build(:user, :pending)
192 post user_new_path, :params => { :user => user.attributes }
194 assert_difference "ActionMailer::Base.deliveries.size", 1 do
195 perform_enqueued_jobs do
196 get user_confirm_resend_path(user)
200 assert_redirected_to login_path
201 assert_equal("confirmations/resend_success_flash", flash[:notice][:partial])
202 assert_equal({ :email => user.email, :sender => Settings.email_from }, flash[:notice][:locals])
204 email = ActionMailer::Base.deliveries.last
206 assert_equal user.email, email.to.first
208 ActionMailer::Base.deliveries.clear
211 def test_confirm_resend_deleted
212 user = build(:user, :pending)
213 post user_new_path, :params => { :user => user.attributes }
215 User.find_by(:display_name => user.display_name).hide!
217 assert_no_difference "ActionMailer::Base.deliveries.size" do
218 perform_enqueued_jobs do
219 get user_confirm_resend_path(user)
223 assert_redirected_to login_path
224 assert_match "User #{user.display_name} not found.", flash[:error]
227 def test_confirm_resend_unknown_user
228 assert_no_difference "ActionMailer::Base.deliveries.size" do
229 perform_enqueued_jobs do
230 get user_confirm_resend_path(:display_name => "No Such User")
234 assert_redirected_to login_path
235 assert_match "User No Such User not found.", flash[:error]
238 def test_confirm_email_get
240 confirm_string = user.generate_token_for(:new_email)
242 get user_confirm_email_path, :params => { :confirm_string => confirm_string }
243 assert_response :success
244 assert_template :confirm_email
247 def test_confirm_email_success
248 user = create(:user, :new_email => "test-new@example.com")
249 stub_gravatar_request(user.new_email)
250 confirm_string = user.generate_token_for(:new_email)
252 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
253 assert_redirected_to edit_account_path
254 assert_match(/Confirmed your change of email address/, flash[:notice])
257 def test_confirm_email_already_confirmed
259 confirm_string = user.generate_token_for(:new_email)
261 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
262 assert_redirected_to edit_account_path
263 assert_match(/already been confirmed/, flash[:error])
266 def test_confirm_email_bad_token
267 post user_confirm_email_path, :params => { :confirm_string => "XXXXX" }
268 assert_redirected_to edit_account_path
269 assert_match(/confirmation code has expired or does not exist/, flash[:error])
273 # test if testing for a gravatar works
274 # this happens when the email is actually changed
275 # which is triggered by the confirmation mail
276 def test_gravatar_auto_enable
277 # switch to email that has a gravatar
278 user = create(:user, :new_email => "test-new@example.com")
279 stub_gravatar_request(user.new_email, 200)
280 confirm_string = user.generate_token_for(:new_email)
281 # precondition gravatar should be turned off
282 assert_not user.image_use_gravatar
283 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
284 assert_redirected_to edit_account_path
285 assert_match(/Confirmed your change of email address/, flash[:notice])
286 # gravatar use should now be enabled
287 assert User.find(user.id).image_use_gravatar
290 def test_gravatar_auto_disable
291 # switch to email without a gravatar
292 user = create(:user, :new_email => "test-new@example.com", :image_use_gravatar => true)
293 stub_gravatar_request(user.new_email, 404)
294 confirm_string = user.generate_token_for(:new_email)
295 # precondition gravatar should be turned on
296 assert user.image_use_gravatar
297 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
298 assert_redirected_to edit_account_path
299 assert_match(/Confirmed your change of email address/, flash[:notice])
300 # gravatar use should now be disabled
301 assert_not User.find(user.id).image_use_gravatar