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 alias Pleroma.{User, Repo, Object, Instances}
9 alias Pleroma.Web.CommonAPI
10 alias Pleroma.Web.OStatus.ActivityRepresenter
13 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
17 describe "salmon_incoming" do
18 test "decodes a salmon", %{conn: conn} do
20 salmon = File.read!("test/fixtures/salmon.xml")
24 |> put_req_header("content-type", "application/atom+xml")
25 |> post("/users/#{user.nickname}/salmon", salmon)
27 assert response(conn, 200)
30 test "decodes a salmon with a changed magic key", %{conn: conn} do
32 salmon = File.read!("test/fixtures/salmon.xml")
36 |> put_req_header("content-type", "application/atom+xml")
37 |> post("/users/#{user.nickname}/salmon", salmon)
39 assert response(conn, 200)
41 # Set a wrong magic-key for a user so it has to refetch
42 salmon_user = User.get_by_ap_id("http://gs.example.org:4040/index.php/user/1")
45 User.Info.remote_user_creation(salmon_user.info, %{
47 "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwrong1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB"
51 |> Ecto.Changeset.change()
52 |> Ecto.Changeset.put_embed(:info, info_cng)
57 |> put_req_header("content-type", "application/atom+xml")
58 |> post("/users/#{user.nickname}/salmon", salmon)
60 assert response(conn, 200)
63 test "it clears `unreachable` federation status of the sender", %{conn: conn} do
64 sender_url = "https://pleroma.soykaf.com"
65 Instances.set_unreachable(sender_url, Instances.reachability_datetime_threshold())
66 refute Instances.reachable?(sender_url)
69 salmon = File.read!("test/fixtures/salmon.xml")
73 |> put_req_header("content-type", "application/atom+xml")
74 |> put_req_header("referer", sender_url)
75 |> post("/users/#{user.nickname}/salmon", salmon)
77 assert response(conn, 200)
78 assert Instances.reachable?(sender_url)
82 test "gets a feed", %{conn: conn} do
83 note_activity = insert(:note_activity)
84 user = User.get_cached_by_ap_id(note_activity.data["actor"])
88 |> put_req_header("content-type", "application/atom+xml")
89 |> get("/users/#{user.nickname}/feed.atom")
91 assert response(conn, 200) =~ note_activity.data["object"]["content"]
94 test "returns 404 for a missing feed", %{conn: conn} do
97 |> put_req_header("content-type", "application/atom+xml")
98 |> get("/users/nonexisting/feed.atom")
100 assert response(conn, 404)
103 test "gets an object", %{conn: conn} do
104 note_activity = insert(:note_activity)
105 user = User.get_by_ap_id(note_activity.data["actor"])
106 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
107 url = "/objects/#{uuid}"
114 ActivityRepresenter.to_simple_form(note_activity, user, true)
115 |> ActivityRepresenter.wrap_with_entry()
116 |> :xmerl.export_simple(:xmerl_xml)
119 assert response(conn, 200) == expected
122 test "404s on private objects", %{conn: conn} do
123 note_activity = insert(:direct_note_activity)
124 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
127 |> get("/objects/#{uuid}")
131 test "404s on nonexisting objects", %{conn: conn} do
133 |> get("/objects/123")
137 test "404s on deleted objects", %{conn: conn} do
138 note_activity = insert(:note_activity)
139 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
140 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
143 |> get("/objects/#{uuid}")
146 Object.delete(object)
149 |> get("/objects/#{uuid}")
153 test "gets an activity", %{conn: conn} do
154 note_activity = insert(:note_activity)
155 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
158 |> get("/activities/#{uuid}")
162 test "404s on private activities", %{conn: conn} do
163 note_activity = insert(:direct_note_activity)
164 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
167 |> get("/activities/#{uuid}")
171 test "404s on nonexistent activities", %{conn: conn} do
173 |> get("/activities/123")
177 test "gets a notice", %{conn: conn} do
178 note_activity = insert(:note_activity)
181 |> get("/notice/#{note_activity.id}")
185 test "gets a notice in AS2 format", %{conn: conn} do
186 note_activity = insert(:note_activity)
189 |> put_req_header("accept", "application/activity+json")
190 |> get("/notice/#{note_activity.id}")
191 |> json_response(200)
194 test "only gets a notice in AS2 format for Create messages", %{conn: conn} do
195 note_activity = insert(:note_activity)
196 url = "/notice/#{note_activity.id}"
200 |> put_req_header("accept", "application/activity+json")
203 assert json_response(conn, 200)
207 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
208 url = "/notice/#{like_activity.id}"
210 assert like_activity.data["type"] == "Like"
214 |> put_req_header("accept", "application/activity+json")
217 assert response(conn, 404)
220 test "gets an activity in AS2 format", %{conn: conn} do
221 note_activity = insert(:note_activity)
222 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
223 url = "/activities/#{uuid}"
227 |> put_req_header("accept", "application/activity+json")
230 assert json_response(conn, 200)
233 test "404s a private notice", %{conn: conn} do
234 note_activity = insert(:direct_note_activity)
235 url = "/notice/#{note_activity.id}"
241 assert response(conn, 404)
244 test "404s a nonexisting notice", %{conn: conn} do
251 assert response(conn, 404)