2017-09-26 02:42:27 -04:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe TopicViewSerializer do
|
2018-03-26 17:04:55 -04:00
|
|
|
def serialize_topic(topic, user_arg)
|
|
|
|
topic_view = TopicView.new(topic.id, user_arg)
|
2018-05-22 18:39:15 -04:00
|
|
|
TopicViewSerializer.new(topic_view, scope: Guardian.new(user_arg), root: false).as_json
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
# ensure no suggested ids are cached cause that can muck up suggested
|
|
|
|
RandomTopicSelector.clear_cache!
|
2018-02-13 15:46:25 -05:00
|
|
|
end
|
|
|
|
|
2017-09-26 02:42:27 -04:00
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let(:user) { Fabricate(:user) }
|
2018-03-26 17:04:55 -04:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
2017-09-26 02:42:27 -04:00
|
|
|
|
2017-11-29 08:52:41 -05:00
|
|
|
describe '#featured_link and #featured_link_root_domain' do
|
|
|
|
let(:featured_link) { 'http://meta.discourse.org' }
|
|
|
|
|
|
|
|
describe 'when topic featured link is disable' do
|
|
|
|
it 'should return the right attributes' do
|
|
|
|
topic.update!(featured_link: featured_link)
|
|
|
|
SiteSetting.topic_featured_link_enabled = false
|
|
|
|
|
2018-02-13 15:46:25 -05:00
|
|
|
json = serialize_topic(topic, user)
|
2017-11-29 08:52:41 -05:00
|
|
|
|
|
|
|
expect(json[:featured_link]).to eq(nil)
|
|
|
|
expect(json[:featured_link_root_domain]).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when topic featured link is enabled' do
|
|
|
|
it 'should return the right attributes' do
|
|
|
|
topic.update!(featured_link: featured_link)
|
|
|
|
|
2018-02-13 15:46:25 -05:00
|
|
|
json = serialize_topic(topic, user)
|
2017-11-29 08:52:41 -05:00
|
|
|
|
|
|
|
expect(json[:featured_link]).to eq(featured_link)
|
|
|
|
expect(json[:featured_link_root_domain]).to eq('discourse.org')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-26 02:42:27 -04:00
|
|
|
describe '#suggested_topics' do
|
|
|
|
let(:topic2) { Fabricate(:topic) }
|
|
|
|
|
|
|
|
before do
|
2017-11-17 16:08:31 -05:00
|
|
|
TopicUser.update_last_read(user, topic2.id, 0, 0, 0)
|
2017-09-26 02:42:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when loading last chunk' do
|
|
|
|
it 'should include suggested topics' do
|
2018-02-13 15:46:25 -05:00
|
|
|
json = serialize_topic(topic, user)
|
2017-09-26 02:42:27 -04:00
|
|
|
|
|
|
|
expect(json[:suggested_topics].first.id).to eq(topic2.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when not loading last chunk' do
|
|
|
|
let(:post) { Fabricate(:post, topic: topic) }
|
|
|
|
let(:post2) { Fabricate(:post, topic: topic) }
|
|
|
|
|
|
|
|
it 'should not include suggested topics' do
|
|
|
|
post
|
|
|
|
post2
|
|
|
|
topic_view = TopicView.new(topic.id, user, post_ids: [post.id])
|
|
|
|
topic_view.next_page
|
|
|
|
json = described_class.new(topic_view, scope: Guardian.new(user), root: false).as_json
|
|
|
|
|
|
|
|
expect(json[:suggested_topics]).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-02-13 15:46:25 -05:00
|
|
|
|
|
|
|
describe 'when tags added to private message topics' do
|
2018-02-22 09:57:02 -05:00
|
|
|
let(:moderator) { Fabricate(:moderator) }
|
|
|
|
let(:tag) { Fabricate(:tag) }
|
|
|
|
let(:pm) do
|
|
|
|
Fabricate(:private_message_topic, tags: [tag], topic_allowed_users: [
|
|
|
|
Fabricate.build(:topic_allowed_user, user: moderator),
|
|
|
|
Fabricate.build(:topic_allowed_user, user: user)
|
|
|
|
])
|
|
|
|
end
|
|
|
|
|
2018-02-13 15:46:25 -05:00
|
|
|
before do
|
|
|
|
SiteSetting.tagging_enabled = true
|
2018-02-21 20:18:34 -05:00
|
|
|
SiteSetting.allow_staff_to_tag_pms = true
|
2018-02-13 15:46:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not include the tag for normal users" do
|
|
|
|
json = serialize_topic(pm, user)
|
|
|
|
expect(json[:tags]).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should include the tag for staff users" do
|
2018-02-22 09:57:02 -05:00
|
|
|
[moderator, admin].each do |user|
|
|
|
|
json = serialize_topic(pm, user)
|
|
|
|
expect(json[:tags]).to eq([tag.name])
|
|
|
|
end
|
2018-02-13 15:46:25 -05:00
|
|
|
end
|
2018-02-21 11:17:02 -05:00
|
|
|
|
|
|
|
it "should not include the tag if pm tags disabled" do
|
2018-02-21 20:18:34 -05:00
|
|
|
SiteSetting.allow_staff_to_tag_pms = false
|
2018-02-21 11:17:02 -05:00
|
|
|
|
2018-02-22 09:57:02 -05:00
|
|
|
[moderator, admin].each do |user|
|
|
|
|
json = serialize_topic(pm, user)
|
|
|
|
expect(json[:tags]).to eq(nil)
|
|
|
|
end
|
2018-02-21 11:17:02 -05:00
|
|
|
end
|
2018-02-13 15:46:25 -05:00
|
|
|
end
|
2018-03-26 17:04:55 -04:00
|
|
|
|
|
|
|
describe 'with hidden tags' do
|
|
|
|
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
|
|
|
|
json = serialize_topic(topic, admin)
|
|
|
|
expect(json[:tags]).to eq([hidden_tag.name])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return hidden tag to non-staff' do
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
expect(json[:tags]).to eq([])
|
|
|
|
end
|
|
|
|
end
|
2017-09-26 02:42:27 -04:00
|
|
|
end
|