logo

pleroma

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

emoji_file_controller_test.exs (15309B)


  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.EmojiFileControllerTest 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. describe "POST/PATCH/DELETE /api/pleroma/emoji/packs/files?name=:name" do
  25. setup do
  26. clear_config([:instance, :admin_privileges], [:emoji_manage_emoji])
  27. pack_file = "#{@emoji_path}/test_pack/pack.json"
  28. original_content = File.read!(pack_file)
  29. on_exit(fn ->
  30. File.write!(pack_file, original_content)
  31. end)
  32. :ok
  33. end
  34. test "upload zip file with emojies", %{admin_conn: admin_conn} do
  35. on_exit(fn ->
  36. [
  37. "128px/a_trusted_friend-128.png",
  38. "auroraborealis.png",
  39. "1000px/baby_in_a_box.png",
  40. "1000px/bear.png",
  41. "128px/bear-128.png"
  42. ]
  43. |> Enum.each(fn path -> File.rm_rf!("#{@emoji_path}/test_pack/#{path}") end)
  44. end)
  45. resp =
  46. admin_conn
  47. |> put_req_header("content-type", "multipart/form-data")
  48. |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
  49. file: %Plug.Upload{
  50. content_type: "application/zip",
  51. filename: "emojis.zip",
  52. path: Path.absname("test/fixtures/emojis.zip")
  53. }
  54. })
  55. |> json_response_and_validate_schema(200)
  56. assert resp == %{
  57. "a_trusted_friend-128" => "128px/a_trusted_friend-128.png",
  58. "auroraborealis" => "auroraborealis.png",
  59. "baby_in_a_box" => "1000px/baby_in_a_box.png",
  60. "bear" => "1000px/bear.png",
  61. "bear-128" => "128px/bear-128.png",
  62. "blank" => "blank.png",
  63. "blank2" => "blank2.png"
  64. }
  65. Enum.each(Map.values(resp), fn path ->
  66. assert File.exists?("#{@emoji_path}/test_pack/#{path}")
  67. end)
  68. end
  69. test "create shortcode exists", %{admin_conn: admin_conn} do
  70. assert admin_conn
  71. |> put_req_header("content-type", "multipart/form-data")
  72. |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
  73. shortcode: "blank",
  74. filename: "dir/blank.png",
  75. file: %Plug.Upload{
  76. filename: "blank.png",
  77. path: "#{@emoji_path}/test_pack/blank.png"
  78. }
  79. })
  80. |> json_response_and_validate_schema(:conflict) == %{
  81. "error" => "An emoji with the \"blank\" shortcode already exists"
  82. }
  83. end
  84. test "don't rewrite old emoji", %{admin_conn: admin_conn} do
  85. on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir/") end)
  86. assert admin_conn
  87. |> put_req_header("content-type", "multipart/form-data")
  88. |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
  89. shortcode: "blank3",
  90. filename: "dir/blank.png",
  91. file: %Plug.Upload{
  92. filename: "blank.png",
  93. path: "#{@emoji_path}/test_pack/blank.png"
  94. }
  95. })
  96. |> json_response_and_validate_schema(200) == %{
  97. "blank" => "blank.png",
  98. "blank2" => "blank2.png",
  99. "blank3" => "dir/blank.png"
  100. }
  101. assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
  102. assert admin_conn
  103. |> put_req_header("content-type", "multipart/form-data")
  104. |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
  105. shortcode: "blank",
  106. new_shortcode: "blank2",
  107. new_filename: "dir_2/blank_3.png"
  108. })
  109. |> json_response_and_validate_schema(:conflict) == %{
  110. "error" =>
  111. "New shortcode \"blank2\" is already used. If you want to override emoji use 'force' option"
  112. }
  113. end
  114. test "rewrite old emoji with force option", %{admin_conn: admin_conn} do
  115. on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir_2/") end)
  116. assert admin_conn
  117. |> put_req_header("content-type", "multipart/form-data")
  118. |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
  119. shortcode: "blank3",
  120. filename: "dir/blank.png",
  121. file: %Plug.Upload{
  122. filename: "blank.png",
  123. path: "#{@emoji_path}/test_pack/blank.png"
  124. }
  125. })
  126. |> json_response_and_validate_schema(200) == %{
  127. "blank" => "blank.png",
  128. "blank2" => "blank2.png",
  129. "blank3" => "dir/blank.png"
  130. }
  131. assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
  132. assert admin_conn
  133. |> put_req_header("content-type", "multipart/form-data")
  134. |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
  135. shortcode: "blank3",
  136. new_shortcode: "blank4",
  137. new_filename: "dir_2/blank_3.png",
  138. force: true
  139. })
  140. |> json_response_and_validate_schema(200) == %{
  141. "blank" => "blank.png",
  142. "blank2" => "blank2.png",
  143. "blank4" => "dir_2/blank_3.png"
  144. }
  145. assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
  146. end
  147. test "with empty filename", %{admin_conn: admin_conn} do
  148. assert admin_conn
  149. |> put_req_header("content-type", "multipart/form-data")
  150. |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
  151. shortcode: "blank2",
  152. filename: "",
  153. file: %Plug.Upload{
  154. filename: "blank.png",
  155. path: "#{@emoji_path}/test_pack/blank.png"
  156. }
  157. })
  158. |> json_response_and_validate_schema(422) == %{
  159. "error" => "pack name, shortcode or filename cannot be empty"
  160. }
  161. end
  162. test "add file with not loaded pack", %{admin_conn: admin_conn} do
  163. assert admin_conn
  164. |> put_req_header("content-type", "multipart/form-data")
  165. |> post("/api/pleroma/emoji/packs/files?name=not_loaded", %{
  166. shortcode: "blank3",
  167. filename: "dir/blank.png",
  168. file: %Plug.Upload{
  169. filename: "blank.png",
  170. path: "#{@emoji_path}/test_pack/blank.png"
  171. }
  172. })
  173. |> json_response_and_validate_schema(:not_found) == %{
  174. "error" => "pack \"not_loaded\" is not found"
  175. }
  176. end
  177. test "returns an error on add file when file system is not writable", %{
  178. admin_conn: admin_conn
  179. } do
  180. pack_file = Path.join([@emoji_path, "not_loaded", "pack.json"])
  181. with_mocks([
  182. {File, [:passthrough], [stat: fn ^pack_file -> {:error, :eacces} end]}
  183. ]) do
  184. assert admin_conn
  185. |> put_req_header("content-type", "multipart/form-data")
  186. |> post("/api/pleroma/emoji/packs/files?name=not_loaded", %{
  187. shortcode: "blank3",
  188. filename: "dir/blank.png",
  189. file: %Plug.Upload{
  190. filename: "blank.png",
  191. path: "#{@emoji_path}/test_pack/blank.png"
  192. }
  193. })
  194. |> json_response_and_validate_schema(500) == %{
  195. "error" =>
  196. "Unexpected error occurred while adding file to pack. (POSIX error: Permission denied)"
  197. }
  198. end
  199. end
  200. test "remove file with not loaded pack", %{admin_conn: admin_conn} do
  201. assert admin_conn
  202. |> delete("/api/pleroma/emoji/packs/files?name=not_loaded&shortcode=blank3")
  203. |> json_response_and_validate_schema(:not_found) == %{
  204. "error" => "pack \"not_loaded\" is not found"
  205. }
  206. end
  207. test "remove file with empty shortcode", %{admin_conn: admin_conn} do
  208. assert admin_conn
  209. |> delete("/api/pleroma/emoji/packs/files?name=not_loaded&shortcode=")
  210. |> json_response_and_validate_schema(:not_found) == %{
  211. "error" => "pack \"not_loaded\" is not found"
  212. }
  213. end
  214. test "update file with not loaded pack", %{admin_conn: admin_conn} do
  215. assert admin_conn
  216. |> put_req_header("content-type", "multipart/form-data")
  217. |> patch("/api/pleroma/emoji/packs/files?name=not_loaded", %{
  218. shortcode: "blank4",
  219. new_shortcode: "blank3",
  220. new_filename: "dir_2/blank_3.png"
  221. })
  222. |> json_response_and_validate_schema(:not_found) == %{
  223. "error" => "pack \"not_loaded\" is not found"
  224. }
  225. end
  226. test "new with shortcode as file with update", %{admin_conn: admin_conn} do
  227. assert admin_conn
  228. |> put_req_header("content-type", "multipart/form-data")
  229. |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
  230. shortcode: "blank4",
  231. filename: "dir/blank.png",
  232. file: %Plug.Upload{
  233. filename: "blank.png",
  234. path: "#{@emoji_path}/test_pack/blank.png"
  235. }
  236. })
  237. |> json_response_and_validate_schema(200) == %{
  238. "blank" => "blank.png",
  239. "blank4" => "dir/blank.png",
  240. "blank2" => "blank2.png"
  241. }
  242. assert File.exists?("#{@emoji_path}/test_pack/dir/blank.png")
  243. assert admin_conn
  244. |> put_req_header("content-type", "multipart/form-data")
  245. |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
  246. shortcode: "blank4",
  247. new_shortcode: "blank3",
  248. new_filename: "dir_2/blank_3.png"
  249. })
  250. |> json_response_and_validate_schema(200) == %{
  251. "blank3" => "dir_2/blank_3.png",
  252. "blank" => "blank.png",
  253. "blank2" => "blank2.png"
  254. }
  255. refute File.exists?("#{@emoji_path}/test_pack/dir/")
  256. assert File.exists?("#{@emoji_path}/test_pack/dir_2/blank_3.png")
  257. assert admin_conn
  258. |> delete("/api/pleroma/emoji/packs/files?name=test_pack&shortcode=blank3")
  259. |> json_response_and_validate_schema(200) == %{
  260. "blank" => "blank.png",
  261. "blank2" => "blank2.png"
  262. }
  263. refute File.exists?("#{@emoji_path}/test_pack/dir_2/")
  264. on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/dir") end)
  265. end
  266. test "new with shortcode from url", %{admin_conn: admin_conn} do
  267. mock(fn
  268. %{
  269. method: :get,
  270. url: "https://test-blank/blank_url.png"
  271. } ->
  272. text(File.read!("#{@emoji_path}/test_pack/blank.png"))
  273. end)
  274. assert admin_conn
  275. |> put_req_header("content-type", "multipart/form-data")
  276. |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
  277. shortcode: "blank_url",
  278. file: "https://test-blank/blank_url.png"
  279. })
  280. |> json_response_and_validate_schema(200) == %{
  281. "blank_url" => "blank_url.png",
  282. "blank" => "blank.png",
  283. "blank2" => "blank2.png"
  284. }
  285. assert File.exists?("#{@emoji_path}/test_pack/blank_url.png")
  286. on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/blank_url.png") end)
  287. end
  288. test "new without shortcode", %{admin_conn: admin_conn} do
  289. on_exit(fn -> File.rm_rf!("#{@emoji_path}/test_pack/shortcode.png") end)
  290. assert admin_conn
  291. |> put_req_header("content-type", "multipart/form-data")
  292. |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
  293. file: %Plug.Upload{
  294. filename: "shortcode.png",
  295. path: "#{Pleroma.Config.get([:instance, :static_dir])}/add/shortcode.png"
  296. }
  297. })
  298. |> json_response_and_validate_schema(200) == %{
  299. "shortcode" => "shortcode.png",
  300. "blank" => "blank.png",
  301. "blank2" => "blank2.png"
  302. }
  303. end
  304. test "remove non existing shortcode in pack.json", %{admin_conn: admin_conn} do
  305. assert admin_conn
  306. |> delete("/api/pleroma/emoji/packs/files?name=test_pack&shortcode=blank3")
  307. |> json_response_and_validate_schema(:bad_request) == %{
  308. "error" => "Emoji \"blank3\" does not exist"
  309. }
  310. end
  311. test "update non existing emoji", %{admin_conn: admin_conn} do
  312. assert admin_conn
  313. |> put_req_header("content-type", "multipart/form-data")
  314. |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
  315. shortcode: "blank3",
  316. new_shortcode: "blank4",
  317. new_filename: "dir_2/blank_3.png"
  318. })
  319. |> json_response_and_validate_schema(:bad_request) == %{
  320. "error" => "Emoji \"blank3\" does not exist"
  321. }
  322. end
  323. test "update with empty shortcode", %{admin_conn: admin_conn} do
  324. assert %{
  325. "error" => "Missing field: new_shortcode."
  326. } =
  327. admin_conn
  328. |> put_req_header("content-type", "multipart/form-data")
  329. |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
  330. shortcode: "blank",
  331. new_filename: "dir_2/blank_3.png"
  332. })
  333. |> json_response_and_validate_schema(:bad_request)
  334. end
  335. test "it requires privileged role :emoji_manage_emoji", %{admin_conn: admin_conn} do
  336. clear_config([:instance, :admin_privileges], [])
  337. assert admin_conn
  338. |> put_req_header("content-type", "multipart/form-data")
  339. |> post("/api/pleroma/emoji/packs/files?name=test_pack", %{
  340. file: %Plug.Upload{
  341. filename: "shortcode.png",
  342. path: "#{Pleroma.Config.get([:instance, :static_dir])}/add/shortcode.png"
  343. }
  344. })
  345. |> json_response(:forbidden)
  346. assert admin_conn
  347. |> put_req_header("content-type", "multipart/form-data")
  348. |> patch("/api/pleroma/emoji/packs/files?name=test_pack", %{
  349. shortcode: "blank",
  350. new_filename: "dir_2/blank_3.png"
  351. })
  352. |> json_response(:forbidden)
  353. assert admin_conn
  354. |> put_req_header("content-type", "multipart/form-data")
  355. |> delete("/api/pleroma/emoji/packs/files?name=test_pack&shortcode=blank3")
  356. |> json_response(:forbidden)
  357. end
  358. end
  359. end