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 setup do: oauth_access(["write:media"])
14 describe "Upload media" do
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 |> post("/api/v1/media", %{"file" => image, "description" => desc})
36 assert media["type"] == "image"
37 assert media["description"] == desc
40 object = Object.get_by_id(media["id"])
41 assert object.data["actor"] == User.ap_id(conn.assigns[:user])
44 test "/api/v2/media", %{conn: conn, image: image} do
45 desc = "Description of the image"
49 |> post("/api/v2/media", %{"file" => image, "description" => desc})
52 assert media_id = response["id"]
56 |> get("/api/v1/media/#{media_id}")
59 assert media["type"] == "image"
60 assert media["description"] == desc
62 object = Object.get_by_id(media["id"])
63 assert object.data["actor"] == User.ap_id(conn.assigns[:user])
67 describe "Update media description" do
68 setup %{user: actor} do
70 content_type: "image/jpg",
71 path: Path.absname("test/fixtures/image.jpg"),
72 filename: "an_image.jpg"
75 {:ok, %Object{} = object} =
78 actor: User.ap_id(actor),
85 test "/api/v1/media/:id good request", %{conn: conn, object: object} do
88 |> put("/api/v1/media/#{object.id}", %{"description" => "test-media"})
91 assert media["description"] == "test-media"
92 assert refresh_record(object).data["name"] == "test-media"
95 test "/api/v1/media/:id bad request", %{conn: conn, object: object} do
98 |> put("/api/v1/media/#{object.id}", %{})
101 assert media == %{"error" => "bad_request"}
105 describe "Get media by id" do
106 setup %{user: actor} do
108 content_type: "image/jpg",
109 path: Path.absname("test/fixtures/image.jpg"),
110 filename: "an_image.jpg"
113 {:ok, %Object{} = object} =
116 actor: User.ap_id(actor),
117 description: "test-media"
123 test "/api/v1/media/:id", %{conn: conn, object: object} do
126 |> get("/api/v1/media/#{object.id}")
127 |> json_response(:ok)
129 assert media["description"] == "test-media"
130 assert media["type"] == "image"