2010-03-23 11:28:17 -04:00
|
|
|
require 'action_dispatch/testing/integration'
|
2009-10-07 20:46:40 -04:00
|
|
|
|
2010-03-23 11:28:17 -04:00
|
|
|
class ActionDispatch::IntegrationTest
|
2009-10-07 20:46:40 -04:00
|
|
|
def warden
|
|
|
|
request.env['warden']
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_user(options={})
|
|
|
|
@user ||= begin
|
|
|
|
user = User.create!(
|
2014-02-25 11:42:55 -05:00
|
|
|
username: 'usertest',
|
|
|
|
email: options[:email] || 'user@test.com',
|
|
|
|
password: options[:password] || '12345678',
|
|
|
|
password_confirmation: options[:password] || '12345678',
|
|
|
|
created_at: Time.now.utc
|
2009-10-07 20:46:40 -04:00
|
|
|
)
|
2012-07-16 05:20:01 -04:00
|
|
|
user.update_attribute(:confirmation_sent_at, options[:confirmation_sent_at]) if options[:confirmation_sent_at]
|
2009-10-07 20:46:40 -04:00
|
|
|
user.confirm! unless options[:confirm] == false
|
2010-03-10 10:13:54 -05:00
|
|
|
user.lock_access! if options[:locked] == true
|
2009-10-07 20:46:40 -04:00
|
|
|
user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-11 09:49:25 -04:00
|
|
|
def create_admin(options={})
|
|
|
|
@admin ||= begin
|
|
|
|
admin = Admin.create!(
|
2014-02-25 11:42:55 -05:00
|
|
|
email: options[:email] || 'admin@test.com',
|
|
|
|
password: '123456', password_confirmation: '123456',
|
|
|
|
active: options[:active]
|
2009-10-11 09:49:25 -04:00
|
|
|
)
|
2011-12-11 13:53:07 -05:00
|
|
|
admin.confirm! unless options[:confirm] == false
|
2009-10-11 09:49:25 -04:00
|
|
|
admin
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def sign_in_as_user(options={}, &block)
|
2009-10-19 22:31:33 -04:00
|
|
|
user = create_user(options)
|
2010-07-07 04:51:14 -04:00
|
|
|
visit_with_option options[:visit], new_user_session_path
|
2014-02-25 11:42:55 -05:00
|
|
|
fill_in 'email', with: options[:email] || 'user@test.com'
|
|
|
|
fill_in 'password', with: options[:password] || '12345678'
|
2009-10-19 22:31:33 -04:00
|
|
|
check 'remember me' if options[:remember_me] == true
|
2009-10-11 09:49:25 -04:00
|
|
|
yield if block_given?
|
|
|
|
click_button 'Sign In'
|
2009-10-19 22:31:33 -04:00
|
|
|
user
|
2009-10-11 09:49:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def sign_in_as_admin(options={}, &block)
|
2009-10-19 22:31:33 -04:00
|
|
|
admin = create_admin(options)
|
2010-07-07 04:51:14 -04:00
|
|
|
visit_with_option options[:visit], new_admin_session_path
|
2014-02-25 11:42:55 -05:00
|
|
|
fill_in 'email', with: 'admin@test.com'
|
|
|
|
fill_in 'password', with: '123456'
|
2009-10-07 20:46:40 -04:00
|
|
|
yield if block_given?
|
|
|
|
click_button 'Sign In'
|
2009-10-19 22:31:33 -04:00
|
|
|
admin
|
2009-10-07 20:46:40 -04:00
|
|
|
end
|
2009-10-11 22:24:57 -04:00
|
|
|
|
|
|
|
# Fix assert_redirect_to in integration sessions because they don't take into
|
|
|
|
# account Middleware redirects.
|
|
|
|
#
|
|
|
|
def assert_redirected_to(url)
|
|
|
|
assert [301, 302].include?(@integration_session.status),
|
|
|
|
"Expected status to be 301 or 302, got #{@integration_session.status}"
|
|
|
|
|
2010-07-06 10:00:07 -04:00
|
|
|
assert_url url, @integration_session.headers["Location"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_current_url(expected)
|
|
|
|
assert_url expected, current_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_url(expected, actual)
|
|
|
|
assert_equal prepend_host(expected), prepend_host(actual)
|
2009-10-11 22:24:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2010-07-07 04:51:14 -04:00
|
|
|
def visit_with_option(given, default)
|
|
|
|
case given
|
|
|
|
when String
|
|
|
|
visit given
|
|
|
|
when FalseClass
|
|
|
|
# Do nothing
|
|
|
|
else
|
|
|
|
visit default
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-11 22:24:57 -04:00
|
|
|
def prepend_host(url)
|
|
|
|
url = "http://#{request.host}#{url}" if url[0] == ?/
|
|
|
|
url
|
|
|
|
end
|
2009-10-07 20:46:40 -04:00
|
|
|
end
|