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"])
16 setup do: clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
17 setup do: clear_config([Pleroma.Uploaders.Local, :uploads], "uploads")
21 content_type: "image/jpeg",
22 path: Path.absname("test/fixtures/image.jpg"),
23 filename: "an_image.jpg"
29 setup do: clear_config([:media_proxy])
30 setup do: clear_config([Pleroma.Upload])
32 test "/api/v1/media", %{conn: conn, image: image} do
33 desc = "Description of the image"
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)
41 assert media["type"] == "image"
42 assert media["description"] == desc
45 object = Object.get_by_id(media["id"])
46 assert object.data["actor"] == User.ap_id(conn.assigns[:user])
49 test "/api/v2/media", %{conn: conn, user: user, image: image} do
50 desc = "Description of the image"
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)
58 assert media_id = response["id"]
60 %{conn: conn} = oauth_access(["read:media"], user: user)
64 |> get("/api/v1/media/#{media_id}")
65 |> json_response_and_validate_schema(200)
67 assert media["type"] == "image"
68 assert media["description"] == desc
71 object = Object.get_by_id(media["id"])
72 assert object.data["actor"] == user.ap_id
75 test "/api/v2/media, upload_limit", %{conn: conn, user: user} do
76 desc = "Description of the binary"
78 upload_limit = Config.get([:instance, :upload_limit]) * 8 + 8
81 File.write(Path.absname("test/tmp/large_binary.data"), <<0::size(upload_limit)>>)
83 large_binary = %Plug.Upload{
85 path: Path.absname("test/tmp/large_binary.data"),
86 filename: "large_binary.data"
89 assert capture_log(fn ->
90 assert %{"error" => "file_too_large"} =
92 |> put_req_header("content-type", "multipart/form-data")
93 |> post("/api/v2/media", %{
94 "file" => large_binary,
97 |> json_response_and_validate_schema(400)
99 "[error] Elixir.Pleroma.Upload store (using Pleroma.Uploaders.Local) failed: :file_too_large"
101 clear_config([:instance, :upload_limit], upload_limit)
105 |> put_req_header("content-type", "multipart/form-data")
106 |> post("/api/v2/media", %{
107 "file" => large_binary,
108 "description" => desc
110 |> json_response_and_validate_schema(202)
112 assert media_id = response["id"]
114 %{conn: conn} = oauth_access(["read:media"], user: user)
118 |> get("/api/v1/media/#{media_id}")
119 |> json_response_and_validate_schema(200)
121 assert media["type"] == "unknown"
122 assert media["description"] == desc
125 assert :ok == File.rm(Path.absname("test/tmp/large_binary.data"))
129 describe "Update media description" do
130 setup do: oauth_access(["write:media"])
132 setup %{user: actor} do
134 content_type: "image/jpeg",
135 path: Path.absname("test/fixtures/image.jpg"),
136 filename: "an_image.jpg"
139 {:ok, %Object{} = object} =
142 actor: User.ap_id(actor),
143 description: "test-m"
149 test "/api/v1/media/:id good request", %{conn: conn, object: object} do
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)
156 assert media["description"] == "test-media"
157 assert refresh_record(object).data["name"] == "test-media"
161 describe "Get media by id (/api/v1/media/:id)" do
162 setup do: oauth_access(["read:media"])
164 setup %{user: actor} do
166 content_type: "image/jpeg",
167 path: Path.absname("test/fixtures/image.jpg"),
168 filename: "an_image.jpg"
171 {:ok, %Object{} = object} =
174 actor: User.ap_id(actor),
175 description: "test-media"
181 test "it returns media object when requested by owner", %{conn: conn, object: object} do
184 |> get("/api/v1/media/#{object.id}")
185 |> json_response_and_validate_schema(:ok)
187 assert media["description"] == "test-media"
188 assert media["type"] == "image"
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"])
195 assert object.data["actor"] == user.ap_id
196 refute user.id == other_user.id
199 |> get("/api/v1/media/#{object.id}")
200 |> json_response_and_validate_schema(403)