2015-04-27 19:24:14 +00:00
|
|
|
# rubocop:disable all
|
2018-06-05 06:40:40 +00:00
|
|
|
|
2012-05-04 11:33:05 +00:00
|
|
|
require 'test_helper'
|
2014-12-29 08:31:44 +00:00
|
|
|
|
2012-05-04 11:33:05 +00:00
|
|
|
class EmailParserTest < ActiveSupport::TestCase
|
2018-08-22 10:05:14 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
to write new .yml files for emails you can use the following code:
|
|
|
|
|
2018-09-07 12:20:11 +00:00
|
|
|
File.write('test/data/mail/mailXXX.yml', Channel::EmailParser.new.parse(File.read('test/data/mail/mailXXX.box')).slice(:from, :from_email, :from_display_name, :to, :cc, :subject, :body, :content_type, :'reply-to', :attachments).to_yaml)
|
2018-08-22 10:05:14 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-05-04 11:33:05 +00:00
|
|
|
test 'parse' do
|
2018-06-05 06:40:40 +00:00
|
|
|
msg_files = Dir.glob(Rails.root.join('test', 'data', 'mail', 'mail*.box')).sort
|
2018-09-07 12:20:11 +00:00
|
|
|
messages = []
|
|
|
|
msg_files.each do |f|
|
|
|
|
next if !File.exists?(f.ext('yml'))
|
|
|
|
item = {
|
|
|
|
source: File.basename(f),
|
|
|
|
content: YAML.load(File.read(f.ext('yml'))),
|
|
|
|
parsed: Channel::EmailParser.new.parse(File.read(f)),
|
|
|
|
}
|
|
|
|
messages.push item
|
|
|
|
end
|
2018-06-05 06:40:40 +00:00
|
|
|
|
|
|
|
messages.each do |m|
|
2018-09-07 12:20:11 +00:00
|
|
|
|
2018-06-05 06:40:40 +00:00
|
|
|
# assert: raw content hash is a subset of parsed message hash
|
2018-07-04 15:03:04 +00:00
|
|
|
expected_msg = m[:content].except(:attachments)
|
|
|
|
parsed_msg = m[:parsed].slice(*expected_msg.keys)
|
2018-08-22 10:05:14 +00:00
|
|
|
|
|
|
|
expected_msg.each do |key, value|
|
2018-09-25 13:27:54 +00:00
|
|
|
if value.nil?
|
|
|
|
assert_nil(parsed_msg[key], "parsed message data does not match test/data/mail/#{m[:source]}: #{key}")
|
|
|
|
else
|
|
|
|
assert_equal(value, parsed_msg[key], "parsed message data does not match test/data/mail/#{m[:source]}: #{key}")
|
|
|
|
end
|
2018-08-22 10:05:14 +00:00
|
|
|
end
|
2018-06-05 06:40:40 +00:00
|
|
|
|
|
|
|
# assert: attachments in parsed message hash match metadata in raw hash
|
|
|
|
next if m[:content][:attachments].blank?
|
2018-09-07 12:20:11 +00:00
|
|
|
attachments_found = []
|
|
|
|
m[:content][:attachments].each do |expected_attachment|
|
|
|
|
expected_attachment_md5 = Digest::MD5.hexdigest(expected_attachment[:data])
|
|
|
|
m[:parsed][:attachments].each do |parsed_attachment|
|
|
|
|
parsed_attachment_md5 = Digest::MD5.hexdigest(parsed_attachment[:data])
|
|
|
|
next if attachments_found.include?(parsed_attachment_md5)
|
|
|
|
next if expected_attachment_md5 != parsed_attachment_md5
|
|
|
|
attachments_found.push parsed_attachment_md5
|
|
|
|
expected_attachment.each do |key, value|
|
|
|
|
assert_equal(value, parsed_attachment[key], "#{key} is different")
|
|
|
|
end
|
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
next if attachments_found.count == m[:content][:attachments].count
|
|
|
|
m[:content][:attachments].each do |expected_attachment|
|
|
|
|
next if attachments_found.include?(Digest::MD5.hexdigest(expected_attachment[:data]))
|
|
|
|
assert(false, "Attachment not found test/data/mail/#{m[:source]}: #{expected_attachment.inspect}")
|
2012-07-02 18:52:27 +00:00
|
|
|
end
|
2018-06-05 06:40:40 +00:00
|
|
|
end
|
2012-05-04 11:33:05 +00:00
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|