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 test "gets an object", %{conn: conn} do
105 note_activity = insert(:note_activity)
106 object = Object.normalize(note_activity)
107 user = User.get_cached_by_ap_id(note_activity.data["actor"])
108 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
109 url = "/objects/#{uuid}"
113 |> put_req_header("accept", "application/xml")
117 ActivityRepresenter.to_simple_form(note_activity, user, true)
118 |> ActivityRepresenter.wrap_with_entry()
119 |> :xmerl.export_simple(:xmerl_xml)
122 assert response(conn, 200) == expected
125 test "404s on private objects", %{conn: conn} do
126 note_activity = insert(:direct_note_activity)
127 object = Object.normalize(note_activity)
128 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
131 |> get("/objects/#{uuid}")
135 test "404s on nonexisting objects", %{conn: conn} do
137 |> get("/objects/123")
141 test "gets an activity in xml format", %{conn: conn} do
142 note_activity = insert(:note_activity)
143 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
146 |> put_req_header("accept", "application/xml")
147 |> get("/activities/#{uuid}")
151 test "404s on deleted objects", %{conn: conn} do
152 note_activity = insert(:note_activity)
153 object = Object.normalize(note_activity)
154 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
157 |> put_req_header("accept", "application/xml")
158 |> get("/objects/#{uuid}")
161 Object.delete(object)
164 |> put_req_header("accept", "application/xml")
165 |> get("/objects/#{uuid}")
169 test "404s on private activities", %{conn: conn} do
170 note_activity = insert(:direct_note_activity)
171 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
174 |> get("/activities/#{uuid}")
178 test "404s on nonexistent activities", %{conn: conn} do
180 |> get("/activities/123")
184 test "gets a notice in xml format", %{conn: conn} do
185 note_activity = insert(:note_activity)
188 |> get("/notice/#{note_activity.id}")
192 test "gets a notice in AS2 format", %{conn: conn} do
193 note_activity = insert(:note_activity)
196 |> put_req_header("accept", "application/activity+json")
197 |> get("/notice/#{note_activity.id}")
198 |> json_response(200)
201 test "only gets a notice in AS2 format for Create messages", %{conn: conn} do
202 note_activity = insert(:note_activity)
203 url = "/notice/#{note_activity.id}"
207 |> put_req_header("accept", "application/activity+json")
210 assert json_response(conn, 200)
214 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
215 url = "/notice/#{like_activity.id}"
217 assert like_activity.data["type"] == "Like"
221 |> put_req_header("accept", "application/activity+json")
224 assert response(conn, 404)
227 test "gets an activity in AS2 format", %{conn: conn} do
228 note_activity = insert(:note_activity)
229 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
230 url = "/activities/#{uuid}"
234 |> put_req_header("accept", "application/activity+json")
237 assert json_response(conn, 200)
240 test "404s a private notice", %{conn: conn} do
241 note_activity = insert(:direct_note_activity)
242 url = "/notice/#{note_activity.id}"
248 assert response(conn, 404)
251 test "404s a nonexisting notice", %{conn: conn} do
258 assert response(conn, 404)