<commit_before>require 'spec_helper'

describe PostfixLogLine do
  let(:line1) { "Apr  5 16:41:54 kedumba postfix/smtp[18733]: 39D9336AFA81: to=<<EMAIL>>, relay=foo.bar.com[1.2.3.4]:25, delay=92780, delays=92777/0.03/1.6/0.91, dsn=4.3.0, status=deferred (host foo.bar.com[1.2.3.4] said: 451 4.3.0 <<EMAIL>>: Temporary lookup failure (in reply to RCPT TO command))" }
  let(:line2) { "Apr  5 18:41:58 kedumba postfix/qmgr[2638]: E69DB36D4A2B: removed" }
  let(:line3) { "Apr  5 17:11:07 kedumba postfix/smtpd[7453]: connect from unknown[111.142.251.143]" }
  let(:line4) { "Apr  5 14:21:51 kedumba postfix/smtp[2500]: 39D9336AFA81: to=<<EMAIL>>, relay=aspmx.l.google.com[172.16.17.32]:25, delay=1, delays=0.08/0/0.58/0.34, dsn=5.1.1, status=bounced (host aspmx.l.google.com[172.16.17.32] said: 550-5.1.1 The email account that you tried to reach does not exist. zb4si15321910pbb.132 - gsmtp (in reply to RCPT TO command))" }

  context "one log line" do
    let (:l) do
      email = Email.create!(postfix_queue_id: "39D9336AFA81")
      PostfixLogLine.create_from_line(line1)
      PostfixLogLine.first
    end

    describe ".to" do
      it { l.to.should == "<EMAIL>" }
    end
    describe ".relay" do
      it { l.relay.should == "foo.bar.com[1.2.3.4]:25" }
    end
    describe ".delay" do
      it { l.delay.should == "92780" }
    end
    describe ".delays" do
      it { l.delays.should == "92777/0.03/1.6/0.91" }
    end
    describe ".dsn" do
      it { l.dsn.should == "4.3.0" }
    end
    describe ".status" do
      it { l.status.should == "deferred (host foo.bar.com[1.2.3.4] said: 451 4.3.0 <<EMAIL>>: Temporary lookup failure (in reply to RCPT TO command))" }
    end
  end

  describe ".create_from_line" do
    it "should extract and save relevant parts of the line" do
      email = Email.create!(postfix_queue_id: "39D9336AFA81")
      PostfixLogLine.create_from_line(line1)
      PostfixLogLine.count.should == 1
      line = email.postfix_log_lines.first
      line.text.should == "to=<<EMAIL>>, relay=foo.bar.com[1.2.3.4]:25, delay=92780, delays=92777/0.03/1.6/0.91, dsn=4.3.0, status=deferred (host foo.bar.com[1.2.3.4] said: 451 4.3.0 <<EMAIL>>: Temporary lookup failure (in reply to RCPT TO command))"
      line.time.should == Time.local(2013,4,5,16,41,54)
    end

    it "should save two lines if two lines are processed" do
      email = Email.create!(postfix_queue_id: "39D9336AFA81")
      PostfixLogLine.create_from_line(line1)
      PostfixLogLine.create_from_line(line4)
      email.postfix_log_lines.count.should == 2
    end

    it "should not reprocess duplicate lines" do
      email = Email.create!(postfix_queue_id: "39D9336AFA81")
      PostfixLogLine.create_from_line(line1)
      PostfixLogLine.create_from_line(line1)
      email.postfix_log_lines.count.should == 1
    end

    it "should not produce any log lines if the queue id is not recognised" do
      PostfixLogLine.should_receive(:puts).with("Skipping postfix queue id 39D9336AFA81 - it's not recognised")
      PostfixLogLine.create_from_line(line1)
      PostfixLogLine.count.should == 0
    end

    it "should only log lines that are delivery attempts" do
      PostfixLogLine.create_from_line(line2)
      PostfixLogLine.count.should == 0
    end<commit_msg>Test that PostfixLogLine.create_from_line also attaches log lines to the Delivery model<commit_after>require 'spec_helper'

