class Authenticate::Testing::IntegrationTestsSignOn
Middleware which allows tests to bypass your sign on screen. Typically used by integration and feature tests, etc. Speeds up these tests by eliminating the need to visit and submit the signon form repeatedly.
Sign a test user in by passing as=USER_ID in a query parameter. If `User#to_param` is overridden you may pass a block to override the default user lookup behaviour.
Configure your application's test environment as follows:
# config/environments/test.rb MyRailsApp::Application.configure do # ... config.middleware.use Authenticate::IntegrationTestsSignOn # ... end
or if `User#to_param` is overridden (to `username` for example):
# config/environments/test.rb MyRailsApp::Application.configure do # ... config.middleware.use Authenticate::IntegrationTestsSignOn do |username| User.find_by(username: username) end # ... end
After configuring your app, usage in an integration tests is simple:
user = ... # load user visit dashboard_path(as: user)
Public Class Methods
new(app, &block)
click to toggle source
# File lib/authenticate/testing/integration_tests_sign_on.rb, line 39 def initialize(app, &block) @app = app @block = block end
Public Instance Methods
call(env)
click to toggle source
# File lib/authenticate/testing/integration_tests_sign_on.rb, line 44 def call(env) do_login(env) @app.call(env) end
Private Instance Methods
do_login(env)
click to toggle source
# File lib/authenticate/testing/integration_tests_sign_on.rb, line 51 def do_login(env) params = Rack::Utils.parse_query(env['QUERY_STRING']) user_param = params['as'] user = find_user(user_param) if user_param.present? if user.present? user.generate_session_token && user.save if user.session_token.nil? request = Rack::Request.new(env) request.cookies[Authenticate.configuration.cookie_name] = user.session_token end end
find_user(user_param)
click to toggle source
# File lib/authenticate/testing/integration_tests_sign_on.rb, line 63 def find_user(user_param) if @block @block.call(user_param) else Authenticate.configuration.user_model_class.find(user_param) end end