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