Skip to content

Instantly share code, notes, and snippets.

@felipeelias
Forked from hgmnz/authorization_matcher.rb
Created April 16, 2011 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipeelias/923513 to your computer and use it in GitHub Desktop.
Save felipeelias/923513 to your computer and use it in GitHub Desktop.
module AuthenticationMatcher
class RequireAuthentication
def initialize(method, action, params, context)
@method = method
@action = action
@params = params
@context = context
end
def matches?(controller)
responds_with_redirect
end
def failure_message
"Expected action :#{@action} with :#{@method} to require authentication"
end
def negative_failure_message
"Did not expect action :#{@action} with :#{@method} to require authentication"
end
private
def responds_with_redirect
do_request
@context.response.location == @context.login_url
end
def do_request
@context.__send__(@method, @action, @params)
end
end
def require_authentication_for(method, action, params = {})
RequireAuthentication.new(method, action, params, self)
end
end
RSpec.configuration.include AuthenticationMatcher
module SessionHelper
def sign_in(account = nil)
session[:current_account_id] = account.nil? ? Factory.create(:account).id : account.id
return account
end
def sign_out
@controller.current_account = nil
end
end
RSpec.configuration.include SessionHelper, type: :controller
# example usage in a controller
describe SomeController, 'authorization' do
it { should require_authentication_for(:get, :new) }
it { should require_authentication_for(:post, :create) }
it { should_not require_authentication_for(:get, :show, :id => "fake_id") }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment