2022-01-01 13:38:12 +00:00
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
2021-06-01 12:20:20 +00:00
2019-03-20 06:42:04 +00:00
require 'rails_helper'
RSpec . describe SignatureDetection do
describe '.find_signature' do
context 'when given an array of hashes' do
let ( :messages ) do
raw_message_files . map do | f |
{ content : File . read ( f ) , content_type : content_type }
end
end
context 'with plain text messages in their :content keys (sample input 1)' do
let ( :content_type ) { 'text/plain' }
let ( :raw_message_files ) do
[
2020-02-18 19:51:31 +00:00
Rails . root . join ( 'test/data/email_signature_detection/client_a_1.txt' ) ,
Rails . root . join ( 'test/data/email_signature_detection/client_a_2.txt' ) ,
Rails . root . join ( 'test/data/email_signature_detection/client_a_3.txt' )
2019-03-20 06:42:04 +00:00
]
end
it 'returns the first 5– 10-line substring they share in common' do
2019-04-15 01:41:17 +00:00
expect ( described_class . find_signature ( messages ) ) . to eq ( << ~ SIG . chomp )
2019-03-20 06:42:04 +00:00
Mit freundlichen Grüßen
Bob Smith
Berechtigungen und dez . Department
________________________________
Musik AG
Berechtigungen und dez . Department ( ITPBM )
Kastanien 2
SIG
end
end
context 'with plain text messages in their :content keys (sample input 2)' do
let ( :content_type ) { 'text/plain' }
let ( :raw_message_files ) do
[
2020-02-18 19:51:31 +00:00
Rails . root . join ( 'test/data/email_signature_detection/client_b_1.txt' ) ,
Rails . root . join ( 'test/data/email_signature_detection/client_b_2.txt' ) ,
Rails . root . join ( 'test/data/email_signature_detection/client_b_3.txt' )
2019-03-20 06:42:04 +00:00
]
end
it 'returns the first 5– 10-line substring they share in common' do
2019-04-15 01:41:17 +00:00
expect ( described_class . find_signature ( messages ) ) . to eq ( << ~ SIG . chomp )
2019-03-20 06:42:04 +00:00
Freundliche Grüße
Günter Lässig
Lokale Daten
Music GmbH
Baustraße 123 , 12345 Max City
Telefon 0123 5432114
Telefax 0123 5432139
SIG
end
end
context 'with HTML messages in their :content keys' do
let ( :content_type ) { 'text/html' }
let ( :raw_message_files ) do
[
2020-02-18 19:51:31 +00:00
Rails . root . join ( 'test/data/email_signature_detection/client_c_1.html' ) ,
Rails . root . join ( 'test/data/email_signature_detection/client_c_2.html' ) ,
Rails . root . join ( 'test/data/email_signature_detection/client_c_3.html' )
2019-03-20 06:42:04 +00:00
]
end
it 'converts messages (via #html2text) then returns the first 5– 10-line substring they share in common' do
2019-04-15 01:41:17 +00:00
expect ( described_class . find_signature ( messages ) ) . to eq ( << ~ SIG . chomp )
2019-03-20 06:42:04 +00:00
ChristianSmith
Technik
Tel : + 49 12 34 56 78 441
Fax : + 49 12 34 56 78 499
Email : Christian . Smith @example . com
Web : www . example . com
ABC KFZ - und Flugzeug B . V . & Co . KG
Hauptverwaltung
SIG
end
end
end
2019-10-11 15:10:41 +00:00
context 'when input messages do not share 5-line common substrings' do
let ( :messages ) do
Array . new ( 2 ) { { content : << ~ RAW , content_type : 'text/plain' } }
Lorem ipsum dolor sit amet , consectetur adipiscing elit .
Ut ut tincidunt nunc . Sed mattis aliquam tellus sit amet lacinia .
Mauris fermentum dictum aliquet .
Nam ex risus , gravida et ornare ut , mollis non sapien .
RAW
end
it 'doesn’ t break' do
expect { described_class . find_signature ( messages ) } . not_to raise_error
end
end
2021-07-14 13:47:26 +00:00
context 'when message contains exchange warning (#3571)' do
let ( :content_type ) { 'text/html' }
let ( :messages ) do
[
{ content : '<div><span style="color:#9c6500;">CAUTION:</span> This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.</div><br><div><p>actual content</p><div><p>actual content 2</p></div><p> </p><div><p>actual quote</p></div><div><blockquote><p>actual quote</p></blockquote></div><div><p> </p></div><p> </p></div></div>' , content_type : 'text/html' } ,
{ content : '<div><span style="color:#9c6500;">CAUTION:</span> This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.</div><br><div><p>333 content</p><div><p>4452134123 content 2</p></div><p> </p><div><p>123124123 quote</p></div><div><blockquote><p>123141 144234</p></blockquote></div><div><p> </p></div><p> </p></div></div>' , content_type : 'text/html' } ,
{ content : '<div><span style="color:#9c6500;">CAUTION:</span> This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.</div><br><div><p>333 content</p><div><p>4452134123 content 2</p></div><p> </p><div><p>9999 quote</p></div><div><blockquote><p>999 144234</p></blockquote></div><div><p> </p></div><p> </p></div></div>' , content_type : 'text/html' } ,
]
end
it 'does not detect the warning information as signature' do
2022-03-01 08:00:40 +00:00
expect ( described_class . find_signature ( messages ) ) . to be_nil
2021-07-14 13:47:26 +00:00
end
end
2019-03-20 06:42:04 +00:00
end
describe '.find_signature_line' do
context 'when given a plain text message' do
let ( :content_type ) { 'text/plain' }
2020-02-18 19:51:31 +00:00
let ( :content ) { File . read ( Rails . root . join ( 'test/data/email_signature_detection/client_a_1.txt' ) ) }
2019-03-20 06:42:04 +00:00
context 'and a substring it contains' do
let ( :signature ) { << ~ SIG . chomp }
Mit freundlichen Grüßen
Bob Smith
Berechtigungen und dez . Department
________________________________
Musik AG
Berechtigungen und dez . Department ( ITPBM )
Kastanien 2
SIG
it 'returns the line of the message where the signature begins' do
2019-04-15 01:41:17 +00:00
expect ( described_class . find_signature_line ( signature , content , content_type ) ) . to eq ( 10 )
2019-03-20 06:42:04 +00:00
end
end
end
context 'when given an HTML message' do
let ( :content_type ) { 'text/html' }
2020-02-18 19:51:31 +00:00
let ( :content ) { File . read ( Rails . root . join ( 'test/data/email_signature_detection/example1.html' ) ) }
2019-03-20 06:42:04 +00:00
context 'and a substring it contains' do
let ( :signature ) { << ~ SIG . chomp }
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Bob Smith
ABC Organisation
EXAMPLE IT - Service GmbH
Dorten 5 F & E
12345 Da / Germany
Phone : + 49 ( 0 ) 1234 567 890 / + 49 ( 0 ) 1234 567 891
Fax : + 49 ( 0 ) 1234 567 892
SIG
it 'converts messages (via #html2text) then returns the line of the message where the signature begins' do
2019-04-15 01:41:17 +00:00
expect ( described_class . find_signature_line ( signature , content , content_type ) ) . to eq ( 11 )
2019-03-20 06:42:04 +00:00
end
end
end
end
describe '.rebuild_all_articles' do
context 'when a user exists with a recorded signature' do
2020-06-19 09:17:18 +00:00
let! ( :customer ) { create ( :customer , preferences : { signature_detection : " \n bar " } ) }
2019-03-20 06:42:04 +00:00
context 'and multiple articles exist for that customer' do
let! ( :articles ) do
[ create ( :ticket_article , created_by_id : customer . id , body : " foo \n foo \n bar " ) ,
create ( :ticket_article , created_by_id : customer . id , body : " foo \n bar " ) ]
end
it 'updates the signature-line data of all articles' do
2019-04-15 01:41:17 +00:00
expect { described_class . rebuild_all_articles }
2019-03-20 06:42:04 +00:00
. to change { articles . first . reload . preferences [ :signature_detection ] } . to ( 3 )
. and change { articles . second . reload . preferences [ :signature_detection ] } . to ( 2 )
end
end
end
end
end