From 4426e836439d9f683b55afd0dd620359cf709468 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 27 Feb 2016 12:48:44 +0100 Subject: [PATCH] Added tests to check if user want to get notifications based on own notification settings. --- .../ticket/notification/background_job.rb | 60 +--- lib/notification_factory.rb | 74 +++++ test/unit/notification_factory_test.rb | 265 ++++++++++++++++-- 3 files changed, 328 insertions(+), 71 deletions(-) diff --git a/app/models/observer/ticket/notification/background_job.rb b/app/models/observer/ticket/notification/background_job.rb index 433828b4a..2c697b000 100644 --- a/app/models/observer/ticket/notification/background_job.rb +++ b/app/models/observer/ticket/notification/background_job.rb @@ -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 diff --git a/lib/notification_factory.rb b/lib/notification_factory.rb index e6db05475..7f1f6d760 100644 --- a/lib/notification_factory.rb +++ b/lib/notification_factory.rb @@ -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( diff --git a/test/unit/notification_factory_test.rb b/test/unit/notification_factory_test.rb index cb573ea86..fb80406e3 100644 --- a/test/unit/notification_factory_test.rb +++ b/test/unit/notification_factory_test.rb @@ -41,17 +41,17 @@ class NotificationFactoryTest < ActiveSupport::TestCase test 'notifications base' do ticket = Ticket.create( title: 'some title äöüß', - group: Group.lookup( name: 'Users'), + group: Group.lookup(name: 'Users'), customer_id: 2, - state: Ticket::State.lookup( name: 'new' ), - priority: Ticket::Priority.lookup( name: '2 normal' ), + state: Ticket::State.lookup(name: 'new'), + priority: Ticket::Priority.lookup(name: '2 normal'), updated_by_id: 2, created_by_id: 2, ) article_plain = Ticket::Article.create( ticket_id: ticket.id, - type_id: Ticket::Article::Type.where(name: 'phone' ).first.id, - sender_id: Ticket::Article::Sender.where(name: 'Customer' ).first.id, + type_id: Ticket::Article::Type.where(name: 'phone').first.id, + sender_id: Ticket::Article::Sender.where(name: 'Customer').first.id, from: 'Zammad Feedback ', body: 'some text', internal: false, @@ -155,7 +155,7 @@ class NotificationFactoryTest < ActiveSupport::TestCase }, locale: test[:locale] ) - assert_equal( test[:result], result, 'verify result' ) + assert_equal(test[:result], result, 'verify result') } ticket.destroy @@ -164,17 +164,17 @@ class NotificationFactoryTest < ActiveSupport::TestCase test 'notifications html' do ticket = Ticket.create( title: 'some title äöüß 2', - group: Group.lookup( name: 'Users'), + group: Group.lookup(name: 'Users'), customer_id: 2, - state: Ticket::State.lookup( name: 'new' ), - priority: Ticket::Priority.lookup( name: '2 normal' ), + state: Ticket::State.lookup(name: 'new'), + priority: Ticket::Priority.lookup(name: '2 normal'), updated_by_id: 1, created_by_id: 1, ) article_html = Ticket::Article.create( ticket_id: ticket.id, - type_id: Ticket::Article::Type.where(name: 'phone' ).first.id, - sender_id: Ticket::Article::Sender.where(name: 'Customer' ).first.id, + type_id: Ticket::Article::Type.where(name: 'phone').first.id, + sender_id: Ticket::Article::Sender.where(name: 'Customer').first.id, from: 'Zammad Feedback ', body: 'some text
next line', content_type: 'text/html', @@ -210,7 +210,7 @@ next line, Group: Users', }, locale: test[:locale] ) - assert_equal( test[:result], result, 'verify result' ) + assert_equal(test[:result], result, 'verify result') } ticket.destroy @@ -219,17 +219,17 @@ next line, Group: Users', test 'notifications attack' do ticket = Ticket.create( title: 'some title äöüß 3', - group: Group.lookup( name: 'Users'), + group: Group.lookup(name: 'Users'), customer_id: 2, - state: Ticket::State.lookup( name: 'new' ), - priority: Ticket::Priority.lookup( name: '2 normal' ), + state: Ticket::State.lookup(name: 'new'), + priority: Ticket::Priority.lookup(name: '2 normal'), updated_by_id: 1, created_by_id: 1, ) article_html = Ticket::Article.create( ticket_id: ticket.id, - type_id: Ticket::Article::Type.where(name: 'phone' ).first.id, - sender_id: Ticket::Article::Sender.where(name: 'Customer' ).first.id, + type_id: Ticket::Article::Type.where(name: 'phone').first.id, + sender_id: Ticket::Article::Sender.where(name: 'Customer').first.id, from: 'Zammad Feedback ', body: 'some text
next line', content_type: 'text/html', @@ -291,7 +291,7 @@ next line, Group: Users', }, locale: test[:locale] ) - assert_equal( test[:result], result, 'verify result' ) + assert_equal(test[:result], result, 'verify result') } ticket.destroy @@ -359,7 +359,7 @@ next line, Group: Users', ticket = 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, + owner_id: User.lookup(login: '-').id, title: 'Welcome to Zammad!', state_id: Ticket::State.lookup(name: 'new').id, priority_id: Ticket::Priority.lookup(name: '2 normal').id, @@ -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: 'Notificationxxx', + 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: 'Notificationxxx', + 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