docbrown/spec/services/badges/award_spec.rb
Ben Halpern 78f9bec3e7
Allow admin to declare whether to include the badge description in message (#20684)
* Allow admin to declare whether to include the badge description in badge achievement message

* Update spec/services/badges/award_spec.rb

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Add proper acceptance to tests

* Tweak tests

* Switch to kwargs

* Clean up test acceptance

* Change interface

* Update spec/requests/admin/badge_achievements_spec.rb

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update app/services/badges/award.rb

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Adjust interface

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2024-02-28 11:02:58 -05:00

40 lines
1.7 KiB
Ruby

require "rails_helper"
RSpec.describe Badges::Award, type: :service do
describe ".call" do
let!(:badge) { create(:badge, title: "one-year-club") }
let!(:user) { create(:user) }
let!(:user2) { create(:user) }
it "awards badges" do
expect do
described_class.call(User.all, "one-year-club", "Congrats on a badge!", include_default_description: true)
end.to change(BadgeAchievement, :count).by(2)
end
it "creates correct badge achievements" do
described_class.call(User.all, "one-year-club", "Congrats on a badge!", include_default_description: true)
expect(user.badge_achievements.pluck(:badge_id)).to eq([badge.id])
expect(user2.badge_achievements.pluck(:rewarding_context_message_markdown))
.to eq(["Congrats on a badge!"])
end
it "creates correct badge achievements without default description" do
described_class.call(User.all, "one-year-club", "Congrats on a badge!", include_default_description: false)
expect(user.badge_achievements.pluck(:include_default_description)).to eq([false])
end
it "creates correct badge achievements with default description" do
described_class.call(User.all, "one-year-club", "Congrats on a badge!", include_default_description: true)
expect(user.badge_achievements.pluck(:include_default_description)).to eq([true])
end
it "doesn't award badges to spam accounts" do
spammer = create(:user, username: "spam_account")
described_class.call(User.all, "one-year-club", "Congrats on a badge!", include_default_description: true)
expect(user.badge_achievements.any?).to be true
expect(spammer.badge_achievements.any?).to be false
end
end
end