<commit_before>require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe MailSafe do
  TEXT_POSTSCRIPT_PHRASE = /The original recipients were:/
  HTML_POSTSCRIPT_PHRASE = /<p>\s+The original recipients were:\s+<\/p>/

  def deliver_message(message_name, *args)
    if ActionMailer::VERSION::MAJOR < 3
      TestMailer.send("deliver_#{message_name}", *args)
    else
      TestMailer.send(message_name, *args).deliver
    end
  end

  describe 'Delivering a plain text email to internal addresses' do
    before(:each) do
      MailSafe::Config.stub(:is_internal_address?).and_return(true)
      @email = deliver_message(:plain_text_message, :to => '<EMAIL>', :bcc => '<EMAIL>', :cc => '<EMAIL>')
    end

    it 'sends the email to the original addresses' do
      @email.to.should  =~ ['<EMAIL>']
      @email.cc.should  =~ ['<EMAIL>']
      @email.bcc.should =~ ['<EMAIL>']
    end

    it 'does not add a post script to the body' do
       @email.body.should_not =~ TEXT_POSTSCRIPT_PHRASE
    end
  end

  describe 'Delivering a plain text email to external addresses' do
    before(:each) do
      MailSafe::Config.stub(:is_internal_address?).and_return(false)
      MailSafe::Config.stub(:get_replacement_address).and_return('<EMAIL>')
      @email = deliver_message(:plain_text_message, :to => '<EMAIL>', :bcc => '<EMAIL>', :cc => '<EMAIL>')
    end

    it 'sends the email to the replacement address' do
      @email.to.should  =~ ['<EMAIL>'] 
      @email.cc.should  =~ ['<EMAIL>']
      @email.bcc.should =~ ['<EMAIL>']
    end
  end

  def deliver_email_with_mix_of_internal_and_external_addresses(message_name)
    MailSafe::Config.internal_address_definition = /internal/
    MailSafe::Config.replacement_address = '<EMAIL>'
    @email = deliver_message(message_name,
      {
        :to  => ['<EMAIL>', '<EMAIL>'],
        :cc  => ['<EMAIL>', '<EMAIL>'],
        :bcc => ['<EMAIL>', '<EMAIL>']
      }
    )
  end

  describe 'Delivering a plain text email to a mix of internal and external addresses' do
    before(:each) do
      deliver_email_with_mix_of_internal_and_external_addresses(:plain_text_message)
    end

    it 'sends the email to the appropriate address' do
      @email.to.should  =~ ['<EMAIL>', '<EMAIL>']
      @email.cc.should  =~ ['<EMAIL>', '<EMAIL>']
      @email.bcc.should =~ ['<EMAIL>',   '<EMAIL>']
    end

    it 'adds a plain text post script to the body' do
      @email.body.should =~ TEXT_POSTSCRIPT_PHRASE
    end
  end

  describe 'Delivering an html email to a mix of internal and external addresses' do
    before(:each) do
      deliver_email_with_mix_of_internal_and_external_addresses(:html_message)
    end

    it 'adds an html post script to the body' do
      @email.body.should =~ HTML_POSTSCRIPT_PHRASE
    end
  end

  describe 'Delivering a multipart email to a mix of internal and external addresses' do
    before(:each) do
      deliver_email_with_mix_of_internal_and_external_addresses(:multipart_message)
    end

    def part(type)
      @email.parts.detect { |p| p.content_type == type }.body
    end

    it 'adds a text post script to the body of the text part' do
      part('text/plain').should =~ TEXT_POSTSCRIPT_PHRASE
    end

    it 'adds an html post script to the body of the html part' do
      part('text/html').should =~  HTML_POSTSCRIPT_PHRASE
    end
  end
