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
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 "500s when user not found", %{conn: conn} do
39 note_activity = insert(:note_activity)
40 object = Object.normalize(note_activity)
41 user = User.get_cached_by_ap_id(note_activity.data["actor"])
42 User.invalidate_cache(user)
43 Pleroma.Repo.delete(user)
44 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
45 url = "/objects/#{uuid}"
49 |> put_req_header("accept", "application/xml")
52 assert response(conn, 500) == ~S({"error":"Something went wrong"})
55 test "404s on private objects", %{conn: conn} do
56 note_activity = insert(:direct_note_activity)
57 object = Object.normalize(note_activity)
58 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
61 |> get("/objects/#{uuid}")
65 test "404s on nonexisting objects", %{conn: conn} do
67 |> get("/objects/123")
72 describe "GET activity/2" do
73 test "redirects to /notice/id for html format", %{conn: conn} do
74 note_activity = insert(:note_activity)
75 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
79 |> put_req_header("accept", "text/html")
80 |> get("/activities/#{uuid}")
82 assert redirected_to(conn) == "/notice/#{note_activity.id}"
85 test "505s when user not found", %{conn: conn} do
86 note_activity = insert(:note_activity)
87 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
88 user = User.get_cached_by_ap_id(note_activity.data["actor"])
89 User.invalidate_cache(user)
90 Pleroma.Repo.delete(user)
94 |> put_req_header("accept", "text/html")
95 |> get("/activities/#{uuid}")
97 assert response(conn, 500) == ~S({"error":"Something went wrong"})
100 test "404s on private activities", %{conn: conn} do
101 note_activity = insert(:direct_note_activity)
102 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
105 |> get("/activities/#{uuid}")
109 test "404s on nonexistent activities", %{conn: conn} do
111 |> get("/activities/123")
115 test "gets an activity in AS2 format", %{conn: conn} do
116 note_activity = insert(:note_activity)
117 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
118 url = "/activities/#{uuid}"
122 |> put_req_header("accept", "application/activity+json")
125 assert json_response(conn, 200)
129 describe "GET notice/2" do
130 test "gets a notice in xml format", %{conn: conn} do
131 note_activity = insert(:note_activity)
134 |> get("/notice/#{note_activity.id}")
138 test "gets a notice in AS2 format", %{conn: conn} do
139 note_activity = insert(:note_activity)
142 |> put_req_header("accept", "application/activity+json")
143 |> get("/notice/#{note_activity.id}")
144 |> json_response(200)
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 "only gets a notice in AS2 format for Create messages", %{conn: conn} do
161 note_activity = insert(:note_activity)
162 url = "/notice/#{note_activity.id}"
166 |> put_req_header("accept", "application/activity+json")
169 assert json_response(conn, 200)
173 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
174 url = "/notice/#{like_activity.id}"
176 assert like_activity.data["type"] == "Like"
180 |> put_req_header("accept", "application/activity+json")
183 assert response(conn, 404)
186 test "render html for redirect for html format", %{conn: conn} do
187 note_activity = insert(:note_activity)
191 |> put_req_header("accept", "text/html")
192 |> get("/notice/#{note_activity.id}")
196 "<meta content=\"#{Pleroma.Web.base_url()}/notice/#{note_activity.id}\" property=\"og:url\">"
200 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
202 assert like_activity.data["type"] == "Like"
206 |> put_req_header("accept", "text/html")
207 |> get("/notice/#{like_activity.id}")
210 assert resp =~ "<!--server-generated-meta-->"
213 test "404s a private notice", %{conn: conn} do
214 note_activity = insert(:direct_note_activity)
215 url = "/notice/#{note_activity.id}"
221 assert response(conn, 404)
224 test "404s a nonexisting notice", %{conn: conn} do
231 assert response(conn, 404)
235 describe "GET /notice/:id/embed_player" do
236 test "render embed player", %{conn: conn} do
237 note_activity = insert(:note_activity)
238 object = Pleroma.Object.normalize(note_activity)
241 Map.put(object.data, "attachment", [
246 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
247 "mediaType" => "video/mp4",
255 |> Ecto.Changeset.change(data: object_data)
256 |> Pleroma.Repo.update()
260 |> get("/notice/#{note_activity.id}/embed_player")
262 assert Plug.Conn.get_resp_header(conn, "x-frame-options") == ["ALLOW"]
264 assert Plug.Conn.get_resp_header(
266 "content-security-policy"
268 "default-src 'none';style-src 'self' 'unsafe-inline';img-src 'self' data: https:; media-src 'self' https:;"
271 assert response(conn, 200) =~
272 "<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>"
275 test "404s when activity isn't create", %{conn: conn} do
276 note_activity = insert(:note_activity, data_attrs: %{"type" => "Like"})
279 |> get("/notice/#{note_activity.id}/embed_player")
283 test "404s when activity is direct message", %{conn: conn} do
284 note_activity = insert(:note_activity, data_attrs: %{"directMessage" => true})
287 |> get("/notice/#{note_activity.id}/embed_player")
291 test "404s when attachment is empty", %{conn: conn} do
292 note_activity = insert(:note_activity)
293 object = Pleroma.Object.normalize(note_activity)
294 object_data = Map.put(object.data, "attachment", [])
297 |> Ecto.Changeset.change(data: object_data)
298 |> Pleroma.Repo.update()
301 |> get("/notice/#{note_activity.id}/embed_player")
305 test "404s when attachment isn't audio or video", %{conn: conn} do
306 note_activity = insert(:note_activity)
307 object = Pleroma.Object.normalize(note_activity)
310 Map.put(object.data, "attachment", [
314 "href" => "https://peertube.moe/static/webseed/480.jpg",
315 "mediaType" => "image/jpg",
323 |> Ecto.Changeset.change(data: object_data)
324 |> Pleroma.Repo.update()
327 |> get("/notice/#{note_activity.id}/embed_player")