github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/tests/system/tests/sendmail_attachment.rb (about)

     1  require_relative '../boot'
     2  require 'minitest/autorun'
     3  require 'pry-rescue/minitest' unless ENV['CI']
     4  
     5  describe "Sending a mail" do
     6    it "can be done with an attachment" do
     7      Helpers.scenario "sendmail_attachment"
     8      Helpers.start_mailhog
     9  
    10      inst = Instance.create locale: "en"
    11      bytes = File.read("../fixtures/wet-cozy_20160910__M4Dz.jpg")
    12      bytes = bytes.force_encoding(Encoding::BINARY)
    13      encoded = Base64.encode64(bytes)
    14      args = {
    15        mode: "from",
    16        to: [
    17          { name: "Jane Doe", email: "jane@cozy.tools" }
    18        ],
    19        subject: "attachment test",
    20        parts: [
    21          { type: "text/html", body: "<html><body><p>This is a test!</p></body></html>" },
    22          { type: "text/plain", body: "This is a test!" }
    23        ],
    24        attachments: [
    25          { filename: "wet.jpg", content: encoded }
    26        ]
    27      }
    28      Job.create inst, "sendmail", args
    29  
    30      received = []
    31      30.times do
    32        sleep 1
    33        received = Email.received kind: "to", query: "jane@cozy.tools"
    34        break if received.any?
    35      end
    36      refute_empty received
    37      mail = received.first
    38      assert_equal mail.subject, args[:subject]
    39      assert mail.parts.length > 2
    40      disposition = mail.parts.dig 1, "Headers", "Content-Disposition", 0
    41      assert_equal "attachment; filename=\"wet.jpg\"", disposition
    42      encoding = mail.parts.dig 1, "Headers", "Content-Transfer-Encoding", 0
    43      assert_equal encoding, "base64"
    44      ctype = mail.parts.dig(1, "Headers", "Content-Type", 0).split(";")[0]
    45      assert_equal ctype, "image/jpeg"
    46      decoded = Base64.decode64(mail.parts.dig(1, "Body"))
    47      assert_equal decoded, bytes
    48  
    49      inst.remove
    50    end
    51  end