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