2014-12-29 15:39:59 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class EmailBuildTest < ActiveSupport::TestCase
|
|
|
|
test 'document complete check' do
|
|
|
|
|
|
|
|
html = '<b>test</b>'
|
|
|
|
result = Channel::EmailBuild.html_complete_check( html )
|
|
|
|
|
|
|
|
assert( result =~ /^<\!DOCTYPE/, 'test 1')
|
|
|
|
assert( result !~ /^.+?<\!DOCTYPE/, 'test 1')
|
|
|
|
assert( result =~ /<html>/, 'test 1')
|
|
|
|
assert( result =~ /font-family/, 'test 1')
|
2014-12-29 17:21:41 +00:00
|
|
|
assert( result =~ /<b>test<\/b>/, 'test 1')
|
2014-12-29 15:39:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
html = 'invalid <!DOCTYPE html><html><b>test</b></html>'
|
|
|
|
result = Channel::EmailBuild.html_complete_check( html )
|
|
|
|
|
|
|
|
assert( result !~ /^<\!DOCTYPE/, 'test 2')
|
|
|
|
assert( result =~ /^.+?<\!DOCTYPE/, 'test 2')
|
|
|
|
assert( result =~ /<html>/, 'test 2')
|
|
|
|
assert( result !~ /font-family/, 'test 2')
|
2014-12-29 17:21:41 +00:00
|
|
|
assert( result =~ /<b>test<\/b>/, 'test 2')
|
2014-12-29 15:39:59 +00:00
|
|
|
|
|
|
|
end
|
2014-12-29 23:25:57 +00:00
|
|
|
|
2014-12-30 14:08:20 +00:00
|
|
|
test 'html email + attachment check' do
|
2014-12-29 23:25:57 +00:00
|
|
|
html = '<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
|
|
|
<head>
|
|
|
|
<body style="font-family:Geneva,Helvetica,Arial,sans-serif; font-size: 12px;">
|
2014-12-30 14:08:20 +00:00
|
|
|
<div>> Welcome!</div><div>></div><div>> Thank you for installing Zammad. äöüß</div><div>></div>
|
2014-12-29 23:25:57 +00:00
|
|
|
</body>
|
|
|
|
</html>'
|
|
|
|
mail = Channel::EmailBuild.build(
|
|
|
|
:from => 'sender@example.com',
|
|
|
|
:to => 'recipient@example.com',
|
|
|
|
:body => html,
|
|
|
|
:content_type => 'text/html',
|
2014-12-30 14:08:20 +00:00
|
|
|
:attachments => [
|
|
|
|
{
|
|
|
|
'Mime-Type' => 'image/png',
|
|
|
|
:content => 'xxx',
|
|
|
|
:filename => 'somename.png',
|
|
|
|
},
|
|
|
|
],
|
2014-12-29 23:25:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
should = '> Welcome!
|
|
|
|
>
|
2014-12-30 14:08:20 +00:00
|
|
|
> Thank you for installing Zammad. äöüß
|
2014-12-29 23:25:57 +00:00
|
|
|
>
|
|
|
|
'
|
|
|
|
assert_equal( should, mail.text_part.body.to_s )
|
|
|
|
assert_equal( html, mail.html_part.body.to_s )
|
|
|
|
|
2014-12-30 14:08:20 +00:00
|
|
|
parser = Channel::EmailParser.new
|
|
|
|
data = parser.parse( mail.to_s )
|
|
|
|
|
|
|
|
# check body
|
|
|
|
assert_equal( should, data[:body] )
|
|
|
|
|
|
|
|
# check count of attachments, only 2, because 3 part is text message and is already in body
|
|
|
|
assert_equal( 2, data[:attachments].length )
|
|
|
|
|
|
|
|
# check attachments
|
|
|
|
if data[:attachments]
|
|
|
|
data[:attachments].each { |attachment|
|
|
|
|
if attachment[:filename] == 'message.html'
|
|
|
|
assert_equal( nil, attachment[:preferences]['Content-ID'] )
|
|
|
|
assert_equal( true, attachment[:preferences]['content-alternative'] )
|
|
|
|
assert_equal( 'text/html', attachment[:preferences]['Mime-Type'] )
|
|
|
|
assert_equal( 'UTF-8', attachment[:preferences]['Charset'] )
|
|
|
|
elsif attachment[:filename] == 'somename.png'
|
|
|
|
assert_equal( nil, attachment[:preferences]['Content-ID'] )
|
|
|
|
assert_equal( nil, attachment[:preferences]['content-alternative'] )
|
|
|
|
assert_equal( 'image/png', attachment[:preferences]['Mime-Type'] )
|
|
|
|
assert_equal( 'UTF-8', attachment[:preferences]['Charset'] )
|
|
|
|
else
|
|
|
|
assert( false, "invalid attachment, should not be there, #{attachment.inspect}" )
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2014-12-29 23:25:57 +00:00
|
|
|
end
|
|
|
|
|
2014-12-30 14:08:20 +00:00
|
|
|
test 'plain email + attachment check' do
|
|
|
|
text = '> Welcome!
|
|
|
|
>
|
|
|
|
> Thank you for installing Zammad. äöüß
|
|
|
|
>
|
|
|
|
'
|
|
|
|
mail = Channel::EmailBuild.build(
|
|
|
|
:from => 'sender@example.com',
|
|
|
|
:to => 'recipient@example.com',
|
|
|
|
:body => text,
|
|
|
|
:attachments => [
|
|
|
|
{
|
|
|
|
'Mime-Type' => 'image/png',
|
|
|
|
:content => 'xxx',
|
|
|
|
:filename => 'somename.png',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
should = '> Welcome!
|
|
|
|
>
|
|
|
|
> Thank you for installing Zammad. äöüß
|
|
|
|
>
|
|
|
|
'
|
|
|
|
assert_equal( should, mail.text_part.body.to_s )
|
|
|
|
assert_equal( 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, 2
|
|
|
|
assert_equal( 1, data[:attachments].length )
|
|
|
|
|
|
|
|
# check attachments
|
|
|
|
if data[:attachments]
|
|
|
|
data[:attachments].each { |attachment|
|
|
|
|
if attachment[:filename] == 'somename.png'
|
|
|
|
assert_equal( nil, attachment[:preferences]['Content-ID'] )
|
|
|
|
assert_equal( nil, attachment[:preferences]['content-alternative'] )
|
|
|
|
assert_equal( 'image/png', 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_equal( 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
|
2014-12-29 23:25:57 +00:00
|
|
|
|
|
|
|
test 'html2text' do
|
|
|
|
html = '<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
|
|
|
<head>
|
|
|
|
<body style="font-family:Geneva,Helvetica,Arial,sans-serif; font-size: 12px;">
|
|
|
|
<div>> Welcome!</div><div>></div><div>> Thank you for installing Zammad.</div><div>></div>
|
|
|
|
</body>
|
|
|
|
</html>'
|
|
|
|
should = '> Welcome!
|
|
|
|
>
|
|
|
|
> Thank you for installing Zammad.
|
|
|
|
>
|
|
|
|
'
|
|
|
|
assert_equal( should, html.html2text )
|
|
|
|
|
|
|
|
|
|
|
|
html = ' line 1<br>
|
|
|
|
you<br/>
|
|
|
|
-----&'
|
|
|
|
should = 'line 1
|
|
|
|
you
|
|
|
|
-----&'
|
|
|
|
assert_equal( should, html.html2text )
|
|
|
|
|
|
|
|
|
|
|
|
html = ' <ul><li>#1</li><li>#2</li></ul>'
|
|
|
|
should = '* #1
|
|
|
|
* #2'
|
|
|
|
assert_equal( should, html.html2text )
|
|
|
|
|
|
|
|
end
|
2014-12-29 15:39:59 +00:00
|
|
|
end
|