Files
devise/test/support/integration.rb
Carlos Antonio da Silva f08e0ad24a Integrate with Hotwire/Turbo by configuring error and response statuses
Treat `:turbo_stream` request format as a navigational format, much like
HTML, so Devise/responders can work properly.

Allow configuring the `error_status` and `redirect_status` using the
latest responders features, via a new custom Devise responder, so we can
customize the both responses to match Hotwire/Turbo behavior, for
example with `422 Unprocessable Entity` and `303 See Other`,
respectively. The defaults aren't changing in Devise itself (yet), so it
still responds on errors cases with `200 OK`, and redirects on non-GET
requests with `302 Found`, but new apps are generated with the new
statuses and existing apps can opt-in. Please note that these defaults
might change in a future release of Devise.

PRs/Issues references:

https://github.com/heartcombo/devise/pull/5545
https://github.com/heartcombo/devise/pull/5529
https://github.com/heartcombo/devise/pull/5516
https://github.com/heartcombo/devise/pull/5499
https://github.com/heartcombo/devise/pull/5487
https://github.com/heartcombo/devise/pull/5467
https://github.com/heartcombo/devise/pull/5440
https://github.com/heartcombo/devise/pull/5410
https://github.com/heartcombo/devise/pull/5340

https://github.com/heartcombo/devise/issues/5542
https://github.com/heartcombo/devise/issues/5530
https://github.com/heartcombo/devise/issues/5519
https://github.com/heartcombo/devise/issues/5513
https://github.com/heartcombo/devise/issues/5478
https://github.com/heartcombo/devise/issues/5468
https://github.com/heartcombo/devise/issues/5463
https://github.com/heartcombo/devise/issues/5458
https://github.com/heartcombo/devise/issues/5448
https://github.com/heartcombo/devise/issues/5446
https://github.com/heartcombo/devise/issues/5439
2023-01-31 11:02:01 -03:00

96 lines
2.6 KiB
Ruby

# frozen_string_literal: true
require 'action_dispatch/testing/integration'
class ActionDispatch::IntegrationTest
def warden
request.env['warden']
end
def create_user(options = {})
@user ||= begin
user = User.create!(
username: 'usertest',
email: options[:email] || 'user@test.com',
password: options[:password] || '12345678',
password_confirmation: options[:password] || '12345678',
created_at: Time.now.utc
)
user.update_attribute(:confirmation_sent_at, options[:confirmation_sent_at]) if options[:confirmation_sent_at]
user.confirm unless options[:confirm] == false
user.lock_access! if options[:locked] == true
User.validations_performed = false
user
end
end
def create_admin(options = {})
@admin ||= begin
admin = Admin.create!(
email: options[:email] || 'admin@test.com',
password: '123456', password_confirmation: '123456',
active: options[:active]
)
admin.confirm unless options[:confirm] == false
admin
end
end
def sign_in_as_user(options = {}, &block)
user = create_user(options)
visit_with_option options[:visit], new_user_session_path
fill_in 'email', with: options[:email] || 'user@test.com'
fill_in 'password', with: options[:password] || '12345678'
check 'remember me' if options[:remember_me] == true
yield if block_given?
click_button 'Log In'
user
end
def sign_in_as_admin(options = {}, &block)
admin = create_admin(options)
visit_with_option options[:visit], new_admin_session_path
fill_in 'email', with: 'admin@test.com'
fill_in 'password', with: '123456'
yield if block_given?
click_button 'Log In'
admin
end
# Fix assert_redirect_to in integration sessions because they don't take into
# account Middleware redirects.
#
def assert_redirected_to(url)
assert_includes [301, 302, 303], @integration_session.status,
"Expected status to be 301, 302, or 303, got #{@integration_session.status}"
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)
end
protected
def visit_with_option(given, default)
case given
when String
visit given
when FalseClass
# Do nothing
else
visit default
end
end
def prepend_host(url)
url = "http://#{request.host}#{url}" if url[0] == ?/
url
end
end