Ensure at least 0 score for new badge award (#20605)

This commit is contained in:
Ben Halpern 2024-02-08 12:00:10 -05:00 committed by GitHub
parent 2f9769a89d
commit 1b083f39a4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 0 deletions

View file

@ -8,6 +8,7 @@ module Badges
Article.joins(:user)
.where("articles.published_at > ?", 1.week.ago)
.where("articles.published_at < ?", 1.hour.ago)
.where("articles.score >= ?", 0)
.where(nth_published_by_author: 1)
.where.not(users: { id: User.with_role(:spam).or(User.with_role(:suspended)) })
.find_each do |article|

View file

@ -33,6 +33,14 @@ RSpec.describe Badges::AwardFirstPost do
Timecop.return
expect { described_class.call }.to change(BadgeAchievement, :count).by(1)
end
it "does not award the badge if the article score is less than zero" do
Timecop.freeze(2.days.ago) do
create(:article, user: create(:user), score: -1)
end
Timecop.return
expect { described_class.call }.not_to change(BadgeAchievement, :count)
end
end
context "when the user has a spam or suspended role" do