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
12 alias Pleroma.Web.CommonAPI
15 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
19 clear_config_all([:instance, :federating]) do
20 Pleroma.Config.put([:instance, :federating], true)
23 describe "GET object/2" do
24 test "redirects to /notice/id for html format", %{conn: conn} do
25 note_activity = insert(:note_activity)
26 object = Object.normalize(note_activity)
27 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
28 url = "/objects/#{uuid}"
32 |> put_req_header("accept", "text/html")
35 assert redirected_to(conn) == "/notice/#{note_activity.id}"
38 test "404s on private objects", %{conn: conn} do
39 note_activity = insert(:direct_note_activity)
40 object = Object.normalize(note_activity)
41 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
44 |> get("/objects/#{uuid}")
48 test "404s on nonexisting objects", %{conn: conn} do
50 |> get("/objects/123")
55 describe "GET activity/2" do
56 test "redirects to /notice/id for html format", %{conn: conn} do
57 note_activity = insert(:note_activity)
58 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
62 |> put_req_header("accept", "text/html")
63 |> get("/activities/#{uuid}")
65 assert redirected_to(conn) == "/notice/#{note_activity.id}"
68 test "404s on private activities", %{conn: conn} do
69 note_activity = insert(:direct_note_activity)
70 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
73 |> get("/activities/#{uuid}")
77 test "404s on nonexistent activities", %{conn: conn} do
79 |> get("/activities/123")
83 test "gets an activity in AS2 format", %{conn: conn} do
84 note_activity = insert(:note_activity)
85 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
86 url = "/activities/#{uuid}"
90 |> put_req_header("accept", "application/activity+json")
93 assert json_response(conn, 200)
97 describe "GET notice/2" do
98 test "redirects to a proper object URL when json requested and the object is local", %{
101 note_activity = insert(:note_activity)
102 expected_redirect_url = Object.normalize(note_activity).data["id"]
106 |> put_req_header("accept", "application/activity+json")
107 |> get("/notice/#{note_activity.id}")
110 assert redirect_url == expected_redirect_url
113 test "returns a 404 on remote notice when json requested", %{conn: conn} do
114 note_activity = insert(:note_activity, local: false)
117 |> put_req_header("accept", "application/activity+json")
118 |> get("/notice/#{note_activity.id}")
122 test "500s when actor not found", %{conn: conn} do
123 note_activity = insert(:note_activity)
124 user = User.get_cached_by_ap_id(note_activity.data["actor"])
125 User.invalidate_cache(user)
126 Pleroma.Repo.delete(user)
130 |> get("/notice/#{note_activity.id}")
132 assert response(conn, 500) == ~S({"error":"Something went wrong"})
135 test "render html for redirect for html format", %{conn: conn} do
136 note_activity = insert(:note_activity)
140 |> put_req_header("accept", "text/html")
141 |> get("/notice/#{note_activity.id}")
145 "<meta content=\"#{Pleroma.Web.base_url()}/notice/#{note_activity.id}\" property=\"og:url\">"
149 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
151 assert like_activity.data["type"] == "Like"
155 |> put_req_header("accept", "text/html")
156 |> get("/notice/#{like_activity.id}")
159 assert resp =~ "<!--server-generated-meta-->"
162 test "404s a private notice", %{conn: conn} do
163 note_activity = insert(:direct_note_activity)
164 url = "/notice/#{note_activity.id}"
170 assert response(conn, 404)
173 test "404s a nonexisting notice", %{conn: conn} do
180 assert response(conn, 404)
184 describe "GET /notice/:id/embed_player" do
185 test "render embed player", %{conn: conn} do
186 note_activity = insert(:note_activity)
187 object = Pleroma.Object.normalize(note_activity)
190 Map.put(object.data, "attachment", [
195 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
196 "mediaType" => "video/mp4",
204 |> Ecto.Changeset.change(data: object_data)
205 |> Pleroma.Repo.update()
209 |> get("/notice/#{note_activity.id}/embed_player")
211 assert Plug.Conn.get_resp_header(conn, "x-frame-options") == ["ALLOW"]
213 assert Plug.Conn.get_resp_header(
215 "content-security-policy"
217 "default-src 'none';style-src 'self' 'unsafe-inline';img-src 'self' data: https:; media-src 'self' https:;"
220 assert response(conn, 200) =~
221 "<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>"
224 test "404s when activity isn't create", %{conn: conn} do
225 note_activity = insert(:note_activity, data_attrs: %{"type" => "Like"})
228 |> get("/notice/#{note_activity.id}/embed_player")
232 test "404s when activity is direct message", %{conn: conn} do
233 note_activity = insert(:note_activity, data_attrs: %{"directMessage" => true})
236 |> get("/notice/#{note_activity.id}/embed_player")
240 test "404s when attachment is empty", %{conn: conn} do
241 note_activity = insert(:note_activity)
242 object = Pleroma.Object.normalize(note_activity)
243 object_data = Map.put(object.data, "attachment", [])
246 |> Ecto.Changeset.change(data: object_data)
247 |> Pleroma.Repo.update()
250 |> get("/notice/#{note_activity.id}/embed_player")
254 test "404s when attachment isn't audio or video", %{conn: conn} do
255 note_activity = insert(:note_activity)
256 object = Pleroma.Object.normalize(note_activity)
259 Map.put(object.data, "attachment", [
263 "href" => "https://peertube.moe/static/webseed/480.jpg",
264 "mediaType" => "image/jpg",
272 |> Ecto.Changeset.change(data: object_data)
273 |> Pleroma.Repo.update()
276 |> get("/notice/#{note_activity.id}/embed_player")