sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/

test/unit/test_edit_message_mode.rb (3285B) - raw

      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     account = {
     28       :name => +"test",
     29       :email => +"sender@example.invalid",
     30       :sendmail => "/bin/false",
     31     }
     32     Redwood::AccountManager.init :default => account
     33     Redwood::CryptoManager.instance_variable_set :@instance, DummyCryptoManager.new
     34   end
     35 
     36   def teardown
     37     Redwood::CryptoManager.deinstantiate!
     38     Redwood::AccountManager.deinstantiate!
     39     Redwood::HookManager.deinstantiate!
     40     FileUtils.rm_r @path
     41     $config = nil
     42   end
     43 
     44   def test_attachment_content_transfer_encoding
     45     ## RMail::Message#make_attachment will choose
     46     ## Content-Transfer-Encoding: 8bit for a CSV file.
     47     ## If we're not GPG signing or encrypting then the attachment will be sent
     48     ## as is. Note this assumes the SMTP servers in the delivery path all
     49     ## support the 8BITMIME extension.
     50     attachment_content = "löl,\ntest,\n"
     51     attachment_filename = File.join @path, "dummy.csv"
     52     File.write attachment_filename, attachment_content
     53 
     54     opts = {
     55       :header => {
     56         "From" => +"sender@example.invalid",
     57         "To" => +"recip@example.invalid",
     58       },
     59       :attachments => {
     60         "dummy.csv" => RMail::Message.make_file_attachment(attachment_filename),
     61       },
     62     }
     63     mode = Redwood::EditMessageMode.new opts
     64 
     65     msg = mode.send :build_message, Time.now
     66     attachment = msg.part(1)
     67     assert_equal attachment_content, attachment.body
     68     assert_equal "8bit", attachment.header["Content-Transfer-Encoding"]
     69   end
     70 
     71   def test_attachment_content_transfer_encoding_signed
     72     attachment_filename = File.join @path, "dummy.csv"
     73     ## Include some high bytes in the attachment contents in order to
     74     ## exercise quote-printable transfer encoding.
     75     File.write attachment_filename, "löl,\ntest,\n"
     76 
     77     opts = {
     78       :header => {
     79         "From" => +"sender@example.invalid",
     80         "To" => +"recip@example.invalid",
     81       },
     82       :attachments => {
     83         "dummy.csv" => RMail::Message.make_file_attachment(attachment_filename),
     84       },
     85     }
     86     mode = Redwood::EditMessageMode.new opts
     87     mode.instance_variable_set :@crypto_selector, DummySelector.new(:sign)
     88 
     89     msg = mode.send :build_message, Time.now
     90     ## The outermost message is a (fake) multipart/signed created by DummyCryptoManager#send.
     91     ## Inside that we have our inline message at index 0 and CSV attachment at index 1.
     92     attachment = msg.part(0).part(1)
     93     ## The attachment should have been re-encoded as quoted-printable for GPG signing.
     94     assert_equal "l=C3=B6l,\ntest,\n", attachment.body
     95     ## There shouldn't be multiple Content-Transfer-Encoding headers.
     96     ## This was: https://github.com/sup-heliotrope/sup/issues/502
     97     assert_equal ["quoted-printable"], attachment.header.fetch_all("Content-Transfer-Encoding")
     98   end
     99 end