2012-05-05 23:35:24 -04:00
|
|
|
require 'test_helper'
|
|
|
|
require 'ostruct'
|
|
|
|
require 'warden/strategies/base'
|
|
|
|
require 'devise/test_helpers'
|
|
|
|
|
2012-05-06 06:09:53 -04:00
|
|
|
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
|
2012-05-05 23:35:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class CustomStrategyTest < ActionController::TestCase
|
2012-05-06 06:09:53 -04:00
|
|
|
tests CustomStrategyController
|
2012-05-05 23:35:24 -04:00
|
|
|
|
|
|
|
include Devise::TestHelpers
|
|
|
|
|
2012-05-06 06:09:53 -04:00
|
|
|
setup do
|
|
|
|
Warden::Strategies.add(:custom_strategy, CustomStrategy)
|
2012-05-05 23:35:24 -04:00
|
|
|
end
|
|
|
|
|
2012-05-06 06:09:53 -04:00
|
|
|
teardown do
|
|
|
|
Warden::Strategies._strategies.delete(:custom_strategy)
|
2012-05-05 23:35:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "custom strategy can return its own status code" do
|
2012-05-06 06:09:53 -04:00
|
|
|
ret = get :new
|
2012-05-06 02:48:39 -04:00
|
|
|
|
|
|
|
# check the returned rack array
|
|
|
|
assert ret.is_a?(Array)
|
|
|
|
assert_equal 400, ret.first
|
|
|
|
|
2012-05-06 05:24:57 -04:00
|
|
|
# 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
|
2012-05-06 02:48:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "custom strategy can return custom headers" do
|
2012-05-06 06:09:53 -04:00
|
|
|
ret = get :new
|
2012-05-05 23:35:24 -04:00
|
|
|
|
2012-05-06 02:48:39 -04:00
|
|
|
# check the returned rack array
|
|
|
|
assert ret.is_a?(Array)
|
|
|
|
assert_equal ret.third['X-FOO'], 'BAR'
|
2012-05-05 23:35:24 -04:00
|
|
|
|
2012-05-06 05:24:57 -04:00
|
|
|
# check the saved response headers as well.
|
|
|
|
assert_equal response.headers['X-FOO'], 'BAR'
|
2012-05-05 23:35:24 -04:00
|
|
|
end
|
|
|
|
end
|