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
10 alias Pleroma.Web.CommonAPI
11 alias Pleroma.Web.OStatus.ActivityRepresenter
14 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
18 describe "salmon_incoming" do
19 test "decodes a salmon", %{conn: conn} do
21 salmon = File.read!("test/fixtures/salmon.xml")
25 |> put_req_header("content-type", "application/atom+xml")
26 |> post("/users/#{user.nickname}/salmon", salmon)
28 assert response(conn, 200)
31 test "decodes a salmon with a changed magic key", %{conn: conn} do
33 salmon = File.read!("test/fixtures/salmon.xml")
37 |> put_req_header("content-type", "application/atom+xml")
38 |> post("/users/#{user.nickname}/salmon", salmon)
40 assert response(conn, 200)
42 # Set a wrong magic-key for a user so it has to refetch
43 salmon_user = User.get_cached_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)
55 |> User.update_and_set_cache()
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 object = Object.normalize(note_activity)
69 user = User.get_cached_by_ap_id(note_activity.data["actor"])
73 |> put_req_header("content-type", "application/atom+xml")
74 |> get("/users/#{user.nickname}/feed.atom")
76 assert response(conn, 200) =~ object.data["content"]
79 test "returns 404 for a missing feed", %{conn: conn} do
82 |> put_req_header("content-type", "application/atom+xml")
83 |> get("/users/nonexisting/feed.atom")
85 assert response(conn, 404)
88 test "gets an object", %{conn: conn} do
89 note_activity = insert(:note_activity)
90 object = Object.normalize(note_activity)
91 user = User.get_cached_by_ap_id(note_activity.data["actor"])
92 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
93 url = "/objects/#{uuid}"
97 |> put_req_header("accept", "application/xml")
101 ActivityRepresenter.to_simple_form(note_activity, user, true)
102 |> ActivityRepresenter.wrap_with_entry()
103 |> :xmerl.export_simple(:xmerl_xml)
106 assert response(conn, 200) == expected
109 test "404s on private objects", %{conn: conn} do
110 note_activity = insert(:direct_note_activity)
111 object = Object.normalize(note_activity)
112 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
115 |> get("/objects/#{uuid}")
119 test "404s on nonexisting objects", %{conn: conn} do
121 |> get("/objects/123")
125 test "gets an activity in xml format", %{conn: conn} do
126 note_activity = insert(:note_activity)
127 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
130 |> put_req_header("accept", "application/xml")
131 |> get("/activities/#{uuid}")
135 test "404s on deleted objects", %{conn: conn} do
136 note_activity = insert(:note_activity)
137 object = Object.normalize(note_activity)
138 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
141 |> put_req_header("accept", "application/xml")
142 |> get("/objects/#{uuid}")
145 Object.delete(object)
148 |> put_req_header("accept", "application/xml")
149 |> get("/objects/#{uuid}")
153 test "404s on private activities", %{conn: conn} do
154 note_activity = insert(:direct_note_activity)
155 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
158 |> get("/activities/#{uuid}")
162 test "404s on nonexistent activities", %{conn: conn} do
164 |> get("/activities/123")
168 test "gets a notice in xml format", %{conn: conn} do
169 note_activity = insert(:note_activity)
172 |> get("/notice/#{note_activity.id}")
176 test "gets a notice in AS2 format", %{conn: conn} do
177 note_activity = insert(:note_activity)
180 |> put_req_header("accept", "application/activity+json")
181 |> get("/notice/#{note_activity.id}")
182 |> json_response(200)
185 test "only gets a notice in AS2 format for Create messages", %{conn: conn} do
186 note_activity = insert(:note_activity)
187 url = "/notice/#{note_activity.id}"
191 |> put_req_header("accept", "application/activity+json")
194 assert json_response(conn, 200)
198 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
199 url = "/notice/#{like_activity.id}"
201 assert like_activity.data["type"] == "Like"
205 |> put_req_header("accept", "application/activity+json")
208 assert response(conn, 404)
211 test "gets an activity in AS2 format", %{conn: conn} do
212 note_activity = insert(:note_activity)
213 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
214 url = "/activities/#{uuid}"
218 |> put_req_header("accept", "application/activity+json")
221 assert json_response(conn, 200)
224 test "404s a private notice", %{conn: conn} do
225 note_activity = insert(:direct_note_activity)
226 url = "/notice/#{note_activity.id}"
232 assert response(conn, 404)
235 test "404s a nonexisting notice", %{conn: conn} do
242 assert response(conn, 404)