2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-10-03 19:48:03 -04:00
|
|
|
require_dependency 'post_action'
|
2013-09-03 17:19:29 -04:00
|
|
|
|
|
|
|
describe PostSerializer do
|
|
|
|
|
2013-10-03 19:48:03 -04:00
|
|
|
context "a post with lots of actions" do
|
2014-06-26 13:19:35 -04:00
|
|
|
let(:post) { Fabricate(:post) }
|
|
|
|
let(:actor) { Fabricate(:user) }
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
let(:acted_ids) {
|
2013-10-03 19:48:03 -04:00
|
|
|
PostActionType.public_types.values
|
|
|
|
.concat([:notify_user,:spam]
|
|
|
|
.map{|k| PostActionType.types[k]})
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
acted_ids.each do|id|
|
|
|
|
PostAction.act(actor, post, id)
|
|
|
|
end
|
|
|
|
post.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "displays the correct info" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(visible_actions_for(actor).sort).to eq([:like,:notify_user,:spam,:vote])
|
|
|
|
expect(visible_actions_for(post.user).sort).to eq([:like,:vote])
|
|
|
|
expect(visible_actions_for(nil).sort).to eq([:like,:vote])
|
|
|
|
expect(visible_actions_for(admin).sort).to eq([:like,:notify_user,:spam,:vote])
|
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
|
2013-10-03 19:48:03 -04:00
|
|
|
end
|
|
|
|
|
2013-09-03 17:19:29 -04:00
|
|
|
context "a post by a nuked user" do
|
|
|
|
let!(:post) { Fabricate(:post, user: Fabricate(:user), deleted_at: Time.zone.now) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
post.user_id = nil
|
|
|
|
post.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { PostSerializer.new(post, scope: Guardian.new(Fabricate(:admin)), root: false).as_json }
|
|
|
|
|
|
|
|
it "serializes correctly" do
|
2014-09-25 11:44:48 -04:00
|
|
|
[:name, :username, :display_username, :avatar_template, :user_title, :trust_level].each do |attr|
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(subject[attr]).to be_nil
|
2013-09-03 17:19:29 -04:00
|
|
|
end
|
2014-09-25 11:44:48 -04:00
|
|
|
[:moderator, :staff, :yours].each do |attr|
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(subject[attr]).to eq(false)
|
2013-09-03 17:19:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-30 15:45:13 -04:00
|
|
|
context "display_username" do
|
|
|
|
let(:user) { Fabricate.build(:user) }
|
|
|
|
let(:post) { Fabricate.build(:post, user: user) }
|
|
|
|
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
|
|
|
|
SiteSetting.stubs(:enable_names).returns(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
|
|
|
|
SiteSetting.stubs(:enable_names).returns(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
|
|
|
|
|
2014-04-15 12:48:49 -04:00
|
|
|
context "a hidden post with add_raw enabled" do
|
|
|
|
let(:user) { Fabricate.build(:user) }
|
2014-04-30 14:21:43 -04:00
|
|
|
let(:raw) { "Raw contents of the post." }
|
2014-04-15 12:48:49 -04:00
|
|
|
|
|
|
|
def serialized_post_for_user(u)
|
|
|
|
s = PostSerializer.new(post, scope: Guardian.new(u), root: false)
|
|
|
|
s.add_raw = true
|
|
|
|
s.as_json
|
|
|
|
end
|
|
|
|
|
2014-04-30 14:21:43 -04:00
|
|
|
context "a public post" do
|
|
|
|
let(:post) { Fabricate.build(: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
|
|
|
|
|
2014-04-30 14:21:43 -04:00
|
|
|
context "a hidden post" do
|
|
|
|
let(:post) { Fabricate.build(:post, raw: raw, user: user, hidden: true, hidden_reason_id: Post.hidden_reasons[:flag_threshold_reached]) }
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
context "a public wiki post" do
|
|
|
|
let(:post) { Fabricate.build(:post, raw: raw, user: user, wiki: true) }
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
context "a hidden wiki post" do
|
|
|
|
let(:post) { Fabricate.build(:post, raw: raw, user: user, wiki: true, hidden: true, hidden_reason_id: Post.hidden_reasons[:flag_threshold_reached]) }
|
|
|
|
|
|
|
|
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-04-30 14:21:43 -04:00
|
|
|
end
|
|
|
|
end
|
2014-06-26 13:19:35 -04:00
|
|
|
|
|
|
|
|
2014-04-15 12:48:49 -04:00
|
|
|
end
|
|
|
|
|
2013-09-03 17:19:29 -04:00
|
|
|
end
|