2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe TopicListItemSerializer do
|
2017-11-29 08:52:41 -05:00
|
|
|
let(:topic) do
|
2013-10-28 00:55:27 -04:00
|
|
|
date = Time.zone.now
|
|
|
|
|
2020-02-13 01:26:02 -05:00
|
|
|
Fabricate(:topic,
|
|
|
|
title: 'This is a test topic title',
|
2017-11-29 08:52:41 -05:00
|
|
|
created_at: date - 2.minutes,
|
2020-02-13 01:26:02 -05:00
|
|
|
bumped_at: date
|
2017-11-29 08:52:41 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "correctly serializes topic" do
|
|
|
|
SiteSetting.topic_featured_link_enabled = true
|
2013-10-28 00:55:27 -04:00
|
|
|
serialized = TopicListItemSerializer.new(topic, scope: Guardian.new, root: false).as_json
|
|
|
|
|
2020-02-13 01:26:02 -05:00
|
|
|
expect(serialized[:title]).to eq("This is a test topic title")
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(serialized[:bumped]).to eq(true)
|
2017-11-29 08:52:41 -05:00
|
|
|
expect(serialized[:featured_link]).to eq(nil)
|
|
|
|
expect(serialized[:featured_link_root_domain]).to eq(nil)
|
|
|
|
|
|
|
|
featured_link = 'http://meta.discourse.org'
|
|
|
|
topic.featured_link = featured_link
|
|
|
|
serialized = TopicListItemSerializer.new(topic, scope: Guardian.new, root: false).as_json
|
|
|
|
|
|
|
|
expect(serialized[:featured_link]).to eq(featured_link)
|
|
|
|
expect(serialized[:featured_link_root_domain]).to eq('discourse.org')
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when topic featured link is disable' do
|
|
|
|
before do
|
|
|
|
SiteSetting.topic_featured_link_enabled = false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not include the topic's featured link" do
|
|
|
|
topic.featured_link = 'http://meta.discourse.org'
|
|
|
|
serialized = TopicListItemSerializer.new(topic, scope: Guardian.new, root: false).as_json
|
|
|
|
|
|
|
|
expect(serialized[:featured_link]).to eq(nil)
|
|
|
|
expect(serialized[:featured_link_root_domain]).to eq(nil)
|
|
|
|
end
|
2013-10-28 00:55:27 -04:00
|
|
|
end
|
2018-03-26 17:04:55 -04:00
|
|
|
|
|
|
|
describe 'hidden tags' do
|
2018-11-18 23:53:33 -05:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
let(:user) { Fabricate(:user) }
|
2018-03-26 17:04:55 -04:00
|
|
|
let(:hidden_tag) { Fabricate(:tag, name: 'hidden') }
|
|
|
|
let(:staff_tag_group) { Fabricate(:tag_group, permissions: { "staff" => 1 }, tag_names: [hidden_tag.name]) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.tagging_enabled = true
|
|
|
|
staff_tag_group
|
|
|
|
topic.tags << hidden_tag
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns hidden tag to staff' do
|
2018-11-18 23:53:33 -05:00
|
|
|
json = TopicListItemSerializer.new(topic,
|
|
|
|
scope: Guardian.new(admin),
|
|
|
|
root: false
|
|
|
|
).as_json
|
|
|
|
|
2018-03-26 17:04:55 -04:00
|
|
|
expect(json[:tags]).to eq([hidden_tag.name])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return hidden tag to non-staff' do
|
2018-11-18 23:53:33 -05:00
|
|
|
json = TopicListItemSerializer.new(topic,
|
|
|
|
scope: Guardian.new(user),
|
|
|
|
root: false
|
|
|
|
).as_json
|
|
|
|
|
|
|
|
expect(json[:tags]).to eq([])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts an option to remove hidden tags' do
|
|
|
|
json = TopicListItemSerializer.new(topic,
|
|
|
|
scope: Guardian.new(user),
|
|
|
|
hidden_tag_names: [hidden_tag.name],
|
|
|
|
root: false
|
|
|
|
).as_json
|
|
|
|
|
2018-03-26 17:04:55 -04:00
|
|
|
expect(json[:tags]).to eq([])
|
|
|
|
end
|
2020-04-22 12:24:54 -04:00
|
|
|
|
|
|
|
it 'return posters' do
|
|
|
|
json = TopicListItemSerializer.new(topic,
|
|
|
|
scope: Guardian.new(user),
|
|
|
|
hidden_tag_names: [hidden_tag.name],
|
|
|
|
root: false
|
|
|
|
).as_json
|
|
|
|
|
|
|
|
expect(json[:posters].length).to eq(1)
|
|
|
|
end
|
2018-03-26 17:04:55 -04:00
|
|
|
end
|
2013-10-28 00:55:27 -04:00
|
|
|
end
|