summaryrefslogtreecommitdiff
path: root/spec/lib/declarative_policy_spec.rb
blob: 5fdb3c277383ff2ae19e0786153f87e0e74d3a1b (plain)
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
# frozen_string_literal: true

require 'spec_helper'

describe DeclarativePolicy do
  describe '.class_for' do
    it 'uses declarative_policy_class if present' do
      instance = Gitlab::ErrorTracking::ErrorEvent.new

      expect(described_class.class_for(instance)).to eq(ErrorTracking::BasePolicy)
    end

    it 'infers policy class from name' do
      instance = PersonalSnippet.new

      expect(described_class.class_for(instance)).to eq(PersonalSnippetPolicy)
    end

    it 'raises error if not found' do
      instance = Object.new

      expect { described_class.class_for(instance) }.to raise_error('no policy for Object')
    end

    context 'when found policy class does not inherit base' do
      before do
        stub_const('Foo', Class.new)
        stub_const('FooPolicy', Class.new)
      end

      it 'raises error if inferred class does not inherit Base' do
        instance = Foo.new

        expect { described_class.class_for(instance) }.to raise_error('no policy for Foo')
      end
    end
  end
end