2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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)
|
2019-04-12 09:55:27 -04:00
|
|
|
serializer = TopicViewSerializer.new(topic_view, scope: Guardian.new(user_arg), root: false).as_json
|
|
|
|
JSON.parse(MultiJson.dump(serializer)).deep_symbolize_keys!
|
2018-05-22 18:39:15 -04:00
|
|
|
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
|
|
|
|
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(: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
|
|
|
|
|
2019-10-22 00:02:49 -04:00
|
|
|
describe '#image_url' do
|
|
|
|
let(:image_url) { 'http://meta.discourse.org/images/welcome/discourse-edit-post-animated.gif' }
|
|
|
|
|
|
|
|
describe 'when a topic has an image' do
|
|
|
|
it 'should return the image url' do
|
|
|
|
topic.update!(image_url: image_url)
|
|
|
|
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
|
|
|
|
expect(json[:image_url]).to eq(image_url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when a topic does not contain an image' do
|
|
|
|
it 'should return a nil image url' do
|
|
|
|
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
|
|
|
|
expect(json.has_key? :image_url).to eq(true)
|
|
|
|
expect(json[:image_url]).to eq(nil)
|
|
|
|
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
|
|
|
|
2019-04-12 09:55:27 -04:00
|
|
|
expect(json[:suggested_topics].first[:id]).to eq(topic2.id)
|
2017-09-26 02:42:27 -04:00
|
|
|
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
|
2019-04-12 09:55:27 -04:00
|
|
|
|
2019-05-03 14:26:37 -04:00
|
|
|
context "with flags" do
|
|
|
|
let!(:post) { Fabricate(:post, topic: topic) }
|
|
|
|
let!(:other_post) { Fabricate(:post, topic: topic) }
|
|
|
|
|
|
|
|
it "will return reviewable counts on posts" do
|
|
|
|
r = PostActionCreator.inappropriate(Fabricate(:user), post).reviewable
|
|
|
|
r.perform(admin, :agree_and_keep)
|
|
|
|
PostActionCreator.spam(Fabricate(:user), post)
|
|
|
|
|
|
|
|
json = serialize_topic(topic, admin)
|
|
|
|
p0 = json[:post_stream][:posts][0]
|
|
|
|
expect(p0[:id]).to eq(post.id)
|
|
|
|
expect(p0[:reviewable_score_count]).to eq(2)
|
|
|
|
expect(p0[:reviewable_score_pending_count]).to eq(1)
|
|
|
|
|
|
|
|
p1 = json[:post_stream][:posts][1]
|
|
|
|
expect(p1[:reviewable_score_count]).to eq(0)
|
|
|
|
expect(p1[:reviewable_score_pending_count]).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-12 09:55:27 -04:00
|
|
|
describe "pending posts" do
|
|
|
|
context "when the queue is enabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.approve_post_count = 1
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:queued_post) do
|
|
|
|
ReviewableQueuedPost.needs_review!(
|
|
|
|
topic: topic,
|
|
|
|
payload: { raw: "hello my raw contents" },
|
|
|
|
created_by: user
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a pending_posts_count when the queue is enabled" do
|
|
|
|
json = serialize_topic(topic, admin)
|
|
|
|
expect(json[:queued_posts_count]).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a user's pending posts" do
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
expect(json[:queued_posts_count]).to be_nil
|
|
|
|
|
|
|
|
post = json[:pending_posts].find { |p| p[:id] = queued_post.id }
|
|
|
|
expect(post[:raw]).to eq("hello my raw contents")
|
|
|
|
expect(post).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "without an enabled queue" do
|
|
|
|
it "returns nil for the count" do
|
|
|
|
json = serialize_topic(topic, admin)
|
|
|
|
expect(json[:queued_posts_count]).to be_nil
|
|
|
|
expect(json[:pending_posts]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "details" do
|
|
|
|
it "returns the details object" do
|
|
|
|
PostCreator.create!(user, topic_id: topic.id, raw: "this is my post content")
|
|
|
|
topic.topic_links.create!(user: user, url: 'https://discourse.org', domain: 'discourse.org', clicks: 100)
|
|
|
|
json = serialize_topic(topic, admin)
|
|
|
|
|
|
|
|
details = json[:details]
|
|
|
|
expect(details).to be_present
|
|
|
|
expect(details[:created_by][:id]).to eq(topic.user_id)
|
|
|
|
expect(details[:last_poster][:id]).to eq(user.id)
|
|
|
|
expect(details[:notification_level]).to be_present
|
|
|
|
expect(details[:can_move_posts]).to eq(true)
|
|
|
|
expect(details[:can_flag_topic]).to eq(true)
|
2019-05-03 14:26:37 -04:00
|
|
|
expect(details[:can_review_topic]).to eq(true)
|
2019-04-12 09:55:27 -04:00
|
|
|
expect(details[:links][0][:clicks]).to eq(100)
|
|
|
|
|
|
|
|
participant = details[:participants].find { |p| p[:id] == user.id }
|
|
|
|
expect(participant[:post_count]).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns extra fields for a personal message" do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
GroupUser.create(group: group, user: user)
|
|
|
|
GroupUser.create(group: group, user: admin)
|
|
|
|
|
|
|
|
group2 = Fabricate(:group)
|
|
|
|
GroupUser.create(group: group2, user: user)
|
|
|
|
|
|
|
|
pm = Fabricate(:private_message_topic)
|
|
|
|
pm.update(archetype: 'private_message')
|
|
|
|
pm.topic_allowed_groups.create!(group: group)
|
|
|
|
pm.topic_allowed_groups.create!(group: group2)
|
|
|
|
|
|
|
|
json = serialize_topic(pm, admin)
|
|
|
|
|
|
|
|
details = json[:details]
|
|
|
|
expect(details[:can_remove_self_id]).to eq(admin.id)
|
|
|
|
expect(details[:allowed_users].find { |au| au[:id] == pm.user_id }).to be_present
|
|
|
|
expect(details[:allowed_groups].find { |ag| ag[:id] == group.id }).to be_present
|
|
|
|
end
|
2019-10-23 14:05:38 -04:00
|
|
|
|
2020-04-08 12:52:36 -04:00
|
|
|
it "has can_publish_page if possible" do
|
|
|
|
SiteSetting.enable_page_publishing = true
|
|
|
|
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
expect(json[:details][:can_publish_page]).to be_blank
|
|
|
|
|
|
|
|
json = serialize_topic(topic, admin)
|
|
|
|
expect(json[:details][:can_publish_page]).to eq(true)
|
|
|
|
end
|
|
|
|
|
2019-10-23 14:05:38 -04:00
|
|
|
context "can_edit_tags" do
|
|
|
|
before do
|
|
|
|
SiteSetting.tagging_enabled = true
|
|
|
|
SiteSetting.min_trust_to_edit_wiki_post = 2
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true when user can edit a wiki topic" do
|
|
|
|
post = Fabricate(:post, wiki: true)
|
|
|
|
topic = Fabricate(:topic, first_post: post)
|
|
|
|
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
expect(json[:details][:can_edit_tags]).to be_nil
|
|
|
|
|
|
|
|
user.update!(trust_level: 2)
|
|
|
|
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
expect(json[:details][:can_edit_tags]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2019-04-12 09:55:27 -04:00
|
|
|
end
|
|
|
|
|
2020-04-08 12:52:36 -04:00
|
|
|
context "published_page" do
|
|
|
|
fab!(:published_page) { Fabricate(:published_page, topic: topic) }
|
|
|
|
|
|
|
|
it "doesn't return the published page if not enabled" do
|
|
|
|
json = serialize_topic(topic, admin)
|
|
|
|
expect(json[:published_page]).to be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't return the published page unless staff" do
|
|
|
|
SiteSetting.enable_page_publishing = true
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
expect(json[:published_page]).to be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the published page if enabled and staff" do
|
|
|
|
SiteSetting.enable_page_publishing = true
|
|
|
|
json = serialize_topic(topic, admin)
|
|
|
|
expect(json[:published_page]).to be_present
|
|
|
|
expect(json[:published_page][:slug]).to eq("published-page-test")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-26 02:42:27 -04:00
|
|
|
end
|