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
11 alias Pleroma.Web.CommonAPI
12 alias Pleroma.Web.OStatus.ActivityRepresenter
15 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
19 describe "salmon_incoming" do
20 test "decodes a salmon", %{conn: conn} do
22 salmon = File.read!("test/fixtures/salmon.xml")
26 |> put_req_header("content-type", "application/atom+xml")
27 |> post("/users/#{user.nickname}/salmon", salmon)
29 assert response(conn, 200)
32 test "decodes a salmon with a changed magic key", %{conn: conn} do
34 salmon = File.read!("test/fixtures/salmon.xml")
38 |> put_req_header("content-type", "application/atom+xml")
39 |> post("/users/#{user.nickname}/salmon", salmon)
41 assert response(conn, 200)
43 # Set a wrong magic-key for a user so it has to refetch
44 salmon_user = User.get_by_ap_id("http://gs.example.org:4040/index.php/user/1")
47 User.Info.remote_user_creation(salmon_user.info, %{
49 "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwrong1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB"
53 |> Ecto.Changeset.change()
54 |> Ecto.Changeset.put_embed(:info, info_cng)
59 |> put_req_header("content-type", "application/atom+xml")
60 |> post("/users/#{user.nickname}/salmon", salmon)
62 assert response(conn, 200)
66 test "gets a feed", %{conn: conn} do
67 note_activity = insert(:note_activity)
68 user = User.get_cached_by_ap_id(note_activity.data["actor"])
72 |> put_req_header("content-type", "application/atom+xml")
73 |> get("/users/#{user.nickname}/feed.atom")
75 assert response(conn, 200) =~ note_activity.data["object"]["content"]
78 test "returns 404 for a missing feed", %{conn: conn} do
81 |> put_req_header("content-type", "application/atom+xml")
82 |> get("/users/nonexisting/feed.atom")
84 assert response(conn, 404)
87 test "gets an object", %{conn: conn} do
88 note_activity = insert(:note_activity)
89 user = User.get_by_ap_id(note_activity.data["actor"])
90 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
91 url = "/objects/#{uuid}"
95 |> put_req_header("accept", "application/xml")
99 ActivityRepresenter.to_simple_form(note_activity, user, true)
100 |> ActivityRepresenter.wrap_with_entry()
101 |> :xmerl.export_simple(:xmerl_xml)
104 assert response(conn, 200) == expected
107 test "404s on private objects", %{conn: conn} do
108 note_activity = insert(:direct_note_activity)
109 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
112 |> get("/objects/#{uuid}")
116 test "404s on nonexisting objects", %{conn: conn} do
118 |> get("/objects/123")
122 test "gets an activity in xml format", %{conn: conn} do
123 note_activity = insert(:note_activity)
124 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
127 |> put_req_header("accept", "application/xml")
128 |> get("/activities/#{uuid}")
132 test "404s on deleted objects", %{conn: conn} do
133 note_activity = insert(:note_activity)
134 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
135 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
138 |> put_req_header("accept", "application/xml")
139 |> get("/objects/#{uuid}")
142 Object.delete(object)
145 |> put_req_header("accept", "application/xml")
146 |> get("/objects/#{uuid}")
150 test "404s on private activities", %{conn: conn} do
151 note_activity = insert(:direct_note_activity)
152 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
155 |> get("/activities/#{uuid}")
159 test "404s on nonexistent activities", %{conn: conn} do
161 |> get("/activities/123")
165 test "gets a notice in xml format", %{conn: conn} do
166 note_activity = insert(:note_activity)
169 |> get("/notice/#{note_activity.id}")
173 test "gets a notice in AS2 format", %{conn: conn} do
174 note_activity = insert(:note_activity)
177 |> put_req_header("accept", "application/activity+json")
178 |> get("/notice/#{note_activity.id}")
179 |> json_response(200)
182 test "only gets a notice in AS2 format for Create messages", %{conn: conn} do
183 note_activity = insert(:note_activity)
184 url = "/notice/#{note_activity.id}"
188 |> put_req_header("accept", "application/activity+json")
191 assert json_response(conn, 200)
195 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
196 url = "/notice/#{like_activity.id}"
198 assert like_activity.data["type"] == "Like"
202 |> put_req_header("accept", "application/activity+json")
205 assert response(conn, 404)
208 test "gets an activity in AS2 format", %{conn: conn} do
209 note_activity = insert(:note_activity)
210 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
211 url = "/activities/#{uuid}"
215 |> put_req_header("accept", "application/activity+json")
218 assert json_response(conn, 200)
221 test "404s a private notice", %{conn: conn} do
222 note_activity = insert(:direct_note_activity)
223 url = "/notice/#{note_activity.id}"
229 assert response(conn, 404)
232 test "404s a nonexisting notice", %{conn: conn} do
239 assert response(conn, 404)