2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-03 23:54:20 +08:00
discourse/spec/serializers/suggested_topic_serializer_spec.rb
Natalie Tay 9e99066b07
DEV: Expand top_tags, topic.tags, etc, to return an array of tag objects instead of tag names (#36678)
Currently in several endpoints, we return an array of strings for tags.
Our goal with this PR is to expand array tag name strings to an array of
tag objects.

#### before: Tags were returned as string arrays
```
{ "tags": ["support", "bug-report"] }
```

#### after: Tags are returned as object arrays
```
{ "tags": [{"id": 12, "name": "support", "slug": "support"}, {"id": 13, "name": "bug-report", "slug": "bug-report"}] }
```

This allows us to start referencing tags by their ids, and return more
information for a tag for future features.

This commit involves updating several areas:
- topic lists (/latest.json, /top.json, /c/:category/:id.json, etc, for
`top_tags`)
- tag chooser components (`MiniTagChooser`, `TagDrop`, etc)
- topic view (/t/:id.json)
- tag groups (/tag_groups.json, tags, parent_tag)
- category settings
- staff action logs
- synonyms
- ...

APIs that reference tags based on their names will still be supported
with a deprecation warning. Moving on, we will reference them using
their tag ids.
2026-02-02 10:03:02 +08:00

80 lines
2.4 KiB
Ruby

# frozen_string_literal: true
RSpec.describe SuggestedTopicSerializer do
let(:user) { Fabricate(:user) }
let(:admin) { Fabricate(:admin) }
describe "#featured_link and #featured_link_root_domain" do
subject(:json) do
SuggestedTopicSerializer.new(topic, scope: Guardian.new(user), root: false).as_json
end
let(:featured_link) { "http://meta.discourse.org" }
let(:topic) do
Fabricate(
:topic,
featured_link: featured_link,
category: Fabricate(:category, topic_featured_link_allowed: true),
)
end
context "when topic featured link is disable" do
before do
SiteSetting.topic_featured_link_enabled = true
topic
SiteSetting.topic_featured_link_enabled = false
end
it "should not return featured link attrs" do
expect(json[:featured_link]).to eq(nil)
expect(json[:featured_link_root_domain]).to eq(nil)
end
end
context "when topic featured link is enabled" do
before { SiteSetting.topic_featured_link_enabled = true }
it "should return featured link attrs" do
expect(json[:featured_link]).to eq(featured_link)
expect(json[:featured_link_root_domain]).to eq("discourse.org")
end
end
end
describe "hidden tags" do
let(:topic) { Fabricate(:topic) }
let(:hidden_tag) { Fabricate(:tag, name: "hidden") }
let(:staff_tag_group) do
Fabricate(:tag_group, permissions: { "staff" => 1 }, tag_names: [hidden_tag.name])
end
before do
SiteSetting.tagging_enabled = true
staff_tag_group
topic.tags << hidden_tag
end
it "returns hidden tag to staff" do
json = SuggestedTopicSerializer.new(topic, scope: Guardian.new(admin), root: false).as_json
expect(json[:tags]).to eq(
[{ id: hidden_tag.id, name: hidden_tag.name, slug: hidden_tag.slug }],
)
end
it "does not return hidden tag to non-staff" do
json = SuggestedTopicSerializer.new(topic, scope: Guardian.new(user), root: false).as_json
expect(json[:tags]).to eq([])
end
end
describe "#op_like_count" do
it "returns the first post's like count" do
topic = Fabricate(:topic)
post = Fabricate(:post, topic: topic, like_count: 5)
topic.update!(first_post: post)
json = SuggestedTopicSerializer.new(topic, scope: Guardian.new(user), root: false).as_json
expect(json[:op_like_count]).to eq(5)
end
end
end