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")
32 test "redirects to /notice/:id for html format", %{conn: conn} do
37 "id" => Endpoint.url() <> "/users/raymoo/statuses/999999999",
38 "actor" => Endpoint.url() <> "/users/raymoo",
39 "to" => [Pleroma.Constants.as_public()]
45 "id" => object.data["id"] <> "/activity",
47 "object" => object.data["id"],
48 "actor" => object.data["actor"],
49 "to" => object.data["to"]
51 |> ActivityPub.persist(local: true)
53 conn = get(conn, "/users/raymoo/statuses/999999999")
54 assert redirected_to(conn) == "/notice/#{activity.id}"
58 # Note: see ActivityPubControllerTest for JSON format tests
59 describe "GET /objects/:uuid (text/html)" do
60 setup %{conn: conn} do
61 conn = put_req_header(conn, "accept", "text/html")
65 test "redirects to /notice/id for html format", %{conn: conn} do
66 note_activity = insert(:note_activity)
67 object = Object.normalize(note_activity)
68 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
69 url = "/objects/#{uuid}"
72 assert redirected_to(conn) == "/notice/#{note_activity.id}"
75 test "404s on private objects", %{conn: conn} do
76 note_activity = insert(:direct_note_activity)
77 object = Object.normalize(note_activity)
78 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
81 |> get("/objects/#{uuid}")
85 test "404s on non-existing objects", %{conn: conn} do
87 |> get("/objects/123")
92 # Note: see ActivityPubControllerTest for JSON format tests
93 describe "GET /activities/:uuid (text/html)" do
94 setup %{conn: conn} do
95 conn = put_req_header(conn, "accept", "text/html")
99 test "redirects to /notice/id for html format", %{conn: conn} do
100 note_activity = insert(:note_activity)
101 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
103 conn = get(conn, "/activities/#{uuid}")
104 assert redirected_to(conn) == "/notice/#{note_activity.id}"
107 test "404s on private activities", %{conn: conn} do
108 note_activity = insert(:direct_note_activity)
109 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
112 |> get("/activities/#{uuid}")
116 test "404s on nonexistent activities", %{conn: conn} do
118 |> get("/activities/123")
123 describe "GET notice/2" do
124 test "redirects to a proper object URL when json requested and the object is local", %{
127 note_activity = insert(:note_activity)
128 expected_redirect_url = Object.normalize(note_activity).data["id"]
132 |> put_req_header("accept", "application/activity+json")
133 |> get("/notice/#{note_activity.id}")
136 assert redirect_url == expected_redirect_url
139 test "returns a 404 on remote notice when json requested", %{conn: conn} do
140 note_activity = insert(:note_activity, local: false)
143 |> put_req_header("accept", "application/activity+json")
144 |> get("/notice/#{note_activity.id}")
148 test "500s when actor not found", %{conn: conn} do
149 note_activity = insert(:note_activity)
150 user = User.get_cached_by_ap_id(note_activity.data["actor"])
151 User.invalidate_cache(user)
152 Pleroma.Repo.delete(user)
156 |> get("/notice/#{note_activity.id}")
158 assert response(conn, 500) == ~S({"error":"Something went wrong"})
161 test "render html for redirect for html format", %{conn: conn} do
162 note_activity = insert(:note_activity)
166 |> put_req_header("accept", "text/html")
167 |> get("/notice/#{note_activity.id}")
171 "<meta content=\"#{Pleroma.Web.base_url()}/notice/#{note_activity.id}\" property=\"og:url\">"
175 {:ok, like_activity} = CommonAPI.favorite(user, note_activity.id)
177 assert like_activity.data["type"] == "Like"
181 |> put_req_header("accept", "text/html")
182 |> get("/notice/#{like_activity.id}")
185 assert resp =~ "<!--server-generated-meta-->"
188 test "404s a private notice", %{conn: conn} do
189 note_activity = insert(:direct_note_activity)
190 url = "/notice/#{note_activity.id}"
196 assert response(conn, 404)
199 test "404s a non-existing notice", %{conn: conn} do
206 assert response(conn, 404)
209 test "it requires authentication if instance is NOT federating", %{
213 note_activity = insert(:note_activity)
215 conn = put_req_header(conn, "accept", "text/html")
217 ensure_federating_or_authenticated(conn, "/notice/#{note_activity.id}", user)
221 describe "GET /notice/:id/embed_player" do
223 note_activity = insert(:note_activity)
224 object = Pleroma.Object.normalize(note_activity)
227 Map.put(object.data, "attachment", [
232 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
233 "mediaType" => "video/mp4",
241 |> Ecto.Changeset.change(data: object_data)
242 |> Pleroma.Repo.update()
244 %{note_activity: note_activity}
247 test "renders embed player", %{conn: conn, note_activity: note_activity} do
248 conn = get(conn, "/notice/#{note_activity.id}/embed_player")
250 assert Plug.Conn.get_resp_header(conn, "x-frame-options") == ["ALLOW"]
252 assert Plug.Conn.get_resp_header(
254 "content-security-policy"
256 "default-src 'none';style-src 'self' 'unsafe-inline';img-src 'self' data: https:; media-src 'self' https:;"
259 assert response(conn, 200) =~
260 "<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>"
263 test "404s when activity isn't create", %{conn: conn} do
264 note_activity = insert(:note_activity, data_attrs: %{"type" => "Like"})
267 |> get("/notice/#{note_activity.id}/embed_player")
271 test "404s when activity is direct message", %{conn: conn} do
272 note_activity = insert(:note_activity, data_attrs: %{"directMessage" => true})
275 |> get("/notice/#{note_activity.id}/embed_player")
279 test "404s when attachment is empty", %{conn: conn} do
280 note_activity = insert(:note_activity)
281 object = Pleroma.Object.normalize(note_activity)
282 object_data = Map.put(object.data, "attachment", [])
285 |> Ecto.Changeset.change(data: object_data)
286 |> Pleroma.Repo.update()
289 |> get("/notice/#{note_activity.id}/embed_player")
293 test "404s when attachment isn't audio or video", %{conn: conn} do
294 note_activity = insert(:note_activity)
295 object = Pleroma.Object.normalize(note_activity)
298 Map.put(object.data, "attachment", [
302 "href" => "https://peertube.moe/static/webseed/480.jpg",
303 "mediaType" => "image/jpg",
311 |> Ecto.Changeset.change(data: object_data)
312 |> Pleroma.Repo.update()
315 |> get("/notice/#{note_activity.id}/embed_player")
319 test "it requires authentication if instance is NOT federating", %{
321 note_activity: note_activity
324 conn = put_req_header(conn, "accept", "text/html")
326 ensure_federating_or_authenticated(conn, "/notice/#{note_activity.id}/embed_player", user)