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.CommonAPI
16 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
20 setup do: clear_config([:instance, :federating], true)
22 # Note: see ActivityPubControllerTest for JSON format tests
23 describe "GET /objects/:uuid (text/html)" do
24 setup %{conn: conn} do
25 conn = put_req_header(conn, "accept", "text/html")
29 test "redirects to /notice/id for html format", %{conn: conn} do
30 note_activity = insert(:note_activity)
31 object = Object.normalize(note_activity)
32 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
33 url = "/objects/#{uuid}"
36 assert redirected_to(conn) == "/notice/#{note_activity.id}"
39 test "404s on private objects", %{conn: conn} do
40 note_activity = insert(:direct_note_activity)
41 object = Object.normalize(note_activity)
42 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
45 |> get("/objects/#{uuid}")
49 test "404s on non-existing objects", %{conn: conn} do
51 |> get("/objects/123")
56 # Note: see ActivityPubControllerTest for JSON format tests
57 describe "GET /activities/:uuid (text/html)" do
58 setup %{conn: conn} do
59 conn = put_req_header(conn, "accept", "text/html")
63 test "redirects to /notice/id for html format", %{conn: conn} do
64 note_activity = insert(:note_activity)
65 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
67 conn = get(conn, "/activities/#{uuid}")
68 assert redirected_to(conn) == "/notice/#{note_activity.id}"
71 test "404s on private activities", %{conn: conn} do
72 note_activity = insert(:direct_note_activity)
73 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
76 |> get("/activities/#{uuid}")
80 test "404s on nonexistent activities", %{conn: conn} do
82 |> get("/activities/123")
87 describe "GET notice/2" do
88 test "redirects to a proper object URL when json requested and the object is local", %{
91 note_activity = insert(:note_activity)
92 expected_redirect_url = Object.normalize(note_activity).data["id"]
96 |> put_req_header("accept", "application/activity+json")
97 |> get("/notice/#{note_activity.id}")
100 assert redirect_url == expected_redirect_url
103 test "returns a 404 on remote notice when json requested", %{conn: conn} do
104 note_activity = insert(:note_activity, local: false)
107 |> put_req_header("accept", "application/activity+json")
108 |> get("/notice/#{note_activity.id}")
112 test "500s when actor not found", %{conn: conn} do
113 note_activity = insert(:note_activity)
114 user = User.get_cached_by_ap_id(note_activity.data["actor"])
115 User.invalidate_cache(user)
116 Pleroma.Repo.delete(user)
120 |> get("/notice/#{note_activity.id}")
122 assert response(conn, 500) == ~S({"error":"Something went wrong"})
125 test "render html for redirect for html format", %{conn: conn} do
126 note_activity = insert(:note_activity)
130 |> put_req_header("accept", "text/html")
131 |> get("/notice/#{note_activity.id}")
135 "<meta content=\"#{Pleroma.Web.base_url()}/notice/#{note_activity.id}\" property=\"og:url\">"
139 {:ok, like_activity} = CommonAPI.favorite(user, note_activity.id)
141 assert like_activity.data["type"] == "Like"
145 |> put_req_header("accept", "text/html")
146 |> get("/notice/#{like_activity.id}")
149 assert resp =~ "<!--server-generated-meta-->"
152 test "404s a private notice", %{conn: conn} do
153 note_activity = insert(:direct_note_activity)
154 url = "/notice/#{note_activity.id}"
160 assert response(conn, 404)
163 test "404s a non-existing notice", %{conn: conn} do
170 assert response(conn, 404)
173 test "it requires authentication if instance is NOT federating", %{
177 note_activity = insert(:note_activity)
179 conn = put_req_header(conn, "accept", "text/html")
181 ensure_federating_or_authenticated(conn, "/notice/#{note_activity.id}", user)
185 describe "GET /notice/:id/embed_player" do
187 note_activity = insert(:note_activity)
188 object = Pleroma.Object.normalize(note_activity)
191 Map.put(object.data, "attachment", [
196 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
197 "mediaType" => "video/mp4",
205 |> Ecto.Changeset.change(data: object_data)
206 |> Pleroma.Repo.update()
208 %{note_activity: note_activity}
211 test "renders embed player", %{conn: conn, note_activity: note_activity} do
212 conn = get(conn, "/notice/#{note_activity.id}/embed_player")
214 assert Plug.Conn.get_resp_header(conn, "x-frame-options") == ["ALLOW"]
216 assert Plug.Conn.get_resp_header(
218 "content-security-policy"
220 "default-src 'none';style-src 'self' 'unsafe-inline';img-src 'self' data: https:; media-src 'self' https:;"
223 assert response(conn, 200) =~
224 "<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>"
227 test "404s when activity isn't create", %{conn: conn} do
228 note_activity = insert(:note_activity, data_attrs: %{"type" => "Like"})
231 |> get("/notice/#{note_activity.id}/embed_player")
235 test "404s when activity is direct message", %{conn: conn} do
236 note_activity = insert(:note_activity, data_attrs: %{"directMessage" => true})
239 |> get("/notice/#{note_activity.id}/embed_player")
243 test "404s when attachment is empty", %{conn: conn} do
244 note_activity = insert(:note_activity)
245 object = Pleroma.Object.normalize(note_activity)
246 object_data = Map.put(object.data, "attachment", [])
249 |> Ecto.Changeset.change(data: object_data)
250 |> Pleroma.Repo.update()
253 |> get("/notice/#{note_activity.id}/embed_player")
257 test "404s when attachment isn't audio or video", %{conn: conn} do
258 note_activity = insert(:note_activity)
259 object = Pleroma.Object.normalize(note_activity)
262 Map.put(object.data, "attachment", [
266 "href" => "https://peertube.moe/static/webseed/480.jpg",
267 "mediaType" => "image/jpg",
275 |> Ecto.Changeset.change(data: object_data)
276 |> Pleroma.Repo.update()
279 |> get("/notice/#{note_activity.id}/embed_player")
283 test "it requires authentication if instance is NOT federating", %{
285 note_activity: note_activity
288 conn = put_req_header(conn, "accept", "text/html")
290 ensure_federating_or_authenticated(conn, "/notice/#{note_activity.id}/embed_player", user)