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.

138 lines
4.1KB

  1. defmodule Pleroma.UploadTest do
  2. alias Pleroma.Upload
  3. use Pleroma.DataCase
  4. describe "Storing a file with the Local uploader" do
  5. setup [:ensure_local_uploader]
  6. test "returns a media url" do
  7. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  8. file = %Plug.Upload{
  9. content_type: "image/jpg",
  10. path: Path.absname("test/fixtures/image_tmp.jpg"),
  11. filename: "image.jpg"
  12. }
  13. {:ok, data} = Upload.store(file)
  14. assert %{"url" => [%{"href" => url}]} = data
  15. assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/")
  16. end
  17. test "returns a media url with configured base_url" do
  18. base_url = "https://cache.pleroma.social"
  19. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  20. file = %Plug.Upload{
  21. content_type: "image/jpg",
  22. path: Path.absname("test/fixtures/image_tmp.jpg"),
  23. filename: "image.jpg"
  24. }
  25. {:ok, data} = Upload.store(file, base_url: base_url)
  26. assert %{"url" => [%{"href" => url}]} = data
  27. assert String.starts_with?(url, base_url <> "/media/")
  28. end
  29. test "copies the file to the configured folder with deduping" do
  30. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  31. file = %Plug.Upload{
  32. content_type: "image/jpg",
  33. path: Path.absname("test/fixtures/image_tmp.jpg"),
  34. filename: "an [image.jpg"
  35. }
  36. {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
  37. assert List.first(data["url"])["href"] ==
  38. Pleroma.Web.base_url() <>
  39. "/media/e7a6d0cf595bff76f14c9a98b6c199539559e8b844e02e51e5efcfd1f614a2df.jpg"
  40. end
  41. test "copies the file to the configured folder without deduping" do
  42. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  43. file = %Plug.Upload{
  44. content_type: "image/jpg",
  45. path: Path.absname("test/fixtures/image_tmp.jpg"),
  46. filename: "an [image.jpg"
  47. }
  48. {:ok, data} = Upload.store(file)
  49. assert data["name"] == "an [image.jpg"
  50. end
  51. test "fixes incorrect content type" do
  52. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  53. file = %Plug.Upload{
  54. content_type: "application/octet-stream",
  55. path: Path.absname("test/fixtures/image_tmp.jpg"),
  56. filename: "an [image.jpg"
  57. }
  58. {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
  59. assert hd(data["url"])["mediaType"] == "image/jpeg"
  60. end
  61. test "adds missing extension" do
  62. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  63. file = %Plug.Upload{
  64. content_type: "image/jpg",
  65. path: Path.absname("test/fixtures/image_tmp.jpg"),
  66. filename: "an [image"
  67. }
  68. {:ok, data} = Upload.store(file)
  69. assert data["name"] == "an [image.jpg"
  70. end
  71. test "fixes incorrect file extension" do
  72. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  73. file = %Plug.Upload{
  74. content_type: "image/jpg",
  75. path: Path.absname("test/fixtures/image_tmp.jpg"),
  76. filename: "an [image.blah"
  77. }
  78. {:ok, data} = Upload.store(file)
  79. assert data["name"] == "an [image.jpg"
  80. end
  81. test "don't modify filename of an unknown type" do
  82. File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
  83. file = %Plug.Upload{
  84. content_type: "text/plain",
  85. path: Path.absname("test/fixtures/test_tmp.txt"),
  86. filename: "test.txt"
  87. }
  88. {:ok, data} = Upload.store(file)
  89. assert data["name"] == "test.txt"
  90. end
  91. test "copies the file to the configured folder with anonymizing filename" do
  92. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  93. file = %Plug.Upload{
  94. content_type: "image/jpg",
  95. path: Path.absname("test/fixtures/image_tmp.jpg"),
  96. filename: "an [image.jpg"
  97. }
  98. {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.AnonymizeFilename])
  99. refute data["name"] == "an [image.jpg"
  100. end
  101. end
  102. end