Improved tests.

This commit is contained in:
Martin Edenhofer 2016-07-12 01:32:20 +02:00
parent c0628a93ef
commit acc71eeb23
4 changed files with 188 additions and 5 deletions

View file

@ -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!( /(<img\s(style.+?|)src=")cid:(.+?)(">)/i ) { |item|
article['body'].gsub!( /(<img[[:space:]](.+?|)src=")cid:(.+?)(">)/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( /<img[[:space:]](.+?|)src="cid:(.+?)">/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

View file

@ -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

View file

@ -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 <span>body</span>',
content_type: 'text/html',
)
@ -34,6 +34,50 @@ class NotificationFactoryMailerTest < ActiveSupport::TestCase
assert_match('text/plain', result.to_s)
assert_match('<span>body</span>', 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 <span>body</span><img style="width: 85.5px; height: 49.5px" src="cid:15.274327094.140938@zammad.example.com">asdasd<br>',
content_type: 'text/html',
attachments: attachments,
)
assert_match('some body', result.to_s)
assert_match('text/plain', result.to_s)
assert_match('<span>body</span>', 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

View file

@ -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 <div><img style="width: 85.5px; height: 49.5px" src="cid:15.274327094.140938@zammad.example.com">asdasd<img src="cid:15.274327094.140939@zammad.example.com"><br>',
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