FEATURE: Allow expanding hidden posts for groups in SiteSetting.can_see_hidden_post (#21853)

Allow expanding hidden posts for groups in SiteSetting.can_see_hidden_post
This commit is contained in:
锦心
2023-06-01 11:32:05 +08:00
committed by GitHub
parent 6fec9628a4
commit 96a2893284
16 changed files with 97 additions and 18 deletions

View File

@@ -1,32 +1,59 @@
# frozen_string_literal: true
RSpec.describe PostGuardian do
fab!(:groupless_user) { Fabricate(:user) }
fab!(:user) { Fabricate(:user) }
fab!(:anon) { Fabricate(:anonymous) }
fab!(:admin) { Fabricate(:admin) }
fab!(:tl3_user) { Fabricate(:trust_level_3) }
fab!(:tl4_user) { Fabricate(:trust_level_4) }
fab!(:moderator) { Fabricate(:moderator) }
fab!(:group) { Fabricate(:group) }
fab!(:group_user) { Fabricate(:group_user, group: group, user: user) }
fab!(:category) { Fabricate(:category) }
fab!(:topic) { Fabricate(:topic, category: category) }
fab!(:hidden_post) { Fabricate(:post, topic: topic, hidden: true) }
describe "#can_see_hidden_post?" do
it "returns false for anonymous users" do
expect(Guardian.new(anon).can_see_hidden_post?(hidden_post)).to eq(false)
context "when the hidden_post_visible_groups contains everyone" do
before { SiteSetting.hidden_post_visible_groups = "#{Group::AUTO_GROUPS[:everyone]}" }
it "returns true for everyone" do
expect(Guardian.new(anon).can_see_hidden_post?(hidden_post)).to eq(true)
expect(Guardian.new(user).can_see_hidden_post?(hidden_post)).to eq(true)
expect(Guardian.new(admin).can_see_hidden_post?(hidden_post)).to eq(true)
expect(Guardian.new(moderator).can_see_hidden_post?(hidden_post)).to eq(true)
end
end
it "returns false for TL3 users" do
expect(Guardian.new(tl3_user).can_see_hidden_post?(hidden_post)).to eq(false)
context "when the post is a created by the user" do
fab!(:hidden_post) { Fabricate(:post, topic: topic, hidden: true, user: user) }
before { SiteSetting.hidden_post_visible_groups = "" }
it "returns true for the author" do
SiteSetting.hidden_post_visible_groups = ""
expect(Guardian.new(user).can_see_hidden_post?(hidden_post)).to eq(true)
end
end
it "returns true for TL4 users" do
expect(Guardian.new(tl4_user).can_see_hidden_post?(hidden_post)).to eq(true)
end
context "when the post is a created by another user" do
before { SiteSetting.hidden_post_visible_groups = "14|#{group.id}" }
it "returns true for staff users" do
expect(Guardian.new(moderator).can_see_hidden_post?(hidden_post)).to eq(true)
expect(Guardian.new(admin).can_see_hidden_post?(hidden_post)).to eq(true)
it "returns true for staff users" do
expect(Guardian.new(admin).can_see_hidden_post?(hidden_post)).to eq(true)
expect(Guardian.new(moderator).can_see_hidden_post?(hidden_post)).to eq(true)
end
it "returns false for anonymous users" do
expect(Guardian.new(anon).can_see_hidden_post?(hidden_post)).to eq(false)
end
it "returns true if the user is in hidden_post_visible_groups" do
expect(Guardian.new(user).can_see_hidden_post?(hidden_post)).to eq(true)
end
it "returns false if the user is not in hidden_post_visible_groups" do
expect(Guardian.new(groupless_user).can_see_hidden_post?(hidden_post)).to eq(false)
end
end
end
end

View File

@@ -131,6 +131,9 @@ RSpec.describe "posts" do
can_recover: {
type: :boolean,
},
can_see_hidden_post: {
type: :boolean,
},
can_wiki: {
type: :boolean,
},

View File

@@ -119,6 +119,9 @@
"can_recover": {
"type": "boolean"
},
"can_see_hidden_post": {
"type": "boolean"
},
"can_wiki": {
"type": "boolean"
},
@@ -252,6 +255,7 @@
"can_edit",
"can_delete",
"can_recover",
"can_see_hidden_post",
"can_wiki",
"user_title",
"reply_to_user",

View File

@@ -106,6 +106,9 @@
"can_recover": {
"type": "boolean"
},
"can_see_hidden_post": {
"type": "boolean"
},
"can_wiki": {
"type": "boolean"
},

View File

@@ -110,6 +110,9 @@
"can_recover": {
"type": "boolean"
},
"can_see_hidden_post": {
"type": "boolean"
},
"can_wiki": {
"type": "boolean"
},

View File

@@ -121,6 +121,9 @@
"can_recover": {
"type": "boolean"
},
"can_see_hidden_post": {
"type": "boolean"
},
"can_wiki": {
"type": "boolean"
},

View File

@@ -117,6 +117,9 @@
"can_recover": {
"type": "boolean"
},
"can_see_hidden_post": {
"type": "boolean"
},
"can_wiki": {
"type": "boolean"
},

View File

@@ -141,6 +141,13 @@ RSpec.describe PostSerializer do
)
end
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
it "shows the raw post only if authorized to see it" do
expect(serialized_post_for_user(nil)[:raw]).to eq(nil)
expect(serialized_post_for_user(Fabricate(:user))[:raw]).to eq(nil)