From fbf644faa126d294548862782bb7f15ca2385da7 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 8 Jun 2014 17:14:51 +0200 Subject: [PATCH] Added support of "x-any-recipient" header. Improved unit tests. --- .../_application_controller_form.js.coffee | 4 +- app/models/channel/email_parser.rb | 25 +- test/unit/email_process_test.rb | 246 ++++++++++++------ 3 files changed, 185 insertions(+), 90 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee b/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee index 9e6aebc5d..d84c8eb05 100644 --- a/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee @@ -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' diff --git a/app/models/channel/email_parser.rb b/app/models/channel/email_parser.rb index f47427ab9..362c549df 100644 --- a/app/models/channel/email_parser.rb +++ b/app/models/channel/email_parser.rb @@ -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| diff --git a/test/unit/email_process_test.rb b/test/unit/email_process_test.rb index 8e4f6f1cf..1c7f15592 100644 --- a/test/unit/email_process_test.rb +++ b/test/unit/email_process_test.rb @@ -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 \ No newline at end of file