2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2019-03-13 23:51:22 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
require 'lib/mixin/has_backends_examples'
|
|
|
|
|
|
|
|
RSpec.describe ObjectManager::Attribute::Validation, application_handle: 'application_server' do
|
|
|
|
it_behaves_like 'Mixin::HasBackends'
|
|
|
|
|
|
|
|
it 'is a ActiveModel::Validator' do
|
|
|
|
expect(described_class).to be < ActiveModel::Validator
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#validate' do
|
|
|
|
|
|
|
|
subject { described_class.new }
|
2019-04-15 01:41:17 +00:00
|
|
|
|
2019-03-13 23:51:22 +00:00
|
|
|
let(:record) { build(:user) }
|
|
|
|
let(:backend) { spy }
|
|
|
|
|
|
|
|
around do |example|
|
|
|
|
original_backends = described_class.backends.dup
|
|
|
|
begin
|
|
|
|
example.run
|
|
|
|
ensure
|
|
|
|
described_class.backends = original_backends
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
described_class.backends = Set[backend]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sends .validate to backends' do
|
|
|
|
subject.validate(record)
|
|
|
|
expect(backend).to have_received(:validate).with(record: record, attribute: instance_of(::ObjectManager::Attribute)).at_least(:once)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for cached ObjectManager::Attribute records' do
|
|
|
|
|
|
|
|
it 'fetches current records when in memory Cache is blank' do
|
|
|
|
allow(::ObjectManager::Attribute).to receive(:where).and_call_original
|
|
|
|
subject.validate(record)
|
|
|
|
expect(::ObjectManager::Attribute).to have_received(:where).twice
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't fetch current records when in memory Cache is valid" do
|
|
|
|
subject.validate(record)
|
|
|
|
|
|
|
|
allow(::ObjectManager::Attribute).to receive(:where).and_call_original
|
|
|
|
subject.validate(record)
|
|
|
|
expect(::ObjectManager::Attribute).to have_received(:where).once
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fetches current records when in memory Cache is outdated' do
|
|
|
|
subject.validate(record)
|
|
|
|
|
|
|
|
::ObjectManager::Attribute.last.touch
|
|
|
|
|
|
|
|
allow(::ObjectManager::Attribute).to receive(:where).and_call_original
|
|
|
|
subject.validate(record)
|
|
|
|
expect(::ObjectManager::Attribute).to have_received(:where).twice
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no validation is performed' do
|
|
|
|
|
|
|
|
it 'is skipped because of irrelevant ApplicationHandleInfo', application_handle: 'non_application_server' do
|
|
|
|
subject.validate(record)
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(backend).not_to have_received(:validate)
|
2019-03-13 23:51:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is skipped because of import_mode is active' do
|
2020-10-22 13:57:01 +00:00
|
|
|
allow(Setting).to receive(:get).with('import_mode').and_return(true)
|
2019-03-13 23:51:22 +00:00
|
|
|
subject.validate(record)
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(backend).not_to have_received(:validate)
|
2019-03-13 23:51:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is skipped because of unchanged attributes' do
|
|
|
|
record.save!
|
|
|
|
RSpec::Mocks.space.proxy_for(backend).reset
|
|
|
|
subject.validate(record)
|
|
|
|
expect(backend).not_to have_received(:validate)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when caused by ObjectManager::Attribute records' do
|
|
|
|
|
|
|
|
it 'is skipped because no custom attributes are present' do
|
|
|
|
::ObjectManager::Attribute.update(editable: false)
|
|
|
|
subject.validate(record)
|
|
|
|
expect(backend).not_to have_received(:validate)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is skipped because no active attributes are present' do
|
|
|
|
::ObjectManager::Attribute.update(active: false)
|
|
|
|
subject.validate(record)
|
|
|
|
expect(backend).not_to have_received(:validate)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-08-20 03:36:30 +00:00
|
|
|
|
|
|
|
context 'when custom attribute exists' do
|
|
|
|
before do
|
|
|
|
allow(subject).to receive(:attributes_unchanged?) # rubocop:disable RSpec/SubjectStub
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'runs validation in default context' do
|
|
|
|
ApplicationHandleInfo.in_context(nil) do
|
|
|
|
subject.validate(record)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(subject).to have_received(:attributes_unchanged?) # rubocop:disable RSpec/SubjectStub
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not run validations in contexts that do not use custom attributes' do
|
|
|
|
ApplicationHandleInfo.in_context('merge') do
|
|
|
|
subject.validate(record)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(subject).not_to have_received(:attributes_unchanged?) # rubocop:disable RSpec/SubjectStub
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#validation_needed' do
|
|
|
|
it 'runs validation in default context' do
|
|
|
|
ApplicationHandleInfo.in_context(nil) do
|
|
|
|
expect(subject.send(:validation_needed?)).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not run validations in contexts that do not use custom attributes' do
|
|
|
|
ApplicationHandleInfo.in_context('merge') do
|
|
|
|
expect(subject.send(:validation_needed?)).to be false
|
|
|
|
end
|
|
|
|
end
|
2019-03-13 23:51:22 +00:00
|
|
|
end
|
|
|
|
end
|