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 PasswordTest < ActionController::IntegrationTest
|
2009-10-12 07:37:42 -04:00
|
|
|
|
|
|
|
def visit_new_password_path
|
2009-10-12 08:56:12 -04:00
|
|
|
visit new_user_session_path
|
2010-03-30 14:06:56 -04:00
|
|
|
click_link 'Forgot your password?'
|
2009-10-12 07:37:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def request_forgot_password(&block)
|
|
|
|
visit_new_password_path
|
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert_template 'passwords/new'
|
2009-10-12 08:56:12 -04:00
|
|
|
assert_not warden.authenticated?(:user)
|
2009-10-12 07:37:42 -04:00
|
|
|
|
2009-10-12 08:56:12 -04:00
|
|
|
fill_in 'email', :with => 'user@test.com'
|
2009-10-12 07:37:42 -04:00
|
|
|
yield if block_given?
|
|
|
|
click_button 'Send me reset password instructions'
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_password(options={}, &block)
|
2009-10-18 07:54:53 -04:00
|
|
|
unless options[:visit] == false
|
|
|
|
visit edit_user_password_path(:reset_password_token => options[:reset_password_token])
|
|
|
|
end
|
2009-10-12 07:37:42 -04:00
|
|
|
assert_response :success
|
|
|
|
assert_template 'passwords/edit'
|
|
|
|
|
|
|
|
fill_in 'Password', :with => '987654321'
|
|
|
|
fill_in 'Password confirmation', :with => '987654321'
|
|
|
|
yield if block_given?
|
|
|
|
click_button 'Change my password'
|
|
|
|
end
|
|
|
|
|
2009-10-12 08:56:12 -04:00
|
|
|
test 'authenticated user should not be able to visit forgot password page' do
|
|
|
|
sign_in_as_user
|
|
|
|
assert warden.authenticated?(:user)
|
2009-10-12 07:37:42 -04:00
|
|
|
|
2009-10-12 08:56:12 -04:00
|
|
|
get new_user_password_path
|
2009-10-12 07:37:42 -04:00
|
|
|
|
|
|
|
assert_response :redirect
|
|
|
|
assert_redirected_to root_path
|
|
|
|
end
|
|
|
|
|
2009-10-12 08:56:12 -04:00
|
|
|
test 'not authenticated user should be able to request a forgot password' do
|
|
|
|
create_user
|
2009-10-12 07:37:42 -04:00
|
|
|
request_forgot_password
|
|
|
|
|
|
|
|
assert_template 'sessions/new'
|
|
|
|
assert_contain 'You will receive an email with instructions about how to reset your password in a few minutes.'
|
|
|
|
end
|
|
|
|
|
2009-10-12 08:56:12 -04:00
|
|
|
test 'not authenticated user with invalid email should receive an error message' do
|
2009-10-12 07:37:42 -04:00
|
|
|
request_forgot_password do
|
|
|
|
fill_in 'email', :with => 'invalid.test@test.com'
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert_template 'passwords/new'
|
|
|
|
assert_have_selector 'input[type=text][value=\'invalid.test@test.com\']'
|
|
|
|
assert_contain 'Email not found'
|
|
|
|
end
|
|
|
|
|
2009-10-12 08:56:12 -04:00
|
|
|
test 'authenticated user should not be able to visit edit password page' do
|
|
|
|
sign_in_as_user
|
2009-10-12 07:37:42 -04:00
|
|
|
|
2009-10-12 08:56:12 -04:00
|
|
|
get edit_user_password_path
|
2009-10-12 07:37:42 -04:00
|
|
|
|
|
|
|
assert_response :redirect
|
|
|
|
assert_redirected_to root_path
|
2009-10-12 08:56:12 -04:00
|
|
|
assert warden.authenticated?(:user)
|
2009-10-12 07:37:42 -04:00
|
|
|
end
|
|
|
|
|
2009-10-18 07:14:52 -04:00
|
|
|
test 'not authenticated user with invalid reset password token should not be able to change his password' do
|
2009-10-12 08:56:12 -04:00
|
|
|
user = create_user
|
2009-10-18 07:14:52 -04:00
|
|
|
reset_password :reset_password_token => 'invalid_reset_password'
|
2009-10-12 07:37:42 -04:00
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert_template 'passwords/edit'
|
2010-04-13 17:28:13 -04:00
|
|
|
assert_have_selector '#error_explanation'
|
2009-12-21 15:10:23 -05:00
|
|
|
assert_contain /Reset password token(.*)invalid/
|
2009-10-12 08:56:12 -04:00
|
|
|
assert_not user.reload.valid_password?('987654321')
|
2009-10-12 07:37:42 -04:00
|
|
|
end
|
|
|
|
|
2009-10-18 07:14:52 -04:00
|
|
|
test 'not authenticated user with valid reset password token but invalid password should not be able to change his password' do
|
2009-10-12 08:56:12 -04:00
|
|
|
user = create_user
|
2009-10-15 16:36:44 -04:00
|
|
|
request_forgot_password
|
2009-10-18 07:14:52 -04:00
|
|
|
reset_password :reset_password_token => user.reload.reset_password_token do
|
2009-10-12 07:37:42 -04:00
|
|
|
fill_in 'Password confirmation', :with => 'other_password'
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert_template 'passwords/edit'
|
2010-04-13 17:28:13 -04:00
|
|
|
assert_have_selector '#error_explanation'
|
2009-10-12 07:37:42 -04:00
|
|
|
assert_contain 'Password doesn\'t match confirmation'
|
2009-10-12 08:56:12 -04:00
|
|
|
assert_not user.reload.valid_password?('987654321')
|
2009-10-12 07:37:42 -04:00
|
|
|
end
|
|
|
|
|
2009-10-12 08:56:12 -04:00
|
|
|
test 'not authenticated user with valid data should be able to change his password' do
|
|
|
|
user = create_user
|
2009-10-15 16:36:44 -04:00
|
|
|
request_forgot_password
|
2009-10-18 07:14:52 -04:00
|
|
|
reset_password :reset_password_token => user.reload.reset_password_token
|
2009-10-12 07:37:42 -04:00
|
|
|
|
2009-10-18 08:36:20 -04:00
|
|
|
assert_template 'home/index'
|
2009-10-12 07:37:42 -04:00
|
|
|
assert_contain 'Your password was changed successfully.'
|
2009-10-12 08:56:12 -04:00
|
|
|
assert user.reload.valid_password?('987654321')
|
2009-10-12 07:37:42 -04:00
|
|
|
end
|
2009-10-18 07:54:53 -04:00
|
|
|
|
|
|
|
test 'after entering invalid data user should still be able to change his password' do
|
|
|
|
user = create_user
|
|
|
|
request_forgot_password
|
|
|
|
reset_password :reset_password_token => user.reload.reset_password_token do
|
|
|
|
fill_in 'Password confirmation', :with => 'other_password'
|
|
|
|
end
|
|
|
|
assert_response :success
|
2010-04-13 17:28:13 -04:00
|
|
|
assert_have_selector '#error_explanation'
|
2009-10-18 07:54:53 -04:00
|
|
|
assert_not user.reload.valid_password?('987654321')
|
|
|
|
|
|
|
|
reset_password :reset_password_token => user.reload.reset_password_token, :visit => false
|
|
|
|
assert_contain 'Your password was changed successfully.'
|
|
|
|
assert user.reload.valid_password?('987654321')
|
|
|
|
end
|
2009-10-18 08:36:20 -04:00
|
|
|
|
|
|
|
test 'sign in user automatically after changing it\'s password' do
|
|
|
|
user = create_user
|
|
|
|
request_forgot_password
|
|
|
|
reset_password :reset_password_token => user.reload.reset_password_token
|
|
|
|
|
|
|
|
assert warden.authenticated?(:user)
|
|
|
|
end
|
2010-02-05 15:34:05 -05:00
|
|
|
|
|
|
|
test 'does not sign in user automatically after changing it\'s password if it\'s not active' do
|
|
|
|
user = create_user(:confirm => false)
|
|
|
|
request_forgot_password
|
|
|
|
reset_password :reset_password_token => user.reload.reset_password_token
|
|
|
|
|
2010-04-03 05:43:31 -04:00
|
|
|
assert_equal new_user_session_path, @request.path
|
2010-02-05 15:34:05 -05:00
|
|
|
assert !warden.authenticated?(:user)
|
|
|
|
end
|
|
|
|
|
2009-10-12 07:37:42 -04:00
|
|
|
end
|