2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe ScreenedEmail do
|
2013-07-25 13:01:27 -04:00
|
|
|
let(:email) { "block@spamfromhome.org" }
|
2014-07-11 19:59:43 -04:00
|
|
|
let(:similar_email) { "bl0ck@spamfromhome.org" }
|
2013-07-25 13:01:27 -04:00
|
|
|
|
|
|
|
describe "new record" do
|
|
|
|
it "sets a default action_type" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(ScreenedEmail.create(email: email).action_type).to eq(ScreenedEmail.actions[:block])
|
2013-07-25 13:01:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "last_match_at is null" do
|
|
|
|
# If we manually load the table with some emails, we can see whether those emails
|
|
|
|
# have ever been blocked by looking at last_match_at.
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(ScreenedEmail.create(email: email).last_match_at).to eq(nil)
|
2013-07-25 13:01:27 -04:00
|
|
|
end
|
2014-07-14 10:16:24 -04:00
|
|
|
|
|
|
|
it "downcases the email" do
|
|
|
|
s = ScreenedEmail.create(email: "SPAMZ@EXAMPLE.COM")
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(s.email).to eq("spamz@example.com")
|
2014-07-14 10:16:24 -04:00
|
|
|
end
|
2013-07-25 13:01:27 -04:00
|
|
|
end
|
|
|
|
|
2013-07-25 15:30:03 -04:00
|
|
|
describe "#block" do
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when email is not being blocked" do
|
2013-07-25 15:30:03 -04:00
|
|
|
it "creates a new record with default action of :block" do
|
2014-07-11 19:59:43 -04:00
|
|
|
record = ScreenedEmail.block(email)
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(record).not_to be_new_record
|
|
|
|
expect(record.email).to eq(email)
|
|
|
|
expect(record.action_type).to eq(ScreenedEmail.actions[:block])
|
2013-07-25 15:30:03 -04:00
|
|
|
end
|
|
|
|
|
2022-04-06 17:17:20 -04:00
|
|
|
it "lets action_type be overridden" do
|
2014-07-11 19:59:43 -04:00
|
|
|
record = ScreenedEmail.block(email, action_type: ScreenedEmail.actions[:do_nothing])
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(record).not_to be_new_record
|
|
|
|
expect(record.email).to eq(email)
|
|
|
|
expect(record.action_type).to eq(ScreenedEmail.actions[:do_nothing])
|
2013-07-25 15:30:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when email is already being blocked" do
|
2013-08-14 11:05:53 -04:00
|
|
|
let!(:existing) { Fabricate(:screened_email, email: email) }
|
2013-07-25 15:30:03 -04:00
|
|
|
|
|
|
|
it "doesn't create a new record" do
|
2014-07-11 19:59:43 -04:00
|
|
|
expect { ScreenedEmail.block(email) }.to_not change { ScreenedEmail.count }
|
2013-07-25 15:30:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the existing record" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(ScreenedEmail.block(email)).to eq(existing)
|
2013-07-25 15:30:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#should_block?" do
|
2014-07-11 19:59:43 -04:00
|
|
|
subject { ScreenedEmail.should_block?(email) }
|
2013-07-25 13:01:27 -04:00
|
|
|
|
2020-04-24 00:09:51 -04:00
|
|
|
it "automatically blocks via email canonicalization" do
|
|
|
|
SiteSetting.levenshtein_distance_spammer_emails = 0
|
|
|
|
ScreenedEmail.block("bad.acTor+1@gmail.com")
|
|
|
|
ScreenedEmail.block("bad.actOr+2@gmail.com")
|
|
|
|
|
|
|
|
expect(ScreenedEmail.should_block?("b.a.dactor@gmail.com")).to eq(true)
|
|
|
|
end
|
|
|
|
|
2014-07-11 19:01:37 -04:00
|
|
|
it "returns false if a record with the email doesn't exist" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(subject).to eq(false)
|
2013-07-25 13:01:27 -04:00
|
|
|
end
|
|
|
|
|
2014-07-11 19:59:43 -04:00
|
|
|
it "returns true when there is a record with the email" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(ScreenedEmail.should_block?(email)).to eq(false)
|
2014-07-11 19:59:43 -04:00
|
|
|
ScreenedEmail.create(email: email).save
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(ScreenedEmail.should_block?(email)).to eq(true)
|
2014-07-11 19:59:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true when there is a record with a similar email" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(ScreenedEmail.should_block?(email)).to eq(false)
|
2014-07-11 19:59:43 -04:00
|
|
|
ScreenedEmail.create(email: similar_email).save
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(ScreenedEmail.should_block?(email)).to eq(true)
|
2014-07-11 19:59:43 -04:00
|
|
|
end
|
|
|
|
|
2014-07-14 10:16:24 -04:00
|
|
|
it "returns true when it's same email, but all caps" do
|
|
|
|
ScreenedEmail.create(email: email).save
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(ScreenedEmail.should_block?(email.upcase)).to eq(true)
|
2014-07-14 10:16:24 -04:00
|
|
|
end
|
|
|
|
|
2013-08-14 11:05:53 -04:00
|
|
|
shared_examples "when a ScreenedEmail record matches" do
|
2013-07-25 13:01:27 -04:00
|
|
|
it "updates statistics" do
|
2020-03-10 17:13:17 -04:00
|
|
|
freeze_time do
|
2013-08-14 11:05:53 -04:00
|
|
|
expect { subject }.to change { screened_email.reload.match_count }.by(1)
|
2020-03-10 17:13:17 -04:00
|
|
|
expect(screened_email.last_match_at).to eq_time(Time.zone.now)
|
2013-07-25 13:01:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when action_type is :block" do
|
2014-07-11 19:59:43 -04:00
|
|
|
let!(:screened_email) do
|
|
|
|
Fabricate(:screened_email, email: email, action_type: ScreenedEmail.actions[:block])
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2015-01-05 11:04:23 -05:00
|
|
|
it { is_expected.to eq(true) }
|
2013-08-14 11:05:53 -04:00
|
|
|
include_examples "when a ScreenedEmail record matches"
|
2013-07-25 13:01:27 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when action_type is :do_nothing" do
|
2014-07-11 19:59:43 -04:00
|
|
|
let!(:screened_email) do
|
|
|
|
Fabricate(:screened_email, email: email, action_type: ScreenedEmail.actions[:do_nothing])
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2015-01-05 11:04:23 -05:00
|
|
|
it { is_expected.to eq(false) }
|
2013-08-14 11:05:53 -04:00
|
|
|
include_examples "when a ScreenedEmail record matches"
|
2013-07-25 13:01:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|