[#534] Merged `upstream/develop`.
[akkoma] / test / web / ostatus / ostatus_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.OStatus.OStatusControllerTest do
6 use Pleroma.Web.ConnCase
7 import Pleroma.Factory
8 alias Pleroma.{User, Repo, Object, Instances}
9 alias Pleroma.Web.CommonAPI
10 alias Pleroma.Web.OStatus.ActivityRepresenter
11
12 setup_all do
13 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
14 :ok
15 end
16
17 describe "salmon_incoming" do
18 test "decodes a salmon", %{conn: conn} do
19 user = insert(:user)
20 salmon = File.read!("test/fixtures/salmon.xml")
21
22 conn =
23 conn
24 |> put_req_header("content-type", "application/atom+xml")
25 |> post("/users/#{user.nickname}/salmon", salmon)
26
27 assert response(conn, 200)
28 end
29
30 test "decodes a salmon with a changed magic key", %{conn: conn} do
31 user = insert(:user)
32 salmon = File.read!("test/fixtures/salmon.xml")
33
34 conn =
35 conn
36 |> put_req_header("content-type", "application/atom+xml")
37 |> post("/users/#{user.nickname}/salmon", salmon)
38
39 assert response(conn, 200)
40
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")
43 # Wrong key
44 info_cng =
45 User.Info.remote_user_creation(salmon_user.info, %{
46 magic_key:
47 "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwrong1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB"
48 })
49
50 salmon_user
51 |> Ecto.Changeset.change()
52 |> Ecto.Changeset.put_embed(:info, info_cng)
53 |> Repo.update()
54
55 conn =
56 build_conn()
57 |> put_req_header("content-type", "application/atom+xml")
58 |> post("/users/#{user.nickname}/salmon", salmon)
59
60 assert response(conn, 200)
61 end
62
63 test "it clears `unreachable` federation status of the sender", %{conn: conn} do
64 sender_url = "https://pleroma.soykaf.com"
65 Instances.set_consistently_unreachable(sender_url)
66 refute Instances.reachable?(sender_url)
67
68 user = insert(:user)
69 salmon = File.read!("test/fixtures/salmon.xml")
70
71 conn =
72 conn
73 |> put_req_header("content-type", "application/atom+xml")
74 |> put_req_header("referer", sender_url)
75 |> post("/users/#{user.nickname}/salmon", salmon)
76
77 assert response(conn, 200)
78 assert Instances.reachable?(sender_url)
79 end
80 end
81
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"])
85
86 conn =
87 conn
88 |> put_req_header("content-type", "application/atom+xml")
89 |> get("/users/#{user.nickname}/feed.atom")
90
91 assert response(conn, 200) =~ note_activity.data["object"]["content"]
92 end
93
94 test "returns 404 for a missing feed", %{conn: conn} do
95 conn =
96 conn
97 |> put_req_header("content-type", "application/atom+xml")
98 |> get("/users/nonexisting/feed.atom")
99
100 assert response(conn, 404)
101 end
102
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}"
108
109 conn =
110 conn
111 |> put_req_header("accept", "application/xml")
112 |> get(url)
113
114 expected =
115 ActivityRepresenter.to_simple_form(note_activity, user, true)
116 |> ActivityRepresenter.wrap_with_entry()
117 |> :xmerl.export_simple(:xmerl_xml)
118 |> to_string
119
120 assert response(conn, 200) == expected
121 end
122
123 test "404s on private objects", %{conn: conn} do
124 note_activity = insert(:direct_note_activity)
125 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
126
127 conn
128 |> get("/objects/#{uuid}")
129 |> response(404)
130 end
131
132 test "404s on nonexisting objects", %{conn: conn} do
133 conn
134 |> get("/objects/123")
135 |> response(404)
136 end
137
138 test "gets an activity in xml format", %{conn: conn} do
139 note_activity = insert(:note_activity)
140 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
141
142 conn
143 |> put_req_header("accept", "application/xml")
144 |> get("/activities/#{uuid}")
145 |> response(200)
146 end
147
148 test "404s on deleted objects", %{conn: conn} do
149 note_activity = insert(:note_activity)
150 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
151 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
152
153 conn
154 |> put_req_header("accept", "application/xml")
155 |> get("/objects/#{uuid}")
156 |> response(200)
157
158 Object.delete(object)
159
160 conn
161 |> put_req_header("accept", "application/xml")
162 |> get("/objects/#{uuid}")
163 |> response(404)
164 end
165
166 test "404s on private activities", %{conn: conn} do
167 note_activity = insert(:direct_note_activity)
168 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
169
170 conn
171 |> get("/activities/#{uuid}")
172 |> response(404)
173 end
174
175 test "404s on nonexistent activities", %{conn: conn} do
176 conn
177 |> get("/activities/123")
178 |> response(404)
179 end
180
181 test "gets a notice in xml format", %{conn: conn} do
182 note_activity = insert(:note_activity)
183
184 conn
185 |> get("/notice/#{note_activity.id}")
186 |> response(200)
187 end
188
189 test "gets a notice in AS2 format", %{conn: conn} do
190 note_activity = insert(:note_activity)
191
192 conn
193 |> put_req_header("accept", "application/activity+json")
194 |> get("/notice/#{note_activity.id}")
195 |> json_response(200)
196 end
197
198 test "only gets a notice in AS2 format for Create messages", %{conn: conn} do
199 note_activity = insert(:note_activity)
200 url = "/notice/#{note_activity.id}"
201
202 conn =
203 conn
204 |> put_req_header("accept", "application/activity+json")
205 |> get(url)
206
207 assert json_response(conn, 200)
208
209 user = insert(:user)
210
211 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
212 url = "/notice/#{like_activity.id}"
213
214 assert like_activity.data["type"] == "Like"
215
216 conn =
217 build_conn()
218 |> put_req_header("accept", "application/activity+json")
219 |> get(url)
220
221 assert response(conn, 404)
222 end
223
224 test "gets an activity in AS2 format", %{conn: conn} do
225 note_activity = insert(:note_activity)
226 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
227 url = "/activities/#{uuid}"
228
229 conn =
230 conn
231 |> put_req_header("accept", "application/activity+json")
232 |> get(url)
233
234 assert json_response(conn, 200)
235 end
236
237 test "404s a private notice", %{conn: conn} do
238 note_activity = insert(:direct_note_activity)
239 url = "/notice/#{note_activity.id}"
240
241 conn =
242 conn
243 |> get(url)
244
245 assert response(conn, 404)
246 end
247
248 test "404s a nonexisting notice", %{conn: conn} do
249 url = "/notice/123"
250
251 conn =
252 conn
253 |> get(url)
254
255 assert response(conn, 404)
256 end
257 end