describe PostfixLogLine do
  let(:line1) { "Apr  5 16:41:54 kedumba postfix/smtp[18733]: 39D9336AFA81: to=<<EMAIL>>, relay=foo.bar.com[1.2.3.4]:25, delay=92780, delays=92777/0.03/1.6/0.91, dsn=4.3.0, status=deferred (host foo.bar.com[1.2.3.4] said: 451 4.3.0 <<EMAIL>>: Temporary lookup failure (in reply to RCPT TO command))" }
  let(:line2) { "Apr  5 18:41:58 kedumba postfix/qmgr[2638]: E69DB36D4A2B: removed" }
  let(:line3) { "Apr  5 17:11:07 kedumba postfix/smtpd[7453]: connect from unknown[1192.168.3.1143]" }
  let(:line4) { "Apr  5 14:21:51 kedumba postfix/smtp[2500]: 39D9336AFA81: to=<<EMAIL>>, relay=aspmx.l.google.com[172.16.17.32]:25, delay=1, delays=0.08/0/0.58/0.34, dsn=5.1.1, status=bounced (host aspmx.l.google.com[172.16.17.32] said: 550-5.1.1 The email account that you tried to reach does not exist. zb4si15321910pbb.132 - gsmtp (in reply to RCPT TO command))" }

  context "one log line" do
    let (:l) do
      email = Email.create!(postfix_queue_id: "39D9336AFA81")
      PostfixLogLine.create_from_line(line1)
      PostfixLogLine.first
    end

    describe ".to" do
      it { l.to.should == "<EMAIL>" }
    end
    describe ".relay" do
      it { l.relay.should == "foo.bar.com[1.2.3.4]:25" }
    end
    describe ".delay" do
      it { l.delay.should == "92780" }
    end
    describe ".delays" do
      it { l.delays.should == "92777/0.03/1.6/0.91" }
    end
    describe ".dsn" do
      it { l.dsn.should == "4.3.0" }
    end
    describe ".status" do
      it { l.status.should == "deferred (host foo.bar.com[1.2.3.4] said: 451 4.3.0 <<EMAIL>>: Temporary lookup failure (in reply to RCPT TO command))" }
    end
  end

  describe ".create_from_line" do
    context "one log line" do
      let(:email) { Email.create!(postfix_queue_id: "39D9336AFA81", to: "<EMAIL>") }
      before :each do
        email
        PostfixLogLine.create_from_line(line1)
      end

      it "should extract and save relevant parts of the line" do
        PostfixLogLine.count.should == 1
        line = email.postfix_log_lines.first
        line.text.should == "to=<<EMAIL>>, relay=foo.bar.com[1.2.3.4]:25, delay=92780, delays=92777/0.03/1.6/0.91, dsn=4.3.0, status=deferred (host foo.bar.com[1.2.3.4] said: 451 4.3.0 <<EMAIL>>: Temporary lookup failure (in reply to RCPT TO command))"
        line.time.should == Time.local(2013,4,5,16,41,54)
      end

      it "should attach it to the delivery" do
        email.deliveries.first.postfix_log_lines.count.should == 1
        line = email.deliveries.first.postfix_log_lines.first
        line.text.should == "to=<<EMAIL>>, relay=foo.bar.com[1.2.3.4]:25, delay=92780, delays=92777/0.03/1.6/0.91, dsn=4.3.0, status=deferred (host foo.bar.com[1.2.3.4] said: 451 4.3.0 <<EMAIL>>: Temporary lookup failure (in reply to RCPT TO command))"
        line.time.should == Time.local(2013,4,5,16,41,54)
      end
    end

    it "should save two lines if two lines are processed" do
      email = Email.create!(postfix_queue_id: "39D9336AFA81")
      PostfixLogLine.create_from_line(line1)
      PostfixLogLine.create_from_line(line4)
      email.postfix_log_lines.count.should == 2
    end

    it "should not reprocess duplicate lines" do
      email = Email.create!(postfix_queue_id: "39D9336AFA81")
      PostfixLogLine.create_from_line(line1)
      PostfixLogLine.create_from_line(line1)
      email.postfix_log_lines.count.should == 1
    end

    it "should not produce any log lines if the queue id is not recognised" do
      PostfixLogLine.should_receive(:puts).with("Skipping postfix queue id 39D9336AFA81 - it's not recognised")
      PostfixLogLine.create_from_line(line1)
      PostfixLogLine.count.should == 0
    end

    it "should only log lines that are delivery attempts" do
      PostfixLogLine.create_from_line(line2)
      PostfixLogLine.count.should == 0
    end
{'fraction_non_alphanumeric': 0.13713021841304948, 'fraction_numerical': 0.09289466408625933, 'mean_word_length': 3.2533803644914756, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 20, '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': '5547031', 'n_tokens_mistral': 3101, 'n_tokens_neox': 2781, 'n_words': 619}