98ec239b181238b4f035a68bac3bf5e5d22b6a91
[akkoma] / test / web / mastodon_api / controllers / media_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.MediaControllerTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.Object
9 alias Pleroma.User
10 alias Pleroma.Web.ActivityPub.ActivityPub
11
12 describe "Upload media" do
13 setup do: oauth_access(["write:media"])
14
15 setup do
16 image = %Plug.Upload{
17 content_type: "image/jpg",
18 path: Path.absname("test/fixtures/image.jpg"),
19 filename: "an_image.jpg"
20 }
21
22 [image: image]
23 end
24
25 setup do: clear_config([:media_proxy])
26 setup do: clear_config([Pleroma.Upload])
27
28 test "/api/v1/media", %{conn: conn, image: image} do
29 desc = "Description of the image"
30
31 media =
32 conn
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)
36
37 assert media["type"] == "image"
38 assert media["description"] == desc
39 assert media["id"]
40
41 object = Object.get_by_id(media["id"])
42 assert object.data["actor"] == User.ap_id(conn.assigns[:user])
43 end
44
45 test "/api/v2/media", %{conn: conn, user: user, image: image} do
46 desc = "Description of the image"
47
48 response =
49 conn
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)
53
54 assert media_id = response["id"]
55
56 %{conn: conn} = oauth_access(["read:media"], user: user)
57
58 media =
59 conn
60 |> get("/api/v1/media/#{media_id}")
61 |> json_response_and_validate_schema(200)
62
63 assert media["type"] == "image"
64 assert media["description"] == desc
65 assert media["id"]
66 object = Object.get_by_id(media["id"])
67
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])
70 end
71 end
72
73 describe "Update media description" do
74 setup do: oauth_access(["write:media"])
75
76 setup %{user: actor} do
77 file = %Plug.Upload{
78 content_type: "image/jpg",
79 path: Path.absname("test/fixtures/image.jpg"),
80 filename: "an_image.jpg"
81 }
82
83 {:ok, %Object{} = object} =
84 ActivityPub.upload(
85 file,
86 actor: User.ap_id(actor),
87 description: "test-m"
88 )
89
90 [object: object]
91 end
92
93 test "/api/v1/media/:id good request", %{conn: conn, object: object} do
94 media =
95 conn
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)
99
100 assert media["description"] == "test-media"
101 assert refresh_record(object).data["name"] == "test-media"
102 end
103 end
104
105 describe "Get media by id" do
106 setup do: oauth_access(["read:media"])
107
108 setup %{user: actor} do
109 file = %Plug.Upload{
110 content_type: "image/jpg",
111 path: Path.absname("test/fixtures/image.jpg"),
112 filename: "an_image.jpg"
113 }
114
115 {:ok, %Object{} = object} =
116 ActivityPub.upload(
117 file,
118 actor: User.ap_id(actor),
119 description: "test-media"
120 )
121
122 [object: object]
123 end
124
125 test "/api/v1/media/:id", %{conn: conn, object: object} do
126 media =
127 conn
128 |> get("/api/v1/media/#{object.id}")
129 |> json_response_and_validate_schema(:ok)
130
131 assert media["description"] == "test-media"
132 assert media["type"] == "image"
133 assert media["id"]
134 end
135 end
136 end