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()]
46 "object" => object.data["id"],
47 "actor" => object.data["actor"],
48 "to" => object.data["to"]
50 |> ActivityPub.persist(local: true)
52 conn = get(conn, "/users/raymoo/statuses/999999999")
53 assert redirected_to(conn) == "/notice/#{activity.id}"
57 # Note: see ActivityPubControllerTest for JSON format tests
58 describe "GET /objects/:uuid (text/html)" do
59 setup %{conn: conn} do
60 conn = put_req_header(conn, "accept", "text/html")
64 test "redirects to /notice/id for html format", %{conn: conn} do
65 note_activity = insert(:note_activity)
66 object = Object.normalize(note_activity)
67 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
68 url = "/objects/#{uuid}"
71 assert redirected_to(conn) == "/notice/#{note_activity.id}"
74 test "404s on private objects", %{conn: conn} do
75 note_activity = insert(:direct_note_activity)
76 object = Object.normalize(note_activity)
77 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
80 |> get("/objects/#{uuid}")
84 test "404s on non-existing objects", %{conn: conn} do
86 |> get("/objects/123")
91 # Note: see ActivityPubControllerTest for JSON format tests
92 describe "GET /activities/:uuid (text/html)" do
93 setup %{conn: conn} do
94 conn = put_req_header(conn, "accept", "text/html")
98 test "redirects to /notice/id for html format", %{conn: conn} do
99 note_activity = insert(:note_activity)
100 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
102 conn = get(conn, "/activities/#{uuid}")
103 assert redirected_to(conn) == "/notice/#{note_activity.id}"
106 test "404s on private activities", %{conn: conn} do
107 note_activity = insert(:direct_note_activity)
108 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
111 |> get("/activities/#{uuid}")
115 test "404s on nonexistent activities", %{conn: conn} do
117 |> get("/activities/123")
122 describe "GET notice/2" do
123 test "redirects to a proper object URL when json requested and the object is local", %{
126 note_activity = insert(:note_activity)
127 expected_redirect_url = Object.normalize(note_activity).data["id"]
131 |> put_req_header("accept", "application/activity+json")
132 |> get("/notice/#{note_activity.id}")
135 assert redirect_url == expected_redirect_url
138 test "returns a 404 on remote notice when json requested", %{conn: conn} do
139 note_activity = insert(:note_activity, local: false)
142 |> put_req_header("accept", "application/activity+json")
143 |> get("/notice/#{note_activity.id}")
147 test "500s when actor not found", %{conn: conn} do
148 note_activity = insert(:note_activity)
149 user = User.get_cached_by_ap_id(note_activity.data["actor"])
150 User.invalidate_cache(user)
151 Pleroma.Repo.delete(user)
155 |> get("/notice/#{note_activity.id}")
157 assert response(conn, 500) == ~S({"error":"Something went wrong"})
160 test "render html for redirect for html format", %{conn: conn} do
161 note_activity = insert(:note_activity)
165 |> put_req_header("accept", "text/html")
166 |> get("/notice/#{note_activity.id}")
170 "<meta content=\"#{Pleroma.Web.base_url()}/notice/#{note_activity.id}\" property=\"og:url\">"
174 {:ok, like_activity} = CommonAPI.favorite(user, note_activity.id)
176 assert like_activity.data["type"] == "Like"
180 |> put_req_header("accept", "text/html")
181 |> get("/notice/#{like_activity.id}")
184 assert resp =~ "<!--server-generated-meta-->"
187 test "404s a private notice", %{conn: conn} do
188 note_activity = insert(:direct_note_activity)
189 url = "/notice/#{note_activity.id}"
195 assert response(conn, 404)
198 test "404s a non-existing notice", %{conn: conn} do
205 assert response(conn, 404)
208 test "it requires authentication if instance is NOT federating", %{
212 note_activity = insert(:note_activity)
214 conn = put_req_header(conn, "accept", "text/html")
216 ensure_federating_or_authenticated(conn, "/notice/#{note_activity.id}", user)
220 describe "GET /notice/:id/embed_player" do
222 note_activity = insert(:note_activity)
223 object = Pleroma.Object.normalize(note_activity)
226 Map.put(object.data, "attachment", [
231 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
232 "mediaType" => "video/mp4",
240 |> Ecto.Changeset.change(data: object_data)
241 |> Pleroma.Repo.update()
243 %{note_activity: note_activity}
246 test "renders embed player", %{conn: conn, note_activity: note_activity} do
247 conn = get(conn, "/notice/#{note_activity.id}/embed_player")
249 assert Plug.Conn.get_resp_header(conn, "x-frame-options") == ["ALLOW"]
251 assert Plug.Conn.get_resp_header(
253 "content-security-policy"
255 "default-src 'none';style-src 'self' 'unsafe-inline';img-src 'self' data: https:; media-src 'self' https:;"
258 assert response(conn, 200) =~
259 "<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>"
262 test "404s when activity isn't create", %{conn: conn} do
263 note_activity = insert(:note_activity, data_attrs: %{"type" => "Like"})
266 |> get("/notice/#{note_activity.id}/embed_player")
270 test "404s when activity is direct message", %{conn: conn} do
271 note_activity = insert(:note_activity, data_attrs: %{"directMessage" => true})
274 |> get("/notice/#{note_activity.id}/embed_player")
278 test "404s when attachment is empty", %{conn: conn} do
279 note_activity = insert(:note_activity)
280 object = Pleroma.Object.normalize(note_activity)
281 object_data = Map.put(object.data, "attachment", [])
284 |> Ecto.Changeset.change(data: object_data)
285 |> Pleroma.Repo.update()
288 |> get("/notice/#{note_activity.id}/embed_player")
292 test "404s when attachment isn't audio or video", %{conn: conn} do
293 note_activity = insert(:note_activity)
294 object = Pleroma.Object.normalize(note_activity)
297 Map.put(object.data, "attachment", [
301 "href" => "https://peertube.moe/static/webseed/480.jpg",
302 "mediaType" => "image/jpg",
310 |> Ecto.Changeset.change(data: object_data)
311 |> Pleroma.Repo.update()
314 |> get("/notice/#{note_activity.id}/embed_player")
318 test "it requires authentication if instance is NOT federating", %{
320 note_activity: note_activity
323 conn = put_req_header(conn, "accept", "text/html")
325 ensure_federating_or_authenticated(conn, "/notice/#{note_activity.id}/embed_player", user)