added tests

This commit is contained in:
Maksim Pechnikov 2020-08-24 09:47:25 +03:00
parent 0922791e4d
commit 14ec12ac95
6 changed files with 182 additions and 68 deletions

View File

@ -56,6 +56,9 @@ def get(name) do
end end
end end
@spec exist?(String.t()) :: boolean()
def exist?(name), do: not is_nil(get(name))
@doc "Returns all the emojos!!" @doc "Returns all the emojos!!"
@spec get_all() :: list({String.t(), String.t(), String.t()}) @spec get_all() :: list({String.t(), String.t(), String.t()})
def get_all do def get_all do

View File

@ -65,41 +65,41 @@ def delete(name) do
end end
end end
@spec unpack_zip_emojies(list(tuple())) :: list(map())
defp unpack_zip_emojies(zip_files) do
Enum.reduce(zip_files, [], fn
{_, path, s, _, _, _}, acc when elem(s, 2) == :regular ->
with(
filename <- Path.basename(path),
shortcode <- Path.basename(filename, Path.extname(filename)),
false <- Emoji.exist?(shortcode)
) do
acc ++ [%{path: path, filename: path, shortcode: shortcode}]
else
_ -> acc
end
_, acc ->
acc
end)
end
@spec add_file(String.t(), String.t(), Path.t(), Plug.Upload.t()) :: @spec add_file(String.t(), String.t(), Path.t(), Plug.Upload.t()) ::
{:ok, t()} {:ok, t()}
| {:error, File.posix() | atom()} | {:error, File.posix() | atom()}
def add_file(%Pack{} = pack, _, _, %Plug.Upload{content_type: "application/zip"} = file) do def add_file(%Pack{} = pack, _, _, %Plug.Upload{content_type: "application/zip"} = file) do
with {:ok, zip_items} <- :zip.table(to_charlist(file.path)) do with {:ok, zip_files} <- :zip.table(to_charlist(file.path)),
emojies = [_ | _] = emojies <- unpack_zip_emojies(zip_files),
for {_, path, s, _, _, _} <- zip_items, elem(s, 2) == :regular do {:ok, tmp_dir} <- Pleroma.Utils.tmp_dir("emoji") do
filename = Path.basename(path)
shortcode = Path.basename(filename, Path.extname(filename))
%{
path: path,
filename: path,
shortcode: shortcode,
exist: not is_nil(Pleroma.Emoji.get(shortcode))
}
end
|> Enum.group_by(& &1[:exist])
case Map.get(emojies, false, []) do
[_ | _] = new_emojies ->
{:ok, tmp_dir} = Pleroma.Utils.tmp_dir("emoji")
try do try do
{:ok, _emoji_files} = {:ok, _emoji_files} =
:zip.unzip( :zip.unzip(
to_charlist(file.path), to_charlist(file.path),
[ [{:file_list, Enum.map(emojies, & &1[:path])}, {:cwd, tmp_dir}]
{:file_list, Enum.map(new_emojies, & &1[:path])},
{:cwd, tmp_dir}
]
) )
{_, updated_pack} = {_, updated_pack} =
Enum.map_reduce(new_emojies, pack, fn item, emoji_pack -> Enum.map_reduce(emojies, pack, fn item, emoji_pack ->
emoji_file = %Plug.Upload{ emoji_file = %Plug.Upload{
filename: item[:filename], filename: item[:filename],
path: Path.join(tmp_dir, item[:path]) path: Path.join(tmp_dir, item[:path])
@ -122,14 +122,16 @@ def add_file(%Pack{} = pack, _, _, %Plug.Upload{content_type: "application/zip"}
after after
File.rm_rf(tmp_dir) File.rm_rf(tmp_dir)
end end
else
{:error, _} = error ->
error
_ -> _ ->
{:ok, pack} {:ok, pack}
end end
end end
end
def add_file(%Pack{} = pack, shortcode, filename, file) do def add_file(%Pack{} = pack, shortcode, filename, %Plug.Upload{} = file) do
with :ok <- validate_not_empty([shortcode, filename]), with :ok <- validate_not_empty([shortcode, filename]),
:ok <- validate_emoji_not_exists(shortcode), :ok <- validate_emoji_not_exists(shortcode),
{:ok, updated_pack} <- do_add_file(pack, shortcode, filename, file) do {:ok, updated_pack} <- do_add_file(pack, shortcode, filename, file) do
@ -139,12 +141,10 @@ def add_file(%Pack{} = pack, shortcode, filename, file) do
end end
defp do_add_file(pack, shortcode, filename, file) do defp do_add_file(pack, shortcode, filename, file) do
with :ok <- save_file(file, pack, filename), with :ok <- save_file(file, pack, filename) do
{:ok, updated_pack} <-
pack pack
|> put_emoji(shortcode, filename) |> put_emoji(shortcode, filename)
|> save_pack() do |> save_pack()
{:ok, updated_pack}
end end
end end
@ -312,9 +312,10 @@ defp validate_emoji_not_exists(shortcode, force \\ false)
defp validate_emoji_not_exists(_shortcode, true), do: :ok defp validate_emoji_not_exists(_shortcode, true), do: :ok
defp validate_emoji_not_exists(shortcode, _) do defp validate_emoji_not_exists(shortcode, _) do
case Emoji.get(shortcode) do if Emoji.exist?(shortcode) do
nil -> :ok {:error, :already_exists}
_ -> {:error, :already_exists} else
:ok
end end
end end
@ -466,7 +467,7 @@ defp save_file(%Plug.Upload{path: upload_path}, pack, filename) do
defp put_emoji(pack, shortcode, filename) do defp put_emoji(pack, shortcode, filename) do
files = Map.put(pack.files, shortcode, filename) files = Map.put(pack.files, shortcode, filename)
%{pack | files: files} %{pack | files: files, files_count: length(Map.keys(files))}
end end
defp delete_emoji(pack, shortcode) do defp delete_emoji(pack, shortcode) do

View File

@ -28,14 +28,16 @@ def command_available?(command) do
@doc "creates the uniq temporary directory" @doc "creates the uniq temporary directory"
@spec tmp_dir(String.t()) :: {:ok, String.t()} | {:error, :file.posix()} @spec tmp_dir(String.t()) :: {:ok, String.t()} | {:error, :file.posix()}
def tmp_dir(prefix \\ "") do def tmp_dir(prefix \\ "") do
sub_dir = [ sub_dir =
[
prefix, prefix,
Timex.to_unix(Timex.now()), Timex.to_unix(Timex.now()),
:os.getpid(), :os.getpid(),
String.downcase(Integer.to_string(:rand.uniform(0x100000000), 36)) String.downcase(Integer.to_string(:rand.uniform(0x100000000), 36))
] ]
|> Enum.join("-")
tmp_dir = Path.join(System.tmp_dir!(), Enum.join(sub_dir, "-")) tmp_dir = Path.join(System.tmp_dir!(), sub_dir)
case File.mkdir(tmp_dir) do case File.mkdir(tmp_dir) do
:ok -> {:ok, tmp_dir} :ok -> {:ok, tmp_dir}

93
test/emoji/pack_test.exs Normal file
View File

@ -0,0 +1,93 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Emoji.PackTest do
use ExUnit.Case, async: true
alias Pleroma.Emoji.Pack
@emoji_path Path.join(
Pleroma.Config.get!([:instance, :static_dir]),
"emoji"
)
setup do
pack_path = Path.join(@emoji_path, "dump_pack")
File.mkdir(pack_path)
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
test "add emojies from zip file", %{pack: pack} do
file = %Plug.Upload{
content_type: "application/zip",
filename: "finland-emojis.zip",
path: Path.absname("test/fixtures/finland-emojis.zip")
}
{: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",
filename: "finland-emojis.zip",
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",
filename: "finland-emojis.zip",
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
end

BIN
test/fixtures/empty.zip vendored Normal file

Binary file not shown.

15
test/utils_test.exs Normal file
View File

@ -0,0 +1,15 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.UtilsTest do
use ExUnit.Case, async: true
describe "tmp_dir/1" do
test "returns unique temporary directory" do
{:ok, path} = Pleroma.Utils.tmp_dir("emoji")
assert path =~ ~r/\/tmp\/emoji-(.*)-#{:os.getpid()}-(.*)/
File.rm_rf(path)
end
end
end