1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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
8 import ExUnit.CaptureLog
13 alias Pleroma.Web.CommonAPI
16 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
20 clear_config_all([:instance, :federating]) do
21 Pleroma.Config.put([:instance, :federating], true)
24 describe "GET object/2" do
25 test "redirects to /notice/id for html format", %{conn: conn} do
26 note_activity = insert(:note_activity)
27 object = Object.normalize(note_activity)
28 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
29 url = "/objects/#{uuid}"
33 |> put_req_header("accept", "text/html")
36 assert redirected_to(conn) == "/notice/#{note_activity.id}"
39 test "500s when user not found", %{conn: conn} do
40 note_activity = insert(:note_activity)
41 object = Object.normalize(note_activity)
42 user = User.get_cached_by_ap_id(note_activity.data["actor"])
43 User.invalidate_cache(user)
44 Pleroma.Repo.delete(user)
45 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
46 url = "/objects/#{uuid}"
50 |> put_req_header("accept", "application/xml")
53 assert response(conn, 500) == ~S({"error":"Something went wrong"})
56 test "404s on private objects", %{conn: conn} do
57 note_activity = insert(:direct_note_activity)
58 object = Object.normalize(note_activity)
59 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
62 |> get("/objects/#{uuid}")
66 test "404s on nonexisting objects", %{conn: conn} do
68 |> get("/objects/123")
73 describe "GET activity/2" do
74 test "redirects to /notice/id for html format", %{conn: conn} do
75 note_activity = insert(:note_activity)
76 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
80 |> put_req_header("accept", "text/html")
81 |> get("/activities/#{uuid}")
83 assert redirected_to(conn) == "/notice/#{note_activity.id}"
86 test "505s when user not found", %{conn: conn} do
87 note_activity = insert(:note_activity)
88 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
89 user = User.get_cached_by_ap_id(note_activity.data["actor"])
90 User.invalidate_cache(user)
91 Pleroma.Repo.delete(user)
95 |> put_req_header("accept", "text/html")
96 |> get("/activities/#{uuid}")
98 assert response(conn, 500) == ~S({"error":"Something went wrong"})
101 test "404s on private activities", %{conn: conn} do
102 note_activity = insert(:direct_note_activity)
103 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
106 |> get("/activities/#{uuid}")
110 test "404s on nonexistent activities", %{conn: conn} do
112 |> get("/activities/123")
116 test "gets an activity in AS2 format", %{conn: conn} do
117 note_activity = insert(:note_activity)
118 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
119 url = "/activities/#{uuid}"
123 |> put_req_header("accept", "application/activity+json")
126 assert json_response(conn, 200)
130 describe "GET notice/2" do
131 test "gets a notice in xml format", %{conn: conn} do
132 note_activity = insert(:note_activity)
135 |> get("/notice/#{note_activity.id}")
139 test "gets a notice in AS2 format", %{conn: conn} do
140 note_activity = insert(:note_activity)
143 |> put_req_header("accept", "application/activity+json")
144 |> get("/notice/#{note_activity.id}")
145 |> json_response(200)
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 "only gets a notice in AS2 format for Create messages", %{conn: conn} do
162 note_activity = insert(:note_activity)
163 url = "/notice/#{note_activity.id}"
167 |> put_req_header("accept", "application/activity+json")
170 assert json_response(conn, 200)
174 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
175 url = "/notice/#{like_activity.id}"
177 assert like_activity.data["type"] == "Like"
181 |> put_req_header("accept", "application/activity+json")
184 assert response(conn, 404)
187 test "render html for redirect for html format", %{conn: conn} do
188 note_activity = insert(:note_activity)
192 |> put_req_header("accept", "text/html")
193 |> get("/notice/#{note_activity.id}")
197 "<meta content=\"#{Pleroma.Web.base_url()}/notice/#{note_activity.id}\" property=\"og:url\">"
201 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
203 assert like_activity.data["type"] == "Like"
207 |> put_req_header("accept", "text/html")
208 |> get("/notice/#{like_activity.id}")
211 assert resp =~ "<!--server-generated-meta-->"
214 test "404s a private notice", %{conn: conn} do
215 note_activity = insert(:direct_note_activity)
216 url = "/notice/#{note_activity.id}"
222 assert response(conn, 404)
225 test "404s a nonexisting notice", %{conn: conn} do
232 assert response(conn, 404)
236 describe "GET /notice/:id/embed_player" do
237 test "render embed player", %{conn: conn} do
238 note_activity = insert(:note_activity)
239 object = Pleroma.Object.normalize(note_activity)
242 Map.put(object.data, "attachment", [
247 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
248 "mediaType" => "video/mp4",
256 |> Ecto.Changeset.change(data: object_data)
257 |> Pleroma.Repo.update()
261 |> get("/notice/#{note_activity.id}/embed_player")
263 assert Plug.Conn.get_resp_header(conn, "x-frame-options") == ["ALLOW"]
265 assert Plug.Conn.get_resp_header(
267 "content-security-policy"
269 "default-src 'none';style-src 'self' 'unsafe-inline';img-src 'self' data: https:; media-src 'self' https:;"
272 assert response(conn, 200) =~
273 "<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>"
276 test "404s when activity isn't create", %{conn: conn} do
277 note_activity = insert(:note_activity, data_attrs: %{"type" => "Like"})
280 |> get("/notice/#{note_activity.id}/embed_player")
284 test "404s when activity is direct message", %{conn: conn} do
285 note_activity = insert(:note_activity, data_attrs: %{"directMessage" => true})
288 |> get("/notice/#{note_activity.id}/embed_player")
292 test "404s when attachment is empty", %{conn: conn} do
293 note_activity = insert(:note_activity)
294 object = Pleroma.Object.normalize(note_activity)
295 object_data = Map.put(object.data, "attachment", [])
298 |> Ecto.Changeset.change(data: object_data)
299 |> Pleroma.Repo.update()
302 |> get("/notice/#{note_activity.id}/embed_player")
306 test "404s when attachment isn't audio or video", %{conn: conn} do
307 note_activity = insert(:note_activity)
308 object = Pleroma.Object.normalize(note_activity)
311 Map.put(object.data, "attachment", [
315 "href" => "https://peertube.moe/static/webseed/480.jpg",
316 "mediaType" => "image/jpg",
324 |> Ecto.Changeset.change(data: object_data)
325 |> Pleroma.Repo.update()
328 |> get("/notice/#{note_activity.id}/embed_player")