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
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('.addSelection select').val('')
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
},
{
value: 'x-zammad-article-ticket_article_internal'
value: 'x-zammad-article-internal'
name: 'Article Internal'
options: { true: 'Yes', false: 'No'}
},
{
value: 'x-zammad-article-ticket_article_type_id'

View file

@ -50,7 +50,7 @@ class Channel::EmailParser
:x-zammad-ticket-owner => 'some_owner_login',
# article headers
:x-zammad-article-visibility => 'internal',
:x-zammad-article-internal => false,
:x-zammad-article-type => 'agent',
:x-zammad-article-sender => 'customer',
@ -79,6 +79,17 @@ class Channel::EmailParser
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
begin
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,
)
object_lookup( ticket, 'ticket', mail )
set_attributes_by_x_headers( ticket, 'ticket', mail )
# create ticket
ticket.save
@ -388,10 +399,6 @@ class Channel::EmailParser
# import mail
# 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(
:ticket_id => ticket.id,
:ticket_article_type_id => Ticket::Article::Type.where( :name => 'email' ).first.id,
@ -402,11 +409,11 @@ class Channel::EmailParser
:cc => mail[:cc],
:subject => mail[:subject],
:message_id => mail[:message_id],
:internal => internal,
:internal => false,
)
# x-headers lookup
object_lookup( article, 'article', mail )
set_attributes_by_x_headers( article, 'article', mail )
# create article
article.save
@ -456,7 +463,7 @@ class Channel::EmailParser
return ticket, article, user
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
item_object.attributes.each{|key,value|

View file

@ -2,7 +2,7 @@
require 'test_helper'
class EmailProcessTest < ActiveSupport::TestCase
test 'process trusted' do
test 'process simple' do
files = [
{
:data => 'From: me@example.com
@ -10,46 +10,16 @@ To: customer@example.com
Subject: some subject
Some Text',
:trusted => false,
: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
To: customer@example.com
Subject: äöü some subject
Some Textäöü",
:trusted => false,
:success => true,
:result => {
0 => {
@ -145,6 +115,7 @@ Subject: Subject: =?utf-8?B?44CQ5LiT5Lia5Li65oKo5rOo5YaM6aaZ5riv5Y+K5rW35aSW5YWs
=?utf-8?B?5YWs5Y+46Zmi5aOr5bel5L2c56uZ5pel5YmN5q2j5byP5bu6Li4uW+ivpue7hl0=?=
Some Text",
:trusted => false,
:success => true,
:result => {
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|
parser = Channel::EmailParser.new
result = parser.process( { :trusted => true }, file[:data] )
if file[:success] && result[1]
assert( true )
if file[:result]
[ 0, 1, 2 ].each { |level|
if file[:result][level]
file[:result][level].each { |key, value|
if result[level].send(key).respond_to?('name')
assert_equal( result[level].send(key).name, value.to_s)
else
assert_equal( result[level].send(key), value)
end
}
end
}
end
elsif !file[:success] && result == true
assert( true )
elsif !file[:success] && result[1]
assert( false, 'ticket should not be created' )
else
assert( false, 'UNKNOWN!' )
end
}
Some Text',
:trusted => true,
: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',
:trusted => true,
:success => true,
:result => {
0 => {
:ticket_priority => '3 high',
:title => 'some subject',
},
1 => {
:ticket_article_sender => 'System',
:ticket_article_type => 'phone',
:internal => true,
},
},
},
]
process(files)
end
test 'process not trusted' do
@ -200,6 +184,7 @@ x-Zammad-Article-Ticket_Article_Type: phone
x-Zammad-Article-Internal: true
Some Text',
:trusted => false,
:success => true,
:result => {
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|
parser = Channel::EmailParser.new
result = parser.process( { :trusted => false }, file[:data] )
if file[:success] && result[1]
assert( true )
if file[:result]
[ 0, 1, 2 ].each { |level|
if file[:result][level]
file[:result][level].each { |key, value|
if result[level].send(key).respond_to?('name')
assert_equal( result[level].send(key).name, value.to_s)
else
assert_equal( result[level].send(key), value)
end
}
end
}
result = parser.process( { :trusted => file[:trusted] }, file[:data] )
if file[:success]
if result && result.class == Array && result[1]
assert( true )
if file[:result]
[ 0, 1, 2 ].each { |level|
if file[:result][level]
file[:result][level].each { |key, value|
if result[level].send(key).respond_to?('name')
assert_equal( result[level].send(key).name, value.to_s)
else
assert_equal( result[level].send(key), value)
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
elsif !file[:success] && result == true
assert( true )
elsif !file[:success] && result[1]
assert( false, 'ticket should not be created' )
else
assert( false, 'UNKNOWN!' )
end
}
end
end
end