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
14 alias Pleroma.Web.OStatus.ActivityRepresenter
17 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
21 clear_config_all([:instance, :federating]) do
22 Pleroma.Config.put([:instance, :federating], true)
25 describe "salmon_incoming" do
26 test "decodes a salmon", %{conn: conn} do
28 salmon = File.read!("test/fixtures/salmon.xml")
30 assert capture_log(fn ->
33 |> put_req_header("content-type", "application/atom+xml")
34 |> post("/users/#{user.nickname}/salmon", salmon)
36 assert response(conn, 200)
40 test "decodes a salmon with a changed magic key", %{conn: conn} do
42 salmon = File.read!("test/fixtures/salmon.xml")
44 assert capture_log(fn ->
47 |> put_req_header("content-type", "application/atom+xml")
48 |> post("/users/#{user.nickname}/salmon", salmon)
50 assert response(conn, 200)
56 "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwrong1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB"
59 # Set a wrong magic-key for a user so it has to refetch
60 "http://gs.example.org:4040/index.php/user/1"
61 |> User.get_cached_by_ap_id()
62 |> User.update_info(&User.Info.remote_user_creation(&1, info))
64 assert capture_log(fn ->
67 |> put_req_header("content-type", "application/atom+xml")
68 |> post("/users/#{user.nickname}/salmon", salmon)
70 assert response(conn, 200)
75 describe "GET object/2" do
76 test "gets an object", %{conn: conn} do
77 note_activity = insert(:note_activity)
78 object = Object.normalize(note_activity)
79 user = User.get_cached_by_ap_id(note_activity.data["actor"])
80 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
81 url = "/objects/#{uuid}"
85 |> put_req_header("accept", "application/xml")
89 ActivityRepresenter.to_simple_form(note_activity, user, true)
90 |> ActivityRepresenter.wrap_with_entry()
91 |> :xmerl.export_simple(:xmerl_xml)
94 assert response(conn, 200) == expected
97 test "redirects to /notice/id for html format", %{conn: conn} do
98 note_activity = insert(:note_activity)
99 object = Object.normalize(note_activity)
100 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
101 url = "/objects/#{uuid}"
105 |> put_req_header("accept", "text/html")
108 assert redirected_to(conn) == "/notice/#{note_activity.id}"
111 test "500s when user not found", %{conn: conn} do
112 note_activity = insert(:note_activity)
113 object = Object.normalize(note_activity)
114 user = User.get_cached_by_ap_id(note_activity.data["actor"])
115 User.invalidate_cache(user)
116 Pleroma.Repo.delete(user)
117 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
118 url = "/objects/#{uuid}"
122 |> put_req_header("accept", "application/xml")
125 assert response(conn, 500) == ~S({"error":"Something went wrong"})
128 test "404s on private objects", %{conn: conn} do
129 note_activity = insert(:direct_note_activity)
130 object = Object.normalize(note_activity)
131 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
134 |> get("/objects/#{uuid}")
138 test "404s on nonexisting objects", %{conn: conn} do
140 |> get("/objects/123")
145 describe "GET activity/2" do
146 test "gets an activity in xml format", %{conn: conn} do
147 note_activity = insert(:note_activity)
148 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
151 |> put_req_header("accept", "application/xml")
152 |> get("/activities/#{uuid}")
156 test "redirects to /notice/id for html format", %{conn: conn} do
157 note_activity = insert(:note_activity)
158 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
162 |> put_req_header("accept", "text/html")
163 |> get("/activities/#{uuid}")
165 assert redirected_to(conn) == "/notice/#{note_activity.id}"
168 test "505s when user not found", %{conn: conn} do
169 note_activity = insert(:note_activity)
170 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
171 user = User.get_cached_by_ap_id(note_activity.data["actor"])
172 User.invalidate_cache(user)
173 Pleroma.Repo.delete(user)
177 |> put_req_header("accept", "text/html")
178 |> get("/activities/#{uuid}")
180 assert response(conn, 500) == ~S({"error":"Something went wrong"})
183 test "404s on deleted objects", %{conn: conn} do
184 note_activity = insert(:note_activity)
185 object = Object.normalize(note_activity)
186 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
189 |> put_req_header("accept", "application/xml")
190 |> get("/objects/#{uuid}")
193 Object.delete(object)
196 |> put_req_header("accept", "application/xml")
197 |> get("/objects/#{uuid}")
201 test "404s on private activities", %{conn: conn} do
202 note_activity = insert(:direct_note_activity)
203 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
206 |> get("/activities/#{uuid}")
210 test "404s on nonexistent activities", %{conn: conn} do
212 |> get("/activities/123")
216 test "gets an activity in AS2 format", %{conn: conn} do
217 note_activity = insert(:note_activity)
218 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
219 url = "/activities/#{uuid}"
223 |> put_req_header("accept", "application/activity+json")
226 assert json_response(conn, 200)
230 describe "GET notice/2" do
231 test "gets a notice in xml format", %{conn: conn} do
232 note_activity = insert(:note_activity)
235 |> get("/notice/#{note_activity.id}")
239 test "gets a notice in AS2 format", %{conn: conn} do
240 note_activity = insert(:note_activity)
243 |> put_req_header("accept", "application/activity+json")
244 |> get("/notice/#{note_activity.id}")
245 |> json_response(200)
248 test "500s when actor not found", %{conn: conn} do
249 note_activity = insert(:note_activity)
250 user = User.get_cached_by_ap_id(note_activity.data["actor"])
251 User.invalidate_cache(user)
252 Pleroma.Repo.delete(user)
256 |> get("/notice/#{note_activity.id}")
258 assert response(conn, 500) == ~S({"error":"Something went wrong"})
261 test "only gets a notice in AS2 format for Create messages", %{conn: conn} do
262 note_activity = insert(:note_activity)
263 url = "/notice/#{note_activity.id}"
267 |> put_req_header("accept", "application/activity+json")
270 assert json_response(conn, 200)
274 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
275 url = "/notice/#{like_activity.id}"
277 assert like_activity.data["type"] == "Like"
281 |> put_req_header("accept", "application/activity+json")
284 assert response(conn, 404)
287 test "render html for redirect for html format", %{conn: conn} do
288 note_activity = insert(:note_activity)
292 |> put_req_header("accept", "text/html")
293 |> get("/notice/#{note_activity.id}")
297 "<meta content=\"#{Pleroma.Web.base_url()}/notice/#{note_activity.id}\" property=\"og:url\">"
301 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
303 assert like_activity.data["type"] == "Like"
307 |> put_req_header("accept", "text/html")
308 |> get("/notice/#{like_activity.id}")
311 assert resp =~ "<!--server-generated-meta-->"
314 test "404s a private notice", %{conn: conn} do
315 note_activity = insert(:direct_note_activity)
316 url = "/notice/#{note_activity.id}"
322 assert response(conn, 404)
325 test "404s a nonexisting notice", %{conn: conn} do
332 assert response(conn, 404)
336 describe "GET /notice/:id/embed_player" do
337 test "render embed player", %{conn: conn} do
338 note_activity = insert(:note_activity)
339 object = Pleroma.Object.normalize(note_activity)
342 Map.put(object.data, "attachment", [
347 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
348 "mediaType" => "video/mp4",
356 |> Ecto.Changeset.change(data: object_data)
357 |> Pleroma.Repo.update()
361 |> get("/notice/#{note_activity.id}/embed_player")
363 assert Plug.Conn.get_resp_header(conn, "x-frame-options") == ["ALLOW"]
365 assert Plug.Conn.get_resp_header(
367 "content-security-policy"
369 "default-src 'none';style-src 'self' 'unsafe-inline';img-src 'self' data: https:; media-src 'self' https:;"
372 assert response(conn, 200) =~
373 "<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>"
376 test "404s when activity isn't create", %{conn: conn} do
377 note_activity = insert(:note_activity, data_attrs: %{"type" => "Like"})
380 |> get("/notice/#{note_activity.id}/embed_player")
384 test "404s when activity is direct message", %{conn: conn} do
385 note_activity = insert(:note_activity, data_attrs: %{"directMessage" => true})
388 |> get("/notice/#{note_activity.id}/embed_player")
392 test "404s when attachment is empty", %{conn: conn} do
393 note_activity = insert(:note_activity)
394 object = Pleroma.Object.normalize(note_activity)
395 object_data = Map.put(object.data, "attachment", [])
398 |> Ecto.Changeset.change(data: object_data)
399 |> Pleroma.Repo.update()
402 |> get("/notice/#{note_activity.id}/embed_player")
406 test "404s when attachment isn't audio or video", %{conn: conn} do
407 note_activity = insert(:note_activity)
408 object = Pleroma.Object.normalize(note_activity)
411 Map.put(object.data, "attachment", [
415 "href" => "https://peertube.moe/static/webseed/480.jpg",
416 "mediaType" => "image/jpg",
424 |> Ecto.Changeset.change(data: object_data)
425 |> Pleroma.Repo.update()
428 |> get("/notice/#{note_activity.id}/embed_player")