Skip to content
This repository has been archived by the owner on Nov 19, 2019. It is now read-only.

Commit

Permalink
Update Rspec version and expectation syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanl committed Jul 17, 2015
1 parent c71a787 commit 1194adf
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 77 deletions.
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in authority.gemspec
gemspec

gem 'rspec', '>= 2.12.0'
2 changes: 2 additions & 0 deletions authority.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Gem::Specification.new do |gem|
gem.add_dependency "activesupport", ">= 3.0.0"
gem.add_dependency "rake", ">= 0.8.7"

gem.add_development_dependency "rspec", "~> 3.3.0"

gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
gem.files = `git ls-files`.split("\n")
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand Down
18 changes: 9 additions & 9 deletions spec/authority/abilities_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@

it "constantizes the authorizer name as the authorizer" do
resource_class.instance_variable_set(:@authorizer, nil)
resource_class.authorizer_name.should_receive(:constantize)
expect(resource_class.authorizer_name).to receive(:constantize)
resource_class.authorizer
end

it "memoizes the authorizer to avoid reconstantizing" do
resource_class.authorizer
resource_class.authorizer_name.should_not_receive(:constantize)
expect(resource_class.authorizer_name).not_to receive(:constantize)
resource_class.authorizer
end

Expand Down Expand Up @@ -106,7 +106,7 @@ class NoAuthorizerModel < resource_class; end ;
context "when given an options hash" do

it "delegates `#{method_name}` to its authorizer class, passing the options" do
resource_class.authorizer.should_receive(method_name).with(user, :lacking => 'nothing')
expect(resource_class.authorizer).to receive(method_name).with(user, :lacking => 'nothing')
resource_class.send(method_name, user, :lacking => 'nothing')
end

Expand All @@ -115,7 +115,7 @@ class NoAuthorizerModel < resource_class; end ;
context "when not given an options hash" do

it "delegates `#{method_name}` to its authorizer class, passing no options" do
resource_class.authorizer.should_receive(method_name).with(user)
expect(resource_class.authorizer).to receive(method_name).with(user)
resource_class.send(method_name, user)
end

Expand Down Expand Up @@ -147,8 +147,8 @@ class NoAuthorizerModel < resource_class; end ;
context "when given an options hash" do

it "delegates `#{method_name}` to a new authorizer instance, passing the options" do
resource_class.authorizer.stub(:new).and_return(@authorizer)
@authorizer.should_receive(method_name).with(user, :with => 'mayo')
allow(resource_class.authorizer).to receive(:new).and_return(@authorizer)
expect(@authorizer).to receive(method_name).with(user, :with => 'mayo')
resource_instance.send(method_name, user, :with => 'mayo')
end

Expand All @@ -157,8 +157,8 @@ class NoAuthorizerModel < resource_class; end ;
context "when not given an options hash" do

it "delegates `#{method_name}` to a new authorizer instance, passing no options" do
resource_class.authorizer.stub(:new).and_return(@authorizer)
@authorizer.should_receive(method_name).with(user)
allow(resource_class.authorizer).to receive(:new).and_return(@authorizer)
expect(@authorizer).to receive(method_name).with(user)
resource_instance.send(method_name, user)
end

Expand All @@ -176,7 +176,7 @@ class NoAuthorizerModel < resource_class; end ;
# instance of the authorizer. Otherwise, you might check, make a change to the
# model instance, check again, and get an outdated answer.
it "always creates a new authorizer instance when accessing the authorizer" do
resource_instance.class.authorizer.should_receive(:new).with(resource_instance).twice
expect(resource_instance.class.authorizer).to receive(:new).with(resource_instance).twice
2.times { resource_instance.authorizer }
end

Expand Down
8 changes: 4 additions & 4 deletions spec/authority/authorizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
context "when given an options hash" do

it "delegates `#{method_name}` to the corresponding class method, passing the options" do
authorizer.class.should_receive(method_name).with(user, :under => 'God')
expect(authorizer.class).to receive(method_name).with(user, :under => 'God')
authorizer.send(method_name, user, :under => 'God')
end

Expand All @@ -48,7 +48,7 @@
context "when not given an options hash" do

it "delegates `#{method_name}` to the corresponding class method, passing no options" do
authorizer.class.should_receive(method_name).with(user)
expect(authorizer.class).to receive(method_name).with(user)
authorizer.send(method_name, user)
end

Expand All @@ -75,7 +75,7 @@

it "delegates `#{method_name}` to the authorizer's `default` method, passing the options" do
able = method_name.sub('_by?', '').to_sym
Authority::Authorizer.should_receive(:default).with(able, user, :with => 'gusto')
expect(Authority::Authorizer).to receive(:default).with(able, user, :with => 'gusto')
Authority::Authorizer.send(method_name, user, :with => 'gusto')
end

Expand All @@ -85,7 +85,7 @@

