email_address_spec.rb 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'models/concerns/has_collection_update_examples'
  4. require 'models/concerns/has_xss_sanitized_note_examples'
  5. require 'models/application_model/has_cache_examples'
  6. RSpec.describe EmailAddress, type: :model do
  7. subject(:email_address) { create(:email_address) }
  8. it_behaves_like 'HasCollectionUpdate', collection_factory: :email_address
  9. it_behaves_like 'HasXssSanitizedNote', model_factory: :email_address
  10. it_behaves_like 'Association clears cache', association: :groups
  11. describe 'Attributes:' do
  12. describe '#active' do
  13. subject(:email_address) do
  14. create(:email_address, channel: channel, active: active)
  15. end
  16. context 'without a Channel association' do
  17. let(:channel) { nil }
  18. let(:active) { true }
  19. it 'always returns false' do
  20. expect(email_address.active).not_to eq(active)
  21. end
  22. end
  23. context 'with a Channel association' do
  24. let(:channel) { create(:email_channel) }
  25. let(:active) { true }
  26. it 'returns the value it was set to' do
  27. expect(email_address.active).to eq(active)
  28. end
  29. end
  30. end
  31. end
  32. describe 'Associations:' do
  33. describe '#groups' do
  34. let(:group) { create(:group, email_address: email_address) }
  35. context 'when an EmailAddress is destroyed' do
  36. it 'removes the #email_address_id from all associated Groups' do
  37. expect { email_address.destroy }
  38. .to change { group.reload.email_address_id }.to(nil)
  39. end
  40. end
  41. end
  42. describe '#channel' do
  43. subject(:email_addresses) { create_list(:email_address, 2, channel: channel) }
  44. let(:channel) { create(:channel) }
  45. context 'when a Channel is destroyed' do
  46. it 'removes the #channel_id from all its associated EmailAddresses' do
  47. expect { channel.destroy }
  48. .to change { email_addresses.map { |x| x.reload.channel_id } }
  49. .to([nil, nil])
  50. end
  51. context 'and then an identical Channel is created' do
  52. it 'removes the #channel_id from all its associated EmailAddresses' do
  53. channel.destroy
  54. expect { create(:channel) }
  55. .not_to change { email_addresses.map { |x| x.reload.channel_id } }
  56. end
  57. end
  58. end
  59. end
  60. end
  61. end