123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- require 'rails_helper'
- require 'lib/mixin/has_backends_examples'
- RSpec.describe Validations::ObjectManager::AttributeValidator, application_handle: 'application_server' do
- subject(:validator) { described_class.new }
- it_behaves_like 'Mixin::HasBackends'
- it 'is a ActiveModel::Validator' do
- expect(described_class).to be < ActiveModel::Validator
- end
- describe '#validate' do
- 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
- validator.validate(record)
- expect(backend).to have_received(:validate).with(record: record, attribute: instance_of(ObjectManager::Attribute)).at_least(:once)
- end
- context 'with 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
- validator.validate(record)
- expect(ObjectManager::Attribute).to have_received(:where).twice
- end
- it "doesn't fetch current records when in memory Cache is valid" do
- validator.validate(record)
- allow(ObjectManager::Attribute).to receive(:where).and_call_original
- validator.validate(record)
- expect(ObjectManager::Attribute).to have_received(:where).once
- end
- it 'fetches current records when in memory Cache is outdated' do
- validator.validate(record)
- ObjectManager::Attribute.last.touch
- allow(ObjectManager::Attribute).to receive(:where).and_call_original
- validator.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
- validator.validate(record)
- expect(backend).not_to have_received(:validate)
- end
- it 'is skipped because of import_mode is active' do
- allow(Setting).to receive(:get).with('import_mode').and_return(true)
- validator.validate(record)
- expect(backend).not_to have_received(:validate)
- end
- it 'is skipped because of unchanged attributes' do
- record.save!
- RSpec::Mocks.space.proxy_for(backend).reset
- validator.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)
- validator.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)
- validator.validate(record)
- expect(backend).not_to have_received(:validate)
- end
- end
- end
- context 'when custom attribute exists' do
- before do
- allow(validator).to receive(:attributes_unchanged?)
- end
- it 'runs validation in default context' do
- ApplicationHandleInfo.in_context(nil) do
- validator.validate(record)
- end
- expect(validator).to have_received(:attributes_unchanged?)
- end
- it 'does not run validations in contexts that do not use custom attributes' do
- ApplicationHandleInfo.in_context('merge') do
- validator.validate(record)
- end
- expect(validator).not_to have_received(:attributes_unchanged?)
- end
- end
- end
- describe '#validation_needed' do
- it 'runs validation in default context' do
- ApplicationHandleInfo.in_context(nil) do
- expect(validator.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(validator.send(:validation_needed?)).to be false
- end
- end
- end
- end
|