verify_perform_rules_validator_spec.rb 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. class SampleModel
  4. include ActiveModel::Validations
  5. attr_accessor :sample
  6. validates :sample, 'validations/verify_perform_rules': true
  7. end
  8. RSpec.describe Validations::VerifyPerformRulesValidator do
  9. let(:instance) { SampleModel.new }
  10. context 'when validating presence' do
  11. it 'is valid when attribute is empty' do
  12. instance.sample = {}
  13. expect(instance).to be_valid
  14. end
  15. it 'is valid when attribute is nil' do
  16. instance.sample = nil
  17. expect(instance).to be_valid
  18. end
  19. it 'is valid when attribute does not have checkable keys' do
  20. instance.sample = { test: :value }
  21. expect(instance).to be_valid
  22. end
  23. it 'is valid when required value is present' do
  24. instance.sample = { 'article.note' => { 'body' => 'a', 'subject' => 'b', 'internal' => 'c' } }
  25. expect(instance).to be_valid
  26. end
  27. it 'is invalid when required value is missing' do
  28. instance.sample = { 'article.note' => {} }
  29. instance.valid?
  30. expect(instance.errors).to have_attributes(
  31. errors: include(have_attributes(message: match(%r{The required 'sample' value for article.note, internal is missing!})))
  32. )
  33. end
  34. it 'is invalid when required value is partially missing' do
  35. instance.sample = { 'article.note' => { 'body' => 'a', 'subject' => 'b' } }
  36. instance.valid?
  37. expect(instance.errors).to have_attributes(
  38. errors: include(have_attributes(message: match(%r{The required 'sample' value for article.note, internal is missing!})))
  39. )
  40. end
  41. end
  42. context 'when validating presence with precondition' do
  43. it 'is valid when required value for specific precondition is present' do
  44. instance.sample = { 'ticket.customer_id' => { 'pre_condition' => 'specific', 'value' => '123' } }
  45. expect(instance).to be_valid
  46. end
  47. it 'is valid when required value without specific precondition is not present' do
  48. instance.sample = { 'ticket.customer_id' => { 'pre_condition' => 'current' } }
  49. expect(instance).to be_valid
  50. end
  51. it 'is invalid when required value for specific precondition is not present' do
  52. instance.sample = { 'ticket.customer_id' => { 'pre_condition' => 'specific', 'value' => '' } }
  53. instance.valid?
  54. expect(instance.errors).to have_attributes(
  55. errors: include(have_attributes(message: match(%r{The required 'sample' value for ticket.customer_id, value is missing!})))
  56. )
  57. end
  58. end
  59. end