2013-07-25 13:01:27 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2013-08-14 11:05:53 -04:00
|
|
|
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
|
2014-07-11 19:59:43 -04:00
|
|
|
ScreenedEmail.create(email: email).action_type.should == 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.
|
2014-07-11 19:59:43 -04:00
|
|
|
ScreenedEmail.create(email: email).last_match_at.should be_nil
|
2013-07-25 13:01:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-25 15:30:03 -04:00
|
|
|
describe '#block' do
|
|
|
|
context 'email is not being blocked' do
|
|
|
|
it 'creates a new record with default action of :block' do
|
2014-07-11 19:59:43 -04:00
|
|
|
record = ScreenedEmail.block(email)
|
2013-07-25 15:30:03 -04:00
|
|
|
record.should_not be_new_record
|
|
|
|
record.email.should == email
|
2014-07-11 19:59:43 -04:00
|
|
|
record.action_type.should == ScreenedEmail.actions[:block]
|
2013-07-25 15:30:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'lets action_type be overriden' do
|
2014-07-11 19:59:43 -04:00
|
|
|
record = ScreenedEmail.block(email, action_type: ScreenedEmail.actions[:do_nothing])
|
2013-07-25 15:30:03 -04:00
|
|
|
record.should_not be_new_record
|
|
|
|
record.email.should == email
|
2014-07-11 19:59:43 -04:00
|
|
|
record.action_type.should == ScreenedEmail.actions[:do_nothing]
|
2013-07-25 15:30:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context '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
|
2014-07-11 19:59:43 -04:00
|
|
|
ScreenedEmail.block(email).should == 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
|
|
|
|
2014-07-11 19:01:37 -04:00
|
|
|
it "returns false if a record with the email doesn't exist" do
|
2013-07-25 13:01:27 -04:00
|
|
|
subject.should be_false
|
|
|
|
end
|
|
|
|
|
2014-07-11 19:59:43 -04:00
|
|
|
it "returns true when there is a record with the email" do
|
|
|
|
ScreenedEmail.should_block?(email).should be_false
|
|
|
|
ScreenedEmail.create(email: email).save
|
|
|
|
ScreenedEmail.should_block?(email).should be_true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true when there is a record with a similar email" do
|
|
|
|
ScreenedEmail.should_block?(email).should be_false
|
|
|
|
ScreenedEmail.create(email: similar_email).save
|
|
|
|
ScreenedEmail.should_block?(email).should be_true
|
|
|
|
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
|
|
|
|
Timecop.freeze(Time.zone.now) do
|
2013-08-14 11:05:53 -04:00
|
|
|
expect { subject }.to change { screened_email.reload.match_count }.by(1)
|
|
|
|
screened_email.last_match_at.should be_within_one_second_of(Time.zone.now)
|
2013-07-25 13:01:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "action_type is :block" do
|
2014-07-11 19:59:43 -04:00
|
|
|
let!(:screened_email) { Fabricate(:screened_email, email: email, action_type: ScreenedEmail.actions[:block]) }
|
2013-07-25 13:01:27 -04:00
|
|
|
it { should be_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
|
|
|
|
|
|
|
|
context "action_type is :do_nothing" do
|
2014-07-11 19:59:43 -04:00
|
|
|
let!(:screened_email) { Fabricate(:screened_email, email: email, action_type: ScreenedEmail.actions[:do_nothing]) }
|
2013-07-25 13:01:27 -04:00
|
|
|
it { should be_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
|