logo

pleroma

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

emoji_file_controller_test.exs (13116B)


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