1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
10 alias Pleroma.Web.ActivityPub.ActivityPub
12 describe "Upload media" do
13 setup do: oauth_access(["write:media"])
17 content_type: "image/jpg",
18 path: Path.absname("test/fixtures/image.jpg"),
19 filename: "an_image.jpg"
25 setup do: clear_config([:media_proxy])
26 setup do: clear_config([Pleroma.Upload])
28 test "/api/v1/media", %{conn: conn, image: image} do
29 desc = "Description of the image"
33 |> put_req_header("content-type", "multipart/form-data")
34 |> post("/api/v1/media", %{"file" => image, "description" => desc})
35 |> json_response_and_validate_schema(:ok)
37 assert media["type"] == "image"
38 assert media["description"] == desc
41 object = Object.get_by_id(media["id"])
42 assert object.data["actor"] == User.ap_id(conn.assigns[:user])
45 test "/api/v2/media", %{conn: conn, user: user, image: image} do
46 desc = "Description of the image"
50 |> put_req_header("content-type", "multipart/form-data")
51 |> post("/api/v2/media", %{"file" => image, "description" => desc})
52 |> json_response_and_validate_schema(202)
54 assert media_id = response["id"]
56 %{conn: conn} = oauth_access(["read:media"], user: user)
60 |> get("/api/v1/media/#{media_id}")
61 |> json_response_and_validate_schema(200)
63 assert media["type"] == "image"
64 assert media["description"] == desc
66 object = Object.get_by_id(media["id"])
68 # TODO: clarify: if this EP allows access to non-owned objects, the following may be false:
69 assert object.data["actor"] == User.ap_id(conn.assigns[:user])
73 describe "Update media description" do
74 setup do: oauth_access(["write:media"])
76 setup %{user: actor} do
78 content_type: "image/jpg",
79 path: Path.absname("test/fixtures/image.jpg"),
80 filename: "an_image.jpg"
83 {:ok, %Object{} = object} =
86 actor: User.ap_id(actor),
93 test "/api/v1/media/:id good request", %{conn: conn, object: object} do
96 |> put_req_header("content-type", "multipart/form-data")
97 |> put("/api/v1/media/#{object.id}", %{"description" => "test-media"})
98 |> json_response_and_validate_schema(:ok)
100 assert media["description"] == "test-media"
101 assert refresh_record(object).data["name"] == "test-media"
105 describe "Get media by id" do
106 setup do: oauth_access(["read:media"])
108 setup %{user: actor} do
110 content_type: "image/jpg",
111 path: Path.absname("test/fixtures/image.jpg"),
112 filename: "an_image.jpg"
115 {:ok, %Object{} = object} =
118 actor: User.ap_id(actor),
119 description: "test-media"
125 test "/api/v1/media/:id", %{conn: conn, object: object} do
128 |> get("/api/v1/media/#{object.id}")
129 |> json_response_and_validate_schema(:ok)
131 assert media["description"] == "test-media"
132 assert media["type"] == "image"