email_address_spec.rb 2.2 KB

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