it "delegates `#{method_name}` to the authorizer's `default` method, passing no options" do
able = method_name.sub('_by?', '').to_sym
Authority::Authorizer.should_receive(:default).with(able, user)
expect(Authority::Authorizer).to receive(:default).with(able, user)
Authority::Authorizer.send(method_name, user)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/authority/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Authority.instance_variable_set :@configuration, nil
null = File.exists?('/dev/null') ? '/dev/null' : 'NUL:' # Allow for Windows
logger = Logger.new(null)
Logger.should_receive(:new).with(STDERR).and_return(logger)
expect(Logger).to receive(:new).with(STDERR).and_return(logger)
Authority.configure
Authority.logger
end
Expand Down
86 changes: 43 additions & 43 deletions spec/authority/controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ def self.before_filter(*args) ; end
context "when including" do

before :each do
Authority::Controller.stub(:security_violation_callback).and_return(Proc.new {|exception| })
allow(Authority::Controller).to receive(:security_violation_callback).and_return(Proc.new {|exception| })
end

after :each do
controller_class.send(:include, Authority::Controller)
end

it "specifies rescuing security violations with a standard callback" do
controller_class.should_receive(:rescue_from).with(
expect(controller_class).to receive(:rescue_from).with(
Authority::SecurityViolation, :with => Authority::Controller.security_violation_callback
)
end
Expand Down Expand Up @@ -55,7 +55,7 @@ def self.before_filter(*args) ; end
# this test, so I'm stealing that behavior.

Authority.configuration.security_violation_handler = :fire_ze_missiles
controller_instance.should_receive(:fire_ze_missiles).with(fake_exception)
expect(controller_instance).to receive(:fire_ze_missiles).with(fake_exception)
controller_instance.instance_exec(fake_exception, &Authority::Controller.security_violation_callback)

end
Expand Down Expand Up @@ -101,20 +101,20 @@ def self.before_filter(*args) ; end

it "sets up a before_filter, passing the options it was given" do
filter_options = {:only => [:show, :edit, :update]}
controller_class.should_receive(:before_filter).with(:run_authorization_check, filter_options)
expect(controller_class).to receive(:before_filter).with(:run_authorization_check, filter_options)
controller_class.authorize_actions_for(resource_class, filter_options)
end

it "if :all_actions option is given, it overrides the action hash to use the action given" do
overridden_action_map = controller_class.authority_action_map
overridden_action_map.update(overridden_action_map) {|k,v| v = :annihilate}
child_controller.authorize_actions_for(resource_class, :all_actions => :annihilate)
child_controller.authority_action_map.should eq(overridden_action_map)
expect(child_controller.authority_action_map).to eq(overridden_action_map)
end

it "passes the action hash to the `add_actions` method" do
new_actions = {:synthesize => :create, :annihilate => 'delete'}
child_controller.should_receive(:add_actions).with(new_actions)
expect(child_controller).to receive(:add_actions).with(new_actions)
child_controller.authorize_actions_for(resource_class, :actions => new_actions)
end

Expand Down Expand Up @@ -190,52 +190,52 @@ def self.before_filter(*args) ; end
let(:controller_instance) { controller_class.new }

before(:each) do
controller_instance.stub(:class).and_return("FooController")
controller_instance.stub(:action_name).and_return(:bar)
allow(controller_instance).to receive(:class).and_return("FooController")
allow(controller_instance).to receive(:action_name).and_return(:bar)
end

it "sets up an after_filter, passing the options it was given" do
filter_options = {:only => [:show, :edit, :update]}
controller_class.should_receive(:after_filter).with(filter_options)
expect(controller_class).to receive(:after_filter).with(filter_options)
controller_class.ensure_authorization_performed(filter_options)
end

it "triggers AuthorizationNotPerformed in after filter" do
controller_class.stub(:after_filter).and_yield(controller_instance)
lambda {
allow(controller_class).to receive(:after_filter).and_yield(controller_instance)
expect {
controller_class.ensure_authorization_performed
}.should raise_error(Authority::Controller::AuthorizationNotPerformed)
}.to raise_error(Authority::Controller::AuthorizationNotPerformed)
end

it "AuthorizationNotPerformed error has meaningful message" do
controller_class.stub(:after_filter).and_yield(controller_instance)
lambda {
allow(controller_class).to receive(:after_filter).and_yield(controller_instance)
expect {
controller_class.ensure_authorization_performed
}.should raise_error("No authorization was performed for FooController#bar")
}.to raise_error("No authorization was performed for FooController#bar")
end

it "does not trigger AuthorizationNotPerformed when :if is false" do
controller_instance.stub(:authorize?) { false }
controller_class.stub(:after_filter).with({}).and_yield(controller_instance)
lambda {
allow(controller_instance).to receive(:authorize?) { false }
allow(controller_class).to receive(:after_filter).with({}).and_yield(controller_instance)
expect {
controller_class.ensure_authorization_performed(:if => :authorize?)
}.should_not raise_error()
}.not_to raise_error()
end

it "does not trigger AuthorizationNotPerformed when :unless is true" do
controller_instance.stub(:skip_authorization?) { true }
controller_class.stub(:after_filter).with({}).and_yield(controller_instance)
lambda {
allow(controller_instance).to receive(:skip_authorization?) { true }
allow(controller_class).to receive(:after_filter).with({}).and_yield(controller_instance)
expect {
controller_class.ensure_authorization_performed(:unless => :skip_authorization?)
}.should_not raise_error()
}.not_to raise_error()
end

it "does not raise error when #authorization_performed is true" do
controller_instance.authorization_performed = true
controller_class.stub(:after_filter).with({}).and_yield(controller_instance)
lambda {
allow(controller_class).to receive(:after_filter).with({}).and_yield(controller_instance)
expect {
controller_class.ensure_authorization_performed
}.should_not raise_error()
}.not_to raise_error()
end

end
Expand All @@ -253,7 +253,7 @@ def self.before_filter(*args) ; end

let(:controller_instance) do
controller_class.new.tap do |cc|
cc.stub(Authority.configuration.user_method).and_return(user)
allow(cc).to receive(Authority.configuration.user_method).and_return(user)
end
end

Expand All @@ -264,7 +264,7 @@ def self.before_filter(*args) ; end
context "if a resource class was specified" do

it "checks authorization on the model specified" do
controller_instance.should_receive(:authorize_action_for).with(resource_class)
expect(controller_instance).to receive(:authorize_action_for).with(resource_class)
controller_instance.send(:run_authorization_check)
end

Expand All @@ -284,11 +284,11 @@ def self.before_filter(*args) ; end

context "and the method returns a class" do
before :each do
controller_instance.stub(:method_to_find_class).and_return(resource_class)
allow(controller_instance).to receive(:method_to_find_class).and_return(resource_class)
end

it "checks authorization on that class" do
controller_instance.should_receive(:authorize_action_for).with(resource_class)
expect(controller_instance).to receive(:authorize_action_for).with(resource_class)
controller_instance.send(:run_authorization_check)
end
end
Expand All @@ -297,11 +297,11 @@ def self.before_filter(*args) ; end
let(:some_options) { { :a => 1, :b => 2 } }

before :each do
controller_instance.stub(:method_to_find_class).and_return([resource_class, some_options])
allow(controller_instance).to receive(:method_to_find_class).and_return([resource_class, some_options])
end

it "checks authorization on that class and passes the options" do
controller_instance.should_receive(:authorize_action_for).with(resource_class, some_options)
expect(controller_instance).to receive(:authorize_action_for).with(resource_class, some_options)
controller_instance.send(:run_authorization_check)
end
end
Expand All @@ -321,7 +321,7 @@ def self.before_filter(*args) ; end
end

it "raises a MissingAction if there is no corresponding action for the controller" do
controller_instance.stub(:action_name).and_return('sculpt')
allow(controller_instance).to receive(:action_name).and_return('sculpt')
expect { controller_instance.send(:run_authorization_check) }.to raise_error(
Authority::Controller::MissingAction
)
Expand All @@ -331,31 +331,31 @@ def self.before_filter(*args) ; end

describe "authorize_action_for" do

before(:each) { controller_instance.stub(:action_name).and_return(:destroy) }
before(:each) { allow(controller_instance).to receive(:action_name).and_return(:destroy) }

it "calls Authority.enforce to authorize the action" do
Authority.should_receive(:enforce)
expect(Authority).to receive(:enforce)
controller_instance.send(:authorize_action_for, resource_class)
end

it "passes along any options it was given" do
options = {:for => 'insolence'}
Authority.should_receive(:enforce).with('delete', resource_class, user, options)
expect(Authority).to receive(:enforce).with('delete', resource_class, user, options)
controller_instance.send(:authorize_action_for, resource_class, options)
end

it "sets correct authorization flag" do
Authority.stub(:enforce)
allow(Authority).to receive(:enforce)
controller_instance.send(:authorize_action_for, resource_class)
controller_instance.authorization_performed?.should eq(true)
expect(controller_instance.authorization_performed?).to eq(true)
end

end

describe "authority_user" do

it "gets the user for the current request from the configured user_method" do
controller_instance.should_receive(Authority.configuration.user_method)
expect(controller_instance).to receive(Authority.configuration.user_method)
controller_instance.send(:authority_user)
end

Expand All @@ -366,15 +366,15 @@ def self.before_filter(*args) ; end
let(:mock_error) { double(:message => 'oh noes! an error!') }

it "logs an error" do
Authority.logger.should_receive(:warn)
controller_instance.stub(:render)
expect(Authority.logger).to receive(:warn)
allow(controller_instance).to receive(:render)
controller_instance.send(:authority_forbidden, mock_error)
end

it "renders the public/403.html file" do
forbidden_page = Rails.root.join('public/403.html')
Authority.logger.stub(:warn)
controller_instance.should_receive(:render).with(:file => forbidden_page, :status => 403, :layout => false)
allow(Authority.logger).to receive(:warn)
expect(controller_instance).to receive(:render).with(:file => forbidden_page, :status => 403, :layout => false)
controller_instance.send(:authority_forbidden, mock_error)
end

Expand Down
Loading

0 comments on commit 1194adf

Please sign in to comment.