2010-03-26 06:27:19 -04:00
require 'test_helper'
2009-10-12 07:37:42 -04:00
2009-10-17 10:12:50 -04:00
class ConfirmationTest < ActionController :: IntegrationTest
2009-10-12 07:37:42 -04:00
2009-10-18 08:36:20 -04:00
def visit_user_confirmation_with_token ( confirmation_token )
visit user_confirmation_path ( :confirmation_token = > confirmation_token )
end
2011-11-05 11:53:27 -04:00
def resend_confirmation
2009-10-15 15:54:04 -04:00
user = create_user ( :confirm = > false )
2009-10-12 07:37:42 -04:00
ActionMailer :: Base . deliveries . clear
2009-10-12 08:56:12 -04:00
visit new_user_session_path
2010-03-30 14:06:56 -04:00
click_link " Didn't receive confirmation instructions? "
2009-10-12 07:37:42 -04:00
2009-10-12 08:56:12 -04:00
fill_in 'email' , :with = > user . email
2009-10-12 07:37:42 -04:00
click_button 'Resend confirmation instructions'
2011-11-05 11:53:27 -04:00
end
test 'user should be able to request a new confirmation' do
resend_confirmation
2009-10-12 07:37:42 -04:00
2010-07-06 10:00:07 -04:00
assert_current_url '/users/sign_in'
2009-10-12 07:37:42 -04:00
assert_contain 'You will receive an email with instructions about how to confirm your account in a few minutes'
assert_equal 1 , ActionMailer :: Base . deliveries . size
2011-11-05 11:53:27 -04:00
assert_equal [ 'please-change-me@config-initializers-devise.com' ] , ActionMailer :: Base . deliveries . first . from
end
test 'user should receive a confirmation from a custom mailer' do
User . any_instance . stubs ( :devise_mailer ) . returns ( Users :: Mailer )
resend_confirmation
assert_equal [ 'custom@example.com' ] , ActionMailer :: Base . deliveries . first . from
2009-10-12 07:37:42 -04:00
end
2009-10-18 07:14:52 -04:00
test 'user with invalid confirmation token should not be able to confirm an account' do
2009-10-18 08:36:20 -04:00
visit_user_confirmation_with_token ( 'invalid_confirmation' )
2010-04-13 17:28:13 -04:00
assert_have_selector '#error_explanation'
2009-12-21 15:10:23 -05:00
assert_contain / Confirmation token(.*)invalid /
2009-10-12 07:37:42 -04:00
end
2009-10-18 07:14:52 -04:00
test 'user with valid confirmation token should be able to confirm an account' do
2009-10-12 08:56:12 -04:00
user = create_user ( :confirm = > false )
assert_not user . confirmed?
2009-10-18 08:36:20 -04:00
visit_user_confirmation_with_token ( user . confirmation_token )
2009-10-12 07:37:42 -04:00
2009-10-18 08:39:22 -04:00
assert_contain 'Your account was successfully confirmed.'
2010-07-06 10:00:07 -04:00
assert_current_url '/'
2009-10-12 08:56:12 -04:00
assert user . reload . confirmed?
2009-10-12 07:37:42 -04:00
end
2011-07-08 05:21:01 -04:00
test 'user should be redirected to a custom path after confirmation' do
Devise :: ConfirmationsController . any_instance . stubs ( :after_confirmation_path_for ) . returns ( " /?custom=1 " )
user = create_user ( :confirm = > false )
visit_user_confirmation_with_token ( user . confirmation_token )
assert_current_url " /?custom=1 "
end
2010-07-06 10:00:07 -04:00
test 'already confirmed user should not be able to confirm the account again' do
2009-11-24 15:02:36 -05:00
user = create_user ( :confirm = > false )
2009-12-21 15:10:23 -05:00
user . confirmed_at = Time . now
user . save
2009-10-18 08:36:20 -04:00
visit_user_confirmation_with_token ( user . confirmation_token )
2009-10-12 07:37:42 -04:00
2010-04-13 17:28:13 -04:00
assert_have_selector '#error_explanation'
2009-10-12 07:37:42 -04:00
assert_contain 'already confirmed'
end
2009-10-18 08:36:20 -04:00
2010-07-06 10:00:07 -04:00
test 'already confirmed user should not be able to confirm the account again neither request confirmation' do
user = create_user ( :confirm = > false )
user . confirmed_at = Time . now
user . save
visit_user_confirmation_with_token ( user . confirmation_token )
assert_contain 'already confirmed'
fill_in 'email' , :with = > user . email
click_button 'Resend confirmation instructions'
assert_contain 'already confirmed'
end
2011-07-29 17:17:31 -04:00
test 'sign in user automatically after confirming its email' do
2009-10-18 08:36:20 -04:00
user = create_user ( :confirm = > false )
visit_user_confirmation_with_token ( user . confirmation_token )
assert warden . authenticated? ( :user )
end
2009-10-21 09:20:10 -04:00
2010-01-07 16:41:14 -05:00
test 'increases sign count when signed in through confirmation' do
user = create_user ( :confirm = > false )
visit_user_confirmation_with_token ( user . confirmation_token )
user . reload
assert_equal 1 , user . sign_in_count
end
2009-11-22 19:19:29 -05:00
test 'not confirmed user with setup to block without confirmation should not be able to sign in' do
2011-12-11 14:18:02 -05:00
swap Devise , :allow_unconfirmed_access_for = > 0 . days do
2009-11-22 19:33:19 -05:00
sign_in_as_user ( :confirm = > false )
2009-10-21 09:20:10 -04:00
2009-11-22 19:33:19 -05:00
assert_contain 'You have to confirm your account before continuing'
assert_not warden . authenticated? ( :user )
end
2009-10-21 09:20:10 -04:00
end
2011-11-05 17:47:58 -04:00
test 'not confirmed user should not see confirmation message if invalid credentials are given' do
2011-12-11 14:18:02 -05:00
swap Devise , :allow_unconfirmed_access_for = > 0 . days do
2011-11-05 17:47:58 -04:00
sign_in_as_user ( :confirm = > false ) do
fill_in 'password' , :with = > 'invalid'
end
assert_contain 'Invalid email or password'
assert_not warden . authenticated? ( :user )
end
end
2009-10-21 09:20:10 -04:00
test 'not confirmed user but configured with some days to confirm should be able to sign in' do
2011-12-11 14:18:02 -05:00
swap Devise , :allow_unconfirmed_access_for = > 1 . day do
2009-11-22 19:33:19 -05:00
sign_in_as_user ( :confirm = > false )
2009-10-21 09:20:10 -04:00
2009-11-22 19:33:19 -05:00
assert_response :success
assert warden . authenticated? ( :user )
end
2009-10-21 09:20:10 -04:00
end
test 'error message is configurable by resource name' do
2009-11-22 19:46:57 -05:00
store_translations :en , :devise = > {
2010-04-03 07:11:45 -04:00
:failure = > { :user = > { :unconfirmed = > " Not confirmed user " } }
2009-11-22 19:46:57 -05:00
} do
2010-04-03 05:43:31 -04:00
sign_in_as_user ( :confirm = > false )
2009-10-21 09:20:10 -04:00
assert_contain 'Not confirmed user'
end
end
2011-01-15 15:13:14 -05:00
test 'resent confirmation token with valid E-Mail in XML format should return valid response' do
user = create_user ( :confirm = > false )
post user_confirmation_path ( :format = > 'xml' ) , :user = > { :email = > user . email }
assert_response :success
2011-06-22 20:26:32 -04:00
assert_equal response . body , { } . to_xml
2011-01-15 15:13:14 -05:00
end
test 'resent confirmation token with invalid E-Mail in XML format should return invalid response' do
user = create_user ( :confirm = > false )
post user_confirmation_path ( :format = > 'xml' ) , :user = > { :email = > 'invalid.test@test.com' }
assert_response :unprocessable_entity
assert response . body . include? %( <?xml version= \" 1.0 \" encoding= \" UTF-8 \" ?> \n <errors> )
end
test 'confirm account with valid confirmation token in XML format should return valid response' do
user = create_user ( :confirm = > false )
get user_confirmation_path ( :confirmation_token = > user . confirmation_token , :format = > 'xml' )
assert_response :success
assert response . body . include? %( <?xml version= \" 1.0 \" encoding= \" UTF-8 \" ?> \n <user> )
end
test 'confirm account with invalid confirmation token in XML format should return invalid response' do
user = create_user ( :confirm = > false )
get user_confirmation_path ( :confirmation_token = > 'invalid_confirmation' , :format = > 'xml' )
assert_response :unprocessable_entity
assert response . body . include? %( <?xml version= \" 1.0 \" encoding= \" UTF-8 \" ?> \n <errors> )
end
2011-06-20 21:29:56 -04:00
2011-06-22 20:26:32 -04:00
test 'request an account confirmation account with JSON, should return an empty JSON' do
user = create_user ( :confirm = > false )
post user_confirmation_path , :user = > { :email = > user . email } , :format = > :json
assert_response :success
assert_equal response . body , { } . to_json
end
2011-06-20 21:29:56 -04:00
test " when in paranoid mode and with a valid e-mail, should not say that the e-mail is valid " do
swap Devise , :paranoid = > true do
user = create_user ( :confirm = > false )
visit new_user_session_path
click_link " Didn't receive confirmation instructions? "
fill_in 'email' , :with = > user . email
click_button 'Resend confirmation instructions'
2012-05-14 16:53:34 -04:00
assert_contain " If your email address exists in our database, you will receive an email with instructions about how to confirm your account in a few minutes. "
2011-10-15 04:51:40 -04:00
assert_current_url " /users/sign_in "
2011-06-20 21:29:56 -04:00
end
end
test " when in paranoid mode and with a invalid e-mail, should not say that the e-mail is invalid " do
swap Devise , :paranoid = > true do
visit new_user_session_path
click_link " Didn't receive confirmation instructions? "
fill_in 'email' , :with = > " idonthavethisemail@gmail.com "
click_button 'Resend confirmation instructions'
assert_not_contain " 1 error prohibited this user from being saved: "
assert_not_contain " Email not found "
2012-05-14 16:53:34 -04:00
assert_contain " If your email address exists in our database, you will receive an email with instructions about how to confirm your account in a few minutes. "
2011-10-15 04:51:40 -04:00
assert_current_url " /users/sign_in "
2011-06-20 21:29:56 -04:00
end
end
2009-10-12 07:37:42 -04:00
end
2011-04-03 15:26:14 -04:00
2011-12-11 14:07:30 -05:00
class ConfirmationOnChangeTest < ActionController :: IntegrationTest
def create_second_admin ( options = { } )
@admin = nil
create_admin ( options )
2011-04-03 15:26:14 -04:00
end
2011-12-11 14:07:30 -05:00
def visit_admin_confirmation_with_token ( confirmation_token )
visit admin_confirmation_path ( :confirmation_token = > confirmation_token )
2011-04-03 15:26:14 -04:00
end
2011-12-11 14:07:30 -05:00
test 'admin should be able to request a new confirmation after email changed' do
admin = create_admin
admin . update_attributes ( :email = > 'new_test@example.com' )
2011-04-03 15:26:14 -04:00
2011-12-11 14:07:30 -05:00
visit new_admin_session_path
2011-04-03 15:26:14 -04:00
click_link " Didn't receive confirmation instructions? "
2011-12-11 14:07:30 -05:00
fill_in 'email' , :with = > admin . unconfirmed_email
2011-12-04 16:14:44 -05:00
assert_difference " ActionMailer::Base.deliveries.size " do
click_button 'Resend confirmation instructions'
end
2011-04-03 15:26:14 -04:00
2011-12-11 14:07:30 -05:00
assert_current_url '/admin_area/sign_in'
2011-04-03 15:26:14 -04:00
assert_contain 'You will receive an email with instructions about how to confirm your account in a few minutes'
end
2011-12-11 14:07:30 -05:00
test 'admin with valid confirmation token should be able to confirm email after email changed' do
admin = create_admin
admin . update_attributes ( :email = > 'new_test@example.com' )
assert_equal 'new_test@example.com' , admin . unconfirmed_email
visit_admin_confirmation_with_token ( admin . confirmation_token )
2011-04-03 15:26:14 -04:00
assert_contain 'Your account was successfully confirmed.'
2011-12-11 14:07:30 -05:00
assert_current_url '/admin_area/home'
assert admin . reload . confirmed?
assert_not admin . reload . pending_reconfirmation?
2011-04-03 15:26:14 -04:00
end
2011-12-11 14:07:30 -05:00
test 'admin email should be unique also within unconfirmed_email' do
admin = create_admin
admin . update_attributes ( :email = > 'new_admin_test@example.com' )
assert_equal 'new_admin_test@example.com' , admin . unconfirmed_email
2011-04-03 15:26:14 -04:00
2011-12-11 14:07:30 -05:00
create_second_admin ( :email = > " new_admin_test@example.com " )
2011-04-03 15:26:14 -04:00
2011-12-11 14:07:30 -05:00
visit_admin_confirmation_with_token ( admin . confirmation_token )
2011-04-17 17:37:47 -04:00
assert_have_selector '#error_explanation'
2011-04-03 15:26:14 -04:00
assert_contain / Email.*already.*taken /
2011-12-11 14:07:30 -05:00
assert admin . reload . pending_reconfirmation?
2011-04-03 15:26:14 -04:00
end
end