logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

s3_test.exs (2671B)


  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.Uploaders.S3Test do
  5. use Pleroma.DataCase
  6. alias Pleroma.Config
  7. alias Pleroma.Uploaders.S3
  8. import Mock
  9. import ExUnit.CaptureLog
  10. setup do:
  11. clear_config(Pleroma.Uploaders.S3,
  12. bucket: "test_bucket",
  13. public_endpoint: "https://s3.amazonaws.com"
  14. )
  15. describe "get_file/1" do
  16. test "it returns path to local folder for files" do
  17. assert S3.get_file("test_image.jpg") == {
  18. :ok,
  19. {:url, "https://s3.amazonaws.com/test_bucket/test_image.jpg"}
  20. }
  21. end
  22. test "it returns path without bucket when truncated_namespace set to ''" do
  23. Config.put([Pleroma.Uploaders.S3],
  24. bucket: "test_bucket",
  25. public_endpoint: "https://s3.amazonaws.com",
  26. truncated_namespace: ""
  27. )
  28. assert S3.get_file("test_image.jpg") == {
  29. :ok,
  30. {:url, "https://s3.amazonaws.com/test_image.jpg"}
  31. }
  32. end
  33. test "it returns path with bucket namespace when namespace is set" do
  34. Config.put([Pleroma.Uploaders.S3],
  35. bucket: "test_bucket",
  36. public_endpoint: "https://s3.amazonaws.com",
  37. bucket_namespace: "family"
  38. )
  39. assert S3.get_file("test_image.jpg") == {
  40. :ok,
  41. {:url, "https://s3.amazonaws.com/family:test_bucket/test_image.jpg"}
  42. }
  43. end
  44. end
  45. describe "put_file/1" do
  46. setup do
  47. file_upload = %Pleroma.Upload{
  48. name: "image-tet.jpg",
  49. content_type: "image/jpg",
  50. path: "test_folder/image-tet.jpg",
  51. tempfile: Path.absname("test/instance_static/add/shortcode.png")
  52. }
  53. [file_upload: file_upload]
  54. end
  55. test "save file", %{file_upload: file_upload} do
  56. with_mock ExAws, request: fn _ -> {:ok, :ok} end do
  57. assert S3.put_file(file_upload) == {:ok, {:file, "test_folder/image-tet.jpg"}}
  58. end
  59. end
  60. test "returns error", %{file_upload: file_upload} do
  61. with_mock ExAws, request: fn _ -> {:error, "S3 Upload failed"} end do
  62. assert capture_log(fn ->
  63. assert S3.put_file(file_upload) == {:error, "S3 Upload failed"}
  64. end) =~ "Elixir.Pleroma.Uploaders.S3: {:error, \"S3 Upload failed\"}"
  65. end
  66. end
  67. end
  68. describe "delete_file/1" do
  69. test_with_mock "deletes file", ExAws, request: fn _req -> {:ok, %{status_code: 204}} end do
  70. assert :ok = S3.delete_file("image.jpg")
  71. assert_called(ExAws.request(:_))
  72. end
  73. end
  74. end