2020-04-23 05:09:46 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe DeclarativePolicy do
|
2020-04-23 05:09:46 -04:00
|
|
|
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
|
2020-05-17 20:08:13 -04:00
|
|
|
before do
|
|
|
|
stub_const('Foo', Class.new)
|
|
|
|
stub_const('FooPolicy', Class.new)
|
|
|
|
end
|
2020-04-23 05:09:46 -04:00
|
|
|
|
|
|
|
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
|