1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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"))
127 describe "Update media description" do
128 setup do: oauth_access(["write:media"])
130 setup %{user: actor} do
132 content_type: "image/jpeg",
133 path: Path.absname("test/fixtures/image.jpg"),
134 filename: "an_image.jpg"
137 {:ok, %Object{} = object} =
140 actor: User.ap_id(actor),
141 description: "test-m"
147 test "/api/v1/media/:id good request", %{conn: conn, object: object} do
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)
154 assert media["description"] == "test-media"
155 assert refresh_record(object).data["name"] == "test-media"
159 describe "Get media by id (/api/v1/media/:id)" do
160 setup do: oauth_access(["read:media"])
162 setup %{user: actor} do
164 content_type: "image/jpeg",
165 path: Path.absname("test/fixtures/image.jpg"),
166 filename: "an_image.jpg"
169 {:ok, %Object{} = object} =
172 actor: User.ap_id(actor),
173 description: "test-media"
179 test "it returns media object when requested by owner", %{conn: conn, object: object} do
182 |> get("/api/v1/media/#{object.id}")
183 |> json_response_and_validate_schema(:ok)
185 assert media["description"] == "test-media"
186 assert media["type"] == "image"
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"])
193 assert object.data["actor"] == user.ap_id
194 refute user.id == other_user.id
197 |> get("/api/v1/media/#{object.id}")
198 |> json_response_and_validate_schema(403)