2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2019-02-07 14:46:16 +00:00
|
|
|
require 'rails_helper'
|
2020-02-20 13:34:03 +00:00
|
|
|
require 'models/concerns/has_collection_update_examples'
|
2021-04-12 09:49:26 +00:00
|
|
|
require 'models/concerns/has_xss_sanitized_note_examples'
|
2019-02-07 14:46:16 +00:00
|
|
|
|
|
|
|
RSpec.describe EmailAddress, type: :model do
|
|
|
|
subject(:email_address) { create(:email_address) }
|
|
|
|
|
2020-09-30 09:07:01 +00:00
|
|
|
it_behaves_like 'HasCollectionUpdate', collection_factory: :email_address
|
2021-04-12 09:49:26 +00:00
|
|
|
it_behaves_like 'HasXssSanitizedNote', model_factory: :email_address
|
2020-09-30 09:07:01 +00:00
|
|
|
|
2019-02-07 14:46:16 +00:00
|
|
|
describe 'Attributes:' do
|
|
|
|
describe '#active' do
|
|
|
|
subject(:email_address) do
|
|
|
|
create(:email_address, channel: channel, active: active)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without a Channel association' do
|
|
|
|
let(:channel) { nil }
|
|
|
|
let(:active) { true }
|
|
|
|
|
|
|
|
it 'always returns false' do
|
|
|
|
expect(email_address.active).not_to eq(active)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a Channel association' do
|
|
|
|
let(:channel) { create(:email_channel) }
|
|
|
|
let(:active) { true }
|
|
|
|
|
|
|
|
it 'returns the value it was set to' do
|
|
|
|
expect(email_address.active).to eq(active)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Associations:' do
|
|
|
|
describe '#groups' do
|
|
|
|
let(:group) { create(:group, email_address: email_address) }
|
|
|
|
|
|
|
|
context 'when an EmailAddress is destroyed' do
|
|
|
|
it 'removes the #email_address_id from all associated Groups' do
|
|
|
|
expect { email_address.destroy }
|
|
|
|
.to change { group.reload.email_address_id }.to(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#channel' do
|
|
|
|
subject(:email_addresses) { create_list(:email_address, 2, channel: channel) }
|
2019-04-15 01:41:17 +00:00
|
|
|
|
2019-02-07 14:46:16 +00:00
|
|
|
let(:channel) { create(:channel) }
|
|
|
|
|
|
|
|
context 'when a Channel is destroyed' do
|
|
|
|
it 'removes the #channel_id from all its associated EmailAddresses' do
|
|
|
|
expect { channel.destroy }
|
|
|
|
.to change { email_addresses.map(&:reload).map(&:channel_id) }
|
|
|
|
.to([nil, nil])
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and then an identical Channel is created' do
|
|
|
|
it 'removes the #channel_id from all its associated EmailAddresses' do
|
|
|
|
channel.destroy
|
|
|
|
|
|
|
|
expect { create(:channel) }
|
|
|
|
.not_to change { email_addresses.map(&:reload).map(&:channel_id) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|