FIX: Safeguard to not award NewUserOfTheMonth if it hasn't been a month

This commit is contained in:
Robin Ward 2017-04-03 13:26:22 -04:00
parent 5ff29ce321
commit a13a8dc96c
2 changed files with 29 additions and 0 deletions

View File

@ -8,6 +8,13 @@ module Jobs
def execute(args)
badge = Badge.find(Badge::NewUserOfTheMonth)
# Don't award it if a month hasn't gone by
return if UserBadge.where("badge_id = ? AND granted_at >= ?",
badge.id,
(1.month.ago + 1.day) # give it a day slack just in case
).exists?
scores.each do |user_id, score|
# Don't bother awarding to users who haven't received any likes
if score > 0.0

View File

@ -6,6 +6,9 @@ describe Jobs::GrantNewUserOfTheMonthBadges do
let(:granter) { described_class.new }
it "runs correctly" do
u0 = Fabricate(:user, created_at: 2.weeks.ago)
BadgeGranter.grant(Badge.find(Badge::NewUserOfTheMonth), u0, created_at: 1.month.ago)
user = Fabricate(:user, created_at: 1.week.ago)
p = Fabricate(:post, user: user)
Fabricate(:post, user: user)
@ -21,6 +24,25 @@ describe Jobs::GrantNewUserOfTheMonthBadges do
expect(badge).to be_present
end
it "does nothing if it's been awarded recently" do
u0 = Fabricate(:user, created_at: 2.weeks.ago)
BadgeGranter.grant(Badge.find(Badge::NewUserOfTheMonth), u0)
user = Fabricate(:user, created_at: 1.week.ago)
p = Fabricate(:post, user: user)
Fabricate(:post, user: user)
old_user = Fabricate(:user, created_at: 6.months.ago)
PostAction.act(old_user, p, PostActionType.types[:like])
old_user = Fabricate(:user, created_at: 6.months.ago)
PostAction.act(old_user, p, PostActionType.types[:like])
granter.execute({})
badge = user.user_badges.where(badge_id: Badge::NewUserOfTheMonth)
expect(badge).to be_blank
end
describe '.scores' do
it "doesn't award it to accounts over a month old" do