<commit_before># encoding: utf-8
require 'spec_helper'

describe Mail::Message do

  def basic_email
    "To: mikel\r\nFrom: bob\r\nSubject: Hello!\r\n\r\nemail message\r\n"
  end

  describe "initialization" do

    it "should instantiate empty" do
      Mail::Message.new.class.should == Mail::Message
    end

    it "should return a basic email" do
      mail = Mail.new
      mail = Mail.new(mail.to_s)
      mail.date.should_not be_blank
      mail.message_id.should_not be_blank
      mail.mime_version.should == "1.0"
      mail.content_type.should == "text/plain"
      mail.content_transfer_encoding.should == "7bit"
      mail.subject.should be_blank
      mail.body.should be_blank
    end

    it "should instantiate with a string" do
      Mail::Message.new(basic_email).class.should == Mail::Message
    end

    it "should allow us to pass it a block" do
      mail = Mail::Message.new do
        from '<EMAIL>'
        to '<EMAIL>'
      end
      mail.from.should == ['<EMAIL>']
      mail.to.should == ['<EMAIL>']
    end

    it "should initialize a body and header class even if called with nothing to begin with" do
      mail = Mail::Message.new
      mail.header.class.should == Mail::Header
      mail.body.class.should == Mail::Body
    end

    it "should not report basic emails as bounced" do
      Mail::Message.new.should_not be_bounced
    end

    it "should be able to parse a basic email" do
      doing { Mail::Message.new(File.read(fixture('emails', 'plain_emails', 'basic_email.eml'))) }.should_not raise_error
    end

    it "should be able to parse an email with only blank lines as body" do
      doing { Mail::Message.new(File.read(fixture('emails', 'error_emails', 'missing_body.eml'))) }.should_not raise_error
    end

    it "should be able to parse an email with a funky date header" do
      doing { Mail::Message.new(File.read(fixture('emails', 'error_emails', 'bad_date_header2.eml'))) }
    end

    it 'should be able to invoke subject on a funky subject header' do
      Mail::Message.new(File.read(fixture('emails', 'error_emails', 'bad_subject.eml'))).subject
    end


    it 'should be able to parse an email missing an encoding' do
      Mail::Message.new(File.read(fixture('emails', 'error_emails', 'must_supply_encoding.eml')))
    end

    it "should be able to parse every email example we have without raising an exception" do
      emails = Dir.glob( fixture('emails/**/*') ).delete_if { |f| File.directory?(f) }

      STDERR.stub!(:puts) # Don't want to get noisy about any warnings
      errors = false
      expected_failures = []
      emails.each do |email|
        begin
          Mail::Message.new(File.read(email))
        rescue => e
          unless expected_failures.include?(email)
            puts "Failed on email #{email}"
            puts "Failure was:\n#{e}\n\n"
            errors = true
          end
        end
      end
      errors.should be_false
    end

    it "should not raise a warning on having non US-ASCII characters in the header (should just handle it)" do
      STDERR.should_not_receive(:puts)
      Mail::Message.new(File.read(fixture('emails', 'plain_emails', 'raw_email_string_in_date_field.eml')))
    end

    it "should raise a warning (and keep parsing) on having an incorrectly formatted header" do
      STDERR.should_receive(:puts).with("WARNING: Could not parse (and so ignorning) 'quite Delivered-To: <EMAIL>'")
      Mail::Message.new(File.read(fixture('emails', 'plain_emails', 'raw_email_incorrect_header.eml')))
    end

    it "should read in an email message and basically parse it" do
      mail = Mail::Message.new(File.read(fixture('emails', 'plain_emails', 'basic_email.eml')))
      mail.to.should == ["<EMAIL>"]
    end

    it "should not fail parsing message with caps in content_type" do
      mail = Mail::Message.new(File.read(fixture('emails', 'plain_emails', 'mix_caps_content_type.eml')))
      mail.content_type.should == 'text/plain; charset=iso-8859-1'
      mail.main_type.should == 'text'
      mail.sub_type.should == 'plain'
    end

    it "should be able to pass an empty reply-to header" do
      mail = Mail.new(File.read(fixture('emails', 'error_emails', 'empty_in_reply_to.eml')))
      mail.in_reply_to.should be_blank
    end

    describe "YAML serialization" do
      before(:each) do
        @yaml_mail = Mail::Message.new(:to => '<EMAIL>',
                                  :cc => '<EMAIL>',
                                  :bcc => '<EMAIL>',
                                  :body => 'body',
                                  :subject => 'subject')
        @smtp_settings = { :address=>"smtp.somewhere.net",
          :port=>"587", :domain=>"somewhere.net", :user_name=>"<EMAIL>",
          :password=>"password", :authentication=>:plain, :enable_starttls_auto => true,
          :openssl_verify_mode => nil }
        @yaml_mail.delivery_method :smtp, @smtp_settings
      end

      it "should serialize the basic information to YAML" do
        yaml = @yaml_mail.to_yaml
        yaml_output = YAML.load(yaml)
        yaml_output['headers']['To'].should       == "<EMAIL>"
        yaml_output['headers']['Cc'].should       == "<EMAIL>"
        yaml_output['headers']['Subject'].should  == "subject"
        yaml_output['headers']['Bcc'].should      == "<EMAIL>"
        yaml_output['@body_raw'].should           == "body"
        yaml_output['@delivery_method'].should_not be_blank<commit_msg>Adding support for the :tls and :ssl options<commit_after># encoding: utf-8
require 'spec_helper'

