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.

274 lines
8.4KB

  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/jpg",
  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. "url" => [
  44. %{
  45. "href" => "http://localhost:4001/media/post-process-file.jpg",
  46. "mediaType" => "image/jpeg",
  47. "type" => "Link"
  48. }
  49. ]
  50. }}
  51. Task.await(Agent.get(TestUploaderSuccess, fn task_pid -> task_pid end))
  52. end
  53. end
  54. describe "Tried storing a file when http callback response error" do
  55. defmodule TestUploaderError do
  56. def http_callback(conn, _params), do: {:error, conn, "Errors"}
  57. def put_file(upload), do: TestUploaderBase.put_file(upload, __MODULE__)
  58. end
  59. setup do: [uploader: TestUploaderError]
  60. setup [:ensure_local_uploader]
  61. test "it returns error" do
  62. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  63. assert capture_log(fn ->
  64. assert Upload.store(@upload_file) == {:error, "Errors"}
  65. Task.await(Agent.get(TestUploaderError, fn task_pid -> task_pid end))
  66. end) =~
  67. "[error] Elixir.Pleroma.Upload store (using Pleroma.UploadTest.TestUploaderError) failed: \"Errors\""
  68. end
  69. end
  70. describe "Tried storing a file when http callback doesn't response by timeout" do
  71. defmodule(TestUploader, do: def(put_file(_upload), do: :wait_callback))
  72. setup do: [uploader: TestUploader]
  73. setup [:ensure_local_uploader]
  74. test "it returns error" do
  75. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  76. assert capture_log(fn ->
  77. assert Upload.store(@upload_file) == {:error, "Uploader callback timeout"}
  78. end) =~
  79. "[error] Elixir.Pleroma.Upload store (using Pleroma.UploadTest.TestUploader) failed: \"Uploader callback timeout\""
  80. end
  81. end
  82. describe "Storing a file with the Local uploader" do
  83. setup [:ensure_local_uploader]
  84. test "returns a media url" do
  85. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  86. file = %Plug.Upload{
  87. content_type: "image/jpg",
  88. path: Path.absname("test/fixtures/image_tmp.jpg"),
  89. filename: "image.jpg"
  90. }
  91. {:ok, data} = Upload.store(file)
  92. assert %{"url" => [%{"href" => url}]} = data
  93. assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/")
  94. end
  95. test "copies the file to the configured folder with deduping" do
  96. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  97. file = %Plug.Upload{
  98. content_type: "image/jpg",
  99. path: Path.absname("test/fixtures/image_tmp.jpg"),
  100. filename: "an [image.jpg"
  101. }
  102. {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
  103. assert List.first(data["url"])["href"] ==
  104. Pleroma.Web.base_url() <>
  105. "/media/e30397b58d226d6583ab5b8b3c5defb0c682bda5c31ef07a9f57c1c4986e3781.jpg"
  106. end
  107. test "copies the file to the configured folder without deduping" do
  108. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  109. file = %Plug.Upload{
  110. content_type: "image/jpg",
  111. path: Path.absname("test/fixtures/image_tmp.jpg"),
  112. filename: "an [image.jpg"
  113. }
  114. {:ok, data} = Upload.store(file)
  115. assert data["name"] == "an [image.jpg"
  116. end
  117. test "fixes incorrect content type" do
  118. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  119. file = %Plug.Upload{
  120. content_type: "application/octet-stream",
  121. path: Path.absname("test/fixtures/image_tmp.jpg"),
  122. filename: "an [image.jpg"
  123. }
  124. {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
  125. assert hd(data["url"])["mediaType"] == "image/jpeg"
  126. end
  127. test "adds missing extension" do
  128. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  129. file = %Plug.Upload{
  130. content_type: "image/jpg",
  131. path: Path.absname("test/fixtures/image_tmp.jpg"),
  132. filename: "an [image"
  133. }
  134. {:ok, data} = Upload.store(file)
  135. assert data["name"] == "an [image.jpg"
  136. end
  137. test "fixes incorrect file extension" do
  138. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  139. file = %Plug.Upload{
  140. content_type: "image/jpg",
  141. path: Path.absname("test/fixtures/image_tmp.jpg"),
  142. filename: "an [image.blah"
  143. }
  144. {:ok, data} = Upload.store(file)
  145. assert data["name"] == "an [image.jpg"
  146. end
  147. test "don't modify filename of an unknown type" do
  148. File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
  149. file = %Plug.Upload{
  150. content_type: "text/plain",
  151. path: Path.absname("test/fixtures/test_tmp.txt"),
  152. filename: "test.txt"
  153. }
  154. {:ok, data} = Upload.store(file)
  155. assert data["name"] == "test.txt"
  156. end
  157. test "copies the file to the configured folder with anonymizing filename" do
  158. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  159. file = %Plug.Upload{
  160. content_type: "image/jpg",
  161. path: Path.absname("test/fixtures/image_tmp.jpg"),
  162. filename: "an [image.jpg"
  163. }
  164. {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.AnonymizeFilename])
  165. refute data["name"] == "an [image.jpg"
  166. end
  167. test "escapes invalid characters in url" do
  168. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  169. file = %Plug.Upload{
  170. content_type: "image/jpg",
  171. path: Path.absname("test/fixtures/image_tmp.jpg"),
  172. filename: "an… image.jpg"
  173. }
  174. {:ok, data} = Upload.store(file)
  175. [attachment_url | _] = data["url"]
  176. assert Path.basename(attachment_url["href"]) == "an%E2%80%A6%20image.jpg"
  177. end
  178. test "escapes reserved uri characters" do
  179. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  180. file = %Plug.Upload{
  181. content_type: "image/jpg",
  182. path: Path.absname("test/fixtures/image_tmp.jpg"),
  183. filename: ":?#[]@!$&\\'()*+,;=.jpg"
  184. }
  185. {:ok, data} = Upload.store(file)
  186. [attachment_url | _] = data["url"]
  187. assert Path.basename(attachment_url["href"]) ==
  188. "%3A%3F%23%5B%5D%40%21%24%26%5C%27%28%29%2A%2B%2C%3B%3D.jpg"
  189. end
  190. end
  191. describe "Setting a custom base_url for uploaded media" do
  192. setup do: clear_config([Pleroma.Upload, :base_url], "https://cache.pleroma.social")
  193. test "returns a media url with configured base_url" do
  194. base_url = Pleroma.Config.get([Pleroma.Upload, :base_url])
  195. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  196. file = %Plug.Upload{
  197. content_type: "image/jpg",
  198. path: Path.absname("test/fixtures/image_tmp.jpg"),
  199. filename: "image.jpg"
  200. }
  201. {:ok, data} = Upload.store(file, base_url: base_url)
  202. assert %{"url" => [%{"href" => url}]} = data
  203. refute String.starts_with?(url, base_url <> "/media/")
  204. end
  205. end
  206. end