<commit_before># Encoding: UTF-8
require "spec_helper"

describe IncomingEmailMailer do

  signed_in_as '<EMAIL>'

  let(:organization){ current_user.organizations.find_by_slug! 'raceteam' }
  let(:conversation){ organization.conversations.find_by_slug! 'layup-body-carbon' }
  let(:group) { organization.groups.find_by_email_address_tag('electronics') }

  let :params do
    create_incoming_email_params(
      subject:       subject_line,
      recipient:     recipient,
      body_html:     body_html,
      body_plain:    body_plain,
    )
  end
  let(:incoming_email){ threadable.incoming_emails.create!(params).first }

  let(:subject_line) { 'i am a subject line, see me subject' }
  let(:body_plain)   { 'i am a body, watch me bod.' }
  let(:body_html)    { '<body>i am a <strong>body</strong>, watch me bod.</body>' }
  let(:recipient)    { 'raceteam@localhost' }
  let(:to)           { 'UCSD Electric Racing <raceteam@localhost>' }

  before do
    incoming_email.find_organization!
    incoming_email.find_groups!
  end

  describe "#message_held_notice" do
    let(:mail){ described_class.new(threadable).generate(:message_held_notice, incoming_email) }

    it "sends a held notice" do
      expect(mail.subject).to eq "[message held] i am a subject line, see me subject"
      expect(mail.to     ).to eq [incoming_email.envelope_from]

      expect(mail.smtp_envelope_from).to eq "no-reply-auto@localhost"
    end
  end

  describe "#message_bounced_dsn" do

    let(:text_part)             { mail.parts[0] }
    let(:delivery_status_part)  { mail.parts[1] }
    let(:original_headers_part) { mail.parts[2] }

    let(:mail){ described_class.new(threadable).generate(:message_bounced_dsn, incoming_email) }

    context 'when the organization/group cannot be found' do
      let(:recipient)    { 'not-there<EMAIL>' }
      let(:to)           { 'This is wrong <<EMAIL>>' }

      it "sends a delivery status notification" do
        expect(mail.subject).to eq "Delivery Status Notification (Failure)"

        expect(mail.header['In-Reply-To'].to_s).to eq params['Message-Id']
        expect(mail.header['References'].to_s).to  eq params['Message-Id']

        expect(mail.to).to eq [params['X-Envelope-From']]
        expect(mail.from).to eq ['no-reply-auto@localhost']
        expect(mail.smtp_envelope_from).to eq "no-reply-auto@localhost"

        expect(mail.content_type).to include 'multipart/report'
        expect(mail.content_type).to include 'report-type=delivery-status'

        expect(text_part.content_type).to eq 'text/plain'
        expect(text_part.body).to include subject_line
        expect(text_part.body).to include 'The threadable organization/group you tried to reach does not exist.'

        expect(delivery_status_part.content_type).to eq 'message/delivery-status'

        expect(delivery_status_part.body.to_s).to include "Final-Recipient: rfc822;not-there@localhost"
        expect(delivery_status_part.body.to_s).to include "Original-Recipient: rfc822;not-there@localhost"
        expect(delivery_status_part.body.to_s).to include "Action: failed"
        expect(delivery_status_part.body.to_s).to include "Status: 5.1.1"
        expect(delivery_status_part.body.to_s).to include "Remote-MTA: dns;mxa.127.0.0.1"
        expect(delivery_status_part.body.to_s).to include "Diagnostic-Code: smtp; 550-5.1.1"

        expect(original_headers_part.content_type).to eq 'message/rfc822'

        JSON.parse(incoming_email.params['message-headers']).each do |header_and_value|
          (header, value) = header_and_value
          header_object = Mail::Header.new
          header_object.fields = ["#{header}: #{value}"]
          expect(original_headers_part.body.to_s).to include header_object.to_s.gsub(/\r/, '')
        end
      end
    end

    context 'with a blank message' do
      let(:subject_line) { '' }
      let(:body_plain)   { '' }
      let(:body_html)    { '' }

      it 'bounces the message with a different error message' do
        expect(delivery_status_part.body.to_s).to include "Diagnostic-Code: smtp; 550-5.6.0"<commit_msg>test presence of other bounce headers, original body in dsn.<commit_after># Encoding: UTF-8
require "spec_helper"

