From acc71eeb23998c9806ec14786847269262bdf258 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 12 Jul 2016 01:32:20 +0200 Subject: [PATCH] Improved tests. --- app/models/ticket/article.rb | 50 ++++++++++- lib/notification_factory/mailer.rb | 4 + test/unit/notification_factory_mailer_test.rb | 50 ++++++++++- test/unit/ticket_test.rb | 89 +++++++++++++++++++ 4 files changed, 188 insertions(+), 5 deletions(-) diff --git a/app/models/ticket/article.rb b/app/models/ticket/article.rb index 3cb3250da..84b91e1f4 100644 --- a/app/models/ticket/article.rb +++ b/app/models/ticket/article.rb @@ -41,10 +41,24 @@ class Ticket::Article < ApplicationModel self.message_id_md5 = Digest::MD5.hexdigest(message_id.to_s) end - # insert inline image urls +=begin + +insert inline image urls to body + + article_attributes = Ticket::Article.insert_urls( + article_attributes, + attachments, + ) + +returns + + article_attributes_with_body_and_urls + +=end + def self.insert_urls(article, attachments) inline_attachments = {} - article['body'].gsub!( /()/i ) { |item| + article['body'].gsub!( /()/i ) { |item| replace = item # look for attachment @@ -65,6 +79,38 @@ class Ticket::Article < ApplicationModel article end +=begin + +get inline attachments of article + + article = Ticket::Article.find(123) + attachments = article.attachments_inline + +returns + + [attachment1, attachment2, ...] + +=end + + def attachments_inline + inline_attachments = {} + body.gsub( //i ) { |_item| + + # look for attachment + attachments.each { |file| + next if !file.preferences['Content-ID'] || file.preferences['Content-ID'] != $2 + inline_attachments[file.id] = true + break + } + } + new_attachments = [] + attachments.each { |file| + next if !inline_attachments[file.id] + new_attachments.push file + } + new_attachments + end + def self.last_customer_agent_article(ticket_id) sender = Ticket::Article::Sender.lookup(name: 'System') Ticket::Article.where('ticket_id = ? AND sender_id NOT IN (?)', ticket_id, sender.id).order('created_at DESC').first diff --git a/lib/notification_factory/mailer.rb b/lib/notification_factory/mailer.rb index e4763d238..de4a9b44a 100644 --- a/lib/notification_factory/mailer.rb +++ b/lib/notification_factory/mailer.rb @@ -82,6 +82,7 @@ returns body: 'some body', content_type: '', # optional, e. g. 'text/html' references: ['message-id123', 'message-id456'], + attachments: [attachments...], # optional ) =end @@ -106,6 +107,7 @@ returns references: data[:references], body: data[:body], content_type: content_type, + attachments: data[:attachments], }, true ) @@ -122,6 +124,7 @@ returns main_object: ticket.find(123), # optional references: ['message-id123', 'message-id456'], standalone: true, # default: false - will send header & footer + attachments: [attachments...], # optional ) =end @@ -147,6 +150,7 @@ returns body: result[:body], content_type: 'text/html', references: data[:references], + attachments: data[:attachments], ) end diff --git a/test/unit/notification_factory_mailer_test.rb b/test/unit/notification_factory_mailer_test.rb index 8e2323a19..31b79d7b5 100644 --- a/test/unit/notification_factory_mailer_test.rb +++ b/test/unit/notification_factory_mailer_test.rb @@ -6,7 +6,7 @@ class NotificationFactoryMailerTest < ActiveSupport::TestCase test 'notifications send' do result = NotificationFactory::Mailer.send( recipient: User.find(2), - subject: 'sime subject', + subject: 'some subject', body: 'some body', content_type: '', ) @@ -16,7 +16,7 @@ class NotificationFactoryMailerTest < ActiveSupport::TestCase result = NotificationFactory::Mailer.send( recipient: User.find(2), - subject: 'sime subject', + subject: 'some subject', body: 'some body', content_type: 'text/plain', ) @@ -26,7 +26,7 @@ class NotificationFactoryMailerTest < ActiveSupport::TestCase result = NotificationFactory::Mailer.send( recipient: User.find(2), - subject: 'sime subject', + subject: 'some subject', body: 'some body', content_type: 'text/html', ) @@ -34,6 +34,50 @@ class NotificationFactoryMailerTest < ActiveSupport::TestCase assert_match('text/plain', result.to_s) assert_match('body', result.to_s) assert_match('text/html', result.to_s) + + attachments = [] + attachments.push Store.add( + object: 'TestMailer', + o_id: 1, + data: 'content_file1_normally_should_be_an_image', + filename: 'some_file1.jpg', + preferences: { + 'Content-Type' => 'image/jpeg', + 'Mime-Type' => 'image/jpeg', + 'Content-ID' => '15.274327094.140938@zammad.example.com', + 'Content-Disposition' => 'inline' + }, + created_by_id: 1, + ) + attachments.push Store.add( + object: 'TestMailer', + o_id: 1, + data: 'content_file2', + filename: 'some_file2.txt', + preferences: { + 'Content-Type' => 'text/stream', + 'Mime-Type' => 'text/stream', + }, + created_by_id: 1, + ) + + result = NotificationFactory::Mailer.send( + recipient: User.find(2), + subject: 'some subject', + body: 'some bodyasdasd
', + content_type: 'text/html', + attachments: attachments, + ) + assert_match('some body', result.to_s) + assert_match('text/plain', result.to_s) + assert_match('body', result.to_s) + assert_match('text/html', result.to_s) + assert_match('Content-Type: image/jpeg', result.to_s) + assert_match('Content-Disposition: inline', result.to_s) + assert_match('Content-ID: <15.274327094.140938@zammad.example.com>', result.to_s) + assert_match('text/stream', result.to_s) + assert_match('some_file2.txt', result.to_s) + end test 'notifications settings' do diff --git a/test/unit/ticket_test.rb b/test/unit/ticket_test.rb index 938856ed9..85a61a5dd 100644 --- a/test/unit/ticket_test.rb +++ b/test/unit/ticket_test.rb @@ -282,4 +282,93 @@ class TicketTest < ActiveSupport::TestCase end + test 'article attachment helper' do + + ticket1 = Ticket.create( + title: 'some article helper test1', + group: Group.lookup(name: 'Users'), + customer_id: 2, + state: Ticket::State.lookup(name: 'new'), + priority: Ticket::Priority.lookup(name: '2 normal'), + updated_by_id: 1, + created_by_id: 1, + ) + assert(ticket1, 'ticket created') + + # create inbound article #1 + article1 = Ticket::Article.create( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + content_type: 'text/html', + body: 'some message article helper test1
asdasd
', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + store1 = Store.add( + object: 'Ticket::Article', + o_id: article1.id, + data: 'content_file1_normally_should_be_an_image', + filename: 'some_file1.jpg', + preferences: { + 'Content-Type' => 'image/jpeg', + 'Mime-Type' => 'image/jpeg', + 'Content-ID' => '15.274327094.140938@zammad.example.com', + 'Content-Disposition' => 'inline' + }, + created_by_id: 1, + ) + store2 = Store.add( + object: 'Ticket::Article', + o_id: article1.id, + data: 'content_file2_normally_should_be_an_image', + filename: 'some_file2.jpg', + preferences: { + 'Content-Type' => 'image/jpeg', + 'Mime-Type' => 'image/jpeg', + 'Content-ID' => '15.274327094.140939@zammad.example.com', + 'Content-Disposition' => 'inline' + }, + created_by_id: 1, + ) + store3 = Store.add( + object: 'Ticket::Article', + o_id: article1.id, + data: 'content_file3', + filename: 'some_file3.txt', + preferences: { + 'Content-Type' => 'text/stream', + 'Mime-Type' => 'text/stream', + 'Content-ID' => '15.274327094.99999@zammad.example.com', + 'Content-Disposition' => 'inline' + }, + created_by_id: 1, + ) + + article_attributes = Ticket::Article.insert_urls( + article1.attributes, + article1.attachments, + ) + + assert_no_match('15.274327094.140938@zammad.example.com', article_attributes['body']) + assert_no_match('15.274327094.140939@zammad.example.com', article_attributes['body']) + assert_no_match('15.274327094.99999@zammad.example.com', article_attributes['body']) + assert_match("api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/1", article_attributes['body']) + assert_match("api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/2", article_attributes['body']) + assert_no_match("api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/3", article_attributes['body']) + + article1 = Ticket::Article.find(article1.id) + attachments = article1.attachments_inline + assert_equal(2, attachments.length) + assert_equal(store1.id, attachments.first.id) + + ticket1.destroy + + end end