mirror of
https://github.com/heartcombo/devise.git
synced 2026-01-08 22:37:57 -05:00
All of these have been deprecated for years, if we're releasing a new major version, let's take the opportunity to do some cleanup. * Remove deprecated `:bypass` option from `sign_in` helper, use `bypass_sign_in` instead. * Remove deprecated `devise_error_messages!` helper, use `render "devise/shared/error_messages", resource: resource` instead. * Remove deprecated `scope` second argument from `sign_in(resource, :admin)` controller test helper, use `sign_in(resource, scope: :admin)` instead. * Remove deprecated `Devise::TestHelpers`, use `Devise::Test::ControllerHelpers` instead. Closes #5739
63 lines
1.8 KiB
Ruby
63 lines
1.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'test_helper'
|
|
require 'ostruct'
|
|
require 'warden/strategies/base'
|
|
require 'devise/test/controller_helpers'
|
|
|
|
class CustomStrategyController < ActionController::Base
|
|
def new
|
|
warden.authenticate!(:custom_strategy)
|
|
end
|
|
end
|
|
|
|
# These tests are to prove that a warden strategy can successfully
|
|
# return a custom response, including a specific status code and
|
|
# custom http response headers. This does work in production,
|
|
# however, at the time of writing this, the Devise test helpers do
|
|
# not recognise the custom response and proceed to calling the
|
|
# Failure App. This makes it impossible to write tests for a
|
|
# strategy that return a custom response with Devise.
|
|
class CustomStrategy < Warden::Strategies::Base
|
|
def authenticate!
|
|
custom_headers = { "X-FOO" => "BAR" }
|
|
response = Rack::Response.new("BAD REQUEST", 400, custom_headers)
|
|
custom! response.finish
|
|
end
|
|
end
|
|
|
|
class CustomStrategyTest < Devise::ControllerTestCase
|
|
tests CustomStrategyController
|
|
|
|
include Devise::Test::ControllerHelpers
|
|
|
|
setup do
|
|
Warden::Strategies.add(:custom_strategy, CustomStrategy)
|
|
end
|
|
|
|
teardown do
|
|
Warden::Strategies._strategies.delete(:custom_strategy)
|
|
end
|
|
|
|
test "custom strategy can return its own status code" do
|
|
ret = get :new
|
|
|
|
# check the returned response
|
|
assert ret.is_a?(ActionDispatch::TestResponse)
|
|
|
|
# check the saved response as well. This is purely so that the response is available to the testing framework
|
|
# for verification. In production, the above array would be delivered directly to Rack.
|
|
assert_response 400
|
|
end
|
|
|
|
test "custom strategy can return custom headers" do
|
|
ret = get :new
|
|
|
|
# check the returned response
|
|
assert ret.is_a?(ActionDispatch::TestResponse)
|
|
|
|
# check the saved response headers as well.
|
|
assert_equal 'BAR', response.headers['X-FOO']
|
|
end
|
|
end
|