Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

781 lines
28KB

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