|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +require "sup" |
| 4 | + |
| 5 | +class DummySelector |
| 6 | + attr_accessor :val |
| 7 | + def initialize val |
| 8 | + @val = val |
| 9 | + end |
| 10 | +end |
| 11 | + |
| 12 | +class DummyCryptoManager |
| 13 | + def have_crypto?; true; end |
| 14 | + def sign from, to, payload |
| 15 | + envelope = RMail::Message.new |
| 16 | + envelope.header["Content-Type"] = "multipart/signed; protocol=testdummy" |
| 17 | + envelope.add_part payload |
| 18 | + envelope |
| 19 | + end |
| 20 | +end |
| 21 | + |
| 22 | +class TestEditMessageMode < Minitest::Test |
| 23 | + def setup |
| 24 | + $config = {} |
| 25 | + @path = Dir.mktmpdir |
| 26 | + Redwood::HookManager.init File.join(@path, "hooks") |
| 27 | + Redwood::AccountManager.init :default => {name: "test", email: "sender@example.invalid"} |
| 28 | + Redwood::CryptoManager.instance_variable_set :@instance, DummyCryptoManager.new |
| 29 | + end |
| 30 | + |
| 31 | + def teardown |
| 32 | + Redwood::CryptoManager.deinstantiate! |
| 33 | + Redwood::AccountManager.deinstantiate! |
| 34 | + Redwood::HookManager.deinstantiate! |
| 35 | + FileUtils.rm_r @path |
| 36 | + $config = nil |
| 37 | + end |
| 38 | + |
| 39 | + def test_attachment_content_transfer_encoding_signed |
| 40 | + ## RMail::Message#make_attachment will choose |
| 41 | + ## Content-Transfer-Encoding: 8bit for a CSV file. |
| 42 | + attachment_filename = File.join @path, "dummy.csv" |
| 43 | + ## Include some high bytes in the attachment contents in order to |
| 44 | + ## exercise quote-printable transfer encoding. |
| 45 | + File.write attachment_filename, "löl,\ntest,\n" |
| 46 | + |
| 47 | + opts = { |
| 48 | + :header => { |
| 49 | + "From" => "sender@example.invalid", |
| 50 | + "To" => "recip@example.invalid", |
| 51 | + }, |
| 52 | + :attachments => { |
| 53 | + "dummy.csv" => RMail::Message.make_file_attachment(attachment_filename), |
| 54 | + }, |
| 55 | + } |
| 56 | + mode = Redwood::EditMessageMode.new opts |
| 57 | + mode.instance_variable_set :@crypto_selector, DummySelector.new(:sign) |
| 58 | + |
| 59 | + msg = mode.send :build_message, Time.now |
| 60 | + ## The outermost message is a (fake) multipart/signed created by DummyCryptoManager#send. |
| 61 | + ## Inside that we have our inline message at index 0 and CSV attachment at index 1. |
| 62 | + attachment = msg.part(0).part(1) |
| 63 | + ## The attachment should have been re-encoded as quoted-printable for GPG signing. |
| 64 | + assert_equal "l=C3=B6l,\ntest,\n", attachment.body |
| 65 | + ## There shouldn't be multiple Content-Transfer-Encoding headers. |
| 66 | + ## This was: https://github.com/sup-heliotrope/sup/issues/502 |
| 67 | + assert_equal ["quoted-printable"], attachment.header.fetch_all("Content-Transfer-Encoding") |
| 68 | + end |
| 69 | +end |
0 commit comments