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_match(/sent a new confirmation/, flash[:notice])
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_response :redirect
235 assert_redirected_to login_path
236 assert_match "User #{user.display_name} not found.", flash[:error]
239 def test_confirm_resend_deleted
240 user = build(:user, :pending)
241 post user_new_path, :params => { :user => user.attributes }
242 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
244 User.find_by(:display_name => user.display_name).update(:status => "deleted")
246 assert_no_difference "ActionMailer::Base.deliveries.size" do
247 perform_enqueued_jobs do
248 get user_confirm_resend_path(user)
252 assert_response :redirect
253 assert_redirected_to login_path
254 assert_match "User #{user.display_name} not found.", flash[:error]
257 def test_confirm_resend_unknown_user
258 assert_no_difference "ActionMailer::Base.deliveries.size" do
259 perform_enqueued_jobs do
260 get user_confirm_resend_path(:display_name => "No Such User")
264 assert_response :redirect
265 assert_redirected_to login_path
266 assert_match "User No Such User not found.", flash[:error]
269 def test_confirm_email_get
271 confirm_string = user.tokens.create.token
273 get user_confirm_email_path, :params => { :confirm_string => confirm_string }
274 assert_response :success
275 assert_template :confirm_email
278 def test_confirm_email_success
279 user = create(:user, :new_email => "test-new@example.com")
280 stub_gravatar_request(user.new_email)
281 confirm_string = user.tokens.create.token
283 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
284 assert_response :redirect
285 assert_redirected_to :controller => :users, :action => :account, :display_name => user.display_name
286 assert_match(/Confirmed your change of email address/, flash[:notice])
289 def test_confirm_email_already_confirmed
291 confirm_string = user.tokens.create.token
293 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
294 assert_response :redirect
295 assert_redirected_to :controller => :users, :action => :account, :display_name => user.display_name
296 assert_match(/already been confirmed/, flash[:error])
299 def test_confirm_email_bad_token
300 post user_confirm_email_path, :params => { :confirm_string => "XXXXX" }
301 assert_response :success
302 assert_template :confirm_email
303 assert_match(/confirmation code has expired or does not exist/, flash[:error])
307 # test if testing for a gravatar works
308 # this happens when the email is actually changed
309 # which is triggered by the confirmation mail
310 def test_gravatar_auto_enable
311 # switch to email that has a gravatar
312 user = create(:user, :new_email => "test-new@example.com")
313 stub_gravatar_request(user.new_email, 200)
314 confirm_string = user.tokens.create.token
315 # precondition gravatar should be turned off
316 assert_not user.image_use_gravatar
317 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
318 assert_response :redirect
319 assert_redirected_to :controller => :users, :action => :account, :display_name => user.display_name
320 assert_match(/Confirmed your change of email address/, flash[:notice])
321 # gravatar use should now be enabled
322 assert User.find(user.id).image_use_gravatar
325 def test_gravatar_auto_disable
326 # switch to email without a gravatar
327 user = create(:user, :new_email => "test-new@example.com", :image_use_gravatar => true)
328 stub_gravatar_request(user.new_email, 404)
329 confirm_string = user.tokens.create.token
330 # precondition gravatar should be turned on
331 assert user.image_use_gravatar
332 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
333 assert_response :redirect
334 assert_redirected_to :controller => :users, :action => :account, :display_name => user.display_name
335 assert_match(/Confirmed your change of email address/, flash[:notice])
336 # gravatar use should now be disabled
337 assert_not User.find(user.id).image_use_gravatar