docbrown/spec/models/settings/rate_limit_spec.rb
Jeremy Friesen 29f6853ee9
Refactoring Spam Handler (#15412)
* Refactoring Spam Handler

There's considerable repeated logic between checking spam for an article
and spam for a comment and user.

This attempts to send things through channels that are similar and close
in organization.

* Fixing broken spec

* Fixing spec around recent user

* Update app/models/reaction.rb

Co-authored-by: Michael Kohl <citizen428@forem.com>

* Update app/models/reaction.rb

Co-authored-by: Michael Kohl <citizen428@forem.com>

* Consolidating new user query logic

Prior to this commit there were two separate queries around new user
logic.  With this commit, we're changing the logic to repurpose a site
wide setting.

* Generalizing a previously specific message

* Fixing method name

As part of a recommended refactor, I extracted a method, then renamed
it.  I failed to account for that renaming.

This commit fixes that.

Co-authored-by: Michael Kohl <citizen428@forem.com>
2021-11-30 12:45:12 -05:00

63 lines
1.7 KiB
Ruby

require "rails_helper"
RSpec.describe Settings::RateLimit, type: :model do
describe ".user_considered_new?" do
subject(:function_call) { described_class.user_considered_new?(user: user) }
before do
allow(described_class).to receive(:user_considered_new_days).and_return(5)
end
context "when given a nil user" do
let(:user) { nil }
it { is_expected.to be_truthy }
end
context "when given a decorated user that was created months ago" do
let(:user) { create(:user).decorate }
before { allow(user).to receive(:created_at).and_return(30.months.ago) }
it { is_expected.to be_falsey }
end
context "when given a decorated user that was created within the user_considered_new_days" do
let(:user) { create(:user).decorate }
before { allow(user).to receive(:created_at).and_return(3.days.ago) }
it { is_expected.to be_truthy }
end
end
describe ".trigger_spam_for?" do
subject { described_class.trigger_spam_for?(text: text) }
let(:text) { "text to test" }
context "when it matches a spam trigger term" do
before do
allow(described_class).to receive(:spam_trigger_terms).and_return(["to test"])
end
it { is_expected.to be_truthy }
end
context "when there are no spam trigger terms" do
before do
allow(described_class).to receive(:spam_trigger_terms).and_return([])
end
it { is_expected.to be_falsey }
end
context "when there are spam trigger terms but they don't match" do
before do
allow(described_class).to receive(:spam_trigger_terms).and_return(["in a hole in the ground"])
end
it { is_expected.to be_falsey }
end
end
end