1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.OStatus.OStatusControllerTest do
6 use Pleroma.Web.ConnCase
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.Endpoint
16 require Pleroma.Constants
19 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
23 setup do: clear_config([:static_fe, :enabled], false)
25 describe "Mastodon compatibility routes" do
26 setup %{conn: conn} do
27 conn = put_req_header(conn, "accept", "text/html")
33 "id" => Endpoint.url() <> "/users/raymoo/statuses/999999999",
34 "actor" => Endpoint.url() <> "/users/raymoo",
35 "to" => [Pleroma.Constants.as_public()]
41 "id" => object.data["id"] <> "/activity",
43 "object" => object.data["id"],
44 "actor" => object.data["actor"],
45 "to" => object.data["to"]
47 |> ActivityPub.persist(local: true)
49 %{conn: conn, activity: activity}
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}"
57 test "redirects to /notice/:id for html format for activity", %{
61 conn = get(conn, "/users/raymoo/statuses/999999999/activity")
62 assert redirected_to(conn) == "/notice/#{activity.id}"
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")
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}"
80 assert redirected_to(conn) == "/notice/#{note_activity.id}"
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"]))
89 |> get("/objects/#{uuid}")
93 test "404s on non-existing objects", %{conn: conn} do
95 |> get("/objects/123")
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")
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"]))
111 conn = get(conn, "/activities/#{uuid}")
112 assert redirected_to(conn) == "/notice/#{note_activity.id}"
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"]))
120 |> get("/activities/#{uuid}")
124 test "404s on nonexistent activities", %{conn: conn} do
126 |> get("/activities/123")
131 describe "GET notice/2" do
132 test "redirects to a proper object URL when json requested and the object is local", %{
135 note_activity = insert(:note_activity)
136 expected_redirect_url = Object.normalize(note_activity, fetch: false).data["id"]
140 |> put_req_header("accept", "application/activity+json")
141 |> get("/notice/#{note_activity.id}")
144 assert redirect_url == expected_redirect_url
147 test "redirects to a proper object URL when json requested and the object is remote", %{
150 note_activity = insert(:note_activity, local: false)
151 expected_redirect_url = Object.normalize(note_activity, fetch: false).data["id"]
155 |> put_req_header("accept", "application/activity+json")
156 |> get("/notice/#{note_activity.id}")
159 assert redirect_url == expected_redirect_url
162 test "500s when actor not found", %{conn: conn} do
163 note_activity = insert(:note_activity)
164 user = User.get_cached_by_ap_id(note_activity.data["actor"])
165 User.invalidate_cache(user)
166 Pleroma.Repo.delete(user)
170 |> get("/notice/#{note_activity.id}")
172 assert response(conn, 500) == ~S({"error":"Something went wrong"})
175 test "render html for redirect for html format", %{conn: conn} do
176 note_activity = insert(:note_activity)
180 |> put_req_header("accept", "text/html")
181 |> get("/notice/#{note_activity.id}")
185 "<meta content=\"#{Pleroma.Web.Endpoint.url()}/notice/#{note_activity.id}\" property=\"og:url\">"
189 {:ok, like_activity} = CommonAPI.favorite(user, note_activity.id)
191 assert like_activity.data["type"] == "Like"
195 |> put_req_header("accept", "text/html")
196 |> get("/notice/#{like_activity.id}")
199 assert resp =~ "<!--server-generated-meta-->"
202 test "404s a private notice", %{conn: conn} do
203 note_activity = insert(:direct_note_activity)
204 url = "/notice/#{note_activity.id}"
210 assert response(conn, 404)
213 test "404s a non-existing notice", %{conn: conn} do
220 assert response(conn, 404)
223 test "does not require authentication on non-federating instances", %{
226 clear_config([:instance, :federating], false)
227 note_activity = insert(:note_activity)
230 |> put_req_header("accept", "text/html")
231 |> get("/notice/#{note_activity.id}")
236 describe "GET /notice/:id/embed_player" do
238 note_activity = insert(:note_activity)
239 object = Pleroma.Object.normalize(note_activity, fetch: false)
242 Map.put(object.data, "attachment", [
247 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
248 "mediaType" => "video/mp4",
256 |> Ecto.Changeset.change(data: object_data)
257 |> Pleroma.Repo.update()
259 %{note_activity: note_activity}
262 test "renders embed player", %{conn: conn, note_activity: note_activity} do
263 conn = get(conn, "/notice/#{note_activity.id}/embed_player")
265 assert Plug.Conn.get_resp_header(conn, "x-frame-options") == ["ALLOW"]
267 assert Plug.Conn.get_resp_header(
269 "content-security-policy"
271 "default-src 'none';style-src 'self' 'unsafe-inline';img-src 'self' data: https:; media-src 'self' https:;"
274 assert response(conn, 200) =~
275 "<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>"
278 test "404s when activity isn't create", %{conn: conn} do
279 note_activity = insert(:note_activity, data_attrs: %{"type" => "Like"})
282 |> get("/notice/#{note_activity.id}/embed_player")
286 test "404s when activity is direct message", %{conn: conn} do
287 note_activity = insert(:note_activity, data_attrs: %{"directMessage" => true})
290 |> get("/notice/#{note_activity.id}/embed_player")
294 test "404s when attachment is empty", %{conn: conn} do
295 note_activity = insert(:note_activity)
296 object = Pleroma.Object.normalize(note_activity, fetch: false)
297 object_data = Map.put(object.data, "attachment", [])
300 |> Ecto.Changeset.change(data: object_data)
301 |> Pleroma.Repo.update()
304 |> get("/notice/#{note_activity.id}/embed_player")
308 test "404s when attachment isn't audio or video", %{conn: conn} do
309 note_activity = insert(:note_activity)
310 object = Pleroma.Object.normalize(note_activity, fetch: false)
313 Map.put(object.data, "attachment", [
317 "href" => "https://peertube.moe/static/webseed/480.jpg",
318 "mediaType" => "image/jpg",
326 |> Ecto.Changeset.change(data: object_data)
327 |> Pleroma.Repo.update()
330 |> get("/notice/#{note_activity.id}/embed_player")
334 test "does not require authentication on non-federating instances", %{
336 note_activity: note_activity
338 clear_config([:instance, :federating], false)
341 |> put_req_header("accept", "text/html")
342 |> get("/notice/#{note_activity.id}/embed_player")
347 describe "notice compatibility routes" do
348 test "Soapbox FE", %{conn: conn} do
350 note_activity = insert(:note_activity, user: user)
354 |> put_req_header("accept", "text/html")
355 |> get("/@#{user.nickname}/posts/#{note_activity.id}")
359 "<meta content=\"#{Endpoint.url()}/notice/#{note_activity.id}\" property=\"og:url\">"
361 assert resp =~ expected
364 test "Mastodon", %{conn: conn} do
366 note_activity = insert(:note_activity, user: user)
370 |> put_req_header("accept", "text/html")
371 |> get("/@#{user.nickname}/#{note_activity.id}")
375 "<meta content=\"#{Endpoint.url()}/notice/#{note_activity.id}\" property=\"og:url\">"
377 assert resp =~ expected
380 test "Twitter", %{conn: conn} do
382 note_activity = insert(:note_activity, user: user)
386 |> put_req_header("accept", "text/html")
387 |> get("/#{user.nickname}/status/#{note_activity.id}")
391 "<meta content=\"#{Endpoint.url()}/notice/#{note_activity.id}\" property=\"og:url\">"
393 assert resp =~ expected