7ba1727f2caf5d58b811a2e4f10d25890cc7ac36
[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 |> 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, 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 media =
57 conn
58 |> get("/api/v1/media/#{media_id}")
59 |> json_response_and_validate_schema(200)
60
61 assert media["type"] == "image"
62 assert media["description"] == desc
63 assert media["id"]
64 object = Object.get_by_id(media["id"])
65 assert object.data["actor"] == User.ap_id(conn.assigns[:user])
66 end
67 end
68
69 describe "Update media description" do
70 setup %{user: actor} do
71 file = %Plug.Upload{
72 content_type: "image/jpg",
73 path: Path.absname("test/fixtures/image.jpg"),
74 filename: "an_image.jpg"
75 }
76
77 {:ok, %Object{} = object} =
78 ActivityPub.upload(
79 file,
80 actor: User.ap_id(actor),
81 description: "test-m"
82 )
83
84 [object: object]
85 end
86
87 test "/api/v1/media/:id good request", %{conn: conn, object: object} do
88 media =
89 conn
90 |> put_req_header("content-type", "multipart/form-data")
91 |> put("/api/v1/media/#{object.id}", %{"description" => "test-media"})
92 |> json_response_and_validate_schema(:ok)
93
94 assert media["description"] == "test-media"
95 assert refresh_record(object).data["name"] == "test-media"
96 end
97 end
98
99 describe "Get media by id" do
100 setup %{user: actor} do
101 file = %Plug.Upload{
102 content_type: "image/jpg",
103 path: Path.absname("test/fixtures/image.jpg"),
104 filename: "an_image.jpg"
105 }
106
107 {:ok, %Object{} = object} =
108 ActivityPub.upload(
109 file,
110 actor: User.ap_id(actor),
111 description: "test-media"
112 )
113
114 [object: object]
115 end
116
117 test "/api/v1/media/:id", %{conn: conn, object: object} do
118 media =
119 conn
120 |> get("/api/v1/media/#{object.id}")
121 |> json_response_and_validate_schema(:ok)
122
123 assert media["description"] == "test-media"
124 assert media["type"] == "image"
125 assert media["id"]
126 end
127 end
128 end