2020-04-09 00:52:36 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe PublishedPagesController do
|
|
|
|
fab!(:published_page) { Fabricate(:published_page) }
|
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
2022-08-19 00:11:19 +08:00
|
|
|
fab!(:user2) { Fabricate(:user) }
|
2020-04-09 00:52:36 +08:00
|
|
|
|
|
|
|
context "when enabled" do
|
|
|
|
before { SiteSetting.enable_page_publishing = true }
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when checking slug availability" do
|
2020-04-09 00:52:36 +08:00
|
|
|
it "returns true for a new slug" do
|
|
|
|
get "/pub/check-slug.json?slug=cool-slug-man"
|
|
|
|
expect(response).to be_successful
|
|
|
|
expect(response.parsed_body["valid_slug"]).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true for a new slug with whitespace" do
|
|
|
|
get "/pub/check-slug.json?slug=cool-slug-man%20"
|
|
|
|
expect(response).to be_successful
|
|
|
|
expect(response.parsed_body["valid_slug"]).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false for an empty value" do
|
|
|
|
get "/pub/check-slug.json?slug="
|
|
|
|
expect(response).to be_successful
|
|
|
|
expect(response.parsed_body["valid_slug"]).to eq(false)
|
|
|
|
expect(response.parsed_body["reason"]).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false for a reserved value" do
|
|
|
|
get "/pub/check-slug.json", params: { slug: "check-slug" }
|
|
|
|
expect(response).to be_successful
|
|
|
|
expect(response.parsed_body["valid_slug"]).to eq(false)
|
|
|
|
expect(response.parsed_body["reason"]).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "#show" do
|
2022-08-19 00:11:19 +08:00
|
|
|
it "records a view" do
|
|
|
|
sign_in(user2)
|
|
|
|
expect do get published_page.path end.to change(TopicViewItem, :count).by(1)
|
|
|
|
end
|
|
|
|
|
2020-04-09 00:52:36 +08:00
|
|
|
it "returns 404 for a missing article" do
|
|
|
|
get "/pub/no-article-here-no-thx"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with private topic" do
|
2020-04-09 00:52:36 +08:00
|
|
|
fab!(:group) { Fabricate(:group) }
|
|
|
|
fab!(:private_category) { Fabricate(:private_category, group: group) }
|
|
|
|
|
|
|
|
before { published_page.topic.update!(category: private_category) }
|
|
|
|
|
|
|
|
it "returns 403 for a topic you can't see" do
|
|
|
|
get published_page.path
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when published page is public" do
|
2020-06-18 15:02:08 +08:00
|
|
|
fab!(:public_published_page) do
|
|
|
|
Fabricate(:published_page, public: true, slug: "a-public-page")
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2020-06-17 18:42:20 +08:00
|
|
|
|
|
|
|
it "returns 200 for a topic you can't see" do
|
|
|
|
get public_published_page.path
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-09 00:52:36 +08:00
|
|
|
context "as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "returns 200" do
|
|
|
|
get published_page.path
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an error for an article you can't see" do
|
|
|
|
get "/pub/no-article-here-no-thx"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when the article is valid" do
|
2020-04-29 00:24:24 +08:00
|
|
|
before do
|
|
|
|
SiteSetting.tagging_enabled = true
|
|
|
|
published_page.topic.tags = [Fabricate(:tag, name: "recipes")]
|
|
|
|
end
|
|
|
|
|
2022-09-29 07:24:33 +08:00
|
|
|
context "when secure uploads is enabled" do
|
2020-11-06 08:33:19 +08:00
|
|
|
before do
|
|
|
|
setup_s3
|
2022-09-29 07:24:33 +08:00
|
|
|
SiteSetting.secure_uploads = true
|
2020-11-06 08:33:19 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 404" do
|
|
|
|
get published_page.path
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-29 00:24:24 +08:00
|
|
|
it "returns 200" do
|
|
|
|
get published_page.path
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
2020-09-21 11:50:39 +08:00
|
|
|
it "works even if image logos are not available" do
|
|
|
|
SiteSetting.logo_small = nil
|
|
|
|
get published_page.path
|
|
|
|
expect(response.body).to include(
|
|
|
|
"<img class=\"published-page-logo\" src=\"#{SiteSetting.logo.url}\"/>",
|
|
|
|
)
|
|
|
|
|
|
|
|
SiteSetting.logo = nil
|
|
|
|
get published_page.path
|
|
|
|
expect(response.body).not_to include("published-page-logo")
|
|
|
|
end
|
|
|
|
|
2020-04-29 00:24:24 +08:00
|
|
|
it "defines correct css classes on body" do
|
|
|
|
get published_page.path
|
2020-06-17 18:42:20 +08:00
|
|
|
expect(response.body).to include(
|
|
|
|
"<body class=\"published-page #{published_page.slug} topic-#{published_page.topic_id} recipes uncategorized\">",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when login is required" do
|
2020-06-17 18:42:20 +08:00
|
|
|
before do
|
|
|
|
SiteSetting.login_required = true
|
|
|
|
SiteSetting.show_published_pages_login_required = false
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when a user is connected" do
|
2020-06-17 18:42:20 +08:00
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
it "returns 200" do
|
|
|
|
get published_page.path
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with no user connected" do
|
2020-06-17 18:42:20 +08:00
|
|
|
it "redirects to login page" do
|
|
|
|
expect(get(published_page.path)).to redirect_to("/login")
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when login required is enabled" do
|
2020-06-17 18:42:20 +08:00
|
|
|
before { SiteSetting.show_published_pages_login_required = true }
|
|
|
|
|
|
|
|
it "returns 200" do
|
|
|
|
get published_page.path
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-29 00:24:24 +08:00
|
|
|
end
|
2020-04-09 00:52:36 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "publishing" do
|
2020-04-09 00:52:36 +08:00
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
|
|
|
|
|
|
|
it "returns invalid access for non-staff" do
|
|
|
|
sign_in(user)
|
|
|
|
put "/pub/by-topic/#{topic.id}.json", params: { published_page: { slug: "cant-do-this" } }
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a valid staff account" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "creates the published page record" do
|
|
|
|
put "/pub/by-topic/#{topic.id}.json", params: { published_page: { slug: "i-hate-salt" } }
|
|
|
|
expect(response).to be_successful
|
|
|
|
expect(response.parsed_body["published_page"]).to be_present
|
|
|
|
expect(response.parsed_body["published_page"]["slug"]).to eq("i-hate-salt")
|
2020-06-17 18:42:20 +08:00
|
|
|
expect(response.parsed_body["published_page"]["public"]).to eq(false)
|
2023-01-09 19:18:21 +08:00
|
|
|
|
2020-04-09 00:52:36 +08:00
|
|
|
expect(
|
|
|
|
PublishedPage.exists?(topic_id: response.parsed_body["published_page"]["id"]),
|
|
|
|
).to eq(true)
|
|
|
|
expect(
|
|
|
|
UserHistory.exists?(
|
|
|
|
acting_user_id: admin.id,
|
|
|
|
action: UserHistory.actions[:page_published],
|
|
|
|
topic_id: topic.id,
|
2023-01-09 19:18:21 +08:00
|
|
|
),
|
2020-04-09 00:52:36 +08:00
|
|
|
).to be(true)
|
|
|
|
end
|
|
|
|
|
2020-06-17 18:42:20 +08:00
|
|
|
it "allows to set public field" do
|
|
|
|
put "/pub/by-topic/#{topic.id}.json",
|
|
|
|
params: {
|
|
|
|
published_page: {
|
|
|
|
slug: "i-hate-salt",
|
|
|
|
public: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expect(response).to be_successful
|
|
|
|
expect(response.parsed_body["published_page"]).to be_present
|
|
|
|
expect(response.parsed_body["published_page"]["slug"]).to eq("i-hate-salt")
|
|
|
|
expect(response.parsed_body["published_page"]["public"]).to eq(true)
|
|
|
|
|
|
|
|
expect(
|
|
|
|
PublishedPage.exists?(topic_id: response.parsed_body["published_page"]["id"]),
|
|
|
|
).to eq(true)
|
|
|
|
end
|
|
|
|
|
2020-04-09 00:52:36 +08:00
|
|
|
it "returns an error if the slug is already taken" do
|
|
|
|
PublishedPage.create!(slug: "i-hate-salt", topic: Fabricate(:topic))
|
|
|
|
put "/pub/by-topic/#{topic.id}.json", params: { published_page: { slug: "i-hate-salt" } }
|
|
|
|
expect(response).not_to be_successful
|
2020-04-21 09:50:20 +08:00
|
|
|
expect(response.parsed_body["errors"]).to eq(["Slug has already been taken"])
|
2020-04-09 00:52:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an error if the topic already has been published" do
|
|
|
|
PublishedPage.create!(slug: "already-done-pal", topic: topic)
|
|
|
|
put "/pub/by-topic/#{topic.id}.json", params: { published_page: { slug: "i-hate-salt" } }
|
|
|
|
expect(response).to be_successful
|
|
|
|
expect(PublishedPage.exists?(topic_id: topic.id)).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "#destroy" do
|
2020-04-09 00:52:36 +08:00
|
|
|
it "returns invalid access for non-staff" do
|
|
|
|
sign_in(user)
|
|
|
|
delete "/pub/by-topic/#{published_page.topic_id}.json"
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a valid staff account" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "deletes the record" do
|
|
|
|
topic_id = published_page.topic_id
|
|
|
|
|
|
|
|
delete "/pub/by-topic/#{topic_id}.json"
|
|
|
|
expect(response).to be_successful
|
|
|
|
expect(PublishedPage.exists?(slug: published_page.slug)).to eq(false)
|
|
|
|
|
|
|
|
expect(
|
|
|
|
UserHistory.exists?(
|
|
|
|
acting_user_id: admin.id,
|
|
|
|
action: UserHistory.actions[:page_unpublished],
|
|
|
|
topic_id: topic_id,
|
2023-01-09 19:18:21 +08:00
|
|
|
),
|
2020-04-09 00:52:36 +08:00
|
|
|
).to be(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when disabled" do
|
|
|
|
before { SiteSetting.enable_page_publishing = false }
|
|
|
|
|
|
|
|
it "returns 404 for any article" do
|
|
|
|
get published_page.path
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|