logo

pleroma

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

upload_test.exs (8395B)


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