docbrown/spec/services/moderator/sink_articles_spec.rb
Daniel Uber 16d17445a5
reenable article rescoring when moderators flag users (#12983)
* Update worker calculation and disable early return

* Enable service tests

This updates the test to look at the individual article scores
changing, rather than the sum of the articles scores changing

The scores lambda was added as a way to observe this eagerly

* Correctly handle raising the score when vomit is cleared

The initial implementation loaded the articles reactions and the
user's reactions in order to rescore. This restores that activity.

It occurs to me that article.reactions.sum(:points) +
user.reactions.sum(:points) feels like something articles should know
how to do, in which case we can move this into article (#rescore!)?
and call that instead of knowing how to handle moderation specially here.

* Move logic to Article#update_score

Having realized "sum of reaction scores" was an Article concern, I
found that we have a worker to score articles that does _exactly_ what
we're doing here.

Let's use that and allow the vomit reaction presence or absence on a
user to automatically affect the article calculations.

This is at least n + 1 and maybe worse.
2021-03-16 14:04:13 -05:00

47 lines
1.5 KiB
Ruby

require "rails_helper"
RSpec.describe Moderator::SinkArticles, type: :service do
let(:moderator) { create(:user, :trusted) }
let(:spam_user) do
user = create(:user)
create_list(:article, 3, user: user)
user
end
let(:scores) { -> { spam_user.articles.reload.pluck(:score) } }
let(:vomit_reaction) { create(:reaction, reactable: spam_user, user: moderator, category: "vomit") }
describe "#call" do
it "lowers all of a user's articles' scores by 25 each if not confirmed" do
vomit_reaction
expect do
sidekiq_perform_enqueued_jobs do
described_class.call(spam_user.id)
end
end.to change(scores, :call).from([0, 0, 0]).to([-25, -25, -25])
end
it "lowers all of the user's articles' scores by 50 each if confirmed" do
vomit_reaction.update(status: "confirmed")
expect do
sidekiq_perform_enqueued_jobs do
described_class.call(spam_user.id)
end
end.to change(scores, :call).from([0, 0, 0]).to([-50, -50, -50])
end
context "when removing a user vomit reaction" do
before do
# pretend we had a confirmed vomit on the user
spam_user.articles.update(score: -50)
end
it "raises all of the user's articles but the moderation score" do
expect do
sidekiq_perform_enqueued_jobs do
described_class.call(spam_user.id)
end
end.to change(scores, :call).from([-50, -50, -50]).to([0, 0, 0])
end
end
end
end