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

View file

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

View file

@ -2,56 +2,57 @@
require 'test_helper'
class TicketNotificationTest < ActiveSupport::TestCase
test 'ticket create' do
# create agent1 & agent2
groups = Group.where( :name => 'Users' )
roles = Role.where( :name => 'Agent' )
agent1 = User.create_or_update(
:login => 'ticket-notification-agent1@example.com',
:firstname => 'Notification',
:lastname => 'Agent1',
:email => 'ticket-notification-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 => 'ticket-notification-agent2@example.com',
:firstname => 'Notification',
:lastname => 'Agent2',
:email => 'ticket-notification-agent2@example.com',
:password => 'agentpw',
:active => true,
:roles => roles,
:groups => groups,
:updated_by_id => 1,
:created_by_id => 1,
)
Group.create_if_not_exists(
:name => 'WithoutAccess',
:note => 'Test for notification check.',
:updated_by_id => 1,
:created_by_id => 1
)
# create agent1 & agent2
groups = Group.where( :name => 'Users' )
roles = Role.where( :name => 'Agent' )
agent1 = User.create_or_update(
:login => 'ticket-notification-agent1@example.com',
:firstname => 'Notification',
:lastname => 'Agent1',
:email => 'ticket-notification-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 => 'ticket-notification-agent2@example.com',
:firstname => 'Notification',
:lastname => 'Agent2',
:email => 'ticket-notification-agent2@example.com',
:password => 'agentpw',
:active => true,
:roles => roles,
:groups => groups,
:updated_by_id => 1,
:created_by_id => 1,
)
Group.create_if_not_exists(
:name => 'WithoutAccess',
:note => 'Test for notification check.',
:updated_by_id => 1,
:created_by_id => 1
)
# create customer
roles = Role.where( :name => 'Customer' )
customer = User.create_or_update(
:login => 'ticket-notification-customer@example.com',
:firstname => 'Notification',
:lastname => 'Customer',
:email => 'ticket-notification-customer@example.com',
:password => 'agentpw',
:active => true,
:roles => roles,
:groups => groups,
:updated_by_id => 1,
:created_by_id => 1,
)
# create customer
roles = Role.where( :name => 'Customer' )
customer = User.create_or_update(
:login => 'ticket-notification-customer@example.com',
:firstname => 'Notification',
:lastname => 'Customer',
:email => 'ticket-notification-customer@example.com',
:password => 'agentpw',
:active => true,
:roles => roles,
:groups => groups,
:updated_by_id => 1,
:created_by_id => 1,
)
test 'ticket notification simple' do
# create ticket in group
ticket1 = Ticket.create(
@ -262,6 +263,64 @@ class TicketNotificationTest < ActiveSupport::TestCase
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)
result = ticket.history_get()
count = 0