Improved notification tests.

This commit is contained in:
Martin Edenhofer 2015-01-03 00:46:11 +01:00
parent b181f3aa96
commit 1e69c8f91f
3 changed files with 161 additions and 82 deletions

View file

@ -20,12 +20,19 @@ class Observer::Ticket::Notification < ActiveRecord::Observer
# get uniq objects # get uniq objects
listObjects = get_uniq_changes(list) listObjects = get_uniq_changes(list)
listObjects.each {|ticket_id, item| listObjects.each {|ticket_id, item|
ticket = Ticket.lookup( :id => ticket_id ) ticket = item[:ticket]
article = ticket.articles[-1] article = item[:article] || ticket.articles[-1]
# if create, send create message / block update messages # if create, send create message / block update messages
if item[:type] == 'create' if item[:type] == 'create'
puts 'send ticket create notify to agent' puts 'send ticket create notify to agent'
article_content = ''
if item[:article]
article_content = '<snip>
#{article.body}
</snip>'
end
send_notify( send_notify(
{ {
:event => item[:type], :event => item[:type],
@ -39,9 +46,7 @@ class Observer::Ticket::Notification < ActiveRecord::Observer
Owner: #{ticket.owner.firstname} #{ticket.owner.lastname} Owner: #{ticket.owner.firstname} #{ticket.owner.lastname}
State: i18n(#{ticket.state.name}) State: i18n(#{ticket.state.name})
<snip> ' + article_content + '
#{article.body}
</snip>
#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}/#{article.id} #{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}/#{article.id}
' '
@ -60,6 +65,13 @@ class Observer::Ticket::Notification < ActiveRecord::Observer
item[:changes].each {|key,value| item[:changes].each {|key,value|
changes = "#{key}: #{value[0]} -> #{value[1]}" changes = "#{key}: #{value[0]} -> #{value[1]}"
} }
article_content = ''
if item[:article]
article_content = '<snip>
#{article.body}
</snip>'
end
send_notify( send_notify(
{ {
:event => item[:type], :event => item[:type],
@ -72,9 +84,7 @@ class Observer::Ticket::Notification < ActiveRecord::Observer
Changes: Changes:
' + changes + ' ' + changes + '
<snip> ' + article_content + '
#{article.body}
</snip>
#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}/#{article.id} #{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}/#{article.id}
' '
@ -110,40 +120,51 @@ class Observer::Ticket::Notification < ActiveRecord::Observer
listObjects = {} listObjects = {}
events.each { |event| events.each { |event|
# get changes
# send notify
# get current state of objects # get current state of objects
#if event[:name] == 'Ticket::Article' if event[:name] == 'Ticket::Article'
# article = Ticket::Article.lookup( :id => event[:id] ) article = Ticket::Article.lookup( :id => event[:id] )
#
# # next if article is already deleted # next if article is already deleted
# next if !article next if !article
#
# ticket = article.ticket ticket = article.ticket
# #listObjects[ticket.id] = event if !listObjects[ticket.id]
#elsif event[:name] == 'Ticket' listObjects[ticket.id] = {}
if event[:name] == 'Ticket' end
listObjects[ticket.id][:article] = article
elsif event[:name] == 'Ticket'
ticket = Ticket.lookup( :id => event[:id] ) ticket = Ticket.lookup( :id => event[:id] )
# next if ticket is already deleted # next if ticket is already deleted
next if !ticket next if !ticket
article = ticket.articles[-1]
next if !article
if !listObjects[ticket.id] if !listObjects[ticket.id]
listObjects[ticket.id] = {} listObjects[ticket.id] = {}
end end
listObjects[ticket.id][:ticket] = ticket
if !listObjects[ticket.id][:type] || listObjects[ticket.id][:type] == 'update' if !listObjects[ticket.id][:type] || listObjects[ticket.id][:type] == 'update'
listObjects[ticket.id][:type] = event[:type] listObjects[ticket.id][:type] = event[:type]
end end
# merge changes
if event[:changes] if event[:changes]
listObjects[ticket.id][:changes] = event[:changes] if !listObjects[ticket.id][:changes]
listObjects[ticket.id][:changes] = event[:changes]
else
event[:changes].each {|key, value|
if !listObjects[ticket.id][:changes][key]
listObjects[ticket.id][:changes][key] = value
else
listObjects[ticket.id][:changes][key][1] = value[1]
end
}
end
end end
#else else
# raise "unknown object for notification #{event[:name]}" raise "unknown object for notification #{event[:name]}"
end end
} }
listObjects listObjects

View file

@ -97,14 +97,13 @@ class Observer::Ticket::Notification::BackgroundJob
# add history record # add history record
if recipient_list != '' if recipient_list != ''
puts "send... #{recipient_list} #{ticket.id}"
History.add( History.add(
:o_id => ticket.id, :o_id => ticket.id,
:history_type => 'notification', :history_type => 'notification',
:history_object => 'Ticket', :history_object => 'Ticket',
:value_from => notification_subject, :value_from => notification_subject,
:value_to => recipient_list, :value_to => recipient_list,
:created_by_id => article.created_by_id || 1 :created_by_id => article.created_by_id || 1
) )
end end
end end

View file

@ -2,56 +2,57 @@
require 'test_helper' require 'test_helper'
class TicketNotificationTest < ActiveSupport::TestCase class TicketNotificationTest < ActiveSupport::TestCase
test 'ticket create' do
# create agent1 & agent2 # create agent1 & agent2
groups = Group.where( :name => 'Users' ) groups = Group.where( :name => 'Users' )
roles = Role.where( :name => 'Agent' ) roles = Role.where( :name => 'Agent' )
agent1 = User.create_or_update( agent1 = User.create_or_update(
:login => 'ticket-notification-agent1@example.com', :login => 'ticket-notification-agent1@example.com',
:firstname => 'Notification', :firstname => 'Notification',
:lastname => 'Agent1', :lastname => 'Agent1',
:email => 'ticket-notification-agent1@example.com', :email => 'ticket-notification-agent1@example.com',
:password => 'agentpw', :password => 'agentpw',
:active => true, :active => true,
:roles => roles, :roles => roles,
:groups => groups, :groups => groups,
:updated_by_id => 1, :updated_by_id => 1,
:created_by_id => 1, :created_by_id => 1,
) )
agent2 = User.create_or_update( agent2 = User.create_or_update(
:login => 'ticket-notification-agent2@example.com', :login => 'ticket-notification-agent2@example.com',
:firstname => 'Notification', :firstname => 'Notification',
:lastname => 'Agent2', :lastname => 'Agent2',
:email => 'ticket-notification-agent2@example.com', :email => 'ticket-notification-agent2@example.com',
:password => 'agentpw', :password => 'agentpw',
:active => true, :active => true,
:roles => roles, :roles => roles,
:groups => groups, :groups => groups,
:updated_by_id => 1, :updated_by_id => 1,
:created_by_id => 1, :created_by_id => 1,
) )
Group.create_if_not_exists( Group.create_if_not_exists(
:name => 'WithoutAccess', :name => 'WithoutAccess',
:note => 'Test for notification check.', :note => 'Test for notification check.',
:updated_by_id => 1, :updated_by_id => 1,
:created_by_id => 1 :created_by_id => 1
) )
# create customer # create customer
roles = Role.where( :name => 'Customer' ) roles = Role.where( :name => 'Customer' )
customer = User.create_or_update( customer = User.create_or_update(
:login => 'ticket-notification-customer@example.com', :login => 'ticket-notification-customer@example.com',
:firstname => 'Notification', :firstname => 'Notification',
:lastname => 'Customer', :lastname => 'Customer',
:email => 'ticket-notification-customer@example.com', :email => 'ticket-notification-customer@example.com',
:password => 'agentpw', :password => 'agentpw',
:active => true, :active => true,
:roles => roles, :roles => roles,
:groups => groups, :groups => groups,
:updated_by_id => 1, :updated_by_id => 1,
:created_by_id => 1, :created_by_id => 1,
) )
test 'ticket notification simple' do
# create ticket in group # create ticket in group
ticket1 = Ticket.create( ticket1 = Ticket.create(
@ -262,6 +263,64 @@ class TicketNotificationTest < ActiveSupport::TestCase
end end
test 'ticket notification events' do
# create ticket in group
ticket1 = Ticket.create(
:title => 'some notification event test 1',
:group => Group.lookup( :name => 'Users'),
:customer => customer,
:state => Ticket::State.lookup( :name => 'new' ),
:priority => Ticket::Priority.lookup( :name => '2 normal' ),
:updated_by_id => customer.id,
:created_by_id => customer.id,
)
article_inbound = Ticket::Article.create(
:ticket_id => ticket1.id,
:from => 'some_sender@example.com',
:to => 'some_recipient@example.com',
:subject => 'some subject',
:message_id => 'some@id',
:body => 'some message',
:internal => false,
:sender => Ticket::Article::Sender.where(:name => 'Customer').first,
:type => Ticket::Article::Type.where(:name => 'email').first,
:updated_by_id => customer.id,
:created_by_id => customer.id,
)
assert( ticket1, "ticket created" )
# execute ticket events
Observer::Ticket::Notification.transaction
# update ticket attributes
ticket1.title = "#{ticket1.title} - #2"
ticket1.priority = Ticket::Priority.lookup( :name => '3 high' )
ticket1.save
list = EventBuffer.list
listObjects = Observer::Ticket::Notification.get_uniq_changes(list)
assert_equal( 'some notification event test 1', listObjects[ticket1.id][:changes]['title'][0] )
assert_equal( 'some notification event test 1 - #2', listObjects[ticket1.id][:changes]['title'][1] )
assert_equal( '2 normal', listObjects[ticket1.id][:changes]['priority'][0] )
assert_equal( '3 high', listObjects[ticket1.id][:changes]['priority'][1] )
# update ticket attributes
ticket1.title = "#{ticket1.title} - #3"
ticket1.priority = Ticket::Priority.lookup( :name => '1 low' )
ticket1.save
list = EventBuffer.list
listObjects = Observer::Ticket::Notification.get_uniq_changes(list)
assert_equal( 'some notification event test 1', listObjects[ticket1.id][:changes]['title'][0] )
assert_equal( 'some notification event test 1 - #2 - #3', listObjects[ticket1.id][:changes]['title'][1] )
assert_equal( '2 normal', listObjects[ticket1.id][:changes]['priority'][0] )
assert_equal( '1 low', listObjects[ticket1.id][:changes]['priority'][1] )
end
def notification_check(ticket, recipient) def notification_check(ticket, recipient)
result = ticket.history_get() result = ticket.history_get()
count = 0 count = 0