Testing

Brooks Swinnerton edited this page Sep 16, 2015 · 5 revisions

Warden is a rack application, and therefore full stack testing is the way to go when you test. In order to get Warden to actually participate in a test, it must pass through the stack.

I tend to favour using rack-test for testing. Warden ships with some test helpers as of version 0.9.5.

All examples assume Rack::Test

Requirements

You need to include the Warden::Test::Helpers in the scope of your application to provide the helpers

Also, and importantly, you need to reset warden in test mode in the cleanup phase of your test framework with Warden.test_reset!

include Warden::Test::Helpers

after { Warden.test_reset! }

Login

# setup the helpers by including them into your test scope
include Warden::Test::Helpers  

# To login for the next action as "A User"
login_as "A User"
get "/foo"

# To login for the next action as "An Admin"
login_as "An Admin", :scope => :admin
get "/foo"

# Login both an admin and standard user
login_as "A User"
login_as "An Admin", :scope => :admin
get "/foo"

Logout

# setup the helpers
include Warden::Test::Helpers

# Logout all users _before_ your application receives the request
logout
get "/foo"

# Logout only the admin user before your application receives a result
logout :admin
get "/foo"

Custom

# setup the helpers
include Warden::Test::Helpers

# When you add an on_next_request block. It's executed when the request hits warden.  Once it's hit, it is consumed and does not affect further requests
# This example logs in a user
Warden.on_next_request do |proxy|
  proxy.set_user("Some User", :scope => :foo)
end