attribute_validator_spec.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'lib/mixin/has_backends_examples'
  4. RSpec.describe Validations::ObjectManager::AttributeValidator, application_handle: 'application_server' do
  5. subject(:validator) { described_class.new }
  6. it_behaves_like 'Mixin::HasBackends'
  7. it 'is a ActiveModel::Validator' do
  8. expect(described_class).to be < ActiveModel::Validator
  9. end
  10. describe '#validate' do
  11. let(:record) { build(:user) }
  12. let(:backend) { spy }
  13. around do |example|
  14. original_backends = described_class.backends.dup
  15. begin
  16. example.run
  17. ensure
  18. described_class.backends = original_backends
  19. end
  20. end
  21. before do
  22. described_class.backends = Set[backend]
  23. end
  24. it 'sends .validate to backends' do
  25. validator.validate(record)
  26. expect(backend).to have_received(:validate).with(record: record, attribute: instance_of(ObjectManager::Attribute)).at_least(:once)
  27. end
  28. context 'with cached ObjectManager::Attribute records' do
  29. it 'fetches current records when in memory Cache is blank' do
  30. allow(ObjectManager::Attribute).to receive(:where).and_call_original
  31. validator.validate(record)
  32. expect(ObjectManager::Attribute).to have_received(:where).twice
  33. end
  34. it "doesn't fetch current records when in memory Cache is valid" do
  35. validator.validate(record)
  36. allow(ObjectManager::Attribute).to receive(:where).and_call_original
  37. validator.validate(record)
  38. expect(ObjectManager::Attribute).to have_received(:where).once
  39. end
  40. it 'fetches current records when in memory Cache is outdated' do
  41. validator.validate(record)
  42. ObjectManager::Attribute.last.touch
  43. allow(ObjectManager::Attribute).to receive(:where).and_call_original
  44. validator.validate(record)
  45. expect(ObjectManager::Attribute).to have_received(:where).twice
  46. end
  47. end
  48. context 'when no validation is performed' do
  49. it 'is skipped because of irrelevant ApplicationHandleInfo', application_handle: 'non_application_server' do
  50. validator.validate(record)
  51. expect(backend).not_to have_received(:validate)
  52. end
  53. it 'is skipped because of import_mode is active' do
  54. allow(Setting).to receive(:get).with('import_mode').and_return(true)
  55. validator.validate(record)
  56. expect(backend).not_to have_received(:validate)
  57. end
  58. it 'is skipped because of unchanged attributes' do
  59. record.save!
  60. RSpec::Mocks.space.proxy_for(backend).reset
  61. validator.validate(record)
  62. expect(backend).not_to have_received(:validate)
  63. end
  64. context 'when caused by ObjectManager::Attribute records' do
  65. it 'is skipped because no custom attributes are present' do
  66. ObjectManager::Attribute.update(editable: false)
  67. validator.validate(record)
  68. expect(backend).not_to have_received(:validate)
  69. end
  70. it 'is skipped because no active attributes are present' do
  71. ObjectManager::Attribute.update(active: false)
  72. validator.validate(record)
  73. expect(backend).not_to have_received(:validate)
  74. end
  75. end
  76. end
  77. context 'when custom attribute exists' do
  78. before do
  79. allow(validator).to receive(:attributes_unchanged?)
  80. end
  81. it 'runs validation in default context' do
  82. ApplicationHandleInfo.in_context(nil) do
  83. validator.validate(record)
  84. end
  85. expect(validator).to have_received(:attributes_unchanged?)
  86. end
  87. it 'does not run validations in contexts that do not use custom attributes' do
  88. ApplicationHandleInfo.in_context('merge') do
  89. validator.validate(record)
  90. end
  91. expect(validator).not_to have_received(:attributes_unchanged?)
  92. end
  93. end
  94. end
  95. describe '#validation_needed' do
  96. it 'runs validation in default context' do
  97. ApplicationHandleInfo.in_context(nil) do
  98. expect(validator.send(:validation_needed?)).to be true
  99. end
  100. end
  101. it 'does not run validations in contexts that do not use custom attributes' do
  102. ApplicationHandleInfo.in_context('merge') do
  103. expect(validator.send(:validation_needed?)).to be false
  104. end
  105. end
  106. end
  107. end