2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Admin::PermalinksController do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2022-11-02 23:42:44 -04:00
|
|
|
fab!(:moderator) { Fabricate(:moderator) }
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
2018-06-11 00:37:21 -04:00
|
|
|
|
|
|
|
describe '#index' do
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it 'filters url' do
|
|
|
|
Fabricate(:permalink, url: "/forum/23")
|
|
|
|
Fabricate(:permalink, url: "/forum/98")
|
|
|
|
Fabricate(:permalink, url: "/discuss/topic/45")
|
|
|
|
Fabricate(:permalink, url: "/discuss/topic/76")
|
|
|
|
|
|
|
|
get "/admin/permalinks.json", params: { filter: "topic" }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
result = response.parsed_body
|
|
|
|
expect(result.length).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters external url' do
|
|
|
|
Fabricate(:permalink, external_url: "http://google.com")
|
|
|
|
Fabricate(:permalink, external_url: "http://wikipedia.org")
|
|
|
|
Fabricate(:permalink, external_url: "http://www.discourse.org")
|
|
|
|
Fabricate(:permalink, external_url: "http://try.discourse.org")
|
|
|
|
|
|
|
|
get "/admin/permalinks.json", params: { filter: "discourse" }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
result = response.parsed_body
|
|
|
|
expect(result.length).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters url and external url both' do
|
|
|
|
Fabricate(:permalink, url: "/forum/23", external_url: "http://google.com")
|
|
|
|
Fabricate(:permalink, url: "/discourse/98", external_url: "http://wikipedia.org")
|
|
|
|
Fabricate(:permalink, url: "/discuss/topic/45", external_url: "http://discourse.org")
|
|
|
|
Fabricate(:permalink, url: "/discuss/topic/76", external_url: "http://try.discourse.org")
|
|
|
|
|
|
|
|
get "/admin/permalinks.json", params: { filter: "discourse" }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
result = response.parsed_body
|
|
|
|
expect(result.length).to eq(3)
|
|
|
|
end
|
|
|
|
end
|
2015-07-15 08:54:28 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
shared_examples "permalinks inaccessible" do
|
|
|
|
it "denies access with a 404 response" do
|
|
|
|
get "/admin/permalinks.json", params: { filter: "topic" }
|
2015-07-15 08:54:28 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
2015-07-15 08:54:28 -04:00
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
2015-07-15 08:54:28 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
include_examples "permalinks inaccessible"
|
2015-07-15 08:54:28 -04:00
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
2015-07-15 08:54:28 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
include_examples "permalinks inaccessible"
|
2015-07-15 08:54:28 -04:00
|
|
|
end
|
|
|
|
end
|
2020-05-25 05:48:54 -04:00
|
|
|
|
|
|
|
describe "#create" do
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "works for topics" do
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
|
|
|
|
post "/admin/permalinks.json", params: {
|
|
|
|
url: "/topics/771",
|
|
|
|
permalink_type: "topic_id",
|
|
|
|
permalink_type_value: topic.id
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(Permalink.last).to have_attributes(url: "topics/771", topic_id: topic.id, post_id: nil, category_id: nil, tag_id: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works for posts" do
|
|
|
|
some_post = Fabricate(:post)
|
|
|
|
|
|
|
|
post "/admin/permalinks.json", params: {
|
|
|
|
url: "/topics/771/8291",
|
|
|
|
permalink_type: "post_id",
|
|
|
|
permalink_type_value: some_post.id
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(Permalink.last).to have_attributes(url: "topics/771/8291", topic_id: nil, post_id: some_post.id, category_id: nil, tag_id: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works for categories" do
|
|
|
|
category = Fabricate(:category)
|
|
|
|
|
|
|
|
post "/admin/permalinks.json", params: {
|
|
|
|
url: "/forums/11",
|
|
|
|
permalink_type: "category_id",
|
|
|
|
permalink_type_value: category.id
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(Permalink.last).to have_attributes(url: "forums/11", topic_id: nil, post_id: nil, category_id: category.id, tag_id: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works for tags" do
|
|
|
|
tag = Fabricate(:tag)
|
|
|
|
|
|
|
|
post "/admin/permalinks.json", params: {
|
|
|
|
url: "/forums/12",
|
|
|
|
permalink_type: "tag_name",
|
|
|
|
permalink_type_value: tag.name
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(Permalink.last).to have_attributes(url: "forums/12", topic_id: nil, post_id: nil, category_id: nil, tag_id: tag.id)
|
|
|
|
end
|
2020-05-25 05:48:54 -04:00
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
shared_examples "permalink creation not allowed" do
|
|
|
|
it "prevents creation with a 404 response" do
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
post "/admin/permalinks.json", params: {
|
|
|
|
url: "/topics/771",
|
|
|
|
permalink_type: "topic_id",
|
|
|
|
permalink_type_value: topic.id
|
|
|
|
}
|
|
|
|
end.not_to change { Permalink.count }
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
2020-05-25 05:48:54 -04:00
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
2020-05-25 05:48:54 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
include_examples "permalink creation not allowed"
|
2020-05-25 05:48:54 -04:00
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
2020-05-25 05:48:54 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
include_examples "permalink creation not allowed"
|
2020-05-25 05:48:54 -04:00
|
|
|
end
|
|
|
|
end
|
2015-07-15 08:54:28 -04:00
|
|
|
end
|