1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
13 alias Pleroma.Web.ActivityPub.ActivityPub
14 alias Pleroma.Web.CommonAPI
15 alias Pleroma.Web.Endpoint
17 require Pleroma.Constants
20 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
24 setup do: clear_config([:instance, :federating], true)
26 describe "Mastodon compatibility routes" do
27 setup %{conn: conn} do
28 conn = put_req_header(conn, "accept", "text/html")
34 "id" => Endpoint.url() <> "/users/raymoo/statuses/999999999",
35 "actor" => Endpoint.url() <> "/users/raymoo",
36 "to" => [Pleroma.Constants.as_public()]
42 "id" => object.data["id"] <> "/activity",
44 "object" => object.data["id"],
45 "actor" => object.data["actor"],
46 "to" => object.data["to"]
48 |> ActivityPub.persist(local: true)
50 %{conn: conn, activity: activity}
53 test "redirects to /notice/:id for html format", %{conn: conn, activity: activity} do
54 conn = get(conn, "/users/raymoo/statuses/999999999")
55 assert redirected_to(conn) == "/notice/#{activity.id}"
58 test "redirects to /notice/:id for html format for activity", %{
62 conn = get(conn, "/users/raymoo/statuses/999999999/activity")
63 assert redirected_to(conn) == "/notice/#{activity.id}"
67 # Note: see ActivityPubControllerTest for JSON format tests
68 describe "GET /objects/:uuid (text/html)" do
69 setup %{conn: conn} do
70 conn = put_req_header(conn, "accept", "text/html")
74 test "redirects to /notice/id for html format", %{conn: conn} do
75 note_activity = insert(:note_activity)
76 object = Object.normalize(note_activity)
77 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
78 url = "/objects/#{uuid}"
81 assert redirected_to(conn) == "/notice/#{note_activity.id}"
84 test "404s on private objects", %{conn: conn} do
85 note_activity = insert(:direct_note_activity)
86 object = Object.normalize(note_activity)
87 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
90 |> get("/objects/#{uuid}")
94 test "404s on non-existing objects", %{conn: conn} do
96 |> get("/objects/123")
101 # Note: see ActivityPubControllerTest for JSON format tests
102 describe "GET /activities/:uuid (text/html)" do
103 setup %{conn: conn} do
104 conn = put_req_header(conn, "accept", "text/html")
108 test "redirects to /notice/id for html format", %{conn: conn} do
109 note_activity = insert(:note_activity)
110 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
112 conn = get(conn, "/activities/#{uuid}")
113 assert redirected_to(conn) == "/notice/#{note_activity.id}"
116 test "404s on private activities", %{conn: conn} do
117 note_activity = insert(:direct_note_activity)
118 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
121 |> get("/activities/#{uuid}")
125 test "404s on nonexistent activities", %{conn: conn} do
127 |> get("/activities/123")
132 describe "GET notice/2" do
133 test "redirects to a proper object URL when json requested and the object is local", %{
136 note_activity = insert(:note_activity)
137 expected_redirect_url = Object.normalize(note_activity).data["id"]
141 |> put_req_header("accept", "application/activity+json")
142 |> get("/notice/#{note_activity.id}")
145 assert redirect_url == expected_redirect_url
148 test "returns a 404 on remote notice when json requested", %{conn: conn} do
149 note_activity = insert(:note_activity, local: false)
152 |> put_req_header("accept", "application/activity+json")
153 |> get("/notice/#{note_activity.id}")
157 test "500s when actor not found", %{conn: conn} do
158 note_activity = insert(:note_activity)
159 user = User.get_cached_by_ap_id(note_activity.data["actor"])
160 User.invalidate_cache(user)
161 Pleroma.Repo.delete(user)
165 |> get("/notice/#{note_activity.id}")
167 assert response(conn, 500) == ~S({"error":"Something went wrong"})
170 test "render html for redirect for html format", %{conn: conn} do
171 note_activity = insert(:note_activity)
175 |> put_req_header("accept", "text/html")
176 |> get("/notice/#{note_activity.id}")
180 "<meta content=\"#{Pleroma.Web.base_url()}/notice/#{note_activity.id}\" property=\"og:url\">"
184 {:ok, like_activity} = CommonAPI.favorite(user, note_activity.id)
186 assert like_activity.data["type"] == "Like"
190 |> put_req_header("accept", "text/html")
191 |> get("/notice/#{like_activity.id}")
194 assert resp =~ "<!--server-generated-meta-->"
197 test "404s a private notice", %{conn: conn} do
198 note_activity = insert(:direct_note_activity)
199 url = "/notice/#{note_activity.id}"
205 assert response(conn, 404)
208 test "404s a non-existing notice", %{conn: conn} do
215 assert response(conn, 404)
218 test "it requires authentication if instance is NOT federating", %{
222 note_activity = insert(:note_activity)
224 conn = put_req_header(conn, "accept", "text/html")
226 ensure_federating_or_authenticated(conn, "/notice/#{note_activity.id}", user)
230 describe "GET /notice/:id/embed_player" do
232 note_activity = insert(:note_activity)
233 object = Pleroma.Object.normalize(note_activity)
236 Map.put(object.data, "attachment", [
241 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
242 "mediaType" => "video/mp4",
250 |> Ecto.Changeset.change(data: object_data)
251 |> Pleroma.Repo.update()
253 %{note_activity: note_activity}
256 test "renders embed player", %{conn: conn, note_activity: note_activity} do
257 conn = get(conn, "/notice/#{note_activity.id}/embed_player")
259 assert Plug.Conn.get_resp_header(conn, "x-frame-options") == ["ALLOW"]
261 assert Plug.Conn.get_resp_header(
263 "content-security-policy"
265 "default-src 'none';style-src 'self' 'unsafe-inline';img-src 'self' data: https:; media-src 'self' https:;"
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>"
272 test "404s when activity isn't create", %{conn: conn} do
273 note_activity = insert(:note_activity, data_attrs: %{"type" => "Like"})
276 |> get("/notice/#{note_activity.id}/embed_player")
280 test "404s when activity is direct message", %{conn: conn} do
281 note_activity = insert(:note_activity, data_attrs: %{"directMessage" => true})
284 |> get("/notice/#{note_activity.id}/embed_player")
288 test "404s when attachment is empty", %{conn: conn} do
289 note_activity = insert(:note_activity)
290 object = Pleroma.Object.normalize(note_activity)
291 object_data = Map.put(object.data, "attachment", [])
294 |> Ecto.Changeset.change(data: object_data)
295 |> Pleroma.Repo.update()
298 |> get("/notice/#{note_activity.id}/embed_player")
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)
307 Map.put(object.data, "attachment", [
311 "href" => "https://peertube.moe/static/webseed/480.jpg",
312 "mediaType" => "image/jpg",
320 |> Ecto.Changeset.change(data: object_data)
321 |> Pleroma.Repo.update()
324 |> get("/notice/#{note_activity.id}/embed_player")
328 test "it requires authentication if instance is NOT federating", %{
330 note_activity: note_activity
333 conn = put_req_header(conn, "accept", "text/html")
335 ensure_federating_or_authenticated(conn, "/notice/#{note_activity.id}/embed_player", user)