Sha256: 003fe08dbec79418c9b2fa9554895a8919d2119a207843290acf06d38b23376c

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

require "rails_helper"

RSpec.describe Fume::Cancan::ControllerExtensions, type: :controller do
  describe "#authorize_object" do
    controller(ApplicationController) {}
    action { get :index }
    skip_do_action

    context "then undefine any authorize_object" do
      it { is_expected_response_ok }
    end

    context "then authorize_object is a symbol" do
      controller { authorize_object :admin }

      context "then allow" do
        before { ability.can :manage, :admin }
        it { is_expected_response_ok }
      end

      context "then deny" do
        it { is_expected_access_denied }
      end
    end

    context "then authorize_object with options" do
      before { ability.can :index, :admin }

      context "with except option" do
        controller { authorize_object :other, except: [ :index ] }
        it { is_expected_response_ok }
      end

      context "with object options" do
        controller { authorize_object object: :admin }
        it { is_expected_response_ok }
      end

      context "with action options" do
        controller { authorize_object :admin, action: :read }
        it { is_expected_access_denied }
      end
    end

    define_method :ability, -> { controller.current_ability }
    define_method :is_expected_access_denied, -> { expect { do_action }.to raise_error CanCan::AccessDenied }
    define_method :is_expected_response_ok, -> { do_action; expect(response.body).to eq "OK" }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fume-cancan-0.1.0 spec/lib/fume/cancan/controller_extensions_spec.rb