Skip to content

Instantly share code, notes, and snippets.

@bjjb
Created September 18, 2010 21:37
Show Gist options
  • Save bjjb/586061 to your computer and use it in GitHub Desktop.
Save bjjb/586061 to your computer and use it in GitHub Desktop.
Testing ApplicationController before_filter logic in Rails3
require 'test_helper'
class DummyController < ApplicationController
def index
render :nothing => true
end
end
MyApp::Application.routes.disable_clear_and_finalize = true
MyApp::Application.routes.clear!
MyApp::Application.routes_reloader.paths.each { |path| load(path) }
MyApp::Application.routes.draw { match '/dummy' => "dummy#index" }
MyApp::Application.routes.finalize!
class ApplicationControllerTest < ActionController::TestCase
tests DummyController
setup do
activate_authlogic
end
test "redirects you to the login page (for HTML requests)" do
get :index
assert_redirected_to :new_user_session
end
%W(xml json js).each do |format|
test "forbids access for #{format}" do
get :index, :format => format
assert_response :forbidden
end
end
%W(html xml json js).each do |format|
test "forbids access for #{format} from ajax" do
xhr :get, :index, :format => format
assert_response :forbidden
end
end
%W(html xml json js).each do |format|
test "grants access for logged in users (#{format})" do
UserSession.create(users(:bob))
get :index, :format => format
assert_response :success
xhr :get, :index, :format => format
end
end
end
bob:
email: [email protected]
name: bob User
password_salt: <%= salt = Authlogic::Random.hex_token %>
crypted_password: <%= Authlogic::CryptoProviders::Sha512.encrypt("secret#{salt}") %>
persistence_token: <%= Authlogic::Random.hex_token %>
single_access_token: <%= Authlogic::Random.friendly_token %>
perishable_token: <%= Authlogic::Random.friendly_token %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment