2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe PostSerializer do
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:post)
|
2013-09-03 17:19:29 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with a post with lots of actions" do
|
2024-01-25 01:28:26 -05:00
|
|
|
fab!(:actor) { Fabricate(:user, refresh_auto_groups: true) }
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:admin)
|
2014-06-26 13:19:35 -04:00
|
|
|
let(:acted_ids) do
|
2013-10-03 19:48:03 -04:00
|
|
|
PostActionType.public_types.values.concat(
|
2017-10-17 13:31:45 -04:00
|
|
|
%i[notify_user spam].map { |k| PostActionType.types[k] },
|
|
|
|
)
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2013-10-03 19:48:03 -04:00
|
|
|
|
|
|
|
def visible_actions_for(user)
|
|
|
|
serializer = PostSerializer.new(post, scope: Guardian.new(user), root: false)
|
|
|
|
# NOTE this is messy, we should extract all this logic elsewhere
|
|
|
|
serializer.post_actions = PostAction.counts_for([post], actor)[post.id] if user.try(:id) ==
|
|
|
|
actor.id
|
|
|
|
actions = serializer.as_json[:actions_summary]
|
|
|
|
lookup = PostActionType.types.invert
|
2015-07-14 15:50:27 -04:00
|
|
|
actions.keep_if { |a| (a[:count] || 0) > 0 }.map { |a| lookup[a[:id]] }
|
2013-10-03 19:48:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2019-01-03 12:03:01 -05:00
|
|
|
acted_ids.each { |id| PostActionCreator.new(actor, post, id).perform }
|
2013-10-03 19:48:03 -04:00
|
|
|
post.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "displays the correct info" do
|
2018-07-09 04:54:18 -04:00
|
|
|
expect(visible_actions_for(actor).sort).to eq(%i[like notify_user spam])
|
|
|
|
expect(visible_actions_for(post.user).sort).to eq([:like])
|
|
|
|
expect(visible_actions_for(nil).sort).to eq([:like])
|
|
|
|
expect(visible_actions_for(admin).sort).to eq(%i[like notify_user spam])
|
2013-10-03 19:48:03 -04:00
|
|
|
end
|
|
|
|
|
2014-07-25 16:36:57 -04:00
|
|
|
it "can't flag your own post to notify yourself" do
|
|
|
|
serializer = PostSerializer.new(post, scope: Guardian.new(post.user), root: false)
|
|
|
|
notify_user_action =
|
|
|
|
serializer.actions_summary.find { |a| a[:id] == PostActionType.types[:notify_user] }
|
2015-07-14 15:50:27 -04:00
|
|
|
expect(notify_user_action).to be_blank
|
2015-07-15 11:18:01 -04:00
|
|
|
end
|
2019-05-09 22:04:29 -04:00
|
|
|
|
|
|
|
it "should not allow user to flag post and notify non human user" do
|
|
|
|
post.update!(user: Discourse.system_user)
|
|
|
|
|
2021-09-20 18:45:47 -04:00
|
|
|
serializer = PostSerializer.new(post, scope: Guardian.new(actor), root: false)
|
2019-05-09 22:04:29 -04:00
|
|
|
|
|
|
|
notify_user_action =
|
|
|
|
serializer.actions_summary.find { |a| a[:id] == PostActionType.types[:notify_user] }
|
|
|
|
|
|
|
|
expect(notify_user_action).to eq(nil)
|
|
|
|
end
|
2013-10-03 19:48:03 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with a post with reviewable content" do
|
2023-12-13 04:18:42 -05:00
|
|
|
let!(:reviewable) do
|
|
|
|
PostActionCreator.spam(Fabricate(:user, refresh_auto_groups: true), post).reviewable
|
|
|
|
end
|
2019-05-03 14:26:37 -04:00
|
|
|
|
|
|
|
it "includes the reviewable data" do
|
|
|
|
json =
|
|
|
|
PostSerializer.new(post, scope: Guardian.new(Fabricate(:moderator)), root: false).as_json
|
|
|
|
expect(json[:reviewable_id]).to eq(reviewable.id)
|
|
|
|
expect(json[:reviewable_score_count]).to eq(1)
|
|
|
|
expect(json[:reviewable_score_pending_count]).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with a post by a nuked user" do
|
2023-06-21 10:00:19 -04:00
|
|
|
subject(:serializer) do
|
2013-09-03 17:19:29 -04:00
|
|
|
PostSerializer.new(post, scope: Guardian.new(Fabricate(:admin)), root: false).as_json
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2013-09-03 17:19:29 -04:00
|
|
|
|
2023-06-21 10:00:19 -04:00
|
|
|
before { post.update!(user_id: nil, deleted_at: Time.zone.now) }
|
|
|
|
|
2013-09-03 17:19:29 -04:00
|
|
|
it "serializes correctly" do
|
2014-09-25 11:44:48 -04:00
|
|
|
%i[name username display_username avatar_template user_title trust_level].each do |attr|
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(serializer[attr]).to be_nil
|
2013-09-03 17:19:29 -04:00
|
|
|
end
|
2023-06-21 10:00:19 -04:00
|
|
|
%i[moderator staff yours].each { |attr| expect(serializer[attr]).to eq(false) }
|
2013-09-03 17:19:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with a post by a suspended user" do
|
2023-06-21 10:00:19 -04:00
|
|
|
def serializer
|
2022-04-13 10:28:09 -04:00
|
|
|
PostSerializer.new(post, scope: Guardian.new(Fabricate(:admin)), root: false).as_json
|
|
|
|
end
|
|
|
|
|
|
|
|
it "serializes correctly" do
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(serializer[:user_suspended]).to be_nil
|
2022-04-13 10:28:09 -04:00
|
|
|
|
|
|
|
post.user.update!(suspended_till: 1.month.from_now)
|
|
|
|
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(serializer[:user_suspended]).to eq(true)
|
2022-04-13 10:28:09 -04:00
|
|
|
|
2023-12-06 17:25:00 -05:00
|
|
|
freeze_time(2.months.from_now)
|
2022-04-13 10:28:09 -04:00
|
|
|
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(serializer[:user_suspended]).to be_nil
|
2022-04-13 10:28:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "#display_username" do
|
2019-05-09 22:04:29 -04:00
|
|
|
let(:user) { post.user }
|
2013-10-30 15:45:13 -04:00
|
|
|
let(:serializer) { PostSerializer.new(post, scope: Guardian.new, root: false) }
|
|
|
|
let(:json) { serializer.as_json }
|
|
|
|
|
|
|
|
it "returns the display_username it when `enable_names` is on" do
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.enable_names = true
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(json[:display_username]).to be_present
|
2013-10-30 15:45:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't return the display_username it when `enable_names` is off" do
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.enable_names = false
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(json[:display_username]).to be_blank
|
2013-10-30 15:45:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with a hidden post with add_raw enabled" do
|
2022-10-24 18:18:40 -04:00
|
|
|
let(:user) { Fabricate(:user) }
|
2014-04-30 14:21:43 -04:00
|
|
|
let(:raw) { "Raw contents of the post." }
|
2014-04-15 12:48:49 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with a public post" do
|
2022-10-23 17:51:41 -04:00
|
|
|
let(:post) { Fabricate(:post, raw: raw, user: user) }
|
2014-04-15 12:48:49 -04:00
|
|
|
|
2014-04-30 14:21:43 -04:00
|
|
|
it "includes the raw post for everyone" do
|
2014-06-26 13:19:35 -04:00
|
|
|
[nil, user, Fabricate(:user), Fabricate(:moderator), Fabricate(:admin)].each do |user|
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(serialized_post_for_user(user)[:raw]).to eq(raw)
|
2014-06-26 13:19:35 -04:00
|
|
|
end
|
2014-04-30 14:21:43 -04:00
|
|
|
end
|
2014-04-15 12:48:49 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with a hidden post" do
|
2022-10-23 17:51:41 -04:00
|
|
|
let(:post) do
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
raw: raw,
|
|
|
|
user: user,
|
|
|
|
hidden: true,
|
|
|
|
hidden_reason_id: Post.hidden_reasons[:flag_threshold_reached],
|
|
|
|
)
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2014-04-30 14:21:43 -04:00
|
|
|
|
2023-05-31 23:32:05 -04:00
|
|
|
it "includes if the user can see it" do
|
|
|
|
expect(serialized_post_for_user(Fabricate(:moderator))[:can_see_hidden_post]).to eq(true)
|
|
|
|
expect(serialized_post_for_user(Fabricate(:admin))[:can_see_hidden_post]).to eq(true)
|
|
|
|
expect(serialized_post_for_user(user)[:can_see_hidden_post]).to eq(true)
|
|
|
|
expect(serialized_post_for_user(Fabricate(:user))[:can_see_hidden_post]).to eq(false)
|
|
|
|
end
|
|
|
|
|
2014-04-30 14:21:43 -04:00
|
|
|
it "shows the raw post only if authorized to see it" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(serialized_post_for_user(nil)[:raw]).to eq(nil)
|
|
|
|
expect(serialized_post_for_user(Fabricate(:user))[:raw]).to eq(nil)
|
2014-06-26 13:19:35 -04:00
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(serialized_post_for_user(user)[:raw]).to eq(raw)
|
|
|
|
expect(serialized_post_for_user(Fabricate(:moderator))[:raw]).to eq(raw)
|
|
|
|
expect(serialized_post_for_user(Fabricate(:admin))[:raw]).to eq(raw)
|
2014-04-30 14:21:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can view edit history only if authorized" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(serialized_post_for_user(nil)[:can_view_edit_history]).to eq(false)
|
|
|
|
expect(serialized_post_for_user(Fabricate(:user))[:can_view_edit_history]).to eq(false)
|
2014-06-26 13:19:35 -04:00
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(serialized_post_for_user(user)[:can_view_edit_history]).to eq(true)
|
|
|
|
expect(serialized_post_for_user(Fabricate(:moderator))[:can_view_edit_history]).to eq(true)
|
|
|
|
expect(serialized_post_for_user(Fabricate(:admin))[:can_view_edit_history]).to eq(true)
|
2014-06-26 13:19:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with a hidden revised post" do
|
2019-05-10 06:59:31 -04:00
|
|
|
fab!(:post) { Fabricate(:post, raw: "Hello world!", hidden: true) }
|
2018-10-31 10:47:00 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.editing_grace_period_max_diff = 1
|
|
|
|
|
|
|
|
revisor = PostRevisor.new(post)
|
|
|
|
revisor.revise!(post.user, raw: "Hello, everyone!")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will not leak version to users" do
|
|
|
|
json = PostSerializer.new(post, scope: Guardian.new(user), root: false).as_json
|
|
|
|
expect(json[:version]).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will show real version to staff" do
|
|
|
|
json = PostSerializer.new(post, scope: Guardian.new(Fabricate(:admin)), root: false).as_json
|
|
|
|
expect(json[:version]).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with a public wiki post" do
|
2022-10-23 17:51:41 -04:00
|
|
|
let(:post) { Fabricate(:post, raw: raw, user: user, wiki: true) }
|
2014-06-26 13:19:35 -04:00
|
|
|
|
|
|
|
it "can view edit history" do
|
|
|
|
[nil, user, Fabricate(:user), Fabricate(:moderator), Fabricate(:admin)].each do |user|
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(serialized_post_for_user(user)[:can_view_edit_history]).to eq(true)
|
2014-06-26 13:19:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with a hidden wiki post" do
|
2018-06-05 03:29:17 -04:00
|
|
|
let(:post) do
|
2022-10-23 17:51:41 -04:00
|
|
|
Fabricate(
|
2018-06-05 03:29:17 -04:00
|
|
|
:post,
|
|
|
|
raw: raw,
|
|
|
|
user: user,
|
|
|
|
wiki: true,
|
|
|
|
hidden: true,
|
|
|
|
hidden_reason_id: Post.hidden_reasons[:flag_threshold_reached],
|
|
|
|
)
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2014-06-26 13:19:35 -04:00
|
|
|
|
|
|
|
it "can view edit history only if authorized" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(serialized_post_for_user(nil)[:can_view_edit_history]).to eq(false)
|
|
|
|
expect(serialized_post_for_user(Fabricate(:user))[:can_view_edit_history]).to eq(false)
|
|
|
|
expect(serialized_post_for_user(user)[:can_view_edit_history]).to eq(true)
|
|
|
|
expect(serialized_post_for_user(Fabricate(:moderator))[:can_view_edit_history]).to eq(true)
|
|
|
|
expect(serialized_post_for_user(Fabricate(:admin))[:can_view_edit_history]).to eq(true)
|
2014-04-30 14:21:43 -04:00
|
|
|
end
|
|
|
|
end
|
2014-04-15 12:48:49 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with a post with notices" do
|
2019-05-10 06:59:31 -04:00
|
|
|
fab!(:user) { Fabricate(:user, trust_level: 1) }
|
|
|
|
fab!(:user_tl1) { Fabricate(:user, trust_level: 1) }
|
|
|
|
fab!(:user_tl2) { Fabricate(:user, trust_level: 2) }
|
2019-03-11 05:19:58 -04:00
|
|
|
|
|
|
|
let(:post) do
|
|
|
|
post = Fabricate(:post, user: user)
|
2020-11-11 07:49:53 -05:00
|
|
|
post.custom_fields[Post::NOTICE] = {
|
|
|
|
type: Post.notices[:returning_user],
|
|
|
|
last_posted_at: 1.day.ago,
|
|
|
|
}
|
2019-03-11 05:19:58 -04:00
|
|
|
post.save_custom_fields
|
|
|
|
post
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2019-03-11 05:19:58 -04:00
|
|
|
|
|
|
|
def json_for_user(user)
|
|
|
|
PostSerializer.new(post, scope: Guardian.new(user), root: false).as_json
|
|
|
|
end
|
|
|
|
|
2019-04-10 15:06:27 -04:00
|
|
|
it "is visible for TL2+ users (except poster)" do
|
2020-11-11 07:49:53 -05:00
|
|
|
expect(json_for_user(nil)[:notice]).to eq(nil)
|
|
|
|
expect(json_for_user(user)[:notice]).to eq(nil)
|
2019-03-12 17:06:28 -04:00
|
|
|
|
2019-04-19 10:53:58 -04:00
|
|
|
SiteSetting.returning_user_notice_tl = 2
|
2020-11-11 07:49:53 -05:00
|
|
|
expect(json_for_user(user_tl1)[:notice]).to eq(nil)
|
|
|
|
expect(json_for_user(user_tl2)[:notice][:type]).to eq(Post.notices[:returning_user])
|
2019-03-12 17:06:28 -04:00
|
|
|
|
2019-04-19 10:53:58 -04:00
|
|
|
SiteSetting.returning_user_notice_tl = 1
|
2020-11-11 07:49:53 -05:00
|
|
|
expect(json_for_user(user_tl1)[:notice][:type]).to eq(Post.notices[:returning_user])
|
|
|
|
expect(json_for_user(user_tl2)[:notice][:type]).to eq(Post.notices[:returning_user])
|
2019-04-10 15:06:27 -04:00
|
|
|
end
|
2019-03-11 05:19:58 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with a post with bookmarks" do
|
2019-12-10 23:04:02 -05:00
|
|
|
let(:current_user) { Fabricate(:user) }
|
2019-12-12 01:28:01 -05:00
|
|
|
let(:topic_view) { TopicView.new(post.topic, current_user) }
|
2019-12-10 23:04:02 -05:00
|
|
|
let(:serialized) do
|
|
|
|
s = serialized_post(current_user)
|
|
|
|
s.post_actions = PostAction.counts_for([post], current_user)[post.id]
|
2019-12-12 01:28:01 -05:00
|
|
|
s.topic_view = topic_view
|
2019-12-10 23:04:02 -05:00
|
|
|
s
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when a Bookmark record exists for the user on the post" do
|
2022-05-22 20:07:15 -04:00
|
|
|
let!(:bookmark) do
|
|
|
|
Fabricate(:bookmark_next_business_day_reminder, user: current_user, bookmarkable: post)
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2019-12-10 23:04:02 -05:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with bookmarks with reminders" do
|
2019-12-10 23:04:02 -05:00
|
|
|
it "returns true" do
|
2020-04-29 20:09:22 -04:00
|
|
|
expect(serialized.as_json[:bookmarked]).to eq(true)
|
2019-12-10 23:04:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the reminder_at for the bookmark" do
|
|
|
|
expect(serialized.as_json[:bookmark_reminder_at]).to eq(bookmark.reminder_at.iso8601)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with posts when group moderation is enabled" do
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:topic)
|
|
|
|
fab!(:group_user)
|
2020-07-28 17:15:04 -04:00
|
|
|
fab!(:post) { Fabricate(:post, topic: topic) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.enable_category_group_moderation = true
|
|
|
|
topic.category.update!(reviewable_by_group_id: group_user.group.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does nothing for regular users" do
|
|
|
|
expect(serialized_post_for_user(nil)[:group_moderator]).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a group_moderator attribute for category group moderators" do
|
|
|
|
post.update!(user: group_user.user)
|
|
|
|
expect(serialized_post_for_user(nil)[:group_moderator]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with a post with small action" do
|
2022-01-11 01:05:16 -05:00
|
|
|
fab!(:post) { Fabricate(:small_action, action_code: "public_topic") }
|
|
|
|
|
|
|
|
it "returns `action_code` based on `login_required` site setting" do
|
|
|
|
expect(serialized_post_for_user(nil)[:action_code]).to eq("public_topic")
|
|
|
|
SiteSetting.login_required = true
|
|
|
|
expect(serialized_post_for_user(nil)[:action_code]).to eq("open_topic")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-21 09:21:07 -04:00
|
|
|
context "with allow_anonymous_likes enabled" do
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:user)
|
2023-07-21 09:21:07 -04:00
|
|
|
fab!(:topic) { Fabricate(:topic, user: user) }
|
|
|
|
fab!(:post) { Fabricate(:post, topic: topic, user: topic.user) }
|
|
|
|
fab!(:anonymous_user) { Fabricate(:anonymous) }
|
|
|
|
|
|
|
|
let(:serializer) { PostSerializer.new(post, scope: Guardian.new(anonymous_user), root: false) }
|
|
|
|
let(:post_action) do
|
|
|
|
user.id = anonymous_user.id
|
|
|
|
post.id = 1
|
|
|
|
|
|
|
|
a =
|
|
|
|
PostAction.new(
|
|
|
|
user: anonymous_user,
|
|
|
|
post: post,
|
|
|
|
post_action_type_id: PostActionType.types[:like],
|
|
|
|
)
|
|
|
|
a.created_at = 1.minute.ago
|
|
|
|
a
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.allow_anonymous_posting = true
|
|
|
|
SiteSetting.allow_anonymous_likes = true
|
|
|
|
SiteSetting.post_undo_action_window_mins = 10
|
|
|
|
PostSerializer.any_instance.stubs(:post_actions).returns({ 2 => post_action })
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when post_undo_action_window_mins has not passed" do
|
|
|
|
before { post_action.created_at = 5.minutes.ago }
|
|
|
|
|
|
|
|
it "allows anonymous users to unlike posts" do
|
|
|
|
like_actions_summary =
|
|
|
|
serializer.actions_summary.find { |a| a[:id] == PostActionType.types[:like] }
|
|
|
|
|
|
|
|
#When :can_act is present, the JavaScript allows the user to click the unlike button
|
|
|
|
expect(like_actions_summary[:can_act]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when post_undo_action_window_mins has passed" do
|
|
|
|
before { post_action.created_at = 20.minutes.ago }
|
|
|
|
|
|
|
|
it "disallows anonymous users from unliking posts" do
|
|
|
|
# There are no other post actions available to anonymous users so the action_summary will be an empty array
|
|
|
|
expect(serializer.actions_summary.find { |a| a[:id] == PostActionType.types[:like] }).to eq(
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-26 08:40:48 -05:00
|
|
|
context "with mentions" do
|
|
|
|
fab!(:user_status)
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:user1) { Fabricate(:user, user_status: user_status) }
|
|
|
|
fab!(:post) { Fabricate(:post, user: user, raw: "Hey @#{user1.username}") }
|
|
|
|
let(:serializer) { described_class.new(post, scope: Guardian.new(user), root: false) }
|
|
|
|
|
|
|
|
it "returns mentioned users with user status when user status is enabled" do
|
|
|
|
SiteSetting.enable_user_status = true
|
|
|
|
|
|
|
|
json = serializer.as_json
|
|
|
|
|
|
|
|
expect(json[:mentioned_users]).to be_present
|
|
|
|
expect(json[:mentioned_users].length).to be(1)
|
|
|
|
expect(json[:mentioned_users][0]).to_not be_nil
|
|
|
|
expect(json[:mentioned_users][0][:id]).to eq(user1.id)
|
|
|
|
expect(json[:mentioned_users][0][:username]).to eq(user1.username)
|
|
|
|
expect(json[:mentioned_users][0][:name]).to eq(user1.name)
|
|
|
|
expect(json[:mentioned_users][0][:status][:description]).to eq(user_status.description)
|
|
|
|
expect(json[:mentioned_users][0][:status][:emoji]).to eq(user_status.emoji)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't return mentioned users when user status is disabled" do
|
|
|
|
SiteSetting.enable_user_status = false
|
|
|
|
json = serializer.as_json
|
|
|
|
expect(json[:mentioned_users]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-08 09:35:26 -04:00
|
|
|
describe "#user_status" do
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:user_status)
|
2022-08-08 09:35:26 -04:00
|
|
|
fab!(:user) { Fabricate(:user, user_status: user_status) }
|
|
|
|
fab!(:post) { Fabricate(:post, user: user) }
|
|
|
|
let(:serializer) { described_class.new(post, scope: Guardian.new(user), root: false) }
|
|
|
|
|
|
|
|
it "adds user status when enabled" do
|
|
|
|
SiteSetting.enable_user_status = true
|
|
|
|
|
|
|
|
json = serializer.as_json
|
|
|
|
|
|
|
|
expect(json[:user_status]).to_not be_nil do |status|
|
|
|
|
expect(status.description).to eq(user_status.description)
|
|
|
|
expect(status.emoji).to eq(user_status.emoji)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't add user status when disabled" do
|
|
|
|
SiteSetting.enable_user_status = false
|
|
|
|
json = serializer.as_json
|
|
|
|
expect(json.keys).not_to include :user_status
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't add status if user doesn't have it" do
|
|
|
|
SiteSetting.enable_user_status = true
|
|
|
|
|
|
|
|
user.clear_status!
|
|
|
|
user.reload
|
|
|
|
json = serializer.as_json
|
|
|
|
|
|
|
|
expect(json.keys).not_to include :user_status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-10 23:04:02 -05:00
|
|
|
def serialized_post(u)
|
|
|
|
s = PostSerializer.new(post, scope: Guardian.new(u), root: false)
|
|
|
|
s.add_raw = true
|
|
|
|
s
|
|
|
|
end
|
|
|
|
|
|
|
|
def serialized_post_for_user(u)
|
|
|
|
s = serialized_post(u)
|
|
|
|
s.as_json
|
|
|
|
end
|
2013-09-03 17:19:29 -04:00
|
|
|
end
|