39d7f99f6596c01d3510a6d6689b994fc80bec67
[akkoma] / test / pleroma / web / mastodon_api / controllers / media_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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/jpeg",
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
67 object = Object.get_by_id(media["id"])
68 assert object.data["actor"] == user.ap_id
69 end
70 end
71
72 describe "Update media description" do
73 setup do: oauth_access(["write:media"])
74
75 setup %{user: actor} do
76 file = %Plug.Upload{
77 content_type: "image/jpeg",
78 path: Path.absname("test/fixtures/image.jpg"),
79 filename: "an_image.jpg"
80 }
81
82 {:ok, %Object{} = object} =
83 ActivityPub.upload(
84 file,
85 actor: User.ap_id(actor),
86 description: "test-m"
87 )
88
89 [object: object]
90 end
91
92 test "/api/v1/media/:id good request", %{conn: conn, object: object} do
93 media =
94 conn
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)
98
99 assert media["description"] == "test-media"
100 assert refresh_record(object).data["name"] == "test-media"
101 end
102 end
103
104 describe "Get media by id (/api/v1/media/:id)" do
105 setup do: oauth_access(["read:media"])
106
107 setup %{user: actor} do
108 file = %Plug.Upload{
109 content_type: "image/jpeg",
110 path: Path.absname("test/fixtures/image.jpg"),
111 filename: "an_image.jpg"
112 }
113
114 {:ok, %Object{} = object} =
115 ActivityPub.upload(
116 file,
117 actor: User.ap_id(actor),
118 description: "test-media"
119 )
120
121 [object: object]
122 end
123
124 test "it returns media object when requested by owner", %{conn: conn, object: object} do
125 media =
126 conn
127 |> get("/api/v1/media/#{object.id}")
128 |> json_response_and_validate_schema(:ok)
129
130 assert media["description"] == "test-media"
131 assert media["type"] == "image"
132 assert media["id"]
133 end
134
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"])
137
138 assert object.data["actor"] == user.ap_id
139 refute user.id == other_user.id
140
141 conn
142 |> get("/api/v1/media/#{object.id}")
143 |> json_response_and_validate_schema(403)
144 end
145 end
146 end