From 6bf3e2aea98ea29827b7889a240abd0b5b4ddf07 Mon Sep 17 00:00:00 2001 From: Daniel Uber Date: Thu, 21 Apr 2022 10:56:59 -0500 Subject: [PATCH] Skip rescoring draft articles (#17379) * Skip rescoring draft articles * add spec for worker --- app/workers/moderator/sink_articles_worker.rb | 2 +- .../moderator/sink_articles_worker_spec.rb | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 spec/workers/moderator/sink_articles_worker_spec.rb diff --git a/app/workers/moderator/sink_articles_worker.rb b/app/workers/moderator/sink_articles_worker.rb index 9b2442335..ca885755a 100644 --- a/app/workers/moderator/sink_articles_worker.rb +++ b/app/workers/moderator/sink_articles_worker.rb @@ -8,7 +8,7 @@ module Moderator user = User.find_by(id: user_id) return unless user - Article.where(user: user).each(&:update_score) + Article.published.where(user: user).each(&:update_score) rescue StandardError => e ForemStatsClient.count("moderators.sink", 1, tags: ["action:failed", "user_id:#{user.id}"]) Honeybadger.notify(e) diff --git a/spec/workers/moderator/sink_articles_worker_spec.rb b/spec/workers/moderator/sink_articles_worker_spec.rb new file mode 100644 index 000000000..437e24c35 --- /dev/null +++ b/spec/workers/moderator/sink_articles_worker_spec.rb @@ -0,0 +1,34 @@ +require "rails_helper" + +RSpec.describe Moderator::SinkArticlesWorker, type: :worker do + include_examples "#enqueues_on_correct_queue", "medium_priority", 1 + + describe "#perform" do + it "returns early when user not found" do + expect { described_class.new.perform(nil) }.not_to raise_error + expect(described_class.new.perform(nil)).to be_nil + end + + it "updates score for user's articles" do + article = create(:article, score: 20.0) + # flagging a user is the normal route to sink articles, + # and ensures score changes in an observable manner + create(:vomit_reaction, reactable: article.user) + allow(BlackBox).to receive(:article_hotness_score).and_call_original + + described_class.new.perform(article.user_id) + + expect(BlackBox).to have_received(:article_hotness_score) + expect(article.reload.score).not_to eq(20) + end + + it "skips draft articles" do + article = create(:article, published: false) + allow(BlackBox).to receive(:article_hotness_score).and_call_original + + described_class.new.perform(article.user_id) + + expect(BlackBox).not_to have_received(:article_hotness_score) + end + end +end