describe IncomingEmailMailer do

  signed_in_as '<EMAIL>'

  let(:organization){ current_user.organizations.find_by_slug! 'raceteam' }
  let(:conversation){ organization.conversations.find_by_slug! 'layup-body-carbon' }
  let(:group) { organization.groups.find_by_email_address_tag('electronics') }

  let :params do
    create_incoming_email_params(
      subject:       subject_line,
      recipient:     recipient,
      body_html:     body_html,
      body_plain:    body_plain,
    )
  end
  let(:incoming_email){ threadable.incoming_emails.create!(params).first }

  let(:subject_line) { 'i am a subject line, see me subject' }
  let(:body_plain)   { 'i am a body, watch me bod.' }
  let(:body_html)    { '<body>i am a <strong>body</strong>, watch me bod.</body>' }
  let(:recipient)    { '<EMAIL>@<EMAIL>' }
  let(:to)           { 'UCSD Electric Racing <<EMAIL>>' }

  before do
    incoming_email.find_organization!
    incoming_email.find_groups!
  end

  describe "#message_held_notice" do
    let(:mail){ described_class.new(threadable).generate(:message_held_notice, incoming_email) }

    it "sends a held notice" do
      expect(mail.subject).to eq "[message held] i am a subject line, see me subject"
      expect(mail.to     ).to eq [incoming_email.envelope_from]

      expect(mail.smtp_envelope_from).to eq "no-reply-auto@localhost"
    end
  end

  describe "#message_bounced_dsn" do

    let(:text_part)             { mail.parts[0] }
    let(:delivery_status_part)  { mail.parts[1] }
    let(:original_mail_part) { mail.parts[2] }

    let(:mail){ described_class.new(threadable).generate(:message_bounced_dsn, incoming_email) }

    context 'when the organization/group cannot be found' do
      let(:recipient)    { 'not-there<EMAIL>' }
      let(:to)           { 'This is wrong <not-there@localhost>' }

      it "sends a delivery status notification" do
        expect(mail.subject).to eq "Delivery Status Notification (Failure)"

        expect(mail.header['In-Reply-To'].to_s).to eq params['Message-Id']
        expect(mail.header['References'].to_s).to  eq params['Message-Id']

        expect(mail.to).to eq [params['X-Envelope-From']]
        expect(mail.from).to eq ['no-reply-auto@localhost']
        expect(mail.smtp_envelope_from).to eq ""

        expect(mail.content_type).to include 'multipart/report'
        expect(mail.content_type).to include 'report-type=delivery-status'

        expect(text_part.content_type).to eq 'text/plain'
        expect(text_part.body).to include subject_line
        expect(text_part.body).to include 'The threadable organization/group you tried to reach does not exist.'

        expect(delivery_status_part.content_type).to eq 'message/delivery-status'

        expect(delivery_status_part.body.to_s).to include "Reporting-MTA: dns;mxa.127.0.0.1"
        expect(delivery_status_part.body.to_s).to include "Arrival-Date:"
        expect(delivery_status_part.body.to_s).to include "Final-Recipient: rfc822;not-there@localhost"
        expect(delivery_status_part.body.to_s).to include "Original-Recipient: rfc822;not-there@localhost"
        expect(delivery_status_part.body.to_s).to include "Action: failed"
        expect(delivery_status_part.body.to_s).to include "Status: 5.1.1"
        expect(delivery_status_part.body.to_s).to include "Diagnostic-Code: smtp; 550-5.1.1"

        expect(original_mail_part.content_type).to eq 'message/rfc822'

        JSON.parse(incoming_email.params['message-headers']).each do |header_and_value|
          (header, value) = header_and_value
          header_object = Mail::Header.new
          header_object.fields = ["#{header}: #{value}"]
          expect(original_mail_part.body.to_s).to include header_object.to_s.gsub(/\r/, '')
        end

        expect(original_mail_part.body.to_s).to include "i am a body"
      end
    end

    context 'with a blank message' do
      let(:subject_line) { '' }
      let(:body_plain)   { '' }
      let(:body_html)    { '' }

      it 'bounces the message with a different error message' do
        expect(delivery_status_part.body.to_s).to include "Diagnostic-Code: smtp; 550-5.6.0"
{'fraction_non_alphanumeric': 0.12444551013067977, 'fraction_numerical': 0.008152499700275746, 'mean_word_length': 3.420773714891362, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3301943', 'n_tokens_mistral': 2788, 'n_tokens_neox': 2699, 'n_words': 526}