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/jpeg",
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
67 object = Object.get_by_id(media["id"])
68 assert object.data["actor"] == user.ap_id
72 describe "Update media description" do
73 setup do: oauth_access(["write:media"])
75 setup %{user: actor} do
77 content_type: "image/jpeg",
78 path: Path.absname("test/fixtures/image.jpg"),
79 filename: "an_image.jpg"
82 {:ok, %Object{} = object} =
85 actor: User.ap_id(actor),
92 test "/api/v1/media/:id good request", %{conn: conn, object: object} do
95 |> put_req_header("content-type", "multipart/form-data")
96 |> put("/api/v1/media/#{object.id}", %{"description" => "test-media"})
97 |> json_response_and_validate_schema(:ok)
99 assert media["description"] == "test-media"
100 assert refresh_record(object).data["name"] == "test-media"
104 describe "Get media by id (/api/v1/media/:id)" do
105 setup do: oauth_access(["read:media"])
107 setup %{user: actor} do
109 content_type: "image/jpeg",
110 path: Path.absname("test/fixtures/image.jpg"),
111 filename: "an_image.jpg"
114 {:ok, %Object{} = object} =
117 actor: User.ap_id(actor),
118 description: "test-media"
124 test "it returns media object when requested by owner", %{conn: conn, object: object} do
127 |> get("/api/v1/media/#{object.id}")
128 |> json_response_and_validate_schema(:ok)
130 assert media["description"] == "test-media"
131 assert media["type"] == "image"
135 test "it returns 403 if media object requested by non-owner", %{object: object, user: user} do
136 %{conn: conn, user: other_user} = oauth_access(["read:media"])
138 assert object.data["actor"] == user.ap_id
139 refute user.id == other_user.id
142 |> get("/api/v1/media/#{object.id}")
143 |> json_response(403)