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 "salmon_incoming" do
25 test "decodes a salmon", %{conn: conn} do
27 salmon = File.read!("test/fixtures/salmon.xml")
29 assert capture_log(fn ->
32 |> put_req_header("content-type", "application/atom+xml")
33 |> post("/users/#{user.nickname}/salmon", salmon)
35 assert response(conn, 200)
39 test "decodes a salmon with a changed magic key", %{conn: conn} do
41 salmon = File.read!("test/fixtures/salmon.xml")
43 assert capture_log(fn ->
46 |> put_req_header("content-type", "application/atom+xml")
47 |> post("/users/#{user.nickname}/salmon", salmon)
49 assert response(conn, 200)
55 "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwrong1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB"
58 # Set a wrong magic-key for a user so it has to refetch
59 "http://gs.example.org:4040/index.php/user/1"
60 |> User.get_cached_by_ap_id()
61 |> User.update_info(&User.Info.remote_user_creation(&1, info))
63 assert capture_log(fn ->
66 |> put_req_header("content-type", "application/atom+xml")
67 |> post("/users/#{user.nickname}/salmon", salmon)
69 assert response(conn, 200)
74 describe "GET object/2" do
75 test "redirects to /notice/id for html format", %{conn: conn} do
76 note_activity = insert(:note_activity)
77 object = Object.normalize(note_activity)
78 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
79 url = "/objects/#{uuid}"
83 |> put_req_header("accept", "text/html")
86 assert redirected_to(conn) == "/notice/#{note_activity.id}"
89 test "500s when user not found", %{conn: conn} do
90 note_activity = insert(:note_activity)
91 object = Object.normalize(note_activity)
92 user = User.get_cached_by_ap_id(note_activity.data["actor"])
93 User.invalidate_cache(user)
94 Pleroma.Repo.delete(user)
95 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
96 url = "/objects/#{uuid}"
100 |> put_req_header("accept", "application/xml")
103 assert response(conn, 500) == ~S({"error":"Something went wrong"})
106 test "404s on private objects", %{conn: conn} do
107 note_activity = insert(:direct_note_activity)
108 object = Object.normalize(note_activity)
109 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
112 |> get("/objects/#{uuid}")
116 test "404s on nonexisting objects", %{conn: conn} do
118 |> get("/objects/123")
123 describe "GET activity/2" do
124 test "gets an activity in xml format", %{conn: conn} do
125 note_activity = insert(:note_activity)
126 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
129 |> put_req_header("accept", "application/xml")
130 |> get("/activities/#{uuid}")
134 test "redirects to /notice/id for html format", %{conn: conn} do
135 note_activity = insert(:note_activity)
136 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
140 |> put_req_header("accept", "text/html")
141 |> get("/activities/#{uuid}")
143 assert redirected_to(conn) == "/notice/#{note_activity.id}"
146 test "505s when user not found", %{conn: conn} do
147 note_activity = insert(:note_activity)
148 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
149 user = User.get_cached_by_ap_id(note_activity.data["actor"])
150 User.invalidate_cache(user)
151 Pleroma.Repo.delete(user)
155 |> put_req_header("accept", "text/html")
156 |> get("/activities/#{uuid}")
158 assert response(conn, 500) == ~S({"error":"Something went wrong"})
161 test "404s on deleted objects", %{conn: conn} do
162 note_activity = insert(:note_activity)
163 object = Object.normalize(note_activity)
164 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
167 |> put_req_header("accept", "application/xml")
168 |> get("/objects/#{uuid}")
171 Object.delete(object)
174 |> put_req_header("accept", "application/xml")
175 |> get("/objects/#{uuid}")
179 test "404s on private activities", %{conn: conn} do
180 note_activity = insert(:direct_note_activity)
181 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
184 |> get("/activities/#{uuid}")
188 test "404s on nonexistent activities", %{conn: conn} do
190 |> get("/activities/123")
194 test "gets an activity in AS2 format", %{conn: conn} do
195 note_activity = insert(:note_activity)
196 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
197 url = "/activities/#{uuid}"
201 |> put_req_header("accept", "application/activity+json")
204 assert json_response(conn, 200)
208 describe "GET notice/2" do
209 test "gets a notice in xml format", %{conn: conn} do
210 note_activity = insert(:note_activity)
213 |> get("/notice/#{note_activity.id}")
217 test "gets a notice in AS2 format", %{conn: conn} do
218 note_activity = insert(:note_activity)
221 |> put_req_header("accept", "application/activity+json")
222 |> get("/notice/#{note_activity.id}")
223 |> json_response(200)
226 test "500s when actor not found", %{conn: conn} do
227 note_activity = insert(:note_activity)
228 user = User.get_cached_by_ap_id(note_activity.data["actor"])
229 User.invalidate_cache(user)
230 Pleroma.Repo.delete(user)
234 |> get("/notice/#{note_activity.id}")
236 assert response(conn, 500) == ~S({"error":"Something went wrong"})
239 test "only gets a notice in AS2 format for Create messages", %{conn: conn} do
240 note_activity = insert(:note_activity)
241 url = "/notice/#{note_activity.id}"
245 |> put_req_header("accept", "application/activity+json")
248 assert json_response(conn, 200)
252 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
253 url = "/notice/#{like_activity.id}"
255 assert like_activity.data["type"] == "Like"
259 |> put_req_header("accept", "application/activity+json")
262 assert response(conn, 404)
265 test "render html for redirect for html format", %{conn: conn} do
266 note_activity = insert(:note_activity)
270 |> put_req_header("accept", "text/html")
271 |> get("/notice/#{note_activity.id}")
275 "<meta content=\"#{Pleroma.Web.base_url()}/notice/#{note_activity.id}\" property=\"og:url\">"
279 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
281 assert like_activity.data["type"] == "Like"
285 |> put_req_header("accept", "text/html")
286 |> get("/notice/#{like_activity.id}")
289 assert resp =~ "<!--server-generated-meta-->"
292 test "404s a private notice", %{conn: conn} do
293 note_activity = insert(:direct_note_activity)
294 url = "/notice/#{note_activity.id}"
300 assert response(conn, 404)
303 test "404s a nonexisting notice", %{conn: conn} do
310 assert response(conn, 404)
314 describe "GET /notice/:id/embed_player" do
315 test "render embed player", %{conn: conn} do
316 note_activity = insert(:note_activity)
317 object = Pleroma.Object.normalize(note_activity)
320 Map.put(object.data, "attachment", [
325 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
326 "mediaType" => "video/mp4",
334 |> Ecto.Changeset.change(data: object_data)
335 |> Pleroma.Repo.update()
339 |> get("/notice/#{note_activity.id}/embed_player")
341 assert Plug.Conn.get_resp_header(conn, "x-frame-options") == ["ALLOW"]
343 assert Plug.Conn.get_resp_header(
345 "content-security-policy"
347 "default-src 'none';style-src 'self' 'unsafe-inline';img-src 'self' data: https:; media-src 'self' https:;"
350 assert response(conn, 200) =~
351 "<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>"
354 test "404s when activity isn't create", %{conn: conn} do
355 note_activity = insert(:note_activity, data_attrs: %{"type" => "Like"})
358 |> get("/notice/#{note_activity.id}/embed_player")
362 test "404s when activity is direct message", %{conn: conn} do
363 note_activity = insert(:note_activity, data_attrs: %{"directMessage" => true})
366 |> get("/notice/#{note_activity.id}/embed_player")
370 test "404s when attachment is empty", %{conn: conn} do
371 note_activity = insert(:note_activity)
372 object = Pleroma.Object.normalize(note_activity)
373 object_data = Map.put(object.data, "attachment", [])
376 |> Ecto.Changeset.change(data: object_data)
377 |> Pleroma.Repo.update()
380 |> get("/notice/#{note_activity.id}/embed_player")
384 test "404s when attachment isn't audio or video", %{conn: conn} do
385 note_activity = insert(:note_activity)
386 object = Pleroma.Object.normalize(note_activity)
389 Map.put(object.data, "attachment", [
393 "href" => "https://peertube.moe/static/webseed/480.jpg",
394 "mediaType" => "image/jpg",
402 |> Ecto.Changeset.change(data: object_data)
403 |> Pleroma.Repo.update()
406 |> get("/notice/#{note_activity.id}/embed_player")