Sha256: 7a5e7f428f42ac1ea350e9831b001302c22a39df4cab990be281028f180f1ce1

Contents?: true

Size: 1.73 KB

Versions: 17

Compression:

Stored size: 1.73 KB

Contents

require "spec_helper"
require "shamu/active_record"

describe Shamu::Security::PolicyRefinement do
  use_active_record

  describe "#match?" do
    let( :relation ) { ActiveRecordSpec::Favorite.all }
    let( :refinement ) do
      Shamu::Security::PolicyRefinement.new( [ :read ], ActiveRecordSpec::Favorite, nil )
    end

    it "is true for matching action" do
      expect( refinement ).to be_match :read, relation, nil
    end

    it "is false for mismatched action" do
      expect( refinement ).not_to be_match :update, relation, nil
    end

    it "is true for matching relation" do
      expect( refinement ).to be_match :read, relation, nil
    end

    it "is false for mismatched relation" do
      klass = Class.new( ActiveRecord::Base )
      expect( refinement ).not_to be_match :read, klass.all, nil
    end
  end

  describe "#apply" do
    it "returns block result" do
      refinement = Shamu::Security::PolicyRefinement.new(
        [ :read ],
        ActiveRecordSpec::Favorite,
        ->( _, _ ) { :refined }
      )
      expect( refinement.apply( :read, ActiveRecordSpec::Favorite.all ) ).to eq :refined
    end

    it "returns original if no block" do
      refinement = Shamu::Security::PolicyRefinement.new(
        [ :read ],
        ActiveRecordSpec::Favorite,
        nil
      )
      relation = ActiveRecordSpec::Favorite.all
      expect( refinement.apply( relation, nil ) ).to be relation
    end

    it "returns original if block returns falsy value" do
      refinement = Shamu::Security::PolicyRefinement.new(
        [ :read ],
        ActiveRecordSpec::Favorite,
        ->( _, _ ) {}
      )
      relation = ActiveRecordSpec::Favorite.all
      expect( refinement.apply( relation, nil ) ).to be relation
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
shamu-0.0.24 spec/lib/shamu/security/policy_refinement_spec.rb
shamu-0.0.21 spec/lib/shamu/security/policy_refinement_spec.rb
shamu-0.0.20 spec/lib/shamu/security/policy_refinement_spec.rb
shamu-0.0.19 spec/lib/shamu/security/policy_refinement_spec.rb
shamu-0.0.18 spec/lib/shamu/security/policy_refinement_spec.rb
shamu-0.0.17 spec/lib/shamu/security/policy_refinement_spec.rb
shamu-0.0.15 spec/lib/shamu/security/policy_refinement_spec.rb
shamu-0.0.14 spec/lib/shamu/security/policy_refinement_spec.rb
shamu-0.0.13 spec/lib/shamu/security/policy_refinement_spec.rb
shamu-0.0.11 spec/lib/shamu/security/policy_refinement_spec.rb
shamu-0.0.9 spec/lib/shamu/security/policy_refinement_spec.rb
shamu-0.0.8 spec/lib/shamu/security/policy_refinement_spec.rb
shamu-0.0.7 spec/lib/shamu/security/policy_refinement_spec.rb
shamu-0.0.5 spec/lib/shamu/security/policy_refinement_spec.rb
shamu-0.0.4 spec/lib/shamu/security/policy_refinement_spec.rb
shamu-0.0.3 spec/lib/shamu/security/policy_refinement_spec.rb
shamu-0.0.2 spec/lib/shamu/security/policy_refinement_spec.rb