describe Mail::Message do

  def basic_email
    "To: mikel\r\nFrom: bob\r\nSubject: Hello!\r\n\r\nemail message\r\n"
  end

  describe "initialization" do

    it "should instantiate empty" do
      Mail::Message.new.class.should == Mail::Message
    end

    it "should return a basic email" do
      mail = Mail.new
      mail = Mail.new(mail.to_s)
      mail.date.should_not be_blank
      mail.message_id.should_not be_blank
      mail.mime_version.should == "1.0"
      mail.content_type.should == "text/plain"
      mail.content_transfer_encoding.should == "7bit"
      mail.subject.should be_blank
      mail.body.should be_blank
    end

    it "should instantiate with a string" do
      Mail::Message.new(basic_email).class.should == Mail::Message
    end

    it "should allow us to pass it a block" do
      mail = Mail::Message.new do
        from '<EMAIL>'
        to '<EMAIL>'
      end
      mail.from.should == ['<EMAIL>']
      mail.to.should == ['<EMAIL>']
    end

    it "should initialize a body and header class even if called with nothing to begin with" do
      mail = Mail::Message.new
      mail.header.class.should == Mail::Header
      mail.body.class.should == Mail::Body
    end

    it "should not report basic emails as bounced" do
      Mail::Message.new.should_not be_bounced
    end

    it "should be able to parse a basic email" do
      doing { Mail::Message.new(File.read(fixture('emails', 'plain_emails', 'basic_email.eml'))) }.should_not raise_error
    end

    it "should be able to parse an email with only blank lines as body" do
      doing { Mail::Message.new(File.read(fixture('emails', 'error_emails', 'missing_body.eml'))) }.should_not raise_error
    end

    it "should be able to parse an email with a funky date header" do
      doing { Mail::Message.new(File.read(fixture('emails', 'error_emails', 'bad_date_header2.eml'))) }
    end

    it 'should be able to invoke subject on a funky subject header' do
      Mail::Message.new(File.read(fixture('emails', 'error_emails', 'bad_subject.eml'))).subject
    end


    it 'should be able to parse an email missing an encoding' do
      Mail::Message.new(File.read(fixture('emails', 'error_emails', 'must_supply_encoding.eml')))
    end

    it "should be able to parse every email example we have without raising an exception" do
      emails = Dir.glob( fixture('emails/**/*') ).delete_if { |f| File.directory?(f) }

      STDERR.stub!(:puts) # Don't want to get noisy about any warnings
      errors = false
      expected_failures = []
      emails.each do |email|
        begin
          Mail::Message.new(File.read(email))
        rescue => e
          unless expected_failures.include?(email)
            puts "Failed on email #{email}"
            puts "Failure was:\n#{e}\n\n"
            errors = true
          end
        end
      end
      errors.should be_false
    end

    it "should not raise a warning on having non US-ASCII characters in the header (should just handle it)" do
      STDERR.should_not_receive(:puts)
      Mail::Message.new(File.read(fixture('emails', 'plain_emails', 'raw_email_string_in_date_field.eml')))
    end

    it "should raise a warning (and keep parsing) on having an incorrectly formatted header" do
      STDERR.should_receive(:puts).with("WARNING: Could not parse (and so ignorning) 'quite Delivered-To: <EMAIL>'")
      Mail::Message.new(File.read(fixture('emails', 'plain_emails', 'raw_email_incorrect_header.eml')))
    end

    it "should read in an email message and basically parse it" do
      mail = Mail::Message.new(File.read(fixture('emails', 'plain_emails', 'basic_email.eml')))
      mail.to.should == ["<EMAIL>"]
    end

    it "should not fail parsing message with caps in content_type" do
      mail = Mail::Message.new(File.read(fixture('emails', 'plain_emails', 'mix_caps_content_type.eml')))
      mail.content_type.should == 'text/plain; charset=iso-8859-1'
      mail.main_type.should == 'text'
      mail.sub_type.should == 'plain'
    end

    it "should be able to pass an empty reply-to header" do
      mail = Mail.new(File.read(fixture('emails', 'error_emails', 'empty_in_reply_to.eml')))
      mail.in_reply_to.should be_blank
    end

    describe "YAML serialization" do
      before(:each) do
        @yaml_mail = Mail::Message.new(:to => '<EMAIL>',
                                  :cc => '<EMAIL>',
                                  :bcc => '<EMAIL>',
                                  :body => 'body',
                                  :subject => 'subject')
        @smtp_settings = { :address=>"smtp.somewhere.net",
          :port=>"587", :domain=>"somewhere.net", :user_name=>"<EMAIL>",
          :password=>"password", :authentication=>:plain, :enable_starttls_auto => true,
          :openssl_verify_mode => nil, :ssl=>nil, :tls=>nil }
        @yaml_mail.delivery_method :smtp, @smtp_settings
      end

      it "should serialize the basic information to YAML" do
        yaml = @yaml_mail.to_yaml
        yaml_output = YAML.load(yaml)
        yaml_output['headers']['To'].should       == "<EMAIL>"
        yaml_output['headers']['Cc'].should       == "<EMAIL>"
        yaml_output['headers']['Subject'].should  == "subject"
        yaml_output['headers']['Bcc'].should      == "<EMAIL>"
        yaml_output['@body_raw'].should           == "body"
        yaml_output['@delivery_method'].should_not be_blank
{'fraction_non_alphanumeric': 0.12615244180739388, 'fraction_numerical': 0.0023733455043359196, 'mean_word_length': 2.7418032786885247, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5185034', 'n_tokens_mistral': 3481, 'n_tokens_neox': 3369, 'n_words': 892}