<commit_before>require "rails_helper"

describe Newsletter do
  let(:newsletter) { build(:newsletter) }

  it "is valid" do
    expect(newsletter).to be_valid
  end

  it "is not valid without a subject" do
    newsletter.subject = nil
    expect(newsletter).not_to be_valid
  end

  it "is not valid without a segment_recipient" do
    newsletter.segment_recipient = nil
    expect(newsletter).not_to be_valid
  end

  it "is not valid with an inexistent user segment for segment_recipient" do
    newsletter.segment_recipient = "invalid_user_segment_name"
    expect(newsletter).not_to be_valid
  end

  it "is not valid without a from" do
    newsletter.from = nil
    expect(newsletter).not_to be_valid
  end

  it "is not valid without a body" do
    newsletter.body = nil
    expect(newsletter).not_to be_valid
  end

  it "validates from attribute email format" do
    newsletter.from = "this_is_not_an_email"
    expect(newsletter).not_to be_valid
  end

  describe "#valid_segment_recipient?" do
    it "is false when segment_recipient value is invalid" do
      newsletter.update(segment_recipient: "invalid_segment_name")
      error = "The user recipients segment is invalid"

      expect(newsletter).not_to be_valid
      expect(newsletter.errors.messages[:segment_recipient]).to include(error)
    end
  end

  describe "#list_of_recipient_emails" do

    before do
      create(:user, newsletter: true, email: "<EMAIL>")
      create(:user, newsletter: false, email: "<EMAIL>")
      create(:user, email: "<EMAIL>").erase
      newsletter.update(segment_recipient: "all_users")
    end

    it "returns list of recipients excluding users with disabled newsletter" do
      expect(newsletter.list_of_recipient_emails).to eq ["<EMAIL>"]
    end
  end

  describe "#deliver", :delay_jobs do
    let!(:proposals) { Array.new(3) { create(:proposal) } }

    let!(:recipients) { proposals.map(&:author).map(&:email) }
    let!(:newsletter) { create(:newsletter, segment_recipient: "proposal_authors") }

    before do
      create(:debate)
      reset_mailer
    end

    it "sends an email with the newsletter to every recipient" do
      newsletter.deliver

      recipients.each do |recipient|
        email = Mailer.newsletter(newsletter, recipient)
        expect(email).to deliver_to(recipient)
      end

      Delayed::Job.all.map(&:invoke_job)
      expect(ActionMailer::Base.deliveries.count).to eq(3)
    end

    it "sends emails in batches" do
      allow(newsletter).to receive(:batch_size).and_return(1)

      newsletter.deliver

      expect(Delayed::Job.count).to eq(3)
    end

    it "sends batches in time intervals" do
      allow(newsletter).to receive(:batch_size).and_return(1)
      allow(newsletter).to receive(:batch_interval).and_return(1.second)
      allow(newsletter).to receive(:first_batch_run_at).and_return(Time.current)

      newsletter.deliver

      now = newsletter.first_batch_run_at
      first_batch_run_at  = now.change(usec: 0)
      second_batch_run_at = (now + 1.second).change(usec: 0)
      third_batch_run_at  = (now + 2.seconds).change(usec: 0)

      expect(Delayed::Job.count).to eq(3)
      expect(Delayed::Job.first.run_at.change(usec: 0)).to eq(first_batch_run_at)
      expect(Delayed::Job.second.run_at.change(usec: 0)).to eq(second_batch_run_at)
      expect(Delayed::Job.third.run_at.change(usec: 0)).to eq(third_batch_run_at)
    end

    it "logs users that have received the newsletter" do
      newsletter.deliver

      expect(Activity.count).to eq(3)

      recipients.each do |email|
        user = User.where(email: email).first
        activity = Activity.where(user: user).first

        expect(activity.user_id).to eq(user.id)
        expect(activity.action).to eq("email")
        expect(activity.actionable).to eq(newsletter)
      end
    end

    it "skips invalid emails" do
      Proposal.destroy_all

      valid_email = "<EMAIL>"
      invalid_email = "john@gmail..com"

      valid_email_user = create(:user, :with_proposal, email: valid_email)
      invalid_email_user = create(:user, :with_proposal, email: invalid_email)

      newsletter.deliver

      expect(Activity.count).to eq(1)
      expect(Activity.first.user_id).to eq(valid_email_user.id)
      expect(Activity.first.action).to eq("email")
      expect(Activity.first.actionable).to eq(newsletter)
    end

  end
end
<commit_msg>Simplify tests checking for invalid email<commit_after>require "rails_helper"

