1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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)
19 config_path = [:instance, :federating]
20 initial_setting = Pleroma.Config.get(config_path)
22 Pleroma.Config.put(config_path, true)
23 on_exit(fn -> Pleroma.Config.put(config_path, initial_setting) end)
28 describe "salmon_incoming" do
29 test "decodes a salmon", %{conn: conn} do
31 salmon = File.read!("test/fixtures/salmon.xml")
33 assert capture_log(fn ->
36 |> put_req_header("content-type", "application/atom+xml")
37 |> post("/users/#{user.nickname}/salmon", salmon)
39 assert response(conn, 200)
43 test "decodes a salmon with a changed magic key", %{conn: conn} do
45 salmon = File.read!("test/fixtures/salmon.xml")
47 assert capture_log(fn ->
50 |> put_req_header("content-type", "application/atom+xml")
51 |> post("/users/#{user.nickname}/salmon", salmon)
53 assert response(conn, 200)
56 # Set a wrong magic-key for a user so it has to refetch
57 salmon_user = User.get_cached_by_ap_id("http://gs.example.org:4040/index.php/user/1")
61 User.Info.remote_user_creation(salmon_user.info, %{
63 "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwrong1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB"
67 |> Ecto.Changeset.change()
68 |> Ecto.Changeset.put_embed(:info, info_cng)
69 |> User.update_and_set_cache()
71 assert capture_log(fn ->
74 |> put_req_header("content-type", "application/atom+xml")
75 |> post("/users/#{user.nickname}/salmon", salmon)
77 assert response(conn, 200)
82 test "gets a feed", %{conn: conn} do
83 note_activity = insert(:note_activity)
84 object = Object.normalize(note_activity)
85 user = User.get_cached_by_ap_id(note_activity.data["actor"])
89 |> put_req_header("content-type", "application/atom+xml")
90 |> get("/users/#{user.nickname}/feed.atom")
92 assert response(conn, 200) =~ object.data["content"]
95 test "returns 404 for a missing feed", %{conn: conn} do
98 |> put_req_header("content-type", "application/atom+xml")
99 |> get("/users/nonexisting/feed.atom")
101 assert response(conn, 404)
104 describe "GET object/2" do
105 test "gets an object", %{conn: conn} do
106 note_activity = insert(:note_activity)
107 object = Object.normalize(note_activity)
108 user = User.get_cached_by_ap_id(note_activity.data["actor"])
109 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
110 url = "/objects/#{uuid}"
114 |> put_req_header("accept", "application/xml")
118 ActivityRepresenter.to_simple_form(note_activity, user, true)
119 |> ActivityRepresenter.wrap_with_entry()
120 |> :xmerl.export_simple(:xmerl_xml)
123 assert response(conn, 200) == expected
126 test "redirects to /notice/id for html format", %{conn: conn} do
127 note_activity = insert(:note_activity)
128 object = Object.normalize(note_activity)
129 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
130 url = "/objects/#{uuid}"
134 |> put_req_header("accept", "text/html")
137 assert redirected_to(conn) == "/notice/#{note_activity.id}"
140 test "500s when user not found", %{conn: conn} do
141 note_activity = insert(:note_activity)
142 object = Object.normalize(note_activity)
143 user = User.get_cached_by_ap_id(note_activity.data["actor"])
144 User.invalidate_cache(user)
145 Pleroma.Repo.delete(user)
146 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
147 url = "/objects/#{uuid}"
151 |> put_req_header("accept", "application/xml")
154 assert response(conn, 500) == ~S({"error":"Something went wrong"})
157 test "404s on private objects", %{conn: conn} do
158 note_activity = insert(:direct_note_activity)
159 object = Object.normalize(note_activity)
160 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
163 |> get("/objects/#{uuid}")
167 test "404s on nonexisting objects", %{conn: conn} do
169 |> get("/objects/123")
174 describe "GET activity/2" do
175 test "gets an activity in xml format", %{conn: conn} do
176 note_activity = insert(:note_activity)
177 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
180 |> put_req_header("accept", "application/xml")
181 |> get("/activities/#{uuid}")
185 test "redirects to /notice/id for html format", %{conn: conn} do
186 note_activity = insert(:note_activity)
187 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
191 |> put_req_header("accept", "text/html")
192 |> get("/activities/#{uuid}")
194 assert redirected_to(conn) == "/notice/#{note_activity.id}"
197 test "505s when user not found", %{conn: conn} do
198 note_activity = insert(:note_activity)
199 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
200 user = User.get_cached_by_ap_id(note_activity.data["actor"])
201 User.invalidate_cache(user)
202 Pleroma.Repo.delete(user)
206 |> put_req_header("accept", "text/html")
207 |> get("/activities/#{uuid}")
209 assert response(conn, 500) == ~S({"error":"Something went wrong"})
212 test "404s on deleted objects", %{conn: conn} do
213 note_activity = insert(:note_activity)
214 object = Object.normalize(note_activity)
215 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
218 |> put_req_header("accept", "application/xml")
219 |> get("/objects/#{uuid}")
222 Object.delete(object)
225 |> put_req_header("accept", "application/xml")
226 |> get("/objects/#{uuid}")
230 test "404s on private activities", %{conn: conn} do
231 note_activity = insert(:direct_note_activity)
232 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
235 |> get("/activities/#{uuid}")
239 test "404s on nonexistent activities", %{conn: conn} do
241 |> get("/activities/123")
245 test "gets an activity in AS2 format", %{conn: conn} do
246 note_activity = insert(:note_activity)
247 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
248 url = "/activities/#{uuid}"
252 |> put_req_header("accept", "application/activity+json")
255 assert json_response(conn, 200)
259 describe "GET notice/2" do
260 test "gets a notice in xml format", %{conn: conn} do
261 note_activity = insert(:note_activity)
264 |> get("/notice/#{note_activity.id}")
268 test "gets a notice in AS2 format", %{conn: conn} do
269 note_activity = insert(:note_activity)
272 |> put_req_header("accept", "application/activity+json")
273 |> get("/notice/#{note_activity.id}")
274 |> json_response(200)
277 test "500s when actor not found", %{conn: conn} do
278 note_activity = insert(:note_activity)
279 user = User.get_cached_by_ap_id(note_activity.data["actor"])
280 User.invalidate_cache(user)
281 Pleroma.Repo.delete(user)
285 |> get("/notice/#{note_activity.id}")
287 assert response(conn, 500) == ~S({"error":"Something went wrong"})
290 test "only gets a notice in AS2 format for Create messages", %{conn: conn} do
291 note_activity = insert(:note_activity)
292 url = "/notice/#{note_activity.id}"
296 |> put_req_header("accept", "application/activity+json")
299 assert json_response(conn, 200)
303 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
304 url = "/notice/#{like_activity.id}"
306 assert like_activity.data["type"] == "Like"
310 |> put_req_header("accept", "application/activity+json")
313 assert response(conn, 404)
316 test "render html for redirect for html format", %{conn: conn} do
317 note_activity = insert(:note_activity)
321 |> put_req_header("accept", "text/html")
322 |> get("/notice/#{note_activity.id}")
326 "<meta content=\"#{Pleroma.Web.base_url()}/notice/#{note_activity.id}\" property=\"og:url\">"
330 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
332 assert like_activity.data["type"] == "Like"
336 |> put_req_header("accept", "text/html")
337 |> get("/notice/#{like_activity.id}")
340 assert resp =~ "<!--server-generated-meta-->"
343 test "404s a private notice", %{conn: conn} do
344 note_activity = insert(:direct_note_activity)
345 url = "/notice/#{note_activity.id}"
351 assert response(conn, 404)
354 test "404s a nonexisting notice", %{conn: conn} do
361 assert response(conn, 404)
365 describe "feed_redirect" do
366 test "undefined format. it redirects to feed", %{conn: conn} do
367 note_activity = insert(:note_activity)
368 user = User.get_cached_by_ap_id(note_activity.data["actor"])
372 |> put_req_header("accept", "application/xml")
373 |> get("/users/#{user.nickname}")
377 "<html><body>You are being <a href=\"#{Pleroma.Web.base_url()}/users/#{
379 }/feed.atom\">redirected</a>.</body></html>"
382 test "undefined format. it returns error when user not found", %{conn: conn} do
385 |> put_req_header("accept", "application/xml")
386 |> get("/users/jimm")
389 assert response == ~S({"error":"Not found"})
392 test "activity+json format. it redirects on actual feed of user", %{conn: conn} do
393 note_activity = insert(:note_activity)
394 user = User.get_cached_by_ap_id(note_activity.data["actor"])
398 |> put_req_header("accept", "application/activity+json")
399 |> get("/users/#{user.nickname}")
400 |> json_response(200)
402 assert response["endpoints"] == %{
403 "oauthAuthorizationEndpoint" => "#{Pleroma.Web.base_url()}/oauth/authorize",
404 "oauthRegistrationEndpoint" => "#{Pleroma.Web.base_url()}/api/v1/apps",
405 "oauthTokenEndpoint" => "#{Pleroma.Web.base_url()}/oauth/token",
406 "sharedInbox" => "#{Pleroma.Web.base_url()}/inbox"
409 assert response["@context"] == [
410 "https://www.w3.org/ns/activitystreams",
411 "http://localhost:4001/schemas/litepub-0.1.jsonld",
412 %{"@language" => "und"}
415 assert Map.take(response, [
420 "manuallyApprovesFollowers",
429 "followers" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/followers",
430 "following" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/following",
431 "id" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}",
432 "inbox" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/inbox",
433 "manuallyApprovesFollowers" => false,
435 "outbox" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/outbox",
436 "preferredUsername" => user.nickname,
437 "summary" => user.bio,
440 "url" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
444 test "activity+json format. it returns error whe use not found", %{conn: conn} do
447 |> put_req_header("accept", "application/activity+json")
448 |> get("/users/jimm")
449 |> json_response(404)
451 assert response == "Not found"
454 test "json format. it redirects on actual feed of user", %{conn: conn} do
455 note_activity = insert(:note_activity)
456 user = User.get_cached_by_ap_id(note_activity.data["actor"])
460 |> put_req_header("accept", "application/json")
461 |> get("/users/#{user.nickname}")
462 |> json_response(200)
464 assert response["endpoints"] == %{
465 "oauthAuthorizationEndpoint" => "#{Pleroma.Web.base_url()}/oauth/authorize",
466 "oauthRegistrationEndpoint" => "#{Pleroma.Web.base_url()}/api/v1/apps",
467 "oauthTokenEndpoint" => "#{Pleroma.Web.base_url()}/oauth/token",
468 "sharedInbox" => "#{Pleroma.Web.base_url()}/inbox"
471 assert response["@context"] == [
472 "https://www.w3.org/ns/activitystreams",
473 "http://localhost:4001/schemas/litepub-0.1.jsonld",
474 %{"@language" => "und"}
477 assert Map.take(response, [
482 "manuallyApprovesFollowers",
491 "followers" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/followers",
492 "following" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/following",
493 "id" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}",
494 "inbox" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/inbox",
495 "manuallyApprovesFollowers" => false,
497 "outbox" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/outbox",
498 "preferredUsername" => user.nickname,
499 "summary" => user.bio,
502 "url" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
506 test "json format. it returns error whe use not found", %{conn: conn} do
509 |> put_req_header("accept", "application/json")
510 |> get("/users/jimm")
511 |> json_response(404)
513 assert response == "Not found"
516 test "html format. it redirects on actual feed of user", %{conn: conn} do
517 note_activity = insert(:note_activity)
518 user = User.get_cached_by_ap_id(note_activity.data["actor"])
522 |> get("/users/#{user.nickname}")
526 Fallback.RedirectController.redirector_with_meta(
532 test "html format. it returns error when user not found", %{conn: conn} do
535 |> get("/users/jimm")
536 |> json_response(404)
538 assert response == %{"error" => "Not found"}
542 describe "GET /notice/:id/embed_player" do
543 test "render embed player", %{conn: conn} do
544 note_activity = insert(:note_activity)
545 object = Pleroma.Object.normalize(note_activity)
548 Map.put(object.data, "attachment", [
553 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
554 "mediaType" => "video/mp4",
562 |> Ecto.Changeset.change(data: object_data)
563 |> Pleroma.Repo.update()
567 |> get("/notice/#{note_activity.id}/embed_player")
569 assert Plug.Conn.get_resp_header(conn, "x-frame-options") == ["ALLOW"]
571 assert Plug.Conn.get_resp_header(
573 "content-security-policy"
575 "default-src 'none';style-src 'self' 'unsafe-inline';img-src 'self' data: https:; media-src 'self' https:;"
578 assert response(conn, 200) =~
579 "<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>"
582 test "404s when activity isn't create", %{conn: conn} do
583 note_activity = insert(:note_activity, data_attrs: %{"type" => "Like"})
586 |> get("/notice/#{note_activity.id}/embed_player")
590 test "404s when activity is direct message", %{conn: conn} do
591 note_activity = insert(:note_activity, data_attrs: %{"directMessage" => true})
594 |> get("/notice/#{note_activity.id}/embed_player")
598 test "404s when attachment is empty", %{conn: conn} do
599 note_activity = insert(:note_activity)
600 object = Pleroma.Object.normalize(note_activity)
601 object_data = Map.put(object.data, "attachment", [])
604 |> Ecto.Changeset.change(data: object_data)
605 |> Pleroma.Repo.update()
608 |> get("/notice/#{note_activity.id}/embed_player")
612 test "404s when attachment isn't audio or video", %{conn: conn} do
613 note_activity = insert(:note_activity)
614 object = Pleroma.Object.normalize(note_activity)
617 Map.put(object.data, "attachment", [
621 "href" => "https://peertube.moe/static/webseed/480.jpg",
622 "mediaType" => "image/jpg",
630 |> Ecto.Changeset.change(data: object_data)
631 |> Pleroma.Repo.update()
634 |> get("/notice/#{note_activity.id}/embed_player")