5cdca019a0ccc033bedf79d5e210bbe903b10de4
[akkoma] / test / pleroma / web / o_status / o_status_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.OStatus.OStatusControllerTest do
6 use Pleroma.Web.ConnCase
7
8 import Pleroma.Factory
9
10 alias Pleroma.Object
11 alias Pleroma.User
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.Endpoint
15
16 require Pleroma.Constants
17
18 setup_all do
19 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
20 :ok
21 end
22
23 setup do: clear_config([:static_fe, :enabled], false)
24
25 describe "Mastodon compatibility routes" do
26 setup %{conn: conn} do
27 conn = put_req_header(conn, "accept", "text/html")
28
29 {:ok, object} =
30 %{
31 "type" => "Note",
32 "content" => "hey",
33 "id" => Endpoint.url() <> "/users/raymoo/statuses/999999999",
34 "actor" => Endpoint.url() <> "/users/raymoo",
35 "to" => [Pleroma.Constants.as_public()]
36 }
37 |> Object.create()
38
39 {:ok, activity, _} =
40 %{
41 "id" => object.data["id"] <> "/activity",
42 "type" => "Create",
43 "object" => object.data["id"],
44 "actor" => object.data["actor"],
45 "to" => object.data["to"]
46 }
47 |> ActivityPub.persist(local: true)
48
49 %{conn: conn, activity: activity}
50 end
51
52 test "redirects to /notice/:id for html format", %{conn: conn, activity: activity} do
53 conn = get(conn, "/users/raymoo/statuses/999999999")
54 assert redirected_to(conn) == "/notice/#{activity.id}"
55 end
56
57 test "redirects to /notice/:id for html format for activity", %{
58 conn: conn,
59 activity: activity
60 } do
61 conn = get(conn, "/users/raymoo/statuses/999999999/activity")
62 assert redirected_to(conn) == "/notice/#{activity.id}"
63 end
64 end
65
66 # Note: see ActivityPubControllerTest for JSON format tests
67 describe "GET /objects/:uuid (text/html)" do
68 setup %{conn: conn} do
69 conn = put_req_header(conn, "accept", "text/html")
70 %{conn: conn}
71 end
72
73 test "redirects to /notice/id for html format", %{conn: conn} do
74 note_activity = insert(:note_activity)
75 object = Object.normalize(note_activity, fetch: false)
76 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
77 url = "/objects/#{uuid}"
78
79 conn = get(conn, url)
80 assert redirected_to(conn) == "/notice/#{note_activity.id}"
81 end
82
83 test "404s on private objects", %{conn: conn} do
84 note_activity = insert(:direct_note_activity)
85 object = Object.normalize(note_activity, fetch: false)
86 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
87
88 conn
89 |> get("/objects/#{uuid}")
90 |> response(404)
91 end
92
93 test "404s on non-existing objects", %{conn: conn} do
94 conn
95 |> get("/objects/123")
96 |> response(404)
97 end
98 end
99
100 # Note: see ActivityPubControllerTest for JSON format tests
101 describe "GET /activities/:uuid (text/html)" do
102 setup %{conn: conn} do
103 conn = put_req_header(conn, "accept", "text/html")
104 %{conn: conn}
105 end
106
107 test "redirects to /notice/id for html format", %{conn: conn} do
108 note_activity = insert(:note_activity)
109 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
110
111 conn = get(conn, "/activities/#{uuid}")
112 assert redirected_to(conn) == "/notice/#{note_activity.id}"
113 end
114
115 test "404s on private activities", %{conn: conn} do
116 note_activity = insert(:direct_note_activity)
117 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
118
119 conn
120 |> get("/activities/#{uuid}")
121 |> response(404)
122 end
123
124 test "404s on nonexistent activities", %{conn: conn} do
125 conn
126 |> get("/activities/123")
127 |> response(404)
128 end
129 end
130
131 describe "GET notice/2" do
132 test "redirects to a proper object URL when json requested and the object is local", %{
133 conn: conn
134 } do
135 note_activity = insert(:note_activity)
136 expected_redirect_url = Object.normalize(note_activity, fetch: false).data["id"]
137
138 redirect_url =
139 conn
140 |> put_req_header("accept", "application/activity+json")
141 |> get("/notice/#{note_activity.id}")
142 |> redirected_to()
143
144 assert redirect_url == expected_redirect_url
145 end
146
147 test "returns a 404 on remote notice when json requested", %{conn: conn} do
148 note_activity = insert(:note_activity, local: false)
149
150 conn
151 |> put_req_header("accept", "application/activity+json")
152 |> get("/notice/#{note_activity.id}")
153 |> response(404)
154 end
155
156 test "500s when actor not found", %{conn: conn} do
157 note_activity = insert(:note_activity)
158 user = User.get_cached_by_ap_id(note_activity.data["actor"])
159 User.invalidate_cache(user)
160 Pleroma.Repo.delete(user)
161
162 conn =
163 conn
164 |> get("/notice/#{note_activity.id}")
165
166 assert response(conn, 500) == ~S({"error":"Something went wrong"})
167 end
168
169 test "render html for redirect for html format", %{conn: conn} do
170 note_activity = insert(:note_activity)
171
172 resp =
173 conn
174 |> put_req_header("accept", "text/html")
175 |> get("/notice/#{note_activity.id}")
176 |> response(200)
177
178 assert resp =~
179 "<meta content=\"#{Pleroma.Web.base_url()}/notice/#{note_activity.id}\" property=\"og:url\">"
180
181 user = insert(:user)
182
183 {:ok, like_activity} = CommonAPI.favorite(user, note_activity.id)
184
185 assert like_activity.data["type"] == "Like"
186
187 resp =
188 conn
189 |> put_req_header("accept", "text/html")
190 |> get("/notice/#{like_activity.id}")
191 |> response(200)
192
193 assert resp =~ "<!--server-generated-meta-->"
194 end
195
196 test "404s a private notice", %{conn: conn} do
197 note_activity = insert(:direct_note_activity)
198 url = "/notice/#{note_activity.id}"
199
200 conn =
201 conn
202 |> get(url)
203
204 assert response(conn, 404)
205 end
206
207 test "404s a non-existing notice", %{conn: conn} do
208 url = "/notice/123"
209
210 conn =
211 conn
212 |> get(url)
213
214 assert response(conn, 404)
215 end
216
217 test "does not require authentication on non-federating instances", %{
218 conn: conn
219 } do
220 clear_config([:instance, :federating], false)
221 note_activity = insert(:note_activity)
222
223 conn
224 |> put_req_header("accept", "text/html")
225 |> get("/notice/#{note_activity.id}")
226 |> response(200)
227 end
228 end
229
230 describe "GET /notice/:id/embed_player" do
231 setup do
232 note_activity = insert(:note_activity)
233 object = Pleroma.Object.normalize(note_activity, fetch: false)
234
235 object_data =
236 Map.put(object.data, "attachment", [
237 %{
238 "url" => [
239 %{
240 "href" =>
241 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
242 "mediaType" => "video/mp4",
243 "type" => "Link"
244 }
245 ]
246 }
247 ])
248
249 object
250 |> Ecto.Changeset.change(data: object_data)
251 |> Pleroma.Repo.update()
252
253 %{note_activity: note_activity}
254 end
255
256 test "renders embed player", %{conn: conn, note_activity: note_activity} do
257 conn = get(conn, "/notice/#{note_activity.id}/embed_player")
258
259 assert Plug.Conn.get_resp_header(conn, "x-frame-options") == ["ALLOW"]
260
261 assert Plug.Conn.get_resp_header(
262 conn,
263 "content-security-policy"
264 ) == [
265 "default-src 'none';style-src 'self' 'unsafe-inline';img-src 'self' data: https:; media-src 'self' https:;"
266 ]
267
268 assert response(conn, 200) =~
269 "<video controls loop><source src=\"https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4\" type=\"video/mp4\">Your browser does not support video/mp4 playback.</video>"
270 end
271
272 test "404s when activity isn't create", %{conn: conn} do
273 note_activity = insert(:note_activity, data_attrs: %{"type" => "Like"})
274
275 assert conn
276 |> get("/notice/#{note_activity.id}/embed_player")
277 |> response(404)
278 end
279
280 test "404s when activity is direct message", %{conn: conn} do
281 note_activity = insert(:note_activity, data_attrs: %{"directMessage" => true})
282
283 assert conn
284 |> get("/notice/#{note_activity.id}/embed_player")
285 |> response(404)
286 end
287
288 test "404s when attachment is empty", %{conn: conn} do
289 note_activity = insert(:note_activity)
290 object = Pleroma.Object.normalize(note_activity, fetch: false)
291 object_data = Map.put(object.data, "attachment", [])
292
293 object
294 |> Ecto.Changeset.change(data: object_data)
295 |> Pleroma.Repo.update()
296
297 assert conn
298 |> get("/notice/#{note_activity.id}/embed_player")
299 |> response(404)
300 end
301
302 test "404s when attachment isn't audio or video", %{conn: conn} do
303 note_activity = insert(:note_activity)
304 object = Pleroma.Object.normalize(note_activity, fetch: false)
305
306 object_data =
307 Map.put(object.data, "attachment", [
308 %{
309 "url" => [
310 %{
311 "href" => "https://peertube.moe/static/webseed/480.jpg",
312 "mediaType" => "image/jpg",
313 "type" => "Link"
314 }
315 ]
316 }
317 ])
318
319 object
320 |> Ecto.Changeset.change(data: object_data)
321 |> Pleroma.Repo.update()
322
323 conn
324 |> get("/notice/#{note_activity.id}/embed_player")
325 |> response(404)
326 end
327
328 test "does not require authentication on non-federating instances", %{
329 conn: conn,
330 note_activity: note_activity
331 } do
332 clear_config([:instance, :federating], false)
333
334 conn
335 |> put_req_header("accept", "text/html")
336 |> get("/notice/#{note_activity.id}/embed_player")
337 |> response(200)
338 end
339 end
340 end