File: base_cache_collision_spec.rb

package info (click to toggle)
ruby-declarative-policy 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 424 kB
  • sloc: ruby: 2,793; makefile: 4
file content (45 lines) | stat: -rw-r--r-- 1,220 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen_string_literal: true

RSpec.describe DeclarativePolicy::Base do
  context 'when a condition is declared in two classes' do
    let(:rules_a) do
      Class.new(described_class) do
        condition(:suitable) { subject == :a }

        rule { suitable }.enable(:ok)
      end
    end

    let(:rules_b) do
      Class.new(described_class) do
        condition(:suitable) { subject == :b }

        rule { suitable }.enable(:ok)
      end
    end

    let(:cache) { {} }

    def policy(rules, object)
      rules.new(nil, object, cache: cache)
    end

    it 'is does not overwrite the cache entries of conditions with the same name' do
      expect(policy(rules_a, :foo)).not_to be_allowed(:ok)
      expect(policy(rules_b, :foo)).not_to be_allowed(:ok)

      expect(policy(rules_a, :a)).to be_allowed(:ok)
      expect(policy(rules_b, :a)).not_to be_allowed(:ok)

      expect(policy(rules_a, :b)).not_to be_allowed(:ok)
      expect(policy(rules_b, :b)).to be_allowed(:ok)
    end

    it 'writes separate cache entries for each condition' do
      expect do
        policy(rules_a, :foo).allowed?(:ok)
        policy(rules_b, :foo).allowed?(:ok)
      end.to change { cache.size }.by(2)
    end
  end
end