1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.MediaControllerTest do
6 use Pleroma.Web.ConnCase
8 import ExUnit.CaptureLog
12 alias Pleroma.Web.ActivityPub.ActivityPub
14 describe "Upload media" do
15 setup do: oauth_access(["write:media"])
19 content_type: "image/jpeg",
20 path: Path.absname("test/fixtures/image.jpg"),
21 filename: "an_image.jpg"
27 setup do: clear_config([:media_proxy])
28 setup do: clear_config([Pleroma.Upload])
30 test "/api/v1/media", %{conn: conn, image: image} do
31 desc = "Description of the image"
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)
39 assert media["type"] == "image"
40 assert media["description"] == desc
43 object = Object.get_by_id(media["id"])
44 assert object.data["actor"] == User.ap_id(conn.assigns[:user])
47 test "/api/v2/media", %{conn: conn, user: user, image: image} do
48 desc = "Description of the image"
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)
56 assert media_id = response["id"]
58 %{conn: conn} = oauth_access(["read:media"], user: user)
62 |> get("/api/v1/media/#{media_id}")
63 |> json_response_and_validate_schema(200)
65 assert media["type"] == "image"
66 assert media["description"] == desc
69 object = Object.get_by_id(media["id"])
70 assert object.data["actor"] == user.ap_id
73 test "/api/v2/media, upload_limit", %{conn: conn, user: user} do
74 desc = "Description of the binary"
76 upload_limit = Config.get([:instance, :upload_limit]) * 8 + 8
79 File.write(Path.absname("test/tmp/large_binary.data"), <<0::size(upload_limit)>>)
81 large_binary = %Plug.Upload{
83 path: Path.absname("test/tmp/large_binary.data"),
84 filename: "large_binary.data"
87 assert capture_log(fn ->
88 assert %{"error" => "file_too_large"} =
90 |> put_req_header("content-type", "multipart/form-data")
91 |> post("/api/v2/media", %{
92 "file" => large_binary,
95 |> json_response_and_validate_schema(400)
97 "[error] Elixir.Pleroma.Upload store (using Pleroma.Uploaders.Local) failed: :file_too_large"
99 clear_config([:instance, :upload_limit], upload_limit)
103 |> put_req_header("content-type", "multipart/form-data")
104 |> post("/api/v2/media", %{
105 "file" => large_binary,
106 "description" => desc
108 |> json_response_and_validate_schema(202)
110 assert media_id = response["id"]
112 %{conn: conn} = oauth_access(["read:media"], user: user)
116 |> get("/api/v1/media/#{media_id}")
117 |> json_response_and_validate_schema(200)
119 assert media["type"] == "unknown"
120 assert media["description"] == desc
123 assert :ok == File.rm(Path.absname("test/tmp/large_binary.data"))
126 test "Do not allow nested filename", %{conn: conn, image: image} do
127 image = %Plug.Upload{
129 | filename: "../../../../../nested/file.jpg"
132 desc = "Description of the image"
136 |> put_req_header("content-type", "multipart/form-data")
137 |> post("/api/v1/media", %{"file" => image, "description" => desc})
138 |> json_response_and_validate_schema(:ok)
140 refute Regex.match?(~r"/nested/", media["url"])
144 describe "Update media description" do
145 setup do: oauth_access(["write:media"])
147 setup %{user: actor} do
149 content_type: "image/jpeg",
150 path: Path.absname("test/fixtures/image.jpg"),
151 filename: "an_image.jpg"
154 {:ok, %Object{} = object} =
157 actor: User.ap_id(actor),
158 description: "test-m"
164 test "/api/v1/media/:id good request", %{conn: conn, object: object} do
167 |> put_req_header("content-type", "multipart/form-data")
168 |> put("/api/v1/media/#{object.id}", %{"description" => "test-media"})
169 |> json_response_and_validate_schema(:ok)
171 assert media["description"] == "test-media"
172 assert refresh_record(object).data["name"] == "test-media"
176 describe "Get media by id (/api/v1/media/:id)" do
177 setup do: oauth_access(["read:media"])
179 setup %{user: actor} do
181 content_type: "image/jpeg",
182 path: Path.absname("test/fixtures/image.jpg"),
183 filename: "an_image.jpg"
186 {:ok, %Object{} = object} =
189 actor: User.ap_id(actor),
190 description: "test-media"
196 test "it returns media object when requested by owner", %{conn: conn, object: object} do
199 |> get("/api/v1/media/#{object.id}")
200 |> json_response_and_validate_schema(:ok)
202 assert media["description"] == "test-media"
203 assert media["type"] == "image"
207 test "it returns 403 if media object requested by non-owner", %{object: object, user: user} do
208 %{conn: conn, user: other_user} = oauth_access(["read:media"])
210 assert object.data["actor"] == user.ap_id
211 refute user.id == other_user.id
214 |> get("/api/v1/media/#{object.id}")
215 |> json_response_and_validate_schema(403)