end<commit_msg>With ActionMailer 3, @email.body is no longer a string but is an object, so we need to add #to_s calls in order to match the body to a regex.<commit_after>require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe MailSafe do
  TEXT_POSTSCRIPT_PHRASE = /The original recipients were:/
  HTML_POSTSCRIPT_PHRASE = /<p>\s+The original recipients were:\s+<\/p>/

  def deliver_message(message_name, *args)
    if ActionMailer::VERSION::MAJOR < 3
      TestMailer.send("deliver_#{message_name}", *args)
    else
      TestMailer.send(message_name, *args).deliver
    end
  end

  describe 'Delivering a plain text email to internal addresses' do
    before(:each) do
      MailSafe::Config.stub(:is_internal_address?).and_return(true)
      @email = deliver_message(:plain_text_message, :to => '<EMAIL>', :bcc => '<EMAIL>', :cc => '<EMAIL>')
    end

    it 'sends the email to the original addresses' do
      @email.to.should  =~ ['<EMAIL>']
      @email.cc.should  =~ ['<EMAIL>']
      @email.bcc.should =~ ['<EMAIL>']
    end

    it 'does not add a post script to the body' do
       @email.body.to_s.should_not =~ TEXT_POSTSCRIPT_PHRASE
    end
  end

  describe 'Delivering a plain text email to external addresses' do
    before(:each) do
      MailSafe::Config.stub(:is_internal_address?).and_return(false)
      MailSafe::Config.stub(:get_replacement_address).and_return('<EMAIL>')
      @email = deliver_message(:plain_text_message, :to => '<EMAIL>', :bcc => '<EMAIL>', :cc => '<EMAIL>')
    end

    it 'sends the email to the replacement address' do
      @email.to.should  =~ ['<EMAIL>'] 
      @email.cc.should  =~ ['<EMAIL>']
      @email.bcc.should =~ ['<EMAIL>']
    end
  end

  def deliver_email_with_mix_of_internal_and_external_addresses(message_name)
    MailSafe::Config.internal_address_definition = /internal/
    MailSafe::Config.replacement_address = '<EMAIL>'
    @email = deliver_message(message_name,
      {
        :to  => ['<EMAIL>', '<EMAIL>'],
        :cc  => ['<EMAIL>', '<EMAIL>'],
        :bcc => ['<EMAIL>', '<EMAIL>']
      }
    )
  end

  describe 'Delivering a plain text email to a mix of internal and external addresses' do
    before(:each) do
      deliver_email_with_mix_of_internal_and_external_addresses(:plain_text_message)
    end

    it 'sends the email to the appropriate address' do
      @email.to.should  =~ ['<EMAIL>', '<EMAIL>']
      @email.cc.should  =~ ['<EMAIL>', '<EMAIL>']
      @email.bcc.should =~ ['<EMAIL>',   '<EMAIL>']
    end

    it 'adds a plain text post script to the body' do
      @email.body.to_s.should =~ TEXT_POSTSCRIPT_PHRASE
    end
  end

  describe 'Delivering an html email to a mix of internal and external addresses' do
    before(:each) do
      deliver_email_with_mix_of_internal_and_external_addresses(:html_message)
    end

    it 'adds an html post script to the body' do
      @email.body.to_s.should =~ HTML_POSTSCRIPT_PHRASE
    end
  end

  describe 'Delivering a multipart email to a mix of internal and external addresses' do
    before(:each) do
      deliver_email_with_mix_of_internal_and_external_addresses(:multipart_message)
    end

    def part(type)
      @email.parts.detect { |p| p.content_type == type }.body.to_s
    end

    it 'adds a text post script to the body of the text part' do
      part('text/plain').should =~ TEXT_POSTSCRIPT_PHRASE
    end

    it 'adds an html post script to the body of the html part' do
      part('text/html').should =~  HTML_POSTSCRIPT_PHRASE
    end
  end
end
{'fraction_non_alphanumeric': 0.12380400115975645, 'fraction_numerical': 0.0004349086691794723, 'mean_word_length': 3.397068196303378, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '5596829', 'n_tokens_mistral': 2344, 'n_tokens_neox': 2306, 'n_words': 601}