2019-07-16 01:47:44 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe Admin::RobotsTxtController do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:admin)
|
|
|
|
fab!(:moderator)
|
|
|
|
fab!(:user)
|
2019-07-16 01:47:44 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
describe "#show" do
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "returns default content if there are no overrides" do
|
2022-10-31 20:02:26 +08:00
|
|
|
get "/admin/customize/robots.json"
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(json["robots_txt"]).to be_present
|
|
|
|
expect(json["overridden"]).to eq(false)
|
2022-10-31 20:02:26 +08:00
|
|
|
end
|
2019-07-16 01:47:44 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "returns overridden content if there are overrides" do
|
|
|
|
SiteSetting.overridden_robots_txt = "something"
|
|
|
|
get "/admin/customize/robots.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(json["robots_txt"]).to eq("something")
|
|
|
|
expect(json["overridden"]).to eq(true)
|
2022-10-31 20:02:26 +08:00
|
|
|
end
|
2022-11-03 11:42:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "robot.txt inaccessible" do
|
|
|
|
it "denies access with a 404 response" do
|
|
|
|
get "/admin/customize/robots.json"
|
2022-10-31 20:02:26 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
2022-10-31 20:02:26 +08:00
|
|
|
end
|
2019-07-16 01:47:44 +08:00
|
|
|
end
|
|
|
|
|
2022-10-31 20:02:26 +08:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "robot.txt inaccessible"
|
2019-07-16 01:47:44 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
2022-10-31 20:02:26 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "robot.txt inaccessible"
|
2019-07-16 01:47:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
describe "#update" do
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "overrides the site's default robots.txt" do
|
|
|
|
put "/admin/customize/robots.json", params: { robots_txt: "new_content" }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(json["robots_txt"]).to eq("new_content")
|
|
|
|
expect(json["overridden"]).to eq(true)
|
|
|
|
expect(SiteSetting.overridden_robots_txt).to eq("new_content")
|
|
|
|
|
|
|
|
get "/robots.txt"
|
|
|
|
expect(response.body).to include("new_content")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "requires `robots_txt` param to be present" do
|
|
|
|
SiteSetting.overridden_robots_txt = "overridden_content"
|
|
|
|
put "/admin/customize/robots.json", params: { robots_txt: "" }
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
2019-07-16 01:47:44 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
shared_examples "robot.txt update not allowed" do
|
|
|
|
it "prevents updates with a 404 response" do
|
|
|
|
put "/admin/customize/robots.json", params: { robots_txt: "adasdasd" }
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
expect(SiteSetting.overridden_robots_txt).to eq("")
|
|
|
|
end
|
2019-07-16 01:47:44 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "robot.txt update not allowed"
|
2019-07-16 01:47:44 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "robot.txt update not allowed"
|
2019-07-16 01:47:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#reset" do
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "resets robots.txt file to the default version" do
|
|
|
|
SiteSetting.overridden_robots_txt = "overridden_content"
|
|
|
|
delete "/admin/customize/robots.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(json["robots_txt"]).not_to include("overridden_content")
|
|
|
|
expect(json["overridden"]).to eq(false)
|
|
|
|
expect(SiteSetting.overridden_robots_txt).to eq("")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "robot.txt reset not allowed" do
|
|
|
|
it "prevents resets with a 404 response" do
|
|
|
|
SiteSetting.overridden_robots_txt = "overridden_content"
|
|
|
|
|
|
|
|
delete "/admin/customize/robots.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
expect(SiteSetting.overridden_robots_txt).to eq("overridden_content")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "robot.txt reset not allowed"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "robot.txt reset not allowed"
|
2019-07-16 01:47:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|