2020-01-13 09:20:26 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe Jobs::MassAwardBadge do
|
|
|
|
describe '#execute' do
|
|
|
|
fab!(:badge) { Fabricate(:badge) }
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
2020-02-18 08:53:12 -05:00
|
|
|
let(:email_mode) { 'email' }
|
2020-01-13 09:20:26 -05:00
|
|
|
|
|
|
|
it 'creates the badge for an existing user' do
|
2020-02-18 08:53:12 -05:00
|
|
|
execute_job([user.email])
|
2020-01-13 09:20:26 -05:00
|
|
|
|
|
|
|
expect(UserBadge.where(user: user, badge: badge).exists?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'works with multiple users' do
|
|
|
|
user_2 = Fabricate(:user)
|
|
|
|
|
2020-02-18 08:53:12 -05:00
|
|
|
execute_job([user.email, user_2.email])
|
2020-01-13 09:20:26 -05:00
|
|
|
|
|
|
|
expect(UserBadge.exists?(user: user, badge: badge)).to eq(true)
|
|
|
|
expect(UserBadge.exists?(user: user_2, badge: badge)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'also creates a notification for the user' do
|
2020-02-18 08:53:12 -05:00
|
|
|
execute_job([user.email])
|
2020-01-13 09:20:26 -05:00
|
|
|
|
|
|
|
expect(Notification.exists?(user: user)).to eq(true)
|
|
|
|
expect(UserBadge.where.not(notification_id: nil).exists?(user: user, badge: badge)).to eq(true)
|
|
|
|
end
|
2020-01-14 09:39:20 -05:00
|
|
|
|
|
|
|
it 'updates badge ranks correctly' do
|
|
|
|
user_2 = Fabricate(:user)
|
|
|
|
|
|
|
|
UserBadge.create!(badge_id: Badge::Member, user: user, granted_by: Discourse.system_user, granted_at: Time.now)
|
|
|
|
|
2020-02-18 08:53:12 -05:00
|
|
|
execute_job([user.email, user_2.email])
|
2020-01-14 09:39:20 -05:00
|
|
|
|
|
|
|
expect(UserBadge.find_by(user: user, badge: badge).featured_rank).to eq(2)
|
|
|
|
expect(UserBadge.find_by(user: user_2, badge: badge).featured_rank).to eq(1)
|
|
|
|
end
|
2020-02-18 08:53:12 -05:00
|
|
|
|
|
|
|
def execute_job(emails)
|
|
|
|
subject.execute(users_batch: emails, badge_id: badge.id, mode: 'email')
|
|
|
|
end
|
2020-01-13 09:20:26 -05:00
|
|
|
end
|
|
|
|
end
|