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.

87 lines
2.4KB

  1. defmodule Pleroma.UploadTest do
  2. alias Pleroma.Upload
  3. use Pleroma.DataCase
  4. describe "Storing a file" do
  5. test "copies the file to the configured folder with deduping" do
  6. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  7. file = %Plug.Upload{
  8. content_type: "image/jpg",
  9. path: Path.absname("test/fixtures/image_tmp.jpg"),
  10. filename: "an [image.jpg"
  11. }
  12. data = Upload.store(file, true)
  13. assert data["name"] ==
  14. "e7a6d0cf595bff76f14c9a98b6c199539559e8b844e02e51e5efcfd1f614a2df.jpeg"
  15. end
  16. test "copies the file to the configured folder without deduping" do
  17. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  18. file = %Plug.Upload{
  19. content_type: "image/jpg",
  20. path: Path.absname("test/fixtures/image_tmp.jpg"),
  21. filename: "an [image.jpg"
  22. }
  23. data = Upload.store(file, false)
  24. assert data["name"] == "an [image.jpg"
  25. end
  26. test "fixes incorrect content type" do
  27. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  28. file = %Plug.Upload{
  29. content_type: "application/octet-stream",
  30. path: Path.absname("test/fixtures/image_tmp.jpg"),
  31. filename: "an [image.jpg"
  32. }
  33. data = Upload.store(file, true)
  34. assert hd(data["url"])["mediaType"] == "image/jpeg"
  35. end
  36. test "adds missing extension" do
  37. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  38. file = %Plug.Upload{
  39. content_type: "image/jpg",
  40. path: Path.absname("test/fixtures/image_tmp.jpg"),
  41. filename: "an [image"
  42. }
  43. data = Upload.store(file, false)
  44. assert data["name"] == "an [image.jpg"
  45. end
  46. test "fixes incorrect file extension" do
  47. File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
  48. file = %Plug.Upload{
  49. content_type: "image/jpg",
  50. path: Path.absname("test/fixtures/image_tmp.jpg"),
  51. filename: "an [image.blah"
  52. }
  53. data = Upload.store(file, false)
  54. assert data["name"] == "an [image.jpg"
  55. end
  56. test "don't modify filename of an unknown type" do
  57. File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
  58. file = %Plug.Upload{
  59. content_type: "text/plain",
  60. path: Path.absname("test/fixtures/test_tmp.txt"),
  61. filename: "test.txt"
  62. }
  63. data = Upload.store(file, false)
  64. assert data["name"] == "test.txt"
  65. end
  66. end
  67. end