2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe UserBadge do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:badge)
|
|
|
|
fab!(:user)
|
2014-03-05 20:52:20 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "Validations" do
|
2019-04-23 18:25:21 +08:00
|
|
|
let(:subject) { BadgeGranter.grant(badge, user) }
|
2014-03-05 20:52:20 +08:00
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
it { is_expected.to validate_presence_of(:badge_id) }
|
|
|
|
it { is_expected.to validate_presence_of(:user_id) }
|
|
|
|
it { is_expected.to validate_presence_of(:granted_at) }
|
|
|
|
it { is_expected.to validate_presence_of(:granted_by) }
|
|
|
|
it { is_expected.to validate_uniqueness_of(:badge_id).scoped_to(:user_id) }
|
2014-03-05 20:52:20 +08:00
|
|
|
end
|
|
|
|
|
2022-07-22 09:06:02 +08:00
|
|
|
describe "#save" do
|
|
|
|
it "triggers the 'user_badge_granted' DiscourseEvent" do
|
|
|
|
user_badge =
|
|
|
|
UserBadge.new(
|
|
|
|
badge: badge,
|
|
|
|
user: user,
|
|
|
|
granted_at: Time.zone.now,
|
|
|
|
granted_by: Discourse.system_user,
|
|
|
|
)
|
|
|
|
|
|
|
|
event =
|
|
|
|
DiscourseEvent.track(:user_badge_granted, args: [badge.id, user.id]) { user_badge.save! }
|
|
|
|
|
|
|
|
expect(event).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-25 04:36:40 +08:00
|
|
|
describe "#destroy" do
|
|
|
|
it "triggers the 'user_badge_revoked' DiscourseEvent" do
|
|
|
|
user_badge =
|
|
|
|
UserBadge.create(
|
|
|
|
badge: badge,
|
|
|
|
user: user,
|
|
|
|
granted_at: Time.zone.now,
|
|
|
|
granted_by: Discourse.system_user,
|
|
|
|
)
|
|
|
|
|
|
|
|
event = DiscourseEvent.track(:user_badge_revoked) { user_badge.destroy! }
|
|
|
|
|
|
|
|
expect(event).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-14 22:26:49 +08:00
|
|
|
describe "featured rank" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:user)
|
2020-01-14 22:26:49 +08:00
|
|
|
fab!(:user_badge_tl1) do
|
|
|
|
UserBadge.create!(
|
|
|
|
badge_id: Badge::BasicUser,
|
|
|
|
user: user,
|
|
|
|
granted_by: Discourse.system_user,
|
|
|
|
granted_at: Time.now,
|
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2020-01-14 22:26:49 +08:00
|
|
|
fab!(:user_badge_tl2) do
|
|
|
|
UserBadge.create!(
|
|
|
|
badge_id: Badge::Member,
|
|
|
|
user: user,
|
|
|
|
granted_by: Discourse.system_user,
|
|
|
|
granted_at: Time.now,
|
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2020-01-14 22:26:49 +08:00
|
|
|
fab!(:user_badge_wiki) do
|
|
|
|
UserBadge.create!(
|
|
|
|
badge_id: Badge::WikiEditor,
|
|
|
|
user: user,
|
|
|
|
granted_by: Discourse.system_user,
|
|
|
|
granted_at: Time.now,
|
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2020-01-14 22:26:49 +08:00
|
|
|
fab!(:user_badge_like) do
|
|
|
|
UserBadge.create!(
|
|
|
|
badge_id: Badge::FirstLike,
|
|
|
|
user: user,
|
|
|
|
granted_by: Discourse.system_user,
|
|
|
|
granted_at: Time.now,
|
2023-01-09 19:18:21 +08:00
|
|
|
)
|
|
|
|
end
|
2020-01-14 22:26:49 +08:00
|
|
|
|
|
|
|
it "gives user badges the correct rank" do
|
|
|
|
expect(user_badge_tl2.reload.featured_rank).to eq(1)
|
|
|
|
expect(user_badge_wiki.reload.featured_rank).to eq(2)
|
|
|
|
expect(user_badge_like.reload.featured_rank).to eq(3)
|
|
|
|
expect(user_badge_tl1.reload.featured_rank).to eq(4) # Previous trust level badges last
|
|
|
|
end
|
|
|
|
|
|
|
|
it "gives duplicate user_badges the same rank" do
|
|
|
|
ub1 =
|
|
|
|
UserBadge.create!(
|
|
|
|
badge_id: Badge::GreatTopic,
|
|
|
|
user: user,
|
|
|
|
granted_by: Discourse.system_user,
|
|
|
|
granted_at: Time.now,
|
|
|
|
)
|
|
|
|
ub2 =
|
|
|
|
UserBadge.create!(
|
|
|
|
badge_id: Badge::GreatTopic,
|
|
|
|
user: user,
|
|
|
|
granted_by: Discourse.system_user,
|
|
|
|
granted_at: Time.now,
|
|
|
|
seq: 1,
|
2023-01-09 19:18:21 +08:00
|
|
|
)
|
2020-01-14 22:26:49 +08:00
|
|
|
|
|
|
|
expect(ub1.reload.featured_rank).to eq(2)
|
|
|
|
expect(ub2.reload.featured_rank).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "skips disabled badges" do
|
|
|
|
user_badge_wiki.badge.update(enabled: false)
|
|
|
|
expect(user_badge_tl2.reload.featured_rank).to eq(1)
|
|
|
|
expect(user_badge_like.reload.featured_rank).to eq(2)
|
|
|
|
expect(user_badge_tl1.reload.featured_rank).to eq(3) # Previous trust level badges last
|
|
|
|
expect(user_badge_wiki.reload.featured_rank).to eq(4) # Disabled
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can ensure consistency per user" do
|
|
|
|
user_badge_tl2.update_column(:featured_rank, 20) # Update without hooks
|
|
|
|
expect(user_badge_tl2.reload.featured_rank).to eq(20) # Double check
|
|
|
|
UserBadge.update_featured_ranks! user.id
|
|
|
|
expect(user_badge_tl2.reload.featured_rank).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can ensure consistency for all users" do
|
|
|
|
user_badge_tl2.update_column(:featured_rank, 20) # Update without hooks
|
|
|
|
expect(user_badge_tl2.reload.featured_rank).to eq(20) # Double check
|
|
|
|
UserBadge.update_featured_ranks!
|
|
|
|
expect(user_badge_tl2.reload.featured_rank).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
2014-03-05 20:52:20 +08:00
|
|
|
end
|