Added support of "x-any-recipient" header. Improved unit tests.

This commit is contained in:
Martin Edenhofer 2014-06-08 17:14:51 +02:00
parent d436872bc7
commit fbf644faa1
3 changed files with 185 additions and 90 deletions

View file

@ -459,7 +459,6 @@ class App.ControllerForm extends App.Controller
) )
# add new item # add new item
console.log(1111222, key, el, el.parent().parent().parent().find('.addSelection select'))
el.parent().parent().parent().find('.list').append(itemInput) el.parent().parent().parent().find('.list').append(itemInput)
el.parent().parent().parent().find('.addSelection select').val('') el.parent().parent().parent().find('.addSelection select').val('')
el.parent().parent().parent().find('.addSelection select option[value="' + key + '"]').prop('disabled', true) el.parent().parent().parent().find('.addSelection select option[value="' + key + '"]').prop('disabled', true)
@ -509,8 +508,9 @@ class App.ControllerForm extends App.Controller
disable: true disable: true
}, },
{ {
value: 'x-zammad-article-ticket_article_internal' value: 'x-zammad-article-internal'
name: 'Article Internal' name: 'Article Internal'
options: { true: 'Yes', false: 'No'}
}, },
{ {
value: 'x-zammad-article-ticket_article_type_id' value: 'x-zammad-article-ticket_article_type_id'

View file

@ -50,7 +50,7 @@ class Channel::EmailParser
:x-zammad-ticket-owner => 'some_owner_login', :x-zammad-ticket-owner => 'some_owner_login',
# article headers # article headers
:x-zammad-article-visibility => 'internal', :x-zammad-article-internal => false,
:x-zammad-article-type => 'agent', :x-zammad-article-type => 'agent',
:x-zammad-article-sender => 'customer', :x-zammad-article-sender => 'customer',
@ -79,6 +79,17 @@ class Channel::EmailParser
end end
} }
# set x-any-recipient
data['x-any-recipient'.to_sym] = ''
['to', 'cc', 'delivered-to', 'x-original-to', 'envelope-to'].each { |item|
if mail[item.to_sym]
if data['x-any-recipient'.to_sym] != ''
data['x-any-recipient'.to_sym] += ', '
end
data['x-any-recipient'.to_sym] += mail[item.to_sym].to_s
end
}
# set extra headers # set extra headers
begin begin
data[:from_email] = Mail::Address.new( from ).address data[:from_email] = Mail::Address.new( from ).address
@ -379,7 +390,7 @@ class Channel::EmailParser
:ticket_priority_id => Ticket::Priority.where( :name => '2 normal' ).first.id, :ticket_priority_id => Ticket::Priority.where( :name => '2 normal' ).first.id,
) )
object_lookup( ticket, 'ticket', mail ) set_attributes_by_x_headers( ticket, 'ticket', mail )
# create ticket # create ticket
ticket.save ticket.save
@ -388,10 +399,6 @@ class Channel::EmailParser
# import mail # import mail
# set attributes # set attributes
internal = false
if mail[ 'X-Zammad-Article-Visibility'.to_sym ] && mail[ 'X-Zammad-Article-Visibility'.to_sym ] == 'internal'
internal = true
end
article = Ticket::Article.new( article = Ticket::Article.new(
:ticket_id => ticket.id, :ticket_id => ticket.id,
:ticket_article_type_id => Ticket::Article::Type.where( :name => 'email' ).first.id, :ticket_article_type_id => Ticket::Article::Type.where( :name => 'email' ).first.id,
@ -402,11 +409,11 @@ class Channel::EmailParser
:cc => mail[:cc], :cc => mail[:cc],
:subject => mail[:subject], :subject => mail[:subject],
:message_id => mail[:message_id], :message_id => mail[:message_id],
:internal => internal, :internal => false,
) )
# x-headers lookup # x-headers lookup
object_lookup( article, 'article', mail ) set_attributes_by_x_headers( article, 'article', mail )
# create article # create article
article.save article.save
@ -456,7 +463,7 @@ class Channel::EmailParser
return ticket, article, user return ticket, article, user
end end
def object_lookup( item_object, header_name, mail ) def set_attributes_by_x_headers( item_object, header_name, mail )
# loop all x-zammad-hedaer-* headers # loop all x-zammad-hedaer-* headers
item_object.attributes.each{|key,value| item_object.attributes.each{|key,value|

View file

@ -2,7 +2,7 @@
require 'test_helper' require 'test_helper'
class EmailProcessTest < ActiveSupport::TestCase class EmailProcessTest < ActiveSupport::TestCase
test 'process trusted' do test 'process simple' do
files = [ files = [
{ {
:data => 'From: me@example.com :data => 'From: me@example.com
@ -10,46 +10,16 @@ To: customer@example.com
Subject: some subject Subject: some subject
Some Text', Some Text',
:trusted => false,
:success => true, :success => true,
}, },
{
:data => 'From: me@example.com
To: customer@example.com
Subject: some subject
X-Zammad-Ignore: true
Some Text',
:success => false,
},
{
:data => 'From: me@example.com
To: customer@example.com
Subject: some subject
X-Zammad-Ticket-Ticket_Priority: 3 high
X-Zammad-Article-Ticket_Article_Sender: System
x-Zammad-Article-Ticket_Article_Type: phone
x-Zammad-Article-Internal: true
Some Text',
:success => true,
:result => {
0 => {
:ticket_priority => '3 high',
:title => 'some subject',
},
1 => {
:ticket_article_sender => 'System',
:ticket_article_type => 'phone',
:internal => true,
},
},
},
{ {
:data => "From: me@example.com :data => "From: me@example.com
To: customer@example.com To: customer@example.com
Subject: äöü some subject Subject: äöü some subject
Some Textäöü", Some Textäöü",
:trusted => false,
:success => true, :success => true,
:result => { :result => {
0 => { 0 => {
@ -145,6 +115,7 @@ Subject: Subject: =?utf-8?B?44CQ5LiT5Lia5Li65oKo5rOo5YaM6aaZ5riv5Y+K5rW35aSW5YWs
=?utf-8?B?5YWs5Y+46Zmi5aOr5bel5L2c56uZ5pel5YmN5q2j5byP5bu6Li4uW+ivpue7hl0=?= =?utf-8?B?5YWs5Y+46Zmi5aOr5bel5L2c56uZ5pel5YmN5q2j5byP5bu6Li4uW+ivpue7hl0=?=
Some Text", Some Text",
:trusted => false,
:success => true, :success => true,
:result => { :result => {
0 => { 0 => {
@ -159,33 +130,46 @@ Some Text",
}, },
}, },
] ]
process(files)
end
test 'process trusted' do
files = [
{
:data => 'From: me@example.com
To: customer@example.com
Subject: some subject
X-Zammad-Ignore: true
files.each { |file| Some Text',
parser = Channel::EmailParser.new :trusted => true,
result = parser.process( { :trusted => true }, file[:data] ) :success => false,
if file[:success] && result[1] },
assert( true ) {
if file[:result] :data => 'From: me@example.com
[ 0, 1, 2 ].each { |level| To: customer@example.com
if file[:result][level] Subject: some subject
file[:result][level].each { |key, value| X-Zammad-Ticket-Ticket_Priority: 3 high
if result[level].send(key).respond_to?('name') X-Zammad-Article-Ticket_Article_Sender: System
assert_equal( result[level].send(key).name, value.to_s) x-Zammad-Article-Ticket_Article_Type: phone
else x-Zammad-Article-Internal: true
assert_equal( result[level].send(key), value)
end Some Text',
} :trusted => true,
end :success => true,
} :result => {
end 0 => {
elsif !file[:success] && result == true :ticket_priority => '3 high',
assert( true ) :title => 'some subject',
elsif !file[:success] && result[1] },
assert( false, 'ticket should not be created' ) 1 => {
else :ticket_article_sender => 'System',
assert( false, 'UNKNOWN!' ) :ticket_article_type => 'phone',
end :internal => true,
} },
},
},
]
process(files)
end end
test 'process not trusted' do test 'process not trusted' do
@ -200,6 +184,7 @@ x-Zammad-Article-Ticket_Article_Type: phone
x-Zammad-Article-Internal: true x-Zammad-Article-Internal: true
Some Text', Some Text',
:trusted => false,
:success => true, :success => true,
:result => { :result => {
0 => { 0 => {
@ -214,31 +199,134 @@ Some Text',
}, },
}, },
] ]
process(files)
end
test 'process with postmaster filter' do
PostmasterFilter.destroy_all
PostmasterFilter.create(
:name => 'not used',
:match => {
:from => 'nobody@example.com',
},
:perform => {
'X-Zammad-Ticket-Ticket_Priority' => '3 high',
},
:channel => 'email',
:active => true,
:created_by_id => 1,
:updated_by_id => 1,
)
PostmasterFilter.create(
:name => 'used',
:match => {
:from => 'me@example.com',
},
:perform => {
'X-Zammad-Ticket-group_id' => 2,
'x-Zammad-Article-Internal' => true,
},
:channel => 'email',
:active => true,
:created_by_id => 1,
:updated_by_id => 1,
)
PostmasterFilter.create(
:name => 'used x-any-recipient',
:match => {
'x-any-recipient' => 'any@example.com',
},
:perform => {
'X-Zammad-Ticket-group_id' => 2,
'x-Zammad-Article-Internal' => true,
},
:channel => 'email',
:active => true,
:created_by_id => 1,
:updated_by_id => 1,
)
files = [
{
:data => 'From: me@example.com
To: customer@example.com
Subject: some subject
Some Text',
:trusted => false,
:success => true,
:result => {
0 => {
:group => 'Twitter',
:ticket_priority => '2 normal',
:title => 'some subject',
},
1 => {
:ticket_article_sender => 'Customer',
:ticket_article_type => 'email',
:internal => true,
},
},
},
{
:data => 'From: somebody@example.com
To: bod@example.com
Cc: any@example.com
Subject: some subject
Some Text',
:trusted => false,
:success => true,
:result => {
0 => {
:group => 'Twitter',
:ticket_priority => '2 normal',
:title => 'some subject',
},
1 => {
:ticket_article_sender => 'Customer',
:ticket_article_type => 'email',
:internal => true,
},
},
},
]
process(files)
PostmasterFilter.destroy_all
end
def process(files)
files.each { |file| files.each { |file|
parser = Channel::EmailParser.new parser = Channel::EmailParser.new
result = parser.process( { :trusted => false }, file[:data] ) result = parser.process( { :trusted => file[:trusted] }, file[:data] )
if file[:success] && result[1] if file[:success]
assert( true ) if result && result.class == Array && result[1]
if file[:result] assert( true )
[ 0, 1, 2 ].each { |level| if file[:result]
if file[:result][level] [ 0, 1, 2 ].each { |level|
file[:result][level].each { |key, value| if file[:result][level]
if result[level].send(key).respond_to?('name') file[:result][level].each { |key, value|
assert_equal( result[level].send(key).name, value.to_s) if result[level].send(key).respond_to?('name')
else assert_equal( result[level].send(key).name, value.to_s)
assert_equal( result[level].send(key), value) else
end assert_equal( result[level].send(key), value)
} end
end }
} end
}
end
else
assert( false, 'ticket not created', file )
end
elsif !file[:success]
if result && result.class == Array && result[1]
puts result.inspect
assert( false, 'ticket should not be created but is created' )
else
assert( true, 'ticket not created - nice' )
end end
elsif !file[:success] && result == true
assert( true )
elsif !file[:success] && result[1]
assert( false, 'ticket should not be created' )
else else
assert( false, 'UNKNOWN!' ) assert( false, 'UNKNOWN!' )
end end
} }
end end
end end