Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new be_allowed_to matcher, specs for matchers
- Loading branch information
1 parent
8faa162
commit 2c038b5
Showing
9 changed files
with
128 additions
and
91 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,4 @@ | ||
module Aegis | ||
module Matchers | ||
# Support aegis/spec for old code. | ||
# Maybe remove this some day. | ||
require 'aegis/spec/matchers' | ||
|
||
class CheckPermissions | ||
|
||
def initialize(expected_resource, expected_options = {}) | ||
@expected_resource = expected_resource | ||
@expected_options = expected_options | ||
end | ||
|
||
def matches?(controller) | ||
@controller_class = controller.class | ||
@actual_resource = @controller_class.instance_variable_get('@aegis_permissions_resource') | ||
@actual_options = @controller_class.instance_variable_get('@aegis_permissions_options') | ||
@actual_resource == @expected_resource && @actual_options == @expected_options | ||
end | ||
|
||
def failure_message | ||
if @actual_resource != @expected_resource | ||
"expected #{@controller_class} to check permissions against resource #{@expected_resource.inspect}, but it checked against #{@actual_resource.inspect}" | ||
else | ||
"expected #{@controller_class} to check permissions with options #{@expected_options.inspect}, but options were #{@actual_options.inspect}" | ||
end | ||
end | ||
|
||
def negative_failure_message | ||
if @actual_resource == @expected_resource | ||
"expected #{@controller_class} to not check permissions against resource #{@expected_resource.inspect}" | ||
else | ||
"expected #{@controller_class} to not check permissions with options #{@expected_options.inspect}" | ||
end | ||
end | ||
|
||
def description | ||
description = "check permissions against resource #{@expected_resource.inspect}" | ||
description << " with options #{@expected_options.inspect}" if @expected_options.any? | ||
description | ||
end | ||
|
||
end | ||
|
||
def check_permissions(*args) | ||
CheckPermissions.new(*args) | ||
end | ||
|
||
end | ||
end | ||
|
||
|
||
ActiveSupport::TestCase.send :include, Aegis::Matchers | ||
|
||
#Spec::Rails::Example::ControllerExampleGroup.extend Aegis::ControllerSpecMacros | ||
|
||
|
||
# def it_should_allow_access_for(*allowed_roles, &block) | ||
# | ||
# denied_roles = Permissions.roles.collect(&:name) - allowed_roles | ||
# | ||
# describe 'permissions' do | ||
# | ||
# before :each do | ||
# sign_out | ||
# end | ||
# | ||
# it "should deny access when no user is signed in" do | ||
# expect { instance_eval(&block) }.to raise_error(Aegis::AccessDenied) | ||
# end | ||
# | ||
# allowed_roles.each do |role| | ||
# it "should allow access for an authenticated #{role}" do | ||
# sign_in User.new(:role_name => role) | ||
# expect { instance_eval(&block) }.to_not raise_error | ||
# response.code.should == '200' | ||
# end | ||
# end | ||
# | ||
# denied_roles.each do |role| | ||
# it "should deny access for an authenticated #{role}" do | ||
# sign_in User.new(:role_name => role) | ||
# expect { instance_eval(&block) }.to raise_error(Aegis::AccessDenied) | ||
# end | ||
# end | ||
# | ||
# end | ||
# | ||
# end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
module Aegis | ||
module Spec | ||
module Matchers | ||
|
||
class CheckPermissions | ||
|
||
def initialize(expected_resource, expected_options = {}) | ||
@expected_resource = expected_resource | ||
@expected_options = expected_options | ||
end | ||
|
||
def matches?(controller) | ||
@controller_class = controller.class | ||
@actual_resource = @controller_class.instance_variable_get('@aegis_permissions_resource') | ||
@actual_options = @controller_class.instance_variable_get('@aegis_permissions_options') | ||
@actual_resource == @expected_resource && @actual_options == @expected_options | ||
end | ||
|
||
def failure_message | ||
if @actual_resource != @expected_resource | ||
"expected #{@controller_class} to check permissions against resource #{@expected_resource.inspect}, but it checked against #{@actual_resource.inspect}" | ||
else | ||
"expected #{@controller_class} to check permissions with options #{@expected_options.inspect}, but options were #{@actual_options.inspect}" | ||
end | ||
end | ||
|
||
def negative_failure_message | ||
if @actual_resource == @expected_resource | ||
"expected #{@controller_class} to not check permissions against resource #{@expected_resource.inspect}" | ||
else | ||
"expected #{@controller_class} to not check permissions with options #{@expected_options.inspect}" | ||
end | ||
end | ||
|
||
def description | ||
description = "check permissions against resource #{@expected_resource.inspect}" | ||
description << " with options #{@expected_options.inspect}" if @expected_options.any? | ||
description | ||
end | ||
|
||
end | ||
|
||
def check_permissions(*args) | ||
CheckPermissions.new(*args) | ||
end | ||
|
||
def be_allowed_to(*args) | ||
simple_matcher do |user, matcher| | ||
action, *action_args = args | ||
target = action.to_s + (action_args.present? ? " given #{action_args.inspect}" : "") | ||
matcher.description = "be allowed to " + target | ||
matcher.failure_message = "expected #{user.inspect} to be allowed to #{target}" | ||
matcher.negative_failure_message = "expected #{user.inspect} to be denied to #{target}" | ||
user.send("may_#{action}?", *action_args) | ||
end | ||
end | ||
|
||
end | ||
end | ||
end | ||
|
||
ActiveSupport::TestCase.send :include, Aegis::Spec::Matchers | ||
|
4 changes: 2 additions & 2 deletions
4
spec/action_controller_spec.rb → spec/aegis/action_controller_spec.rb
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
require File.dirname(__FILE__) + "/spec_helper" | ||
require "spec_helper" | ||
|
||
describe Aegis::HasRole do | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
require File.dirname(__FILE__) + "/spec_helper" | ||
require "spec_helper" | ||
|
||
describe Aegis::Loader do | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
require File.dirname(__FILE__) + "/spec_helper" | ||
require "spec_helper" | ||
|
||
describe Aegis::Permissions do | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
require File.dirname(__FILE__) + "/spec_helper" | ||
require "spec_helper" | ||
|
||
describe Aegis::Sieve do | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require 'spec_helper' | ||
|
||
describe Aegis::Spec::Matchers do | ||
|
||
describe 'be_allowed_to' do | ||
|
||
before(:each) do | ||
|
||
permissions = @permissions = Class.new(Aegis::Permissions) do | ||
role :user | ||
resources :files do | ||
allow :user do | ||
object == 'allowed-file' | ||
end | ||
end | ||
end | ||
|
||
@user_class = Class.new(ActiveRecord::Base) do | ||
set_table_name 'users' | ||
has_role :permissions => permissions | ||
end | ||
|
||
@user = @user_class.new(:role_name => 'user') | ||
|
||
end | ||
|
||
it 'should match the positive case' do | ||
@user.should be_allowed_to(:update_file, 'allowed-file') | ||
end | ||
|
||
it 'should match the negative case' do | ||
@user.should_not be_allowed_to(:update_file, 'denied-file') | ||
end | ||
|
||
end | ||
|
||
describe 'check_permissions' do | ||
|
||
before(:each) do | ||
@controller = Class.new(ActionController::Base) do | ||
permissions :post | ||
end.new | ||
end | ||
|
||
it 'should match the positive case' do | ||
@controller.should check_permissions(:post) | ||
end | ||
|
||
it 'should match the negative case' do | ||
@controller.should_not check_permissions(:reviews) | ||
end | ||
|
||
end | ||
|
||
end |