Skip to content

Commit

Permalink
Add an allow macro to the authorization macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
Manfred committed Sep 14, 2009
1 parent 5ac2c87 commit c5bb316
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
25 changes: 20 additions & 5 deletions lib/test/spec/rails/macros/authorization.rb
Expand Up @@ -11,11 +11,24 @@ class Should
# should.disallow.get :index
def disallow
Test::Spec::Rails::Macros::Authorization::TestGenerator.new(test_case,
:access_denied?,
:access_denied?, true,
'Expected access to be denied'
)
end

# Generates a test which tests authorization code. It assumes a method called <code>access_denied?</code>
# on the test case. The <code>access_denied?</code> method should return true when access is denied
# (ie. a 403 status code) and false in other cases.
#
# Example:
# should.allow.get :index
def allow
Test::Spec::Rails::Macros::Authorization::TestGenerator.new(test_case,
:access_denied?, false,
'Expected access to be allowed'
)
end

# Generates a test which tests authorization code. It assumes a method called <code>access_denied?</code>
# on the test case. The <code>login_required?</code> method should return true when the visitor was
# asked for credentials (ie. a 401 status code or a redirect to a login page) and false in other cases.
Expand All @@ -24,18 +37,19 @@ def disallow
# should.require_login.get :index
def require_login
Test::Spec::Rails::Macros::Authorization::TestGenerator.new(test_case,
:login_required?,
:login_required?, true,
'Expected login to be required'
)
end
end

module Authorization
class TestGenerator < Proxy
attr_accessor :validation_method, :message
attr_accessor :validation_method, :message, :expected

def initialize(test_case, validation_method, message)
def initialize(test_case, validation_method, expected, message)
self.validation_method = validation_method
self.expected = expected
self.message = message

super(test_case)
Expand All @@ -47,11 +61,12 @@ def method_missing(verb, action, params={})
description << " #{params.inspect}" unless params.blank?

validation_method = self.validation_method
expected = self.expected
message = self.message

test_case.it description do
send(verb, action, immediate_values(params))
send(validation_method).should.messaging(message) == true
send(validation_method).should.messaging(message) == expected
end
else
super
Expand Down
26 changes: 24 additions & 2 deletions test/macros/authorization_test.rb
Expand Up @@ -4,7 +4,7 @@
describe "TestGenerator, concerning generation" do
before do
@test = mock('Test')
@generator = Test::Spec::Rails::Macros::Authorization::TestGenerator.new(@test, :access_denied?, 'Expected access to be denied')
@generator = Test::Spec::Rails::Macros::Authorization::TestGenerator.new(@test, :access_denied?, true, 'Expected access to be denied')
end

it "should generate a test description for a GET" do
Expand Down Expand Up @@ -32,7 +32,7 @@ def self.it(description, &block)

describe "TestGenerator, concerning test contents" do
before do
@generator = Test::Spec::Rails::Macros::Authorization::TestGenerator.new(Immediate, :access_denied?, 'Expected access to be denied')
@generator = Test::Spec::Rails::Macros::Authorization::TestGenerator.new(Immediate, :access_denied?, true, 'Expected access to be denied')
@generator.stubs(:send).with(:access_denied?).returns(true)
end

Expand All @@ -52,6 +52,16 @@ def self.it(description, &block)

@generator.post(:create, params)
end

it "should test the return value of the validation method against the expected method" do
@generator.expected = false
params = {:name => 'bitterzoet'}

@generator.expects(:immediate_values).with(params).returns(params)
@generator.stubs(:send).returns(false)

@generator.post(:create, params)
end
end

describe "Macros::Authorization" do
Expand All @@ -67,6 +77,17 @@ def self.it(description, &block)
generator.test_case.should == @test_case
generator.validation_method.should == :access_denied?
generator.message.should == 'Expected access to be denied'
generator.expected.should == true
end

it "should return a test generator when a new allow rule is invoked" do
generator = @proxy.allow

generator.should.is_a(Test::Spec::Rails::Macros::Authorization::TestGenerator)
generator.test_case.should == @test_case
generator.validation_method.should == :access_denied?
generator.message.should == 'Expected access to be denied'
generator.expected.should == false
end

it "should return a test generator when a new login_required rule is invoked" do
Expand All @@ -76,5 +97,6 @@ def self.it(description, &block)
generator.test_case.should == @test_case
generator.validation_method.should == :login_required?
generator.message.should == 'Expected login to be required'
generator.expected.should == true
end
end

0 comments on commit c5bb316

Please sign in to comment.