2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2013-01-04 14:28:55 +00:00
|
|
|
require 'test_helper'
|
2014-05-27 13:22:21 +00:00
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
class NotificationFactoryMailerTest < ActiveSupport::TestCase
|
2016-01-18 01:35:07 +00:00
|
|
|
|
2015-01-07 12:11:30 +00:00
|
|
|
test 'notifications send' do
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.send(
|
2018-12-19 17:31:51 +00:00
|
|
|
recipient: User.find(2),
|
|
|
|
subject: 'some subject',
|
|
|
|
body: 'some body',
|
2015-04-27 13:42:53 +00:00
|
|
|
content_type: '',
|
2015-01-07 12:11:30 +00:00
|
|
|
)
|
|
|
|
assert_match('some body', result.to_s)
|
|
|
|
assert_match('text/plain', result.to_s)
|
|
|
|
assert_no_match('text/html', result.to_s)
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.send(
|
2018-12-19 17:31:51 +00:00
|
|
|
recipient: User.find(2),
|
|
|
|
subject: 'some subject',
|
|
|
|
body: 'some body',
|
2015-04-27 13:42:53 +00:00
|
|
|
content_type: 'text/plain',
|
2015-01-07 12:11:30 +00:00
|
|
|
)
|
|
|
|
assert_match('some body', result.to_s)
|
|
|
|
assert_match('text/plain', result.to_s)
|
|
|
|
assert_no_match('text/html', result.to_s)
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.send(
|
2018-12-19 17:31:51 +00:00
|
|
|
recipient: User.find(2),
|
|
|
|
subject: 'some subject',
|
|
|
|
body: 'some <span>body</span>',
|
2015-04-27 13:42:53 +00:00
|
|
|
content_type: 'text/html',
|
2015-01-07 12:11:30 +00:00
|
|
|
)
|
|
|
|
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)
|
2016-07-11 23:32:20 +00:00
|
|
|
|
|
|
|
attachments = []
|
|
|
|
attachments.push Store.add(
|
2018-12-19 17:31:51 +00:00
|
|
|
object: 'TestMailer',
|
|
|
|
o_id: 1,
|
|
|
|
data: 'content_file1_normally_should_be_an_image',
|
|
|
|
filename: 'some_file1.jpg',
|
|
|
|
preferences: {
|
2016-07-11 23:32:20 +00:00
|
|
|
'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.add(
|
2018-12-19 17:31:51 +00:00
|
|
|
object: 'TestMailer',
|
|
|
|
o_id: 1,
|
|
|
|
data: 'content_file2',
|
|
|
|
filename: 'some_file2.txt',
|
|
|
|
preferences: {
|
2016-07-11 23:32:20 +00:00
|
|
|
'Content-Type' => 'text/stream',
|
|
|
|
'Mime-Type' => 'text/stream',
|
|
|
|
},
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
|
|
|
|
result = NotificationFactory::Mailer.send(
|
2018-12-19 17:31:51 +00:00
|
|
|
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>',
|
2016-07-11 23:32:20 +00:00
|
|
|
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)
|
|
|
|
|
2015-01-07 12:11:30 +00:00
|
|
|
end
|
|
|
|
|
2016-02-27 11:48:44 +00:00
|
|
|
test 'notifications settings' do
|
|
|
|
|
|
|
|
groups = Group.all
|
|
|
|
roles = Role.where(name: 'Agent')
|
2018-12-31 19:15:17 +00:00
|
|
|
agent1 = User.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
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,
|
2016-02-27 11:48:44 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
|
2018-12-31 19:15:17 +00:00
|
|
|
agent2 = User.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
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,
|
2016-02-27 11:48:44 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
|
2018-12-31 19:15:17 +00:00
|
|
|
group_notification_setting = Group.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'NotificationSetting',
|
2016-02-27 11:48:44 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
|
|
|
|
ticket1 = Ticket.create(
|
2018-12-19 17:31:51 +00:00
|
|
|
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,
|
2016-02-27 11:48:44 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
|
|
|
|
ticket2 = Ticket.create(
|
2018-12-19 17:31:51 +00:00
|
|
|
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,
|
2016-02-27 11:48:44 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
|
|
|
|
ticket3 = Ticket.create(
|
2018-12-19 17:31:51 +00:00
|
|
|
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,
|
2016-02-27 11:48:44 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
|
|
|
|
ticket4 = Ticket.create(
|
2018-12-19 17:31:51 +00:00
|
|
|
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,
|
2016-02-27 11:48:44 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
|
|
|
|
agent1.preferences[:notification_config][:group_ids] = nil
|
|
|
|
agent1.save
|
2020-01-17 16:13:30 +00:00
|
|
|
travel 30.seconds
|
2016-02-27 11:48:44 +00:00
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent1, ticket1, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent1, ticket2, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent1, ticket3, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent1, ticket4, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
|
|
|
agent2.preferences[:notification_config][:group_ids] = nil
|
|
|
|
agent2.save
|
2020-01-17 16:13:30 +00:00
|
|
|
travel 30.seconds
|
2016-02-27 11:48:44 +00:00
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent2, ticket1, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent2, ticket2, 'create')
|
2017-03-24 10:02:13 +00:00
|
|
|
assert_nil(result)
|
2016-02-27 11:48:44 +00:00
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent2, ticket3, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent2, ticket4, 'create')
|
2017-03-24 10:02:13 +00:00
|
|
|
assert_nil(result)
|
2016-02-27 11:48:44 +00:00
|
|
|
|
|
|
|
# no group selection
|
|
|
|
agent1.preferences[:notification_config][:group_ids] = []
|
|
|
|
agent1.save
|
2020-01-17 16:13:30 +00:00
|
|
|
travel 30.seconds
|
2016-02-27 11:48:44 +00:00
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent1, ticket1, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent1, ticket2, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent1, ticket3, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent1, ticket4, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
|
|
|
agent2.preferences[:notification_config][:group_ids] = []
|
|
|
|
agent2.save
|
2020-01-17 16:13:30 +00:00
|
|
|
travel 30.seconds
|
2016-02-27 11:48:44 +00:00
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent2, ticket1, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent2, ticket2, 'create')
|
2017-03-24 10:02:13 +00:00
|
|
|
assert_nil(result)
|
2016-02-27 11:48:44 +00:00
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent2, ticket3, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent2, ticket4, 'create')
|
2017-03-24 10:02:13 +00:00
|
|
|
assert_nil(result)
|
2016-02-27 11:48:44 +00:00
|
|
|
|
|
|
|
agent1.preferences[:notification_config][:group_ids] = ['-']
|
|
|
|
agent1.save
|
2020-01-17 16:13:30 +00:00
|
|
|
travel 30.seconds
|
2016-02-27 11:48:44 +00:00
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent1, ticket1, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent1, ticket2, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent1, ticket3, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent1, ticket4, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
|
|
|
agent2.preferences[:notification_config][:group_ids] = ['-']
|
|
|
|
agent2.save
|
2020-01-17 16:13:30 +00:00
|
|
|
travel 30.seconds
|
2016-02-27 11:48:44 +00:00
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent2, ticket1, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent2, ticket2, 'create')
|
2017-03-24 10:02:13 +00:00
|
|
|
assert_nil(result)
|
2016-02-27 11:48:44 +00:00
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent2, ticket3, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent2, ticket4, 'create')
|
2017-03-24 10:02:13 +00:00
|
|
|
assert_nil(result)
|
2016-02-27 11:48:44 +00:00
|
|
|
|
|
|
|
# dedecated group selection
|
|
|
|
agent1.preferences[:notification_config][:group_ids] = [Group.lookup(name: 'Users').id]
|
|
|
|
agent1.save
|
2020-01-17 16:13:30 +00:00
|
|
|
travel 30.seconds
|
2016-02-27 11:48:44 +00:00
|
|
|
|
2021-07-02 11:46:37 +00:00
|
|
|
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.
|
|
|
|
Cache.clear
|
|
|
|
end
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent1, ticket1, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent1, ticket2, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent1, ticket3, 'create')
|
2017-03-24 10:02:13 +00:00
|
|
|
assert_nil(result)
|
2016-02-27 11:48:44 +00:00
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent1, ticket4, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
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
|
2020-01-17 16:13:30 +00:00
|
|
|
travel 30.seconds
|
2016-02-27 11:48:44 +00:00
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent2, ticket1, 'create')
|
2016-02-27 11:48:44 +00:00
|
|
|
assert_equal(true, result[:channels][:online])
|
|
|
|
assert_equal(true, result[:channels][:email])
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent2, ticket2, 'create')
|
2017-03-24 10:02:13 +00:00
|
|
|
assert_nil(result)
|
2016-02-27 11:48:44 +00:00
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent2, ticket3, 'create')
|
2017-03-24 10:02:13 +00:00
|
|
|
assert_nil(result)
|
|
|
|
assert_nil(result)
|
2016-02-27 11:48:44 +00:00
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
result = NotificationFactory::Mailer.notification_settings(agent2, ticket4, 'create')
|
2017-03-24 10:02:13 +00:00
|
|
|
assert_nil(result)
|
2016-02-27 11:48:44 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|