2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe Admin::EmbeddableHostsController do
|
2022-11-03 11:42:44 +08:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
|
|
|
fab!(:moderator) { Fabricate(:moderator) }
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:embeddable_host) { Fabricate(:embeddable_host) }
|
2019-03-30 00:05:51 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
describe "#create" do
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2019-03-30 00:05:51 +08:00
|
|
|
|
|
|
|
it "logs embeddable host create" do
|
|
|
|
post "/admin/embeddable_hosts.json", params: { embeddable_host: { host: "test.com" } }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(
|
|
|
|
UserHistory.where(
|
|
|
|
acting_user_id: admin.id,
|
|
|
|
action: UserHistory.actions[:embeddable_host_create],
|
|
|
|
).exists?,
|
|
|
|
).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
shared_examples "embeddable host creation not allowed" do
|
|
|
|
it "prevents embeddable host creation with a 404 response" do
|
|
|
|
post "/admin/embeddable_hosts.json", params: { embeddable_host: { host: "test.com" } }
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "embeddable host creation not allowed"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "embeddable host creation not allowed"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#update" do
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
2019-05-01 05:09:17 +08:00
|
|
|
it "logs embeddable host update" do
|
2019-04-29 14:56:51 +08:00
|
|
|
category = Fabricate(:category)
|
2019-05-01 02:07:03 +08:00
|
|
|
|
2019-03-30 00:05:51 +08:00
|
|
|
put "/admin/embeddable_hosts/#{embeddable_host.id}.json",
|
|
|
|
params: {
|
2019-04-29 14:56:51 +08:00
|
|
|
embeddable_host: {
|
|
|
|
host: "test.com",
|
|
|
|
category_id: category.id,
|
|
|
|
},
|
2019-03-30 00:05:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2019-04-29 13:19:05 +08:00
|
|
|
|
2019-04-29 14:56:51 +08:00
|
|
|
history_exists =
|
|
|
|
UserHistory.where(
|
2019-04-29 13:19:05 +08:00
|
|
|
acting_user_id: admin.id,
|
|
|
|
action: UserHistory.actions[:embeddable_host_update],
|
2023-02-28 20:31:59 +08:00
|
|
|
new_value: "category_id: #{category.id}, host: test.com",
|
2021-04-21 17:36:32 +08:00
|
|
|
).exists?
|
2019-04-29 14:56:51 +08:00
|
|
|
|
|
|
|
expect(history_exists).to eq(true)
|
2022-11-03 11:42:44 +08:00
|
|
|
end
|
|
|
|
end
|
2019-04-29 13:19:05 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
shared_examples "embeddable host update not allowed" do
|
|
|
|
it "prevents updates with a 404 response" do
|
|
|
|
category = Fabricate(:category)
|
|
|
|
|
|
|
|
put "/admin/embeddable_hosts/#{embeddable_host.id}.json",
|
|
|
|
params: {
|
|
|
|
embeddable_host: {
|
|
|
|
host: "test.com",
|
|
|
|
category_id: category.id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
2019-03-30 00:05:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "embeddable host update not allowed"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "embeddable host update not allowed"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#destroy" do
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
2019-03-30 00:05:51 +08:00
|
|
|
it "logs embeddable host destroy" do
|
|
|
|
delete "/admin/embeddable_hosts/#{embeddable_host.id}.json", params: {}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(
|
|
|
|
UserHistory.where(
|
|
|
|
acting_user_id: admin.id,
|
|
|
|
action: UserHistory.actions[:embeddable_host_destroy],
|
|
|
|
).exists?,
|
|
|
|
).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2022-11-03 11:42:44 +08:00
|
|
|
|
|
|
|
shared_examples "embeddable host deletion not allowed" do
|
|
|
|
it "prevents deletion with a 404 response" do
|
|
|
|
delete "/admin/embeddable_hosts/#{embeddable_host.id}.json", params: {}
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "embeddable host deletion not allowed"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "embeddable host deletion not allowed"
|
|
|
|
end
|
2019-03-30 00:05:51 +08:00
|
|
|
end
|
2015-08-19 05:15:46 +08:00
|
|
|
end
|