describe Newsletter do
  let(:newsletter) { build(:newsletter) }

  it "is valid" do
    expect(newsletter).to be_valid
  end

  it "is not valid without a subject" do
    newsletter.subject = nil
    expect(newsletter).not_to be_valid
  end

  it "is not valid without a segment_recipient" do
    newsletter.segment_recipient = nil
    expect(newsletter).not_to be_valid
  end

  it "is not valid with an inexistent user segment for segment_recipient" do
    newsletter.segment_recipient = "invalid_user_segment_name"
    expect(newsletter).not_to be_valid
  end

  it "is not valid without a from" do
    newsletter.from = nil
    expect(newsletter).not_to be_valid
  end

  it "is not valid without a body" do
    newsletter.body = nil
    expect(newsletter).not_to be_valid
  end

  it "validates from attribute email format" do
    newsletter.from = "this_is_not_an_email"
    expect(newsletter).not_to be_valid
  end

  describe "#valid_segment_recipient?" do
    it "is false when segment_recipient value is invalid" do
      newsletter.update(segment_recipient: "invalid_segment_name")
      error = "The user recipients segment is invalid"

      expect(newsletter).not_to be_valid
      expect(newsletter.errors.messages[:segment_recipient]).to include(error)
    end
  end

  describe "#list_of_recipient_emails" do

    before do
      create(:user, newsletter: true, email: "<EMAIL>")
      create(:user, newsletter: false, email: "<EMAIL>")
      create(:user, email: "<EMAIL>").erase
      newsletter.update(segment_recipient: "all_users")
    end

    it "returns list of recipients excluding users with disabled newsletter" do
      expect(newsletter.list_of_recipient_emails).to eq ["<EMAIL>"]
    end
  end

  describe "#deliver", :delay_jobs do
    let!(:proposals) { Array.new(3) { create(:proposal) } }

    let!(:recipients) { proposals.map(&:author).map(&:email) }
    let!(:newsletter) { create(:newsletter, segment_recipient: "proposal_authors") }

    before do
      create(:debate)
      reset_mailer
    end

    it "sends an email with the newsletter to every recipient" do
      newsletter.deliver

      recipients.each do |recipient|
        email = Mailer.newsletter(newsletter, recipient)
        expect(email).to deliver_to(recipient)
      end

      Delayed::Job.all.map(&:invoke_job)
      expect(ActionMailer::Base.deliveries.count).to eq(3)
    end

    it "sends emails in batches" do
      allow(newsletter).to receive(:batch_size).and_return(1)

      newsletter.deliver

      expect(Delayed::Job.count).to eq(3)
    end

    it "sends batches in time intervals" do
      allow(newsletter).to receive(:batch_size).and_return(1)
      allow(newsletter).to receive(:batch_interval).and_return(1.second)
      allow(newsletter).to receive(:first_batch_run_at).and_return(Time.current)

      newsletter.deliver

      now = newsletter.first_batch_run_at
      first_batch_run_at  = now.change(usec: 0)
      second_batch_run_at = (now + 1.second).change(usec: 0)
      third_batch_run_at  = (now + 2.seconds).change(usec: 0)

      expect(Delayed::Job.count).to eq(3)
      expect(Delayed::Job.first.run_at.change(usec: 0)).to eq(first_batch_run_at)
      expect(Delayed::Job.second.run_at.change(usec: 0)).to eq(second_batch_run_at)
      expect(Delayed::Job.third.run_at.change(usec: 0)).to eq(third_batch_run_at)
    end

    it "logs users that have received the newsletter" do
      newsletter.deliver

      expect(Activity.count).to eq(3)

      recipients.each do |email|
        user = User.where(email: email).first
        activity = Activity.where(user: user).first

        expect(activity.user_id).to eq(user.id)
        expect(activity.action).to eq("email")
        expect(activity.actionable).to eq(newsletter)
      end
    end

    it "skips invalid emails" do
      Proposal.destroy_all
      create(:user, :with_proposal, email: "<EMAIL>")
      create(:user, :with_proposal, email: "<EMAIL>")

      newsletter.deliver

      expect(Activity.count).to eq(1)
      expect(Activity.first.user.email).to eq("<EMAIL>")
      expect(Activity.first.action).to eq("email")
      expect(Activity.first.actionable).to eq(newsletter)
    end

  end
end

{'fraction_non_alphanumeric': 0.10575481490024219, 'fraction_numerical': 0.003921116364894475, 'mean_word_length': 3.447179487179487, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '4601701', 'n_tokens_mistral': 2887, 'n_tokens_neox': 2742, 'n_words': 627}