link_uniqueness_validator_spec.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Validations::LinkUniquenessValidator do
  4. let(:instance) { described_class.new }
  5. shared_examples 'adds an error' do
  6. it 'adds an error' do
  7. instance.validate(record)
  8. expect(record.errors.full_messages).to include('Link already exists')
  9. end
  10. end
  11. shared_examples 'does not add an error' do
  12. it 'does not add an error' do
  13. instance.validate(record)
  14. expect(record.errors).to be_blank
  15. end
  16. end
  17. context 'when creating a new link' do
  18. let(:record) { build(:link) }
  19. context 'when an unrelated link exists' do
  20. before do
  21. create(:link, from: create(:ticket), to: create(:ticket), link_type: 'other')
  22. end
  23. include_examples 'does not add an error'
  24. end
  25. context 'when an identical link exists' do
  26. before { create(:link) }
  27. include_examples 'adds an error'
  28. end
  29. context 'when a partially identical link exists' do
  30. before { create(:link, link_type: 'other') }
  31. include_examples 'does not add an error'
  32. end
  33. end
  34. context 'when editing an existing link' do
  35. let(:record) { create(:link) }
  36. context 'when an unrelated link exists' do
  37. before do
  38. create(:link, from: create(:ticket), to: create(:ticket), link_type: 'other')
  39. end
  40. include_examples 'does not add an error'
  41. end
  42. context 'when an identical link exists' do
  43. before do
  44. create(:link, link_type: 'target')
  45. record.link_type = Link::Type.create_if_not_exists(name: 'target', active: true)
  46. end
  47. include_examples 'adds an error'
  48. end
  49. context 'when a partially identical link exists' do
  50. before do
  51. create(:link, link_type: 'other')
  52. record.link_type = Link::Type.create_if_not_exists(name: 'target', active: true)
  53. end
  54. include_examples 'does not add an error'
  55. end
  56. end
  57. end