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.

173 lines
5.2KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.UploadTest do
  5. alias Pleroma.Upload
  6. use Pleroma.DataCase
  7. describe "Storing a file with the Local uploader" do
  8. setup [:ensure_local_uploader]
  9. test "returns a media url" do
  10. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  11. file = %Plug.Upload{
  12. content_type: "image/jpg",
  13. path: Path.absname("test/fixtures/image_tmp.jpg"),
  14. filename: "image.jpg"
  15. }
  16. {:ok, data} = Upload.store(file)
  17. assert %{"url" => [%{"href" => url}]} = data
  18. assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/")
  19. end
  20. test "returns a media url with configured base_url" do
  21. base_url = "https://cache.pleroma.social"
  22. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  23. file = %Plug.Upload{
  24. content_type: "image/jpg",
  25. path: Path.absname("test/fixtures/image_tmp.jpg"),
  26. filename: "image.jpg"
  27. }
  28. {:ok, data} = Upload.store(file, base_url: base_url)
  29. assert %{"url" => [%{"href" => url}]} = data
  30. assert String.starts_with?(url, base_url <> "/media/")
  31. end
  32. test "copies the file to the configured folder with deduping" do
  33. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  34. file = %Plug.Upload{
  35. content_type: "image/jpg",
  36. path: Path.absname("test/fixtures/image_tmp.jpg"),
  37. filename: "an [image.jpg"
  38. }
  39. {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
  40. assert List.first(data["url"])["href"] ==
  41. Pleroma.Web.base_url() <>
  42. "/media/e30397b58d226d6583ab5b8b3c5defb0c682bda5c31ef07a9f57c1c4986e3781.jpg"
  43. end
  44. test "copies the file to the configured folder without deduping" do
  45. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  46. file = %Plug.Upload{
  47. content_type: "image/jpg",
  48. path: Path.absname("test/fixtures/image_tmp.jpg"),
  49. filename: "an [image.jpg"
  50. }
  51. {:ok, data} = Upload.store(file)
  52. assert data["name"] == "an [image.jpg"
  53. end
  54. test "fixes incorrect content type" do
  55. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  56. file = %Plug.Upload{
  57. content_type: "application/octet-stream",
  58. path: Path.absname("test/fixtures/image_tmp.jpg"),
  59. filename: "an [image.jpg"
  60. }
  61. {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
  62. assert hd(data["url"])["mediaType"] == "image/jpeg"
  63. end
  64. test "adds missing extension" do
  65. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  66. file = %Plug.Upload{
  67. content_type: "image/jpg",
  68. path: Path.absname("test/fixtures/image_tmp.jpg"),
  69. filename: "an [image"
  70. }
  71. {:ok, data} = Upload.store(file)
  72. assert data["name"] == "an [image.jpg"
  73. end
  74. test "fixes incorrect file extension" do
  75. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  76. file = %Plug.Upload{
  77. content_type: "image/jpg",
  78. path: Path.absname("test/fixtures/image_tmp.jpg"),
  79. filename: "an [image.blah"
  80. }
  81. {:ok, data} = Upload.store(file)
  82. assert data["name"] == "an [image.jpg"
  83. end
  84. test "don't modify filename of an unknown type" do
  85. File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
  86. file = %Plug.Upload{
  87. content_type: "text/plain",
  88. path: Path.absname("test/fixtures/test_tmp.txt"),
  89. filename: "test.txt"
  90. }
  91. {:ok, data} = Upload.store(file)
  92. assert data["name"] == "test.txt"
  93. end
  94. test "copies the file to the configured folder with anonymizing filename" do
  95. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  96. file = %Plug.Upload{
  97. content_type: "image/jpg",
  98. path: Path.absname("test/fixtures/image_tmp.jpg"),
  99. filename: "an [image.jpg"
  100. }
  101. {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.AnonymizeFilename])
  102. refute data["name"] == "an [image.jpg"
  103. end
  104. test "escapes invalid characters in url" do
  105. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  106. file = %Plug.Upload{
  107. content_type: "image/jpg",
  108. path: Path.absname("test/fixtures/image_tmp.jpg"),
  109. filename: "an… image.jpg"
  110. }
  111. {:ok, data} = Upload.store(file)
  112. [attachment_url | _] = data["url"]
  113. assert Path.basename(attachment_url["href"]) == "an%E2%80%A6%20image.jpg"
  114. end
  115. test "escapes reserved uri characters" do
  116. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  117. file = %Plug.Upload{
  118. content_type: "image/jpg",
  119. path: Path.absname("test/fixtures/image_tmp.jpg"),
  120. filename: ":?#[]@!$&\\'()*+,;=.jpg"
  121. }
  122. {:ok, data} = Upload.store(file)
  123. [attachment_url | _] = data["url"]
  124. assert Path.basename(attachment_url["href"]) ==
  125. "%3A%3F%23%5B%5D%40%21%24%26%5C%27%28%29%2A%2B%2C%3B%3D.jpg"
  126. end
  127. end
  128. end