-
-
Save felipeelias/923513 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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