qb_akkoma/test/pleroma/emoji/pack_test.exs

100 lines
2.8 KiB
Elixir
Raw Normal View History

2020-08-24 06:47:25 +00:00
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
2020-08-24 06:47:25 +00:00
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Emoji.PackTest do
2022-12-16 13:20:48 +00:00
use Pleroma.DataCase, async: false
2020-08-24 06:47:25 +00:00
alias Pleroma.Emoji.Pack
2023-08-03 22:00:30 +00:00
@static_dir Pleroma.Config.get!([:instance, :static_dir])
2020-08-24 06:47:25 +00:00
@emoji_path Path.join(
Pleroma.Config.get!([:instance, :static_dir]),
"emoji"
)
setup do
pack_path = Path.join(@emoji_path, "dump_pack")
File.mkdir_p!(pack_path)
2023-08-03 22:00:30 +00:00
clear_config([:instance, :static_dir], @static_dir)
2020-08-24 06:47:25 +00:00
File.write!(Path.join(pack_path, "pack.json"), """
{
"files": { },
"pack": {
"description": "Dump pack", "homepage": "https://pleroma.social",
"license": "Test license", "share-files": true
}}
""")
{:ok, pack} = Pleroma.Emoji.Pack.load_pack("dump_pack")
on_exit(fn ->
File.rm_rf!(pack_path)
end)
{:ok, pack: pack}
end
describe "add_file/4" do
2023-08-03 22:00:30 +00:00
test "add emojis from zip file", %{pack: pack} do
2020-08-24 06:47:25 +00:00
file = %Plug.Upload{
content_type: "application/zip",
2020-09-22 18:58:30 +00:00
filename: "emojis.zip",
path: Path.absname("test/fixtures/emojis.zip")
2020-08-24 06:47:25 +00:00
}
{:ok, updated_pack} = Pack.add_file(pack, nil, nil, file)
assert updated_pack.files == %{
"a_trusted_friend-128" => "128px/a_trusted_friend-128.png",
"auroraborealis" => "auroraborealis.png",
"baby_in_a_box" => "1000px/baby_in_a_box.png",
"bear" => "1000px/bear.png",
"bear-128" => "128px/bear-128.png"
}
assert updated_pack.files_count == 5
end
end
test "returns error when zip file is bad", %{pack: pack} do
file = %Plug.Upload{
content_type: "application/zip",
2020-09-22 18:58:30 +00:00
filename: "emojis.zip",
2020-08-24 06:47:25 +00:00
path: Path.absname("test/instance_static/emoji/test_pack/blank.png")
}
assert Pack.add_file(pack, nil, nil, file) == {:error, :einval}
end
test "returns pack when zip file is empty", %{pack: pack} do
file = %Plug.Upload{
content_type: "application/zip",
2020-09-22 18:58:30 +00:00
filename: "emojis.zip",
2020-08-24 06:47:25 +00:00
path: Path.absname("test/fixtures/empty.zip")
}
{:ok, updated_pack} = Pack.add_file(pack, nil, nil, file)
assert updated_pack == pack
end
test "add emoji file", %{pack: pack} do
file = %Plug.Upload{
filename: "blank.png",
path: "#{@emoji_path}/test_pack/blank.png"
}
{:ok, updated_pack} = Pack.add_file(pack, "test_blank", "test_blank.png", file)
assert updated_pack.files == %{
"test_blank" => "test_blank.png"
}
assert updated_pack.files_count == 1
end
test "load_pack/1 ignores path traversal in a forged pack name", %{pack: pack} do
assert {:ok, ^pack} = Pack.load_pack("../../../../../dump_pack")
end
2020-08-24 06:47:25 +00:00
end