Add test on changing [:instance, :upload_limit]
[akkoma] / test / pleroma / web / mastodon_api / controllers / media_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.MediaControllerTest do
6 use Pleroma.Web.ConnCase
7
8 import ExUnit.CaptureLog
9
10 alias Pleroma.Object
11 alias Pleroma.User
12 alias Pleroma.Web.ActivityPub.ActivityPub
13
14 describe "Upload media" do
15 setup do: oauth_access(["write:media"])
16
17 setup do
18 image = %Plug.Upload{
19 content_type: "image/jpeg",
20 path: Path.absname("test/fixtures/image.jpg"),
21 filename: "an_image.jpg"
22 }
23
24 [image: image]
25 end
26
27 setup do: clear_config([:media_proxy])
28 setup do: clear_config([Pleroma.Upload])
29
30 test "/api/v1/media", %{conn: conn, image: image} do
31 desc = "Description of the image"
32
33 media =
34 conn
35 |> put_req_header("content-type", "multipart/form-data")
36 |> post("/api/v1/media", %{"file" => image, "description" => desc})
37 |> json_response_and_validate_schema(:ok)
38
39 assert media["type"] == "image"
40 assert media["description"] == desc
41 assert media["id"]
42
43 object = Object.get_by_id(media["id"])
44 assert object.data["actor"] == User.ap_id(conn.assigns[:user])
45 end
46
47 test "/api/v2/media", %{conn: conn, user: user, image: image} do
48 desc = "Description of the image"
49
50 response =
51 conn
52 |> put_req_header("content-type", "multipart/form-data")
53 |> post("/api/v2/media", %{"file" => image, "description" => desc})
54 |> json_response_and_validate_schema(202)
55
56 assert media_id = response["id"]
57
58 %{conn: conn} = oauth_access(["read:media"], user: user)
59
60 media =
61 conn
62 |> get("/api/v1/media/#{media_id}")
63 |> json_response_and_validate_schema(200)
64
65 assert media["type"] == "image"
66 assert media["description"] == desc
67 assert media["id"]
68
69 object = Object.get_by_id(media["id"])
70 assert object.data["actor"] == user.ap_id
71 end
72
73 test "/api/v2/media, upload_limit", %{conn: conn, user: user} do
74 desc = "Description of the binary"
75
76 upload_limit = Config.get([:instance, :upload_limit]) * 8 + 8
77
78 assert :ok ==
79 File.write(Path.absname("test/tmp/large_binary.data"), <<0::size(upload_limit)>>)
80
81 large_binary = %Plug.Upload{
82 content_type: nil,
83 path: Path.absname("test/tmp/large_binary.data"),
84 filename: "large_binary.data"
85 }
86
87 assert capture_log(fn ->
88 assert %{"error" => "file_too_large"} =
89 conn
90 |> put_req_header("content-type", "multipart/form-data")
91 |> post("/api/v2/media", %{
92 "file" => large_binary,
93 "description" => desc
94 })
95 |> json_response_and_validate_schema(400)
96 end) =~
97 "[error] Elixir.Pleroma.Upload store (using Pleroma.Uploaders.Local) failed: :file_too_large"
98
99 clear_config([:instance, :upload_limit], upload_limit)
100
101 assert response =
102 conn
103 |> put_req_header("content-type", "multipart/form-data")
104 |> post("/api/v2/media", %{
105 "file" => large_binary,
106 "description" => desc
107 })
108 |> json_response_and_validate_schema(202)
109
110 assert media_id = response["id"]
111
112 %{conn: conn} = oauth_access(["read:media"], user: user)
113
114 media =
115 conn
116 |> get("/api/v1/media/#{media_id}")
117 |> json_response_and_validate_schema(200)
118
119 assert media["type"] == "unknown"
120 assert media["description"] == desc
121 assert media["id"]
122
123 assert :ok == File.rm(Path.absname("test/tmp/large_binary.data"))
124 end
125 end
126
127 describe "Update media description" do
128 setup do: oauth_access(["write:media"])
129
130 setup %{user: actor} do
131 file = %Plug.Upload{
132 content_type: "image/jpeg",
133 path: Path.absname("test/fixtures/image.jpg"),
134 filename: "an_image.jpg"
135 }
136
137 {:ok, %Object{} = object} =
138 ActivityPub.upload(
139 file,
140 actor: User.ap_id(actor),
141 description: "test-m"
142 )
143
144 [object: object]
145 end
146
147 test "/api/v1/media/:id good request", %{conn: conn, object: object} do
148 media =
149 conn
150 |> put_req_header("content-type", "multipart/form-data")
151 |> put("/api/v1/media/#{object.id}", %{"description" => "test-media"})
152 |> json_response_and_validate_schema(:ok)
153
154 assert media["description"] == "test-media"
155 assert refresh_record(object).data["name"] == "test-media"
156 end
157 end
158
159 describe "Get media by id (/api/v1/media/:id)" do
160 setup do: oauth_access(["read:media"])
161
162 setup %{user: actor} do
163 file = %Plug.Upload{
164 content_type: "image/jpeg",
165 path: Path.absname("test/fixtures/image.jpg"),
166 filename: "an_image.jpg"
167 }
168
169 {:ok, %Object{} = object} =
170 ActivityPub.upload(
171 file,
172 actor: User.ap_id(actor),
173 description: "test-media"
174 )
175
176 [object: object]
177 end
178
179 test "it returns media object when requested by owner", %{conn: conn, object: object} do
180 media =
181 conn
182 |> get("/api/v1/media/#{object.id}")
183 |> json_response_and_validate_schema(:ok)
184
185 assert media["description"] == "test-media"
186 assert media["type"] == "image"
187 assert media["id"]
188 end
189
190 test "it returns 403 if media object requested by non-owner", %{object: object, user: user} do
191 %{conn: conn, user: other_user} = oauth_access(["read:media"])
192
193 assert object.data["actor"] == user.ap_id
194 refute user.id == other_user.id
195
196 conn
197 |> get("/api/v1/media/#{object.id}")
198 |> json_response_and_validate_schema(403)
199 end
200 end
201 end