2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-24 11:01:11 +08:00
|
|
|
RSpec.describe Admin::EmojisController do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:admin)
|
|
|
|
fab!(:moderator)
|
|
|
|
fab!(:user)
|
|
|
|
fab!(:upload)
|
2017-02-02 17:41:57 +08:00
|
|
|
|
2018-06-11 12:39:31 +08:00
|
|
|
describe "#index" do
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "returns a list of custom emojis" do
|
2024-11-01 23:32:59 +08:00
|
|
|
CustomEmoji.create!(name: "osama-test-emoji", upload: upload, user: admin)
|
2022-11-03 11:42:44 +08:00
|
|
|
Emoji.clear_cache
|
|
|
|
|
|
|
|
get "/admin/customize/emojis.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(json[0]["name"]).to eq("osama-test-emoji")
|
|
|
|
expect(json[0]["url"]).to eq(upload.url)
|
2024-11-01 23:32:59 +08:00
|
|
|
expect(json[0]["created_by"]).to eq(admin.username)
|
2022-11-03 11:42:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "custom emojis inaccessible" do
|
|
|
|
it "denies access with a 404 response" do
|
|
|
|
get "/admin/customize/emojis.json"
|
2018-06-11 12:39:31 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
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 "custom emojis inaccessible"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
2018-06-11 12:39:31 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "custom emojis inaccessible"
|
2018-06-11 12:39:31 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-24 11:01:11 +08:00
|
|
|
describe "#create" do
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
context "when upload is invalid" do
|
|
|
|
it "should publish the right error" do
|
|
|
|
post "/admin/customize/emojis.json",
|
|
|
|
params: {
|
|
|
|
name: "test",
|
|
|
|
file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/fake.jpg"),
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
parsed = response.parsed_body
|
|
|
|
expect(parsed["errors"]).to eq([I18n.t("upload.images.size_not_found")])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when emoji name already exists" do
|
|
|
|
it "should publish the right error" do
|
|
|
|
CustomEmoji.create!(name: "test", upload: upload)
|
|
|
|
|
|
|
|
post "/admin/customize/emojis.json",
|
|
|
|
params: {
|
|
|
|
name: "test",
|
|
|
|
file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/logo.png"),
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
parsed = response.parsed_body
|
|
|
|
expect(parsed["errors"]).to eq(
|
|
|
|
["Name #{I18n.t("activerecord.errors.models.custom_emoji.attributes.name.taken")}"],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should allow an admin to add a custom emoji" do
|
|
|
|
Emoji.expects(:clear_cache)
|
2017-12-18 07:31:19 +08:00
|
|
|
|
|
|
|
post "/admin/customize/emojis.json",
|
|
|
|
params: {
|
|
|
|
name: "test",
|
2022-11-03 11:42:44 +08:00
|
|
|
file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/logo.png"),
|
2017-12-18 07:31:19 +08:00
|
|
|
}
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
custom_emoji = CustomEmoji.last
|
|
|
|
upload = custom_emoji.upload
|
|
|
|
|
|
|
|
expect(upload.original_filename).to eq("logo.png")
|
|
|
|
|
|
|
|
data = response.parsed_body
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(data["errors"]).to eq(nil)
|
|
|
|
expect(data["name"]).to eq(custom_emoji.name)
|
|
|
|
expect(data["url"]).to eq(upload.url)
|
|
|
|
expect(custom_emoji.group).to eq(nil)
|
2024-07-22 14:44:49 +08:00
|
|
|
expect(custom_emoji.user_id).to eq(admin.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should log the action" do
|
|
|
|
Emoji.expects(:clear_cache)
|
|
|
|
|
|
|
|
post "/admin/customize/emojis.json",
|
|
|
|
params: {
|
|
|
|
name: "test",
|
|
|
|
file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/logo.png"),
|
|
|
|
}
|
|
|
|
|
|
|
|
last_log = UserHistory.last
|
|
|
|
|
|
|
|
expect(last_log.action).to eq(UserHistory.actions[:custom_emoji_create])
|
|
|
|
expect(last_log.acting_user_id).to eq(admin.id)
|
|
|
|
expect(last_log.new_value).to eq("test")
|
2017-02-02 17:41:57 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "should allow an admin to add a custom emoji with a custom group" do
|
|
|
|
Emoji.expects(:clear_cache)
|
2017-02-02 17:41:57 +08:00
|
|
|
|
2017-12-18 07:31:19 +08:00
|
|
|
post "/admin/customize/emojis.json",
|
|
|
|
params: {
|
|
|
|
name: "test",
|
2022-11-03 11:42:44 +08:00
|
|
|
group: "Foo",
|
2017-12-18 07:31:19 +08:00
|
|
|
file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/logo.png"),
|
|
|
|
}
|
2017-02-02 17:41:57 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
custom_emoji = CustomEmoji.last
|
|
|
|
|
|
|
|
data = response.parsed_body
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(custom_emoji.group).to eq("foo")
|
2017-02-02 17:41:57 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "should fix up the emoji name" do
|
|
|
|
Emoji.expects(:clear_cache).times(3)
|
2017-02-02 17:41:57 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
post "/admin/customize/emojis.json",
|
|
|
|
params: {
|
|
|
|
name: "test.png",
|
|
|
|
file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/logo.png"),
|
|
|
|
}
|
2017-02-02 17:41:57 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
custom_emoji = CustomEmoji.last
|
|
|
|
upload = custom_emoji.upload
|
2017-02-02 17:41:57 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(upload.original_filename).to eq("logo.png")
|
|
|
|
expect(custom_emoji.name).to eq("test")
|
|
|
|
expect(response.status).to eq(200)
|
2017-12-18 07:31:19 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
post "/admin/customize/emojis.json",
|
|
|
|
params: {
|
|
|
|
name: "st&#* onk$",
|
|
|
|
file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/logo.png"),
|
|
|
|
}
|
2020-03-31 02:16:10 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
custom_emoji = CustomEmoji.last
|
|
|
|
expect(custom_emoji.name).to eq("st_onk_")
|
|
|
|
expect(response.status).to eq(200)
|
2020-03-31 02:16:10 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
post "/admin/customize/emojis.json",
|
|
|
|
params: {
|
|
|
|
name: "PaRTYpaRrot",
|
|
|
|
file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/logo.png"),
|
|
|
|
}
|
2020-03-31 02:16:10 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
custom_emoji = CustomEmoji.last
|
|
|
|
expect(custom_emoji.name).to eq("partyparrot")
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
2017-02-02 17:41:57 +08:00
|
|
|
end
|
2021-11-17 07:20:44 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
shared_examples "custom emoji creation not allowed" do
|
|
|
|
it "prevents creation with a 404 response" do
|
|
|
|
post "/admin/customize/emojis.json",
|
|
|
|
params: {
|
|
|
|
name: "test",
|
|
|
|
file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/logo.png"),
|
|
|
|
}
|
2021-11-17 07:20:44 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
|
|
|
end
|
2021-11-17 07:20:44 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
2021-11-17 07:20:44 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "custom emoji creation not allowed"
|
|
|
|
end
|
2021-11-17 07:20:44 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
2021-11-17 07:20:44 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "custom emoji creation not allowed"
|
2021-11-17 07:20:44 +08:00
|
|
|
end
|
2017-02-02 17:41:57 +08:00
|
|
|
end
|
|
|
|
|
2017-08-24 11:01:11 +08:00
|
|
|
describe "#destroy" do
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "should allow an admin to delete a custom emoji" do
|
|
|
|
custom_emoji = CustomEmoji.create!(name: "test", upload: upload)
|
|
|
|
Emoji.clear_cache
|
|
|
|
|
|
|
|
expect do
|
|
|
|
delete "/admin/customize/emojis/#{custom_emoji.name}.json", params: { name: "test" }
|
|
|
|
end.to change { CustomEmoji.count }.by(-1)
|
|
|
|
end
|
2024-07-22 14:44:49 +08:00
|
|
|
|
|
|
|
it "should log the action" do
|
|
|
|
custom_emoji = CustomEmoji.create!(name: "test", upload: upload)
|
|
|
|
Emoji.clear_cache
|
|
|
|
|
|
|
|
delete "/admin/customize/emojis/#{custom_emoji.name}.json", params: { name: "test" }
|
|
|
|
|
|
|
|
last_log = UserHistory.last
|
|
|
|
|
|
|
|
expect(last_log.action).to eq(UserHistory.actions[:custom_emoji_destroy])
|
|
|
|
expect(last_log.acting_user_id).to eq(admin.id)
|
|
|
|
expect(last_log.previous_value).to eq("test")
|
|
|
|
end
|
2022-11-03 11:42:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "custom emoji deletion not allowed" do
|
|
|
|
it "prevents deletion with a 404 response" do
|
|
|
|
custom_emoji = CustomEmoji.create!(name: "test", upload: upload)
|
|
|
|
Emoji.clear_cache
|
2017-02-02 17:41:57 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
delete "/admin/customize/emojis/#{custom_emoji.name}.json", params: { name: "test" }
|
2022-11-03 11:42:44 +08:00
|
|
|
|
|
|
|
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 "custom emoji deletion not allowed"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "custom emoji deletion not allowed"
|
2017-02-02 17:41:57 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|