logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

emoji_api_controller_test.exs (25337B)


      1 # Pleroma: A lightweight social networking server
      2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
      3 # SPDX-License-Identifier: AGPL-3.0-only
      4 
      5 defmodule Pleroma.Web.PleromaAPI.EmojiAPIControllerTest do
      6   use Pleroma.Web.ConnCase
      7 
      8   import Tesla.Mock
      9   import Pleroma.Factory
     10 
     11   @emoji_path Path.join(
     12                 Pleroma.Config.get!([:instance, :static_dir]),
     13                 "emoji"
     14               )
     15   setup do: clear_config([:auth, :enforce_oauth_admin_scope_usage], false)
     16 
     17   setup do
     18     admin = insert(:user, is_admin: true)
     19     token = insert(:oauth_admin_token, user: admin)
     20 
     21     admin_conn =
     22       build_conn()
     23       |> assign(:user, admin)
     24       |> assign(:token, token)
     25 
     26     Pleroma.Emoji.reload()
     27     {:ok, %{admin_conn: admin_conn}}
     28   end
     29 
     30   test "GET /api/pleroma/emoji/packs", %{conn: conn} do
     31     resp = conn |> get("/api/pleroma/emoji/packs") |> json_response(200)
     32 
     33     shared = resp["test_pack"]
     34     assert shared["files"] == %{"blank" => "blank.png"}
     35     assert Map.has_key?(shared["pack"], "download-sha256")
     36     assert shared["pack"]["can-download"]
     37     assert shared["pack"]["share-files"]
     38 
     39     non_shared = resp["test_pack_nonshared"]
     40     assert non_shared["pack"]["share-files"] == false
     41     assert non_shared["pack"]["can-download"] == false
     42   end
     43 
     44   describe "GET /api/pleroma/emoji/packs/remote" do
     45     test "shareable instance", %{admin_conn: admin_conn, conn: conn} do
     46       resp =
     47         conn
     48         |> get("/api/pleroma/emoji/packs")
     49         |> json_response(200)
     50 
     51       mock(fn
     52         %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
     53           json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
     54 
     55         %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
     56           json(%{metadata: %{features: ["shareable_emoji_packs"]}})
     57 
     58         %{method: :get, url: "https://example.com/api/pleroma/emoji/packs"} ->
     59           json(resp)
     60       end)
     61 
     62       assert admin_conn
     63              |> get("/api/pleroma/emoji/packs/remote", %{
     64                url: "https://example.com"
     65              })
     66              |> json_response(200) == resp
     67     end
     68 
     69     test "non shareable instance", %{admin_conn: admin_conn} do
     70       mock(fn
     71         %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
     72           json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
     73 
     74         %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
     75           json(%{metadata: %{features: []}})
     76       end)
     77 
     78       assert admin_conn
     79              |> get("/api/pleroma/emoji/packs/remote", %{url: "https://example.com"})
     80              |> json_response(500) == %{
     81                "error" => "The requested instance does not support sharing emoji packs"
     82              }
     83     end
     84   end
     85 
     86   describe "GET /api/pleroma/emoji/packs/:name/archive" do
     87     test "download shared pack", %{conn: conn} do
     88       resp =
     89         conn
     90         |> get("/api/pleroma/emoji/packs/test_pack/archive")
     91         |> response(200)
     92 
     93       {:ok, arch} = :zip.unzip(resp, [:memory])
     94 
     95       assert Enum.find(arch, fn {n, _} -> n == 'pack.json' end)
     96       assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end)
     97     end
     98 
     99     test "non existing pack", %{conn: conn} do
    100       assert conn
    101              |> get("/api/pleroma/emoji/packs/test_pack_for_import/archive")
    102              |> json_response(:not_found) == %{
    103                "error" => "Pack test_pack_for_import does not exist"
    104              }
    105     end
    106 
    107     test "non downloadable pack", %{conn: conn} do
    108       assert conn
    109              |> get("/api/pleroma/emoji/packs/test_pack_nonshared/archive")
    110              |> json_response(:forbidden) == %{
    111                "error" =>
    112                  "Pack test_pack_nonshared cannot be downloaded from this instance, either pack sharing was disabled for this pack or some files are missing"
    113              }
    114     end
    115   end
    116 
    117   describe "POST /api/pleroma/emoji/packs/download" do
    118     test "shared pack from remote and non shared from fallback-src", %{
    119       admin_conn: admin_conn,
    120       conn: conn
    121     } do
    122       mock(fn
    123         %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
    124           json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
    125 
    126         %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
    127           json(%{metadata: %{features: ["shareable_emoji_packs"]}})
    128 
    129         %{
    130           method: :get,
    131           url: "https://example.com/api/pleroma/emoji/packs/test_pack"
    132         } ->
    133           conn
    134           |> get("/api/pleroma/emoji/packs/test_pack")
    135           |> json_response(200)
    136           |> json()
    137 
    138         %{
    139           method: :get,
    140           url: "https://example.com/api/pleroma/emoji/packs/test_pack/archive"
    141         } ->
    142           conn
    143           |> get("/api/pleroma/emoji/packs/test_pack/archive")
    144           |> response(200)
    145           |> text()
    146 
    147         %{
    148           method: :get,
    149           url: "https://example.com/api/pleroma/emoji/packs/test_pack_nonshared"
    150         } ->
    151           conn
    152           |> get("/api/pleroma/emoji/packs/test_pack_nonshared")
    153           |> json_response(200)
    154           |> json()
    155 
    156         %{
    157           method: :get,
    158           url: "https://nonshared-pack"
    159         } ->
    160           text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
    161       end)
    162 
    163       assert admin_conn
    164              |> post("/api/pleroma/emoji/packs/download", %{
    165                url: "https://example.com",
    166                name: "test_pack",
    167                as: "test_pack2"
    168              })
    169              |> json_response(200) == "ok"
    170 
    171       assert File.exists?("#{@emoji_path}/test_pack2/pack.json")
    172       assert File.exists?("#{@emoji_path}/test_pack2/blank.png")
    173 
    174       assert admin_conn
    175              |> delete("/api/pleroma/emoji/packs/test_pack2")
    176              |> json_response(200) == "ok"
    177 
    178       refute File.exists?("#{@emoji_path}/test_pack2")
    179 
    180       assert admin_conn
    181              |> post(
    182                "/api/pleroma/emoji/packs/download",
    183                %{
    184                  url: "https://example.com",
    185                  name: "test_pack_nonshared",
    186                  as: "test_pack_nonshared2"
    187                }
    188              )
    189              |> json_response(200) == "ok"
    190 
    191       assert File.exists?("#{@emoji_path}/test_pack_nonshared2/pack.json")
    192       assert File.exists?("#{@emoji_path}/test_pack_nonshared2/blank.png")
    193 
    194       assert admin_conn
    195              |> delete("/api/pleroma/emoji/packs/test_pack_nonshared2")
    196              |> json_response(200) == "ok"
    197 
    198       refute File.exists?("#{@emoji_path}/test_pack_nonshared2")
    199     end
    200 
    201     test "nonshareable instance", %{admin_conn: admin_conn} do
    202       mock(fn
    203         %{method: :get, url: "https://old-instance/.well-known/nodeinfo"} ->
    204           json(%{links: [%{href: "https://old-instance/nodeinfo/2.1.json"}]})
    205 
    206         %{method: :get, url: "https://old-instance/nodeinfo/2.1.json"} ->
    207           json(%{metadata: %{features: []}})
    208       end)
    209 
    210       assert admin_conn
    211              |> post(
    212                "/api/pleroma/emoji/packs/download",
    213                %{
    214                  url: "https://old-instance",
    215                  name: "test_pack",
    216                  as: "test_pack2"
    217                }
    218              )
    219              |> json_response(500) == %{
    220                "error" => "The requested instance does not support sharing emoji packs"
    221              }
    222     end
    223 
    224     test "checksum fail", %{admin_conn: admin_conn} do
    225       mock(fn
    226         %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
    227           json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
    228 
    229         %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
    230           json(%{metadata: %{features: ["shareable_emoji_packs"]}})
    231 
    232         %{
    233           method: :get,
    234           url: "https://example.com/api/pleroma/emoji/packs/pack_bad_sha"
    235         } ->
    236           %Tesla.Env{
    237             status: 200,
    238             body: Pleroma.Emoji.Pack.load_pack("pack_bad_sha") |> Jason.encode!()
    239           }
    240 
    241         %{
    242           method: :get,
    243           url: "https://example.com/api/pleroma/emoji/packs/pack_bad_sha/archive"
    244         } ->
    245           %Tesla.Env{
    246             status: 200,
    247             body: File.read!("test/instance_static/emoji/pack_bad_sha/pack_bad_sha.zip")
    248           }
    249       end)
    250 
    251       assert admin_conn
    252              |> post("/api/pleroma/emoji/packs/download", %{
    253                url: "https://example.com",
    254                name: "pack_bad_sha",
    255                as: "pack_bad_sha2"
    256              })
    257              |> json_response(:internal_server_error) == %{
    258                "error" => "SHA256 for the pack doesn't match the one sent by the server"
    259              }
    260     end
    261 
    262     test "other error", %{admin_conn: admin_conn} do
    263       mock(fn
    264         %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
    265           json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
    266 
    267         %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
    268           json(%{metadata: %{features: ["shareable_emoji_packs"]}})
    269 
    270         %{
    271           method: :get,
    272           url: "https://example.com/api/pleroma/emoji/packs/test_pack"
    273         } ->
    274           %Tesla.Env{
    275             status: 200,
    276             body: Pleroma.Emoji.Pack.load_pack("test_pack") |> Jason.encode!()
    277           }
    278       end)
    279 
    280       assert admin_conn
    281              |> post("/api/pleroma/emoji/packs/download", %{
    282                url: "https://example.com",
    283                name: "test_pack",
    284                as: "test_pack2"
    285              })
    286              |> json_response(:internal_server_error) == %{
    287                "error" =>
    288                  "The pack was not set as shared and there is no fallback src to download from"
    289              }
    290     end
    291   end
    292 
    293   describe "PATCH /api/pleroma/emoji/packs/:name" do
    294     setup do
    295       pack_file = "#{@emoji_path}/test_pack/pack.json"
    296       original_content = File.read!(pack_file)
    297 
    298       on_exit(fn ->
    299         File.write!(pack_file, original_content)
    300       end)
    301 
    302       {:ok,
    303        pack_file: pack_file,
    304        new_data: %{
    305          "license" => "Test license changed",
    306          "homepage" => "https://pleroma.social",
    307          "description" => "Test description",
    308          "share-files" => false
    309        }}
    310     end
    311 
    312     test "for a pack without a fallback source", ctx do
    313       assert ctx[:admin_conn]
    314              |> patch("/api/pleroma/emoji/packs/test_pack", %{"metadata" => ctx[:new_data]})
    315              |> json_response(200) == ctx[:new_data]
    316 
    317       assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
    318     end
    319 
    320     test "for a pack with a fallback source", ctx do
    321       mock(fn
    322         %{
    323           method: :get,
    324           url: "https://nonshared-pack"
    325         } ->
    326           text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
    327       end)
    328 
    329       new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
    330 
    331       new_data_with_sha =
    332         Map.put(
    333           new_data,
    334           "fallback-src-sha256",
    335           "74409E2674DAA06C072729C6C8426C4CB3B7E0B85ED77792DB7A436E11D76DAF"
    336         )
    337 
    338       assert ctx[:admin_conn]
    339              |> patch("/api/pleroma/emoji/packs/test_pack", %{metadata: new_data})
    340              |> json_response(200) == new_data_with_sha
    341 
    342       assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
    343     end
    344 
    345     test "when the fallback source doesn't have all the files", ctx do
    346       mock(fn
    347         %{
    348           method: :get,
    349           url: "https://nonshared-pack"
    350         } ->
    351           {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
    352           text(empty_arch)
    353       end)
    354 
    355       new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
    356 
    357       assert ctx[:admin_conn]
    358              |> patch("/api/pleroma/emoji/packs/test_pack", %{metadata: new_data})
    359              |> json_response(:bad_request) == %{
    360                "error" => "The fallback archive does not have all files specified in pack.json"
    361              }
    362     end
    363   end
    364 
    365   describe "POST/PATCH/DELETE /api/pleroma/emoji/packs/:name/files" do
    366     setup do
    367       pack_file = "#{@emoji_path}/test_pack/pack.json"
    368       original_content = File.read!(pack_file)
    369 
    370       on_exit(fn ->
    371         File.write!(pack_file, original_content)
    372       end)
    373 
    374       :ok
    375     end
    376 
    377     test "create shortcode exists", %{admin_conn: admin_conn} do
    378       assert admin_conn
    379              |> post("/api/pleroma/emoji/packs/test_pack/files", %{
    380                shortcode: "blank",
    381                filename: "dir/blank.png",
    382                file: %Plug.Upload{
    383                  filename: "blank.png",
    384                  path: "#{@emoji_path}/test_pack/blank.png"
    385                }
    386              })
    387              |> json_response(:conflict) == %{
    388                "error" => "An emoji with the \"blank\" shortcode already exists"
    389              }
    390     end
    391 
    392     test "don't rewrite old emoji", %{admin_conn: admin_conn} do
    393       on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir/") end)
    394 
    395       assert admin_conn
    396              |> post("/api/pleroma/emoji/packs/test_pack/files", %{
    397                shortcode: "blank2",
    398                filename: "dir/blank.png",
    399                file: %Plug.Upload{
    400                  filename: "blank.png",
    401                  path: "#{@emoji_path}/test_pack/blank.png"
    402                }
    403              })
    404              |> json_response(200) == %{"blank" => "blank.png", "blank2" => "dir/blank.png"}
    405 
    406       assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
    407 
    408       assert admin_conn
    409              |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
    410                shortcode: "blank",
    411                new_shortcode: "blank2",
    412                new_filename: "dir_2/blank_3.png"
    413              })
    414              |> json_response(:conflict) == %{
    415                "error" =>
    416                  "New shortcode \"blank2\" is already used. If you want to override emoji use 'force' option"
    417              }
    418     end
    419 
    420     test "rewrite old emoji with force option", %{admin_conn: admin_conn} do
    421       on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir_2/") end)
    422 
    423       assert admin_conn
    424              |> post("/api/pleroma/emoji/packs/test_pack/files", %{
    425                shortcode: "blank2",
    426                filename: "dir/blank.png",
    427                file: %Plug.Upload{
    428                  filename: "blank.png",
    429                  path: "#{@emoji_path}/test_pack/blank.png"
    430                }
    431              })
    432              |> json_response(200) == %{"blank" => "blank.png", "blank2" => "dir/blank.png"}
    433 
    434       assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
    435 
    436       assert admin_conn
    437              |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
    438                shortcode: "blank2",
    439                new_shortcode: "blank3",
    440                new_filename: "dir_2/blank_3.png",
    441                force: true
    442              })
    443              |> json_response(200) == %{
    444                "blank" => "blank.png",
    445                "blank3" => "dir_2/blank_3.png"
    446              }
    447 
    448       assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
    449     end
    450 
    451     test "with empty filename", %{admin_conn: admin_conn} do
    452       assert admin_conn
    453              |> post("/api/pleroma/emoji/packs/test_pack/files", %{
    454                shortcode: "blank2",
    455                filename: "",
    456                file: %Plug.Upload{
    457                  filename: "blank.png",
    458                  path: "#{@emoji_path}/test_pack/blank.png"
    459                }
    460              })
    461              |> json_response(:bad_request) == %{
    462                "error" => "pack name, shortcode or filename cannot be empty"
    463              }
    464     end
    465 
    466     test "add file with not loaded pack", %{admin_conn: admin_conn} do
    467       assert admin_conn
    468              |> post("/api/pleroma/emoji/packs/not_loaded/files", %{
    469                shortcode: "blank2",
    470                filename: "dir/blank.png",
    471                file: %Plug.Upload{
    472                  filename: "blank.png",
    473                  path: "#{@emoji_path}/test_pack/blank.png"
    474                }
    475              })
    476              |> json_response(:bad_request) == %{
    477                "error" => "pack \"not_loaded\" is not found"
    478              }
    479     end
    480 
    481     test "remove file with not loaded pack", %{admin_conn: admin_conn} do
    482       assert admin_conn
    483              |> delete("/api/pleroma/emoji/packs/not_loaded/files", %{shortcode: "blank3"})
    484              |> json_response(:bad_request) == %{"error" => "pack \"not_loaded\" is not found"}
    485     end
    486 
    487     test "remove file with empty shortcode", %{admin_conn: admin_conn} do
    488       assert admin_conn
    489              |> delete("/api/pleroma/emoji/packs/not_loaded/files", %{shortcode: ""})
    490              |> json_response(:bad_request) == %{
    491                "error" => "pack name or shortcode cannot be empty"
    492              }
    493     end
    494 
    495     test "update file with not loaded pack", %{admin_conn: admin_conn} do
    496       assert admin_conn
    497              |> patch("/api/pleroma/emoji/packs/not_loaded/files", %{
    498                shortcode: "blank4",
    499                new_shortcode: "blank3",
    500                new_filename: "dir_2/blank_3.png"
    501              })
    502              |> json_response(:bad_request) == %{"error" => "pack \"not_loaded\" is not found"}
    503     end
    504 
    505     test "new with shortcode as file with update", %{admin_conn: admin_conn} do
    506       assert admin_conn
    507              |> post("/api/pleroma/emoji/packs/test_pack/files", %{
    508                shortcode: "blank4",
    509                filename: "dir/blank.png",
    510                file: %Plug.Upload{
    511                  filename: "blank.png",
    512                  path: "#{@emoji_path}/test_pack/blank.png"
    513                }
    514              })
    515              |> json_response(200) == %{"blank" => "blank.png", "blank4" => "dir/blank.png"}
    516 
    517       assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
    518 
    519       assert admin_conn
    520              |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
    521                shortcode: "blank4",
    522                new_shortcode: "blank3",
    523                new_filename: "dir_2/blank_3.png"
    524              })
    525              |> json_response(200) == %{"blank3" => "dir_2/blank_3.png", "blank" => "blank.png"}
    526 
    527       refute File.exists?("#{@emoji_path}/test_pack/dir/")
    528       assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
    529 
    530       assert admin_conn
    531              |> delete("/api/pleroma/emoji/packs/test_pack/files", %{shortcode: "blank3"})
    532              |> json_response(200) == %{"blank" => "blank.png"}
    533 
    534       refute File.exists?("#{@emoji_path}/test_pack/dir_2/")
    535 
    536       on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir") end)
    537     end
    538 
    539     test "new with shortcode from url", %{admin_conn: admin_conn} do
    540       mock(fn
    541         %{
    542           method: :get,
    543           url: "https://test-blank/blank_url.png"
    544         } ->
    545           text(File.read!("#{@emoji_path}/test_pack/blank.png"))
    546       end)
    547 
    548       assert admin_conn
    549              |> post("/api/pleroma/emoji/packs/test_pack/files", %{
    550                shortcode: "blank_url",
    551                file: "https://test-blank/blank_url.png"
    552              })
    553              |> json_response(200) == %{
    554                "blank_url" => "blank_url.png",
    555                "blank" => "blank.png"
    556              }
    557 
    558       assert File.exists?("#{@emoji_path}/test_pack/blank_url.png")
    559 
    560       on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/blank_url.png") end)
    561     end
    562 
    563     test "new without shortcode", %{admin_conn: admin_conn} do
    564       on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/shortcode.png") end)
    565 
    566       assert admin_conn
    567              |> post("/api/pleroma/emoji/packs/test_pack/files", %{
    568                file: %Plug.Upload{
    569                  filename: "shortcode.png",
    570                  path: "#{Pleroma.Config.get([:instance, :static_dir])}/add/shortcode.png"
    571                }
    572              })
    573              |> json_response(200) == %{"shortcode" => "shortcode.png", "blank" => "blank.png"}
    574     end
    575 
    576     test "remove non existing shortcode in pack.json", %{admin_conn: admin_conn} do
    577       assert admin_conn
    578              |> delete("/api/pleroma/emoji/packs/test_pack/files", %{shortcode: "blank2"})
    579              |> json_response(:bad_request) == %{"error" => "Emoji \"blank2\" does not exist"}
    580     end
    581 
    582     test "update non existing emoji", %{admin_conn: admin_conn} do
    583       assert admin_conn
    584              |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
    585                shortcode: "blank2",
    586                new_shortcode: "blank3",
    587                new_filename: "dir_2/blank_3.png"
    588              })
    589              |> json_response(:bad_request) == %{"error" => "Emoji \"blank2\" does not exist"}
    590     end
    591 
    592     test "update with empty shortcode", %{admin_conn: admin_conn} do
    593       assert admin_conn
    594              |> patch("/api/pleroma/emoji/packs/test_pack/files", %{
    595                shortcode: "blank",
    596                new_filename: "dir_2/blank_3.png"
    597              })
    598              |> json_response(:bad_request) == %{
    599                "error" => "new_shortcode or new_filename cannot be empty"
    600              }
    601     end
    602   end
    603 
    604   describe "POST/DELETE /api/pleroma/emoji/packs/:name" do
    605     test "creating and deleting a pack", %{admin_conn: admin_conn} do
    606       assert admin_conn
    607              |> post("/api/pleroma/emoji/packs/test_created")
    608              |> json_response(200) == "ok"
    609 
    610       assert File.exists?("#{@emoji_path}/test_created/pack.json")
    611 
    612       assert Jason.decode!(File.read!("#{@emoji_path}/test_created/pack.json")) == %{
    613                "pack" => %{},
    614                "files" => %{}
    615              }
    616 
    617       assert admin_conn
    618              |> delete("/api/pleroma/emoji/packs/test_created")
    619              |> json_response(200) == "ok"
    620 
    621       refute File.exists?("#{@emoji_path}/test_created/pack.json")
    622     end
    623 
    624     test "if pack exists", %{admin_conn: admin_conn} do
    625       path = Path.join(@emoji_path, "test_created")
    626       File.mkdir(path)
    627       pack_file = Jason.encode!(%{files: %{}, pack: %{}})
    628       File.write!(Path.join(path, "pack.json"), pack_file)
    629 
    630       assert admin_conn
    631              |> post("/api/pleroma/emoji/packs/test_created")
    632              |> json_response(:conflict) == %{
    633                "error" => "A pack named \"test_created\" already exists"
    634              }
    635 
    636       on_exit(fn -> File.rm_rf(path) end)
    637     end
    638 
    639     test "with empty name", %{admin_conn: admin_conn} do
    640       assert admin_conn
    641              |> post("/api/pleroma/emoji/packs/ ")
    642              |> json_response(:bad_request) == %{"error" => "pack name cannot be empty"}
    643     end
    644   end
    645 
    646   test "deleting nonexisting pack", %{admin_conn: admin_conn} do
    647     assert admin_conn
    648            |> delete("/api/pleroma/emoji/packs/non_existing")
    649            |> json_response(:not_found) == %{"error" => "Pack non_existing does not exist"}
    650   end
    651 
    652   test "deleting with empty name", %{admin_conn: admin_conn} do
    653     assert admin_conn
    654            |> delete("/api/pleroma/emoji/packs/ ")
    655            |> json_response(:bad_request) == %{"error" => "pack name cannot be empty"}
    656   end
    657 
    658   test "filesystem import", %{admin_conn: admin_conn, conn: conn} do
    659     on_exit(fn ->
    660       File.rm!("#{@emoji_path}/test_pack_for_import/emoji.txt")
    661       File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
    662     end)
    663 
    664     resp = conn |> get("/api/pleroma/emoji/packs") |> json_response(200)
    665 
    666     refute Map.has_key?(resp, "test_pack_for_import")
    667 
    668     assert admin_conn
    669            |> get("/api/pleroma/emoji/packs/import")
    670            |> json_response(200) == ["test_pack_for_import"]
    671 
    672     resp = conn |> get("/api/pleroma/emoji/packs") |> json_response(200)
    673     assert resp["test_pack_for_import"]["files"] == %{"blank" => "blank.png"}
    674 
    675     File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
    676     refute File.exists?("#{@emoji_path}/test_pack_for_import/pack.json")
    677 
    678     emoji_txt_content = """
    679     blank, blank.png, Fun
    680     blank2, blank.png
    681     foo, /emoji/test_pack_for_import/blank.png
    682     bar
    683     """
    684 
    685     File.write!("#{@emoji_path}/test_pack_for_import/emoji.txt", emoji_txt_content)
    686 
    687     assert admin_conn
    688            |> get("/api/pleroma/emoji/packs/import")
    689            |> json_response(200) == ["test_pack_for_import"]
    690 
    691     resp = conn |> get("/api/pleroma/emoji/packs") |> json_response(200)
    692 
    693     assert resp["test_pack_for_import"]["files"] == %{
    694              "blank" => "blank.png",
    695              "blank2" => "blank.png",
    696              "foo" => "blank.png"
    697            }
    698   end
    699 
    700   describe "GET /api/pleroma/emoji/packs/:name" do
    701     test "shows pack.json", %{conn: conn} do
    702       assert %{
    703                "files" => %{"blank" => "blank.png"},
    704                "pack" => %{
    705                  "can-download" => true,
    706                  "description" => "Test description",
    707                  "download-sha256" => _,
    708                  "homepage" => "https://pleroma.social",
    709                  "license" => "Test license",
    710                  "share-files" => true
    711                }
    712              } =
    713                conn
    714                |> get("/api/pleroma/emoji/packs/test_pack")
    715                |> json_response(200)
    716     end
    717 
    718     test "non existing pack", %{conn: conn} do
    719       assert conn
    720              |> get("/api/pleroma/emoji/packs/non_existing")
    721              |> json_response(:not_found) == %{"error" => "Pack non_existing does not exist"}
    722     end
    723 
    724     test "error name", %{conn: conn} do
    725       assert conn
    726              |> get("/api/pleroma/emoji/packs/ ")
    727              |> json_response(:bad_request) == %{"error" => "pack name cannot be empty"}
    728     end
    729   end
    730 end