logo

pleroma

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

upload_test.exs (8137B)


  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.UploadTest do
  5. use Pleroma.DataCase
  6. import ExUnit.CaptureLog
  7. alias Pleroma.Upload
  8. alias Pleroma.Uploaders.Uploader
  9. @upload_file %Plug.Upload{
  10. content_type: "image/jpeg",
  11. path: Path.absname("test/fixtures/image_tmp.jpg"),
  12. filename: "image.jpg"
  13. }
  14. defmodule TestUploaderBase do
  15. def put_file(%{path: path} = _upload, module_name) do
  16. task_pid =
  17. Task.async(fn ->
  18. :timer.sleep(10)
  19. {Uploader, path}
  20. |> :global.whereis_name()
  21. |> send({Uploader, self(), {:test}, %{}})
  22. assert_receive {Uploader, {:test}}, 4_000
  23. end)
  24. Agent.start(fn -> task_pid end, name: module_name)
  25. :wait_callback
  26. end
  27. end
  28. describe "Tried storing a file when http callback response success result" do
  29. defmodule TestUploaderSuccess do
  30. def http_callback(conn, _params),
  31. do: {:ok, conn, {:file, "post-process-file.jpg"}}
  32. def put_file(upload), do: TestUploaderBase.put_file(upload, __MODULE__)
  33. end
  34. setup do: [uploader: TestUploaderSuccess]
  35. setup [:ensure_local_uploader]
  36. test "it returns file" do
  37. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  38. assert Upload.store(@upload_file) ==
  39. {:ok,
  40. %{
  41. "name" => "image.jpg",
  42. "type" => "Document",
  43. "mediaType" => "image/jpeg",
  44. "url" => [
  45. %{
  46. "href" => "http://localhost:4001/media/post-process-file.jpg",
  47. "mediaType" => "image/jpeg",
  48. "type" => "Link"
  49. }
  50. ]
  51. }}
  52. Task.await(Agent.get(TestUploaderSuccess, fn task_pid -> task_pid end))
  53. end
  54. end
  55. describe "Tried storing a file when http callback response error" do
  56. defmodule TestUploaderError do
  57. def http_callback(conn, _params), do: {:error, conn, "Errors"}
  58. def put_file(upload), do: TestUploaderBase.put_file(upload, __MODULE__)
  59. end
  60. setup do: [uploader: TestUploaderError]
  61. setup [:ensure_local_uploader]
  62. test "it returns error" do
  63. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  64. assert capture_log(fn ->
  65. assert Upload.store(@upload_file) == {:error, "Errors"}
  66. Task.await(Agent.get(TestUploaderError, fn task_pid -> task_pid end))
  67. end) =~
  68. "[error] Elixir.Pleroma.Upload store (using Pleroma.UploadTest.TestUploaderError) failed: \"Errors\""
  69. end
  70. end
  71. describe "Tried storing a file when http callback doesn't response by timeout" do
  72. defmodule(TestUploader, do: def(put_file(_upload), do: :wait_callback))
  73. setup do: [uploader: TestUploader]
  74. setup [:ensure_local_uploader]
  75. test "it returns error" do
  76. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  77. assert capture_log(fn ->
  78. assert Upload.store(@upload_file) == {:error, "Uploader callback timeout"}
  79. end) =~
  80. "[error] Elixir.Pleroma.Upload store (using Pleroma.UploadTest.TestUploader) failed: \"Uploader callback timeout\""
  81. end
  82. end
  83. describe "Storing a file with the Local uploader" do
  84. setup [:ensure_local_uploader]
  85. test "does not allow descriptions longer than the post limit" do
  86. clear_config([:instance, :description_limit], 2)
  87. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  88. file = %Plug.Upload{
  89. content_type: "image/jpg",
  90. path: Path.absname("test/fixtures/image_tmp.jpg"),
  91. filename: "image.jpg"
  92. }
  93. {:error, :description_too_long} = Upload.store(file, description: "123")
  94. end
  95. test "returns a media url" do
  96. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  97. file = %Plug.Upload{
  98. content_type: "image/jpeg",
  99. path: Path.absname("test/fixtures/image_tmp.jpg"),
  100. filename: "image.jpg"
  101. }
  102. {:ok, data} = Upload.store(file)
  103. assert %{"url" => [%{"href" => url}]} = data
  104. assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/")
  105. end
  106. test "copies the file to the configured folder with deduping" do
  107. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  108. file = %Plug.Upload{
  109. content_type: "image/jpeg",
  110. path: Path.absname("test/fixtures/image_tmp.jpg"),
  111. filename: "an [image.jpg"
  112. }
  113. {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
  114. assert List.first(data["url"])["href"] ==
  115. Pleroma.Web.base_url() <>
  116. "/media/e30397b58d226d6583ab5b8b3c5defb0c682bda5c31ef07a9f57c1c4986e3781.jpg"
  117. end
  118. test "copies the file to the configured folder without deduping" do
  119. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  120. file = %Plug.Upload{
  121. content_type: "image/jpeg",
  122. path: Path.absname("test/fixtures/image_tmp.jpg"),
  123. filename: "an [image.jpg"
  124. }
  125. {:ok, data} = Upload.store(file)
  126. assert data["name"] == "an [image.jpg"
  127. end
  128. test "fixes incorrect content type when base64 is given" do
  129. params = %{
  130. img: "data:image/png;base64,#{Base.encode64(File.read!("test/fixtures/image.jpg"))}"
  131. }
  132. {:ok, data} = Upload.store(params)
  133. assert hd(data["url"])["mediaType"] == "image/jpeg"
  134. end
  135. test "adds extension when base64 is given" do
  136. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  137. params = %{
  138. img: "data:image/png;base64,#{Base.encode64(File.read!("test/fixtures/image.jpg"))}"
  139. }
  140. {:ok, data} = Upload.store(params)
  141. assert String.ends_with?(data["name"], ".jpg")
  142. end
  143. test "copies the file to the configured folder with anonymizing filename" do
  144. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  145. file = %Plug.Upload{
  146. content_type: "image/jpeg",
  147. path: Path.absname("test/fixtures/image_tmp.jpg"),
  148. filename: "an [image.jpg"
  149. }
  150. {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.AnonymizeFilename])
  151. refute data["name"] == "an [image.jpg"
  152. end
  153. test "escapes invalid characters in url" do
  154. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  155. file = %Plug.Upload{
  156. content_type: "image/jpeg",
  157. path: Path.absname("test/fixtures/image_tmp.jpg"),
  158. filename: "an… image.jpg"
  159. }
  160. {:ok, data} = Upload.store(file)
  161. [attachment_url | _] = data["url"]
  162. assert Path.basename(attachment_url["href"]) == "an%E2%80%A6%20image.jpg"
  163. end
  164. test "escapes reserved uri characters" do
  165. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  166. file = %Plug.Upload{
  167. content_type: "image/jpeg",
  168. path: Path.absname("test/fixtures/image_tmp.jpg"),
  169. filename: ":?#[]@!$&\\'()*+,;=.jpg"
  170. }
  171. {:ok, data} = Upload.store(file)
  172. [attachment_url | _] = data["url"]
  173. assert Path.basename(attachment_url["href"]) ==
  174. "%3A%3F%23%5B%5D%40%21%24%26%5C%27%28%29%2A%2B%2C%3B%3D.jpg"
  175. end
  176. end
  177. describe "Setting a custom base_url for uploaded media" do
  178. setup do: clear_config([Pleroma.Upload, :base_url], "https://cache.pleroma.social")
  179. test "returns a media url with configured base_url" do
  180. base_url = Pleroma.Config.get([Pleroma.Upload, :base_url])
  181. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  182. file = %Plug.Upload{
  183. content_type: "image/jpeg",
  184. path: Path.absname("test/fixtures/image_tmp.jpg"),
  185. filename: "image.jpg"
  186. }
  187. {:ok, data} = Upload.store(file, base_url: base_url)
  188. assert %{"url" => [%{"href" => url}]} = data
  189. refute String.starts_with?(url, base_url <> "/media/")
  190. end
  191. end
  192. end