require 'test_helper'
class EmailBuildTest < ActiveSupport::TestCase
test 'document complete check' do
html = 'test'
result = Channel::EmailBuild.html_complete_check(html)
assert(result =~ /^<\!DOCTYPE/, 'test 1')
assert(result !~ /^.+?<\!DOCTYPE/, 'test 1')
assert(result =~ //, 'test 1')
assert(result =~ /font-family/, 'test 1')
assert(result =~ %r{test}, 'test 1')
html = 'invalid test'
result = Channel::EmailBuild.html_complete_check(html)
assert(result !~ /^<\!DOCTYPE/, 'test 2')
assert(result =~ /^.+?<\!DOCTYPE/, 'test 2')
assert(result =~ //, 'test 2')
assert(result !~ /font-family/, 'test 2')
assert(result =~ %r{test}, 'test 2')
# Issue #1230, missing backslashes
# 'Test URL: \\storage\project\100242-Inc'
html = 'Test URL: \\\\storage\\project\\100242-Inc'
result = Channel::EmailBuild.html_complete_check(html)
assert(result.include?(html), 'backslashes must be kept')
end
test 'html email + attachment check' do
html = '
> Thank you for installing Zammad. äöüß
> Thank you for installing Zammad. äöüß
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_ics_calendar_file',
filename: 'schedule.ics',
preferences: {
'Mime-Type' => 'text/calendar'
},
created_by_id: 1,
)
text = '> Welcome!
>
> Thank you for installing Zammad. äöüß
>'
mail = Channel::EmailBuild.build(
from: 'sender@example.com',
to: 'recipient@example.com',
body: text,
attachments: [
store1
],
)
should = '> Welcome!
>
> Thank you for installing Zammad. äöüß
>'
assert_equal(should, mail.text_part.body.to_s)
assert_nil(mail.html_part)
assert_equal('text/calendar; filename=schedule.ics', mail.attachments[0].content_type)
parser = Channel::EmailParser.new
data = parser.parse(mail.to_s)
# check body
assert_equal(should, data[:body])
# check count of attachments, 2
assert_equal(1, data[:attachments].length)
# check attachments
data[:attachments]&.each do |attachment|
if attachment[:filename] == 'schedule.ics'
assert(attachment[:preferences]['Content-ID'])
assert_nil(attachment[:preferences]['content-alternative'])
assert_equal('text/calendar', attachment[:preferences]['Mime-Type'])
assert_equal('UTF-8', attachment[:preferences]['Charset'])
else
assert(false, "invalid attachment, should not be there, #{attachment.inspect}")
end
end
end
test 'plain email + without attachment check' do
text = '> Welcome!
>
> Thank you for installing Zammad. äöüß
>'
mail = Channel::EmailBuild.build(
from: 'sender@example.com',
to: 'recipient@example.com',
body: text,
)
should = '> Welcome!
>
> Thank you for installing Zammad. äöüß
>'
assert_equal(should, mail.body.to_s)
assert_nil(mail.html_part)
parser = Channel::EmailParser.new
data = parser.parse(mail.to_s)
# check body
assert_equal(should, data[:body])
# check count of attachments, 0
assert_equal(0, data[:attachments].length)
end
test 'email - html email client fixes' do
# https://github.com/martini/zammad/issues/165
html_raw = '
some
text
123
some
text
'
html_with_fixes = Channel::EmailBuild.html_mail_client_fixes(html_raw)
assert_not_equal(html_with_fixes, html_raw)
html_should = '
some
text
123
some
text
'
assert_equal(html_should, html_with_fixes)
html_raw = '
some
text
123
'
html_with_fixes = Channel::EmailBuild.html_mail_client_fixes(html_raw)
assert_not_equal(html_with_fixes, html_raw)
html_should = '
some
text
123
'
assert_equal(html_should, html_with_fixes)
html_raw = '
sometext
123
'
html_with_fixes = Channel::EmailBuild.html_mail_client_fixes(html_raw)
assert_not_equal(html_with_fixes, html_raw)
html_should = '
sometext
123
'
assert_equal(html_should, html_with_fixes)
end
test 'from checks' do
quoted_in_one_line = Channel::EmailBuild.recipient_line('Somebody @ "Company"', 'some.body@example.com')
assert_equal('"Somebody @ \"Company\""
', quoted_in_one_line)
quoted_in_one_line = Channel::EmailBuild.recipient_line('Somebody', 'some.body@example.com')
assert_equal('Somebody ', quoted_in_one_line)
quoted_in_one_line = Channel::EmailBuild.recipient_line('Somebody | Some Org', 'some.body@example.com')
assert_equal('"Somebody | Some Org" ', quoted_in_one_line)
quoted_in_one_line = Channel::EmailBuild.recipient_line('Test Master Agent via Support', 'some.body@example.com')
assert_equal('"Test Master Agent via Support" ', quoted_in_one_line)
end
end