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}
9 alias Pleroma.Web.CommonAPI
10 alias Pleroma.Web.OStatus.ActivityRepresenter
13 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
17 test "decodes a salmon", %{conn: conn} do
19 salmon = File.read!("test/fixtures/salmon.xml")
23 |> put_req_header("content-type", "application/atom+xml")
24 |> post("/users/#{user.nickname}/salmon", salmon)
26 assert response(conn, 200)
29 test "decodes a salmon with a changed magic key", %{conn: conn} do
31 salmon = File.read!("test/fixtures/salmon.xml")
35 |> put_req_header("content-type", "application/atom+xml")
36 |> post("/users/#{user.nickname}/salmon", salmon)
38 assert response(conn, 200)
40 # Set a wrong magic-key for a user so it has to refetch
41 salmon_user = User.get_by_ap_id("http://gs.example.org:4040/index.php/user/1")
44 User.Info.remote_user_creation(salmon_user.info, %{
46 "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwrong1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB"
50 |> Ecto.Changeset.change()
51 |> Ecto.Changeset.put_embed(:info, info_cng)
56 |> put_req_header("content-type", "application/atom+xml")
57 |> post("/users/#{user.nickname}/salmon", salmon)
59 assert response(conn, 200)
62 test "gets a feed", %{conn: conn} do
63 note_activity = insert(:note_activity)
64 user = User.get_cached_by_ap_id(note_activity.data["actor"])
68 |> put_req_header("content-type", "application/atom+xml")
69 |> get("/users/#{user.nickname}/feed.atom")
71 assert response(conn, 200) =~ note_activity.data["object"]["content"]
74 test "returns 404 for a missing feed", %{conn: conn} do
77 |> put_req_header("content-type", "application/atom+xml")
78 |> get("/users/nonexisting/feed.atom")
80 assert response(conn, 404)
83 test "gets an object", %{conn: conn} do
84 note_activity = insert(:note_activity)
85 user = User.get_by_ap_id(note_activity.data["actor"])
86 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
87 url = "/objects/#{uuid}"
91 |> put_req_header("accept", "application/xml")
95 ActivityRepresenter.to_simple_form(note_activity, user, true)
96 |> ActivityRepresenter.wrap_with_entry()
97 |> :xmerl.export_simple(:xmerl_xml)
100 assert response(conn, 200) == expected
103 test "404s on private objects", %{conn: conn} do
104 note_activity = insert(:direct_note_activity)
105 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
108 |> get("/objects/#{uuid}")
112 test "404s on nonexisting objects", %{conn: conn} do
114 |> get("/objects/123")
118 test "gets an activity in xml format", %{conn: conn} do
119 note_activity = insert(:note_activity)
120 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
123 |> put_req_header("accept", "application/xml")
124 |> get("/activities/#{uuid}")
128 test "404s on deleted objects", %{conn: conn} do
129 note_activity = insert(:note_activity)
130 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
131 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
134 |> put_req_header("accept", "application/xml")
135 |> get("/objects/#{uuid}")
138 Object.delete(object)
141 |> put_req_header("accept", "application/xml")
142 |> get("/objects/#{uuid}")
146 test "404s on private activities", %{conn: conn} do
147 note_activity = insert(:direct_note_activity)
148 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
151 |> get("/activities/#{uuid}")
155 test "404s on nonexistent activities", %{conn: conn} do
157 |> get("/activities/123")
161 test "gets a notice in xml format", %{conn: conn} do
162 note_activity = insert(:note_activity)
165 |> get("/notice/#{note_activity.id}")
169 test "gets a notice in AS2 format", %{conn: conn} do
170 note_activity = insert(:note_activity)
173 |> put_req_header("accept", "application/activity+json")
174 |> get("/notice/#{note_activity.id}")
175 |> json_response(200)
178 test "only gets a notice in AS2 format for Create messages", %{conn: conn} do
179 note_activity = insert(:note_activity)
180 url = "/notice/#{note_activity.id}"
184 |> put_req_header("accept", "application/activity+json")
187 assert json_response(conn, 200)
191 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
192 url = "/notice/#{like_activity.id}"
194 assert like_activity.data["type"] == "Like"
198 |> put_req_header("accept", "application/activity+json")
201 assert response(conn, 404)
204 test "gets an activity in AS2 format", %{conn: conn} do
205 note_activity = insert(:note_activity)
206 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
207 url = "/activities/#{uuid}"
211 |> put_req_header("accept", "application/activity+json")
214 assert json_response(conn, 200)
217 test "404s a private notice", %{conn: conn} do
218 note_activity = insert(:direct_note_activity)
219 url = "/notice/#{note_activity.id}"
225 assert response(conn, 404)
228 test "404s a nonexisting notice", %{conn: conn} do
235 assert response(conn, 404)