d872ff484554df842933be08441b74f6f2827a06
[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 setup do: oauth_access(["write:media"])
13
14 describe "Upload media" do
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 |> post("/api/v1/media", %{"file" => image, "description" => desc})
34 |> json_response(:ok)
35
36 assert media["type"] == "image"
37 assert media["description"] == desc
38 assert media["id"]
39
40 object = Object.get_by_id(media["id"])
41 assert object.data["actor"] == User.ap_id(conn.assigns[:user])
42 end
43
44 test "/api/v2/media", %{conn: conn, image: image} do
45 desc = "Description of the image"
46
47 response =
48 conn
49 |> post("/api/v2/media", %{"file" => image, "description" => desc})
50 |> json_response(202)
51
52 assert media_id = response["id"]
53
54 media =
55 conn
56 |> get("/api/v1/media/#{media_id}")
57 |> json_response(200)
58
59 assert media["type"] == "image"
60 assert media["description"] == desc
61 assert media["id"]
62 object = Object.get_by_id(media["id"])
63 assert object.data["actor"] == User.ap_id(conn.assigns[:user])
64 end
65 end
66
67 describe "Update media description" do
68 setup %{user: actor} do
69 file = %Plug.Upload{
70 content_type: "image/jpg",
71 path: Path.absname("test/fixtures/image.jpg"),
72 filename: "an_image.jpg"
73 }
74
75 {:ok, %Object{} = object} =
76 ActivityPub.upload(
77 file,
78 actor: User.ap_id(actor),
79 description: "test-m"
80 )
81
82 [object: object]
83 end
84
85 test "/api/v1/media/:id good request", %{conn: conn, object: object} do
86 media =
87 conn
88 |> put("/api/v1/media/#{object.id}", %{"description" => "test-media"})
89 |> json_response(:ok)
90
91 assert media["description"] == "test-media"
92 assert refresh_record(object).data["name"] == "test-media"
93 end
94
95 test "/api/v1/media/:id bad request", %{conn: conn, object: object} do
96 media =
97 conn
98 |> put("/api/v1/media/#{object.id}", %{})
99 |> json_response(400)
100
101 assert media == %{"error" => "bad_request"}
102 end
103 end
104
105 describe "Get media by id" do
106 setup %{user: actor} do
107 file = %Plug.Upload{
108 content_type: "image/jpg",
109 path: Path.absname("test/fixtures/image.jpg"),
110 filename: "an_image.jpg"
111 }
112
113 {:ok, %Object{} = object} =
114 ActivityPub.upload(
115 file,
116 actor: User.ap_id(actor),
117 description: "test-media"
118 )
119
120 [object: object]
121 end
122
123 test "/api/v1/media/:id", %{conn: conn, object: object} do
124 media =
125 conn
126 |> get("/api/v1/media/#{object.id}")
127 |> json_response(:ok)
128
129 assert media["description"] == "test-media"
130 assert media["type"] == "image"
131 assert media["id"]
132 end
133 end
134 end