123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
- require 'test_helper'
- class NotificationFactoryMailerTest < ActiveSupport::TestCase
- test 'notifications send' do
- result = NotificationFactory::Mailer.send(
- recipient: User.find(2),
- subject: 'some subject',
- body: 'some body',
- content_type: '',
- )
- assert_match('some body', result.to_s)
- assert_match('text/plain', result.to_s)
- assert_no_match('text/html', result.to_s)
- result = NotificationFactory::Mailer.send(
- recipient: User.find(2),
- subject: 'some subject',
- body: 'some body',
- content_type: 'text/plain',
- )
- assert_match('some body', result.to_s)
- assert_match('text/plain', result.to_s)
- assert_no_match('text/html', result.to_s)
- result = NotificationFactory::Mailer.send(
- recipient: User.find(2),
- subject: 'some subject',
- body: 'some <span>body</span>',
- content_type: 'text/html',
- )
- assert_match('some body', result.to_s)
- assert_match('text/plain', result.to_s)
- assert_match('<span>body</span>', result.to_s)
- assert_match('text/html', result.to_s)
- attachments = []
- attachments.push Store.create!(
- object: 'TestMailer',
- o_id: 1,
- data: 'content_file1_normally_should_be_an_image',
- filename: 'some_file1.jpg',
- preferences: {
- 'Content-Type' => 'image/jpeg',
- 'Mime-Type' => 'image/jpeg',
- 'Content-ID' => '15.274327094.140938@zammad.example.com',
- 'Content-Disposition' => 'inline'
- },
- created_by_id: 1,
- )
- attachments.push Store.create!(
- object: 'TestMailer',
- o_id: 1,
- data: 'content_file2',
- filename: 'some_file2.txt',
- preferences: {
- 'Content-Type' => 'text/stream',
- 'Mime-Type' => 'text/stream',
- },
- created_by_id: 1,
- )
- result = NotificationFactory::Mailer.send(
- recipient: User.find(2),
- subject: 'some subject',
- body: 'some <span>body</span><img style="width: 85.5px; height: 49.5px" src="cid:15.274327094.140938@zammad.example.com">asdasd<br>',
- content_type: 'text/html',
- attachments: attachments,
- )
- assert_match('some body', result.to_s)
- assert_match('text/plain', result.to_s)
- assert_match('<span>body</span>', result.to_s)
- assert_match('text/html', result.to_s)
- assert_match('Content-Type: image/jpeg', result.to_s)
- assert_match('Content-Disposition: inline', result.to_s)
- assert_match('Content-ID: <15.274327094.140938@zammad.example.com>', result.to_s)
- assert_match('text/stream', result.to_s)
- assert_match('some_file2.txt', result.to_s)
- end
- test 'notifications settings' do
- groups = Group.all
- roles = Role.where(name: 'Agent')
- agent1 = User.create!(
- login: 'notification-settings-agent1@example.com',
- firstname: 'Notification<b>xxx</b>',
- lastname: 'Agent1',
- email: 'notification-settings-agent1@example.com',
- password: 'agentpw',
- active: true,
- roles: roles,
- groups: groups,
- updated_by_id: 1,
- created_by_id: 1,
- )
- agent2 = User.create!(
- login: 'notification-settings-agent2@example.com',
- firstname: 'Notification<b>xxx</b>',
- lastname: 'Agent2',
- email: 'notification-settings-agent2@example.com',
- password: 'agentpw',
- active: true,
- roles: roles,
- groups: groups,
- updated_by_id: 1,
- created_by_id: 1,
- )
- group_notification_setting = Group.create!(
- name: 'NotificationSetting',
- updated_by_id: 1,
- created_by_id: 1,
- )
- ticket1 = Ticket.create(
- group_id: Group.lookup(name: 'Users').id,
- customer_id: User.lookup(email: 'nicole.braun@zammad.org').id,
- owner_id: User.lookup(login: '-').id,
- title: 'Notification Settings Test 1!',
- state_id: Ticket::State.lookup(name: 'new').id,
- priority_id: Ticket::Priority.lookup(name: '2 normal').id,
- updated_by_id: 1,
- created_by_id: 1,
- )
- ticket2 = Ticket.create(
- group_id: Group.lookup(name: 'Users').id,
- customer_id: User.lookup(email: 'nicole.braun@zammad.org').id,
- owner_id: agent1.id,
- title: 'Notification Settings Test 2!',
- state_id: Ticket::State.lookup(name: 'new').id,
- priority_id: Ticket::Priority.lookup(name: '2 normal').id,
- updated_by_id: 1,
- created_by_id: 1,
- )
- ticket3 = Ticket.create(
- group_id: group_notification_setting.id,
- customer_id: User.lookup(email: 'nicole.braun@zammad.org').id,
- owner_id: User.lookup(login: '-').id,
- title: 'Notification Settings Test 1!',
- state_id: Ticket::State.lookup(name: 'new').id,
- priority_id: Ticket::Priority.lookup(name: '2 normal').id,
- updated_by_id: 1,
- created_by_id: 1,
- )
- ticket4 = Ticket.create(
- group_id: group_notification_setting.id,
- customer_id: User.lookup(email: 'nicole.braun@zammad.org').id,
- owner_id: agent1.id,
- title: 'Notification Settings Test 2!',
- state_id: Ticket::State.lookup(name: 'new').id,
- priority_id: Ticket::Priority.lookup(name: '2 normal').id,
- updated_by_id: 1,
- created_by_id: 1,
- )
- agent1.preferences[:notification_config][:group_ids] = nil
- agent1.save
- travel 30.seconds
- result = NotificationFactory::Mailer.notification_settings(agent1, ticket1, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory::Mailer.notification_settings(agent1, ticket2, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory::Mailer.notification_settings(agent1, ticket3, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory::Mailer.notification_settings(agent1, ticket4, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- agent2.preferences[:notification_config][:group_ids] = nil
- agent2.save
- travel 30.seconds
- result = NotificationFactory::Mailer.notification_settings(agent2, ticket1, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory::Mailer.notification_settings(agent2, ticket2, 'create')
- assert_nil(result)
- result = NotificationFactory::Mailer.notification_settings(agent2, ticket3, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory::Mailer.notification_settings(agent2, ticket4, 'create')
- assert_nil(result)
- # no group selection
- agent1.preferences[:notification_config][:group_ids] = []
- agent1.save
- travel 30.seconds
- result = NotificationFactory::Mailer.notification_settings(agent1, ticket1, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory::Mailer.notification_settings(agent1, ticket2, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory::Mailer.notification_settings(agent1, ticket3, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory::Mailer.notification_settings(agent1, ticket4, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- agent2.preferences[:notification_config][:group_ids] = []
- agent2.save
- travel 30.seconds
- result = NotificationFactory::Mailer.notification_settings(agent2, ticket1, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory::Mailer.notification_settings(agent2, ticket2, 'create')
- assert_nil(result)
- result = NotificationFactory::Mailer.notification_settings(agent2, ticket3, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory::Mailer.notification_settings(agent2, ticket4, 'create')
- assert_nil(result)
- agent1.preferences[:notification_config][:group_ids] = ['-']
- agent1.save
- travel 30.seconds
- result = NotificationFactory::Mailer.notification_settings(agent1, ticket1, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory::Mailer.notification_settings(agent1, ticket2, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory::Mailer.notification_settings(agent1, ticket3, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory::Mailer.notification_settings(agent1, ticket4, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- agent2.preferences[:notification_config][:group_ids] = ['-']
- agent2.save
- travel 30.seconds
- result = NotificationFactory::Mailer.notification_settings(agent2, ticket1, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory::Mailer.notification_settings(agent2, ticket2, 'create')
- assert_nil(result)
- result = NotificationFactory::Mailer.notification_settings(agent2, ticket3, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory::Mailer.notification_settings(agent2, ticket4, 'create')
- assert_nil(result)
- # dedecated group selection
- agent1.preferences[:notification_config][:group_ids] = [Group.lookup(name: 'Users').id]
- agent1.save
- travel 30.seconds
- if Rails.application.config.cache_store.first.eql? :mem_cache_store
- # External memcached does not support time travel, so clear the cache to avoid an outdated match.
- Rails.cache.clear
- end
- result = NotificationFactory::Mailer.notification_settings(agent1, ticket1, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory::Mailer.notification_settings(agent1, ticket2, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory::Mailer.notification_settings(agent1, ticket3, 'create')
- assert_nil(result)
- result = NotificationFactory::Mailer.notification_settings(agent1, ticket4, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- agent2.preferences[:notification_config][:group_ids] = [Group.lookup(name: 'Users').id]
- agent2.save
- travel 30.seconds
- result = NotificationFactory::Mailer.notification_settings(agent2, ticket1, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory::Mailer.notification_settings(agent2, ticket2, 'create')
- assert_nil(result)
- result = NotificationFactory::Mailer.notification_settings(agent2, ticket3, 'create')
- assert_nil(result)
- assert_nil(result)
- result = NotificationFactory::Mailer.notification_settings(agent2, ticket4, 'create')
- assert_nil(result)
- end
- end
|