logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

emoji_pack_controller_test.exs (24815B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
  5. use Pleroma.Web.ConnCase, async: false
  6. import Mock
  7. import Tesla.Mock
  8. import Pleroma.Factory
  9. @emoji_path Path.join(
  10. Pleroma.Config.get!([:instance, :static_dir]),
  11. "emoji"
  12. )
  13. setup do: clear_config([:instance, :public], true)
  14. setup do
  15. admin = insert(:user, is_admin: true)
  16. token = insert(:oauth_admin_token, user: admin)
  17. admin_conn =
  18. build_conn()
  19. |> assign(:user, admin)
  20. |> assign(:token, token)
  21. Pleroma.Emoji.reload()
  22. {:ok, %{admin_conn: admin_conn}}
  23. end
  24. test "GET /api/pleroma/emoji/packs when :public: false", %{conn: conn} do
  25. clear_config([:instance, :public], false)
  26. conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
  27. end
  28. test "GET /api/pleroma/emoji/packs", %{conn: conn} do
  29. resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
  30. assert resp["count"] == 4
  31. assert resp["packs"]
  32. |> Map.keys()
  33. |> length() == 4
  34. shared = resp["packs"]["test_pack"]
  35. assert shared["files"] == %{"blank" => "blank.png", "blank2" => "blank2.png"}
  36. assert Map.has_key?(shared["pack"], "download-sha256")
  37. assert shared["pack"]["can-download"]
  38. assert shared["pack"]["share-files"]
  39. non_shared = resp["packs"]["test_pack_nonshared"]
  40. assert non_shared["pack"]["share-files"] == false
  41. assert non_shared["pack"]["can-download"] == false
  42. resp =
  43. conn
  44. |> get("/api/pleroma/emoji/packs?page_size=1")
  45. |> json_response_and_validate_schema(200)
  46. assert resp["count"] == 4
  47. packs = Map.keys(resp["packs"])
  48. assert length(packs) == 1
  49. [pack1] = packs
  50. resp =
  51. conn
  52. |> get("/api/pleroma/emoji/packs?page_size=1&page=2")
  53. |> json_response_and_validate_schema(200)
  54. assert resp["count"] == 4
  55. packs = Map.keys(resp["packs"])
  56. assert length(packs) == 1
  57. [pack2] = packs
  58. resp =
  59. conn
  60. |> get("/api/pleroma/emoji/packs?page_size=1&page=3")
  61. |> json_response_and_validate_schema(200)
  62. assert resp["count"] == 4
  63. packs = Map.keys(resp["packs"])
  64. assert length(packs) == 1
  65. [pack3] = packs
  66. resp =
  67. conn
  68. |> get("/api/pleroma/emoji/packs?page_size=1&page=4")
  69. |> json_response_and_validate_schema(200)
  70. assert resp["count"] == 4
  71. packs = Map.keys(resp["packs"])
  72. assert length(packs) == 1
  73. [pack4] = packs
  74. assert [pack1, pack2, pack3, pack4] |> Enum.uniq() |> length() == 4
  75. end
  76. describe "GET /api/pleroma/emoji/packs/remote" do
  77. setup do
  78. clear_config([:instance, :admin_privileges], [:emoji_manage_emoji])
  79. end
  80. test "shareable instance", %{admin_conn: admin_conn, conn: conn} do
  81. resp =
  82. conn
  83. |> get("/api/pleroma/emoji/packs?page=2&page_size=1")
  84. |> json_response_and_validate_schema(200)
  85. mock(fn
  86. %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
  87. json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
  88. %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
  89. json(%{metadata: %{features: ["shareable_emoji_packs"]}})
  90. %{method: :get, url: "https://example.com/api/v1/pleroma/emoji/packs?page=2&page_size=1"} ->
  91. json(resp)
  92. end)
  93. assert admin_conn
  94. |> get("/api/pleroma/emoji/packs/remote?url=https://example.com&page=2&page_size=1")
  95. |> json_response_and_validate_schema(200) == resp
  96. end
  97. test "non shareable instance", %{admin_conn: admin_conn} do
  98. mock(fn
  99. %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
  100. json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
  101. %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
  102. json(%{metadata: %{features: []}})
  103. end)
  104. assert admin_conn
  105. |> get("/api/pleroma/emoji/packs/remote?url=https://example.com")
  106. |> json_response_and_validate_schema(500) == %{
  107. "error" => "The requested instance does not support sharing emoji packs"
  108. }
  109. end
  110. test "it requires privileged role :emoji_manage_emoji", %{admin_conn: admin_conn} do
  111. clear_config([:instance, :admin_privileges], [])
  112. assert admin_conn
  113. |> get("/api/pleroma/emoji/packs/remote?url=https://example.com")
  114. |> json_response(:forbidden)
  115. end
  116. end
  117. describe "GET /api/pleroma/emoji/packs/archive?name=:name" do
  118. test "download shared pack", %{conn: conn} do
  119. resp =
  120. conn
  121. |> get("/api/pleroma/emoji/packs/archive?name=test_pack")
  122. |> response(200)
  123. {:ok, arch} = :zip.unzip(resp, [:memory])
  124. assert Enum.find(arch, fn {n, _} -> n == 'pack.json' end)
  125. assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end)
  126. end
  127. test "non existing pack", %{conn: conn} do
  128. assert conn
  129. |> get("/api/pleroma/emoji/packs/archive?name=test_pack_for_import")
  130. |> json_response_and_validate_schema(:not_found) == %{
  131. "error" => "Pack test_pack_for_import does not exist"
  132. }
  133. end
  134. test "non downloadable pack", %{conn: conn} do
  135. assert conn
  136. |> get("/api/pleroma/emoji/packs/archive?name=test_pack_nonshared")
  137. |> json_response_and_validate_schema(:forbidden) == %{
  138. "error" =>
  139. "Pack test_pack_nonshared cannot be downloaded from this instance, either pack sharing was disabled for this pack or some files are missing"
  140. }
  141. end
  142. end
  143. describe "POST /api/pleroma/emoji/packs/download" do
  144. setup do
  145. clear_config([:instance, :admin_privileges], [:emoji_manage_emoji])
  146. end
  147. test "shared pack from remote and non shared from fallback-src", %{
  148. admin_conn: admin_conn,
  149. conn: conn
  150. } do
  151. mock(fn
  152. %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
  153. json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
  154. %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
  155. json(%{metadata: %{features: ["shareable_emoji_packs"]}})
  156. %{
  157. method: :get,
  158. url: "https://example.com/api/v1/pleroma/emoji/pack?name=test_pack&page_size=" <> _n
  159. } ->
  160. conn
  161. |> get("/api/pleroma/emoji/pack?name=test_pack")
  162. |> json_response_and_validate_schema(200)
  163. |> json()
  164. %{
  165. method: :get,
  166. url: "https://example.com/api/v1/pleroma/emoji/packs/archive?name=test_pack"
  167. } ->
  168. conn
  169. |> get("/api/pleroma/emoji/packs/archive?name=test_pack")
  170. |> response(200)
  171. |> text()
  172. %{
  173. method: :get,
  174. url:
  175. "https://example.com/api/v1/pleroma/emoji/pack?name=test_pack_nonshared&page_size=" <>
  176. _n
  177. } ->
  178. conn
  179. |> get("/api/pleroma/emoji/pack?name=test_pack_nonshared")
  180. |> json_response_and_validate_schema(200)
  181. |> json()
  182. %{
  183. method: :get,
  184. url: "https://nonshared-pack"
  185. } ->
  186. text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
  187. end)
  188. assert admin_conn
  189. |> put_req_header("content-type", "multipart/form-data")
  190. |> post("/api/pleroma/emoji/packs/download", %{
  191. url: "https://example.com",
  192. name: "test_pack",
  193. as: "test_pack2"
  194. })
  195. |> json_response_and_validate_schema(200) == "ok"
  196. assert File.exists?("#{@emoji_path}/test_pack2/pack.json")
  197. assert File.exists?("#{@emoji_path}/test_pack2/blank.png")
  198. assert admin_conn
  199. |> delete("/api/pleroma/emoji/pack?name=test_pack2")
  200. |> json_response_and_validate_schema(200) == "ok"
  201. refute File.exists?("#{@emoji_path}/test_pack2")
  202. assert admin_conn
  203. |> put_req_header("content-type", "multipart/form-data")
  204. |> post(
  205. "/api/pleroma/emoji/packs/download",
  206. %{
  207. url: "https://example.com",
  208. name: "test_pack_nonshared",
  209. as: "test_pack_nonshared2"
  210. }
  211. )
  212. |> json_response_and_validate_schema(200) == "ok"
  213. assert File.exists?("#{@emoji_path}/test_pack_nonshared2/pack.json")
  214. assert File.exists?("#{@emoji_path}/test_pack_nonshared2/blank.png")
  215. assert admin_conn
  216. |> delete("/api/pleroma/emoji/pack?name=test_pack_nonshared2")
  217. |> json_response_and_validate_schema(200) == "ok"
  218. refute File.exists?("#{@emoji_path}/test_pack_nonshared2")
  219. end
  220. test "nonshareable instance", %{admin_conn: admin_conn} do
  221. mock(fn
  222. %{method: :get, url: "https://old-instance/.well-known/nodeinfo"} ->
  223. json(%{links: [%{href: "https://old-instance/nodeinfo/2.1.json"}]})
  224. %{method: :get, url: "https://old-instance/nodeinfo/2.1.json"} ->
  225. json(%{metadata: %{features: []}})
  226. end)
  227. assert admin_conn
  228. |> put_req_header("content-type", "multipart/form-data")
  229. |> post(
  230. "/api/pleroma/emoji/packs/download",
  231. %{
  232. url: "https://old-instance",
  233. name: "test_pack",
  234. as: "test_pack2"
  235. }
  236. )
  237. |> json_response_and_validate_schema(500) == %{
  238. "error" => "The requested instance does not support sharing emoji packs"
  239. }
  240. end
  241. test "checksum fail", %{admin_conn: admin_conn} do
  242. mock(fn
  243. %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
  244. json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
  245. %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
  246. json(%{metadata: %{features: ["shareable_emoji_packs"]}})
  247. %{
  248. method: :get,
  249. url: "https://example.com/api/v1/pleroma/emoji/pack?name=pack_bad_sha&page_size=" <> _n
  250. } ->
  251. {:ok, pack} = Pleroma.Emoji.Pack.load_pack("pack_bad_sha")
  252. %Tesla.Env{status: 200, body: Jason.encode!(pack)}
  253. %{
  254. method: :get,
  255. url: "https://example.com/api/v1/pleroma/emoji/packs/archive?name=pack_bad_sha"
  256. } ->
  257. %Tesla.Env{
  258. status: 200,
  259. body: File.read!("test/instance_static/emoji/pack_bad_sha/pack_bad_sha.zip")
  260. }
  261. end)
  262. assert admin_conn
  263. |> put_req_header("content-type", "multipart/form-data")
  264. |> post("/api/pleroma/emoji/packs/download", %{
  265. url: "https://example.com",
  266. name: "pack_bad_sha",
  267. as: "pack_bad_sha2"
  268. })
  269. |> json_response_and_validate_schema(:internal_server_error) == %{
  270. "error" => "SHA256 for the pack doesn't match the one sent by the server"
  271. }
  272. end
  273. test "other error", %{admin_conn: admin_conn} do
  274. mock(fn
  275. %{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
  276. json(%{links: [%{href: "https://example.com/nodeinfo/2.1.json"}]})
  277. %{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
  278. json(%{metadata: %{features: ["shareable_emoji_packs"]}})
  279. %{
  280. method: :get,
  281. url: "https://example.com/api/v1/pleroma/emoji/pack?name=test_pack&page_size=" <> _n
  282. } ->
  283. {:ok, pack} = Pleroma.Emoji.Pack.load_pack("test_pack")
  284. %Tesla.Env{status: 200, body: Jason.encode!(pack)}
  285. end)
  286. assert admin_conn
  287. |> put_req_header("content-type", "multipart/form-data")
  288. |> post("/api/pleroma/emoji/packs/download", %{
  289. url: "https://example.com",
  290. name: "test_pack",
  291. as: "test_pack2"
  292. })
  293. |> json_response_and_validate_schema(:internal_server_error) == %{
  294. "error" =>
  295. "The pack was not set as shared and there is no fallback src to download from"
  296. }
  297. end
  298. test "it requires privileged role :emoji_manage_emoji", %{admin_conn: conn} do
  299. clear_config([:instance, :admin_privileges], [])
  300. assert conn
  301. |> put_req_header("content-type", "multipart/form-data")
  302. |> post("/api/pleroma/emoji/packs/download", %{
  303. url: "https://example.com",
  304. name: "test_pack",
  305. as: "test_pack2"
  306. })
  307. |> json_response(:forbidden)
  308. end
  309. end
  310. describe "PATCH/update /api/pleroma/emoji/pack?name=:name" do
  311. setup do
  312. clear_config([:instance, :admin_privileges], [:emoji_manage_emoji])
  313. pack_file = "#{@emoji_path}/test_pack/pack.json"
  314. original_content = File.read!(pack_file)
  315. on_exit(fn ->
  316. File.write!(pack_file, original_content)
  317. end)
  318. {:ok,
  319. pack_file: pack_file,
  320. new_data: %{
  321. "license" => "Test license changed",
  322. "homepage" => "https://pleroma.social",
  323. "description" => "Test description",
  324. "share-files" => false
  325. }}
  326. end
  327. test "returns error when file system not writable", %{admin_conn: conn} = ctx do
  328. with_mocks([
  329. {File, [:passthrough], [stat: fn _ -> {:error, :eacces} end]}
  330. ]) do
  331. assert conn
  332. |> put_req_header("content-type", "multipart/form-data")
  333. |> patch(
  334. "/api/pleroma/emoji/pack?name=test_pack",
  335. %{"metadata" => ctx[:new_data]}
  336. )
  337. |> json_response_and_validate_schema(500)
  338. end
  339. end
  340. test "for a pack without a fallback source", ctx do
  341. assert ctx[:admin_conn]
  342. |> put_req_header("content-type", "multipart/form-data")
  343. |> patch("/api/pleroma/emoji/pack?name=test_pack", %{
  344. "metadata" => ctx[:new_data]
  345. })
  346. |> json_response_and_validate_schema(200) == ctx[:new_data]
  347. assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == ctx[:new_data]
  348. end
  349. test "for a pack with a fallback source", ctx do
  350. mock(fn
  351. %{
  352. method: :get,
  353. url: "https://nonshared-pack"
  354. } ->
  355. text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
  356. end)
  357. new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
  358. new_data_with_sha =
  359. Map.put(
  360. new_data,
  361. "fallback-src-sha256",
  362. "1967BB4E42BCC34BCC12D57BE7811D3B7BE52F965BCE45C87BD377B9499CE11D"
  363. )
  364. assert ctx[:admin_conn]
  365. |> put_req_header("content-type", "multipart/form-data")
  366. |> patch("/api/pleroma/emoji/pack?name=test_pack", %{metadata: new_data})
  367. |> json_response_and_validate_schema(200) == new_data_with_sha
  368. assert Jason.decode!(File.read!(ctx[:pack_file]))["pack"] == new_data_with_sha
  369. end
  370. test "when the fallback source doesn't have all the files", ctx do
  371. mock(fn
  372. %{
  373. method: :get,
  374. url: "https://nonshared-pack"
  375. } ->
  376. {:ok, {'empty.zip', empty_arch}} = :zip.zip('empty.zip', [], [:memory])
  377. text(empty_arch)
  378. end)
  379. new_data = Map.put(ctx[:new_data], "fallback-src", "https://nonshared-pack")
  380. assert ctx[:admin_conn]
  381. |> put_req_header("content-type", "multipart/form-data")
  382. |> patch("/api/pleroma/emoji/pack?name=test_pack", %{metadata: new_data})
  383. |> json_response_and_validate_schema(:bad_request) == %{
  384. "error" => "The fallback archive does not have all files specified in pack.json"
  385. }
  386. end
  387. test "it requires privileged role :emoji_manage_emoji", %{
  388. admin_conn: conn,
  389. new_data: new_data
  390. } do
  391. clear_config([:instance, :admin_privileges], [])
  392. assert conn
  393. |> put_req_header("content-type", "multipart/form-data")
  394. |> patch("/api/pleroma/emoji/pack?name=test_pack", %{metadata: new_data})
  395. |> json_response(:forbidden)
  396. end
  397. end
  398. describe "POST/DELETE /api/pleroma/emoji/pack?name=:name" do
  399. setup do
  400. clear_config([:instance, :admin_privileges], [:emoji_manage_emoji])
  401. end
  402. test "returns an error on creates pack when file system not writable", %{
  403. admin_conn: admin_conn
  404. } do
  405. path_pack = Path.join(@emoji_path, "test_pack")
  406. with_mocks([
  407. {File, [:passthrough], [mkdir: fn ^path_pack -> {:error, :eacces} end]}
  408. ]) do
  409. assert admin_conn
  410. |> post("/api/pleroma/emoji/pack?name=test_pack")
  411. |> json_response_and_validate_schema(500) == %{
  412. "error" =>
  413. "Unexpected error occurred while creating pack. (POSIX error: Permission denied)"
  414. }
  415. end
  416. end
  417. test "returns an error on deletes pack when the file system is not writable", %{
  418. admin_conn: admin_conn
  419. } do
  420. path_pack = Path.join(@emoji_path, "test_emoji_pack")
  421. try do
  422. {:ok, _pack} = Pleroma.Emoji.Pack.create("test_emoji_pack")
  423. with_mocks([
  424. {File, [:passthrough], [rm_rf: fn ^path_pack -> {:error, :eacces, path_pack} end]}
  425. ]) do
  426. assert admin_conn
  427. |> delete("/api/pleroma/emoji/pack?name=test_emoji_pack")
  428. |> json_response_and_validate_schema(500) == %{
  429. "error" =>
  430. "Couldn't delete the `test_emoji_pack` pack (POSIX error: Permission denied)"
  431. }
  432. end
  433. after
  434. File.rm_rf(path_pack)
  435. end
  436. end
  437. test "creating and deleting a pack", %{admin_conn: admin_conn} do
  438. assert admin_conn
  439. |> post("/api/pleroma/emoji/pack?name=test_created")
  440. |> json_response_and_validate_schema(200) == "ok"
  441. assert File.exists?("#{@emoji_path}/test_created/pack.json")
  442. assert Jason.decode!(File.read!("#{@emoji_path}/test_created/pack.json")) == %{
  443. "pack" => %{},
  444. "files" => %{},
  445. "files_count" => 0
  446. }
  447. assert admin_conn
  448. |> delete("/api/pleroma/emoji/pack?name=test_created")
  449. |> json_response_and_validate_schema(200) == "ok"
  450. refute File.exists?("#{@emoji_path}/test_created/pack.json")
  451. end
  452. test "if pack exists", %{admin_conn: admin_conn} do
  453. path = Path.join(@emoji_path, "test_created")
  454. File.mkdir(path)
  455. pack_file = Jason.encode!(%{files: %{}, pack: %{}})
  456. File.write!(Path.join(path, "pack.json"), pack_file)
  457. assert admin_conn
  458. |> post("/api/pleroma/emoji/pack?name=test_created")
  459. |> json_response_and_validate_schema(:conflict) == %{
  460. "error" => "A pack named \"test_created\" already exists"
  461. }
  462. on_exit(fn -> File.rm_rf(path) end)
  463. end
  464. test "with empty name", %{admin_conn: admin_conn} do
  465. assert admin_conn
  466. |> post("/api/pleroma/emoji/pack?name= ")
  467. |> json_response_and_validate_schema(:bad_request) == %{
  468. "error" => "pack name cannot be empty"
  469. }
  470. end
  471. test "it requires privileged role :emoji_manage_emoji", %{admin_conn: admin_conn} do
  472. clear_config([:instance, :admin_privileges], [])
  473. assert admin_conn
  474. |> post("/api/pleroma/emoji/pack?name= ")
  475. |> json_response(:forbidden)
  476. assert admin_conn
  477. |> delete("/api/pleroma/emoji/pack?name= ")
  478. |> json_response(:forbidden)
  479. end
  480. end
  481. test "deleting nonexisting pack", %{admin_conn: admin_conn} do
  482. assert admin_conn
  483. |> delete("/api/pleroma/emoji/pack?name=non_existing")
  484. |> json_response_and_validate_schema(:not_found) == %{
  485. "error" => "Pack non_existing does not exist"
  486. }
  487. end
  488. test "deleting with empty name", %{admin_conn: admin_conn} do
  489. assert admin_conn
  490. |> delete("/api/pleroma/emoji/pack?name= ")
  491. |> json_response_and_validate_schema(:bad_request) == %{
  492. "error" => "pack name cannot be empty"
  493. }
  494. end
  495. test "filesystem import", %{admin_conn: admin_conn, conn: conn} do
  496. on_exit(fn ->
  497. File.rm!("#{@emoji_path}/test_pack_for_import/emoji.txt")
  498. File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
  499. end)
  500. resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
  501. refute Map.has_key?(resp["packs"], "test_pack_for_import")
  502. assert admin_conn
  503. |> get("/api/pleroma/emoji/packs/import")
  504. |> json_response_and_validate_schema(200) == ["test_pack_for_import"]
  505. resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
  506. assert resp["packs"]["test_pack_for_import"]["files"] == %{"blank" => "blank.png"}
  507. File.rm!("#{@emoji_path}/test_pack_for_import/pack.json")
  508. refute File.exists?("#{@emoji_path}/test_pack_for_import/pack.json")
  509. emoji_txt_content = """
  510. blank, blank.png, Fun
  511. blank2, blank.png
  512. foo, /emoji/test_pack_for_import/blank.png
  513. bar
  514. """
  515. File.write!("#{@emoji_path}/test_pack_for_import/emoji.txt", emoji_txt_content)
  516. assert admin_conn
  517. |> get("/api/pleroma/emoji/packs/import")
  518. |> json_response_and_validate_schema(200) == ["test_pack_for_import"]
  519. resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
  520. assert resp["packs"]["test_pack_for_import"]["files"] == %{
  521. "blank" => "blank.png",
  522. "blank2" => "blank.png",
  523. "foo" => "blank.png"
  524. }
  525. clear_config([:instance, :admin_privileges], [])
  526. assert admin_conn
  527. |> get("/api/pleroma/emoji/packs/import")
  528. |> json_response(:forbidden)
  529. end
  530. describe "GET /api/pleroma/emoji/pack?name=:name" do
  531. test "shows pack.json", %{conn: conn} do
  532. assert %{
  533. "files" => files,
  534. "files_count" => 2,
  535. "pack" => %{
  536. "can-download" => true,
  537. "description" => "Test description",
  538. "download-sha256" => _,
  539. "homepage" => "https://pleroma.social",
  540. "license" => "Test license",
  541. "share-files" => true
  542. }
  543. } =
  544. conn
  545. |> get("/api/pleroma/emoji/pack?name=test_pack")
  546. |> json_response_and_validate_schema(200)
  547. assert files == %{"blank" => "blank.png", "blank2" => "blank2.png"}
  548. assert %{
  549. "files" => files,
  550. "files_count" => 2
  551. } =
  552. conn
  553. |> get("/api/pleroma/emoji/pack?name=test_pack&page_size=1")
  554. |> json_response_and_validate_schema(200)
  555. assert files |> Map.keys() |> length() == 1
  556. assert %{
  557. "files" => files,
  558. "files_count" => 2
  559. } =
  560. conn
  561. |> get("/api/pleroma/emoji/pack?name=test_pack&page_size=1&page=2")
  562. |> json_response_and_validate_schema(200)
  563. assert files |> Map.keys() |> length() == 1
  564. end
  565. test "for pack name with special chars", %{conn: conn} do
  566. assert %{
  567. "files" => _files,
  568. "files_count" => 1,
  569. "pack" => %{
  570. "can-download" => true,
  571. "description" => "Test description",
  572. "download-sha256" => _,
  573. "homepage" => "https://pleroma.social",
  574. "license" => "Test license",
  575. "share-files" => true
  576. }
  577. } =
  578. conn
  579. |> get("/api/pleroma/emoji/pack?name=blobs.gg")
  580. |> json_response_and_validate_schema(200)
  581. end
  582. test "non existing pack", %{conn: conn} do
  583. assert conn
  584. |> get("/api/pleroma/emoji/pack?name=non_existing")
  585. |> json_response_and_validate_schema(:not_found) == %{
  586. "error" => "Pack non_existing does not exist"
  587. }
  588. end
  589. test "error name", %{conn: conn} do
  590. assert conn
  591. |> get("/api/pleroma/emoji/pack?name= ")
  592. |> json_response_and_validate_schema(:bad_request) == %{
  593. "error" => "pack name cannot be empty"
  594. }
  595. end
  596. end
  597. end