trabajo-afectivo/test/unit/email_parser_test.rb

55 lines
2.3 KiB
Ruby
Raw Normal View History

2015-04-27 19:24:14 +00:00
# rubocop:disable all
2018-06-05 06:40:40 +00:00
require 'test_helper'
2014-12-29 08:31:44 +00:00
class EmailParserTest < ActiveSupport::TestCase
test 'parse' do
2018-06-05 06:40:40 +00:00
msg_files = Dir.glob(Rails.root.join('test', 'data', 'mail', 'mail*.box')).sort
messages = msg_files.select { |f| File.exists?(f.ext('yml')) }
.map do |f|
{
source: File.basename(f),
content: YAML.load(File.read(f.ext('yml'))),
parsed: Channel::EmailParser.new.parse(File.read(f)),
}
end
messages.each do |m|
# assert: raw content hash is a subset of parsed message hash
expected_msg = m[:content].except(:attachments)
parsed_msg = m[:parsed].slice(*expected_msg.keys)
failure_msg = [parsed_msg, expected_msg]
.map(&:to_a).map(&:sort).reduce(&:zip)
.reject { |a| a.uniq.one? }
.map { |a, b| "#{a.first.upcase}\n #{m[:source]}: #{a.last}\n #{m[:source].ext('yml')}: #{b.last}" }
.join("\n")
assert_operator(expected_msg, :<=, parsed_msg,
"parsed message data does not match message content:\n" +
failure_msg)
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?
# the formats of m[:content][:attachments] and m[:parsed][:attachments] don't match,
# so we have to convert one to the other
parsed_attachment_metadata = m[:parsed][:attachments].map do |a|
{
md5: Digest::MD5.hexdigest(a[:data]),
cid: a[:preferences]['Content-ID'],
filename: a[:filename],
}.with_indifferent_access
end
m[:content][:attachments].sort_by { |a| a[:md5] }
.zip(parsed_attachment_metadata.sort_by { |a| a[:md5] })
.each do |content, parsed|
assert_operator(content, :<=, parsed,
"parsed attachment data from #{m[:source]} does not match " \
"attachment metadata from #{m[:source].ext('yml')}")
2012-07-02 18:52:27 +00:00
end
2018-06-05 06:40:40 +00:00
end
end
end