discourse/spec/serializers/detailed_user_badge_serializer_spec.rb
Daniel Waterworth 6e161d3e75
DEV: Allow fab! without block (#24314)
The most common thing that we do with fab! is:

    fab!(:thing) { Fabricate(:thing) }

This commit adds a shorthand for this which is just simply:

    fab!(:thing)

i.e. If you omit the block, then, by default, you'll get a `Fabricate`d object using the fabricator of the same name.
2023-11-09 16:47:59 -06:00

100 lines
3.4 KiB
Ruby

# frozen_string_literal: true
RSpec.describe DetailedUserBadgeSerializer do
describe "#topic_id and #topic_title attributes" do
fab!(:user)
fab!(:admin)
fab!(:post)
fab!(:badge) { Fabricate(:badge, show_posts: true) }
fab!(:user_badge) { Fabricate(:user_badge, badge: badge, post_id: post.id) }
let(:guardian) { Guardian.new(user_badge.user) }
it "does not include attributes in serialized object when badge has not been configured to show posts" do
badge.update!(show_posts: false)
guardian = Guardian.new
serialized =
described_class.new(
user_badge,
scope: guardian,
allowed_user_badge_topic_ids: guardian.can_see_topic_ids(topic_ids: [post.topic_id]),
root: false,
).as_json
expect(serialized[:topic_id]).to eq(nil)
expect(serialized[:topic_title]).to eq(nil)
end
it "does not include attributes in serialized object when user badge is not associated with a post" do
user_badge.update!(post_id: nil)
serialized =
described_class.new(
user_badge,
scope: guardian,
allowed_user_badge_topic_ids: guardian.can_see_topic_ids(topic_ids: [post.topic_id]),
root: false,
).as_json
expect(serialized[:topic_id]).to eq(nil)
expect(serialized[:topic_title]).to eq(nil)
end
it "does not include attributes in serialized object when user badge is not associated with a topic" do
post.topic.destroy!
serialized =
described_class.new(
user_badge,
scope: guardian,
allowed_user_badge_topic_ids: guardian.can_see_topic_ids(topic_ids: [post.topic_id]),
root: false,
).as_json
expect(serialized[:topic_id]).to eq(nil)
expect(serialized[:topic_title]).to eq(nil)
end
it "does not include attributes in serialized object when allowed_user_badge_topic_ids option is not provided" do
serialized = described_class.new(user_badge, scope: guardian, root: false).as_json
expect(serialized[:topic_id]).to eq(nil)
expect(serialized[:topic_title]).to eq(nil)
end
it "does not included attributes in serialized object when topic id is not present in allowed_user_badge_topic_ids option" do
serialized =
described_class.new(
user_badge,
scope: guardian,
allowed_user_badge_topic_ids: guardian.can_see_topic_ids(topic_ids: [post.topic_id + 1]),
root: false,
).as_json
expect(serialized[:topic_id]).to eq(nil)
expect(serialized[:topic_title]).to eq(nil)
end
it "includes attributes in serialized object for admin scope even if allowed_user_badge_topic_ids option is not provided" do
serialized = described_class.new(user_badge, scope: Guardian.new(admin), root: false).as_json
expect(serialized[:topic_id]).to eq(post.topic_id)
expect(serialized[:topic_title]).to eq(post.topic.title)
end
it "includes attributes in serialized object when topic id is present in allowed_user_badge_topic_ids option" do
serialized =
described_class.new(
user_badge,
scope: guardian,
allowed_user_badge_topic_ids: guardian.can_see_topic_ids(topic_ids: [post.topic_id]),
root: false,
).as_json
expect(serialized[:topic_id]).to eq(post.topic_id)
expect(serialized[:topic_title]).to eq(post.topic.title)
end
end
end