Added tests to check if user want to get notifications based on own notification settings.

This commit is contained in:
Martin Edenhofer 2016-02-27 12:48:44 +01:00
parent 77e61c3631
commit 4426e83643
3 changed files with 328 additions and 71 deletions

View file

@ -4,9 +4,9 @@ class Observer::Ticket::Notification::BackgroundJob
def initialize(params, via_web = false)
=begin
:type => 'update',
:ticket_id => 123,
:changes => {
type: 'update',
ticket_id: 123,
changes: {
'attribute1' => [before,now],
'attribute2' => [before,now],
}
@ -59,55 +59,11 @@ class Observer::Ticket::Notification::BackgroundJob
end
already_checked_recipient_ids = {}
possible_recipients.each {|user|
next if already_checked_recipient_ids[user.id]
already_checked_recipient_ids[user.id] = true
next if !user.preferences
next if !user.preferences['notification_config']
matrix = user.preferences['notification_config']['matrix']
if ticket.owner_id != user.id
if user.preferences['notification_config']['group_ids'] ||
(user.preferences['notification_config']['group_ids'].class == Array && (!user.preferences['notification_config']['group_ids'].empty? || user.preferences['notification_config']['group_ids'][0] != '-'))
hit = false
user.preferences['notification_config']['group_ids'].each {|notify_group_id|
user.group_ids.each {|local_group_id|
if local_group_id.to_s == notify_group_id.to_s
hit = true
end
}
}
next if !hit
end
end
next if !matrix
next if !matrix[@p[:type]]
data = matrix[@p[:type]]
next if !data
next if !data['criteria']
channels = data['channel']
next if !channels
if data['criteria']['owned_by_me'] && ticket.owner_id == user.id
data = {
user: user,
channels: channels
}
recipients_and_channels.push data
next
end
if data['criteria']['owned_by_nobody'] && ticket.owner_id == 1
data = {
user: user,
channels: channels
}
recipients_and_channels.push data
next
end
next unless data['criteria']['no']
data = {
user: user,
channels: channels
}
recipients_and_channels.push data
next
result = NotificationFactory.notification_settings(user, ticket, @p[:type])
next if !result
next if already_checked_recipient_ids[result[:user].id]
already_checked_recipient_ids[result[:user].id] = true
recipients_and_channels.push result
}
# send notifications

View file

@ -2,6 +2,80 @@ module NotificationFactory
=begin
get notification settings for user and notification type
result = NotificationFactory.notification_settings(user, ticket, type)
type: create | update | reminder_reached | pending
returns
{
user: user,
channels: {
online: true,
email: true,
},
}
=end
def self.notification_settings(user, ticket, type)
return if !user.preferences
return if !user.preferences['notification_config']
matrix = user.preferences['notification_config']['matrix']
return if !matrix
# check if group is in selecd groups
if ticket.owner_id != user.id
selected_group_ids = user.preferences['notification_config']['group_ids']
if selected_group_ids
if selected_group_ids.class == Array
hit = nil
if selected_group_ids.empty?
hit = true
elsif selected_group_ids[0] == '-' && selected_group_ids.count == 1
hit = true
else
hit = false
selected_group_ids.each {|selected_group_id|
if selected_group_id.to_s == ticket.group_id.to_s
hit = true
break
end
}
end
return if !hit
end
end
end
return if !matrix[type]
data = matrix[type]
return if !data
return if !data['criteria']
channels = data['channel']
return if !channels
if data['criteria']['owned_by_me'] && ticket.owner_id == user.id
return {
user: user,
channels: channels
}
end
if data['criteria']['owned_by_nobody'] && ticket.owner_id == 1
return {
user: user,
channels: channels
}
end
return if !data['criteria']['no']
{
user: user,
channels: channels
}
end
=begin
# deprecated, will be removed with 2.0
result_string = NotificationFactory.build(

View file

@ -466,4 +466,231 @@ next line, Group: Users',
end
test 'notifications settings' do
groups = Group.all
roles = Role.where(name: 'Agent')
agent1 = User.create_or_update(
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_or_update(
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_or_update(
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
result = NotificationFactory.notification_settings(agent1, ticket1, 'create')
assert_equal(true, result[:channels][:online])
assert_equal(true, result[:channels][:email])
result = NotificationFactory.notification_settings(agent1, ticket2, 'create')
assert_equal(true, result[:channels][:online])
assert_equal(true, result[:channels][:email])
result = NotificationFactory.notification_settings(agent1, ticket3, 'create')
assert_equal(true, result[:channels][:online])
assert_equal(true, result[:channels][:email])
result = NotificationFactory.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
result = NotificationFactory.notification_settings(agent2, ticket1, 'create')
assert_equal(true, result[:channels][:online])
assert_equal(true, result[:channels][:email])
result = NotificationFactory.notification_settings(agent2, ticket2, 'create')
assert_equal(nil, result)
result = NotificationFactory.notification_settings(agent2, ticket3, 'create')
assert_equal(true, result[:channels][:online])
assert_equal(true, result[:channels][:email])
result = NotificationFactory.notification_settings(agent2, ticket4, 'create')
assert_equal(nil, result)
# no group selection
agent1.preferences[:notification_config][:group_ids] = []
agent1.save
result = NotificationFactory.notification_settings(agent1, ticket1, 'create')
assert_equal(true, result[:channels][:online])
assert_equal(true, result[:channels][:email])
result = NotificationFactory.notification_settings(agent1, ticket2, 'create')
assert_equal(true, result[:channels][:online])
assert_equal(true, result[:channels][:email])
result = NotificationFactory.notification_settings(agent1, ticket3, 'create')
assert_equal(true, result[:channels][:online])
assert_equal(true, result[:channels][:email])
result = NotificationFactory.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
result = NotificationFactory.notification_settings(agent2, ticket1, 'create')
assert_equal(true, result[:channels][:online])
assert_equal(true, result[:channels][:email])
result = NotificationFactory.notification_settings(agent2, ticket2, 'create')
assert_equal(nil, result)
result = NotificationFactory.notification_settings(agent2, ticket3, 'create')
assert_equal(true, result[:channels][:online])
assert_equal(true, result[:channels][:email])
result = NotificationFactory.notification_settings(agent2, ticket4, 'create')
assert_equal(nil, result)
agent1.preferences[:notification_config][:group_ids] = ['-']
agent1.save
result = NotificationFactory.notification_settings(agent1, ticket1, 'create')
assert_equal(true, result[:channels][:online])
assert_equal(true, result[:channels][:email])
result = NotificationFactory.notification_settings(agent1, ticket2, 'create')
assert_equal(true, result[:channels][:online])
assert_equal(true, result[:channels][:email])
result = NotificationFactory.notification_settings(agent1, ticket3, 'create')
assert_equal(true, result[:channels][:online])
assert_equal(true, result[:channels][:email])
result = NotificationFactory.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
result = NotificationFactory.notification_settings(agent2, ticket1, 'create')
assert_equal(true, result[:channels][:online])
assert_equal(true, result[:channels][:email])
result = NotificationFactory.notification_settings(agent2, ticket2, 'create')
assert_equal(nil, result)
result = NotificationFactory.notification_settings(agent2, ticket3, 'create')
assert_equal(true, result[:channels][:online])
assert_equal(true, result[:channels][:email])
result = NotificationFactory.notification_settings(agent2, ticket4, 'create')
assert_equal(nil, result)
# dedecated group selection
agent1.preferences[:notification_config][:group_ids] = [Group.lookup(name: 'Users').id]
agent1.save
result = NotificationFactory.notification_settings(agent1, ticket1, 'create')
assert_equal(true, result[:channels][:online])
assert_equal(true, result[:channels][:email])
result = NotificationFactory.notification_settings(agent1, ticket2, 'create')
assert_equal(true, result[:channels][:online])
assert_equal(true, result[:channels][:email])
result = NotificationFactory.notification_settings(agent1, ticket3, 'create')
assert_equal(nil, result)
result = NotificationFactory.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
result = NotificationFactory.notification_settings(agent2, ticket1, 'create')
assert_equal(true, result[:channels][:online])
assert_equal(true, result[:channels][:email])
result = NotificationFactory.notification_settings(agent2, ticket2, 'create')
assert_equal(nil, result)
result = NotificationFactory.notification_settings(agent2, ticket3, 'create')
assert_equal(nil, result)
assert_equal(nil, result)
result = NotificationFactory.notification_settings(agent2, ticket4, 'create')
assert_equal(nil, result)
end
end