[#1304] Moved all non-mutes / non-blocks fields from User.Info to User. WIP.
[akkoma] / test / web / ostatus / ostatus_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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
8 import ExUnit.CaptureLog
9 import Pleroma.Factory
10
11 alias Pleroma.Object
12 alias Pleroma.User
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.OStatus.ActivityRepresenter
15
16 setup_all do
17 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
18 :ok
19 end
20
21 clear_config_all([:instance, :federating]) do
22 Pleroma.Config.put([:instance, :federating], true)
23 end
24
25 describe "salmon_incoming" do
26 test "decodes a salmon", %{conn: conn} do
27 user = insert(:user)
28 salmon = File.read!("test/fixtures/salmon.xml")
29
30 assert capture_log(fn ->
31 conn =
32 conn
33 |> put_req_header("content-type", "application/atom+xml")
34 |> post("/users/#{user.nickname}/salmon", salmon)
35
36 assert response(conn, 200)
37 end) =~ "[error]"
38 end
39
40 test "decodes a salmon with a changed magic key", %{conn: conn} do
41 user = insert(:user)
42 salmon = File.read!("test/fixtures/salmon.xml")
43
44 assert capture_log(fn ->
45 conn =
46 conn
47 |> put_req_header("content-type", "application/atom+xml")
48 |> post("/users/#{user.nickname}/salmon", salmon)
49
50 assert response(conn, 200)
51 end) =~ "[error]"
52
53 # Wrong key
54 update_params = %{
55 magic_key:
56 "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwrong1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB"
57 }
58
59 # Set a wrong magic-key for a user so it has to refetch
60 "http://gs.example.org:4040/index.php/user/1"
61 |> User.get_cached_by_ap_id()
62 |> User.update_changeset(update_params)
63 |> User.update_and_set_cache()
64
65 assert capture_log(fn ->
66 conn =
67 build_conn()
68 |> put_req_header("content-type", "application/atom+xml")
69 |> post("/users/#{user.nickname}/salmon", salmon)
70
71 assert response(conn, 200)
72 end) =~ "[error]"
73 end
74 end
75
76 describe "GET object/2" do
77 test "gets an object", %{conn: conn} do
78 note_activity = insert(:note_activity)
79 object = Object.normalize(note_activity)
80 user = User.get_cached_by_ap_id(note_activity.data["actor"])
81 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
82 url = "/objects/#{uuid}"
83
84 conn =
85 conn
86 |> put_req_header("accept", "application/xml")
87 |> get(url)
88
89 expected =
90 ActivityRepresenter.to_simple_form(note_activity, user, true)
91 |> ActivityRepresenter.wrap_with_entry()
92 |> :xmerl.export_simple(:xmerl_xml)
93 |> to_string
94
95 assert response(conn, 200) == expected
96 end
97
98 test "redirects to /notice/id for html format", %{conn: conn} do
99 note_activity = insert(:note_activity)
100 object = Object.normalize(note_activity)
101 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
102 url = "/objects/#{uuid}"
103
104 conn =
105 conn
106 |> put_req_header("accept", "text/html")
107 |> get(url)
108
109 assert redirected_to(conn) == "/notice/#{note_activity.id}"
110 end
111
112 test "500s when user not found", %{conn: conn} do
113 note_activity = insert(:note_activity)
114 object = Object.normalize(note_activity)
115 user = User.get_cached_by_ap_id(note_activity.data["actor"])
116 User.invalidate_cache(user)
117 Pleroma.Repo.delete(user)
118 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
119 url = "/objects/#{uuid}"
120
121 conn =
122 conn
123 |> put_req_header("accept", "application/xml")
124 |> get(url)
125
126 assert response(conn, 500) == ~S({"error":"Something went wrong"})
127 end
128
129 test "404s on private objects", %{conn: conn} do
130 note_activity = insert(:direct_note_activity)
131 object = Object.normalize(note_activity)
132 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
133
134 conn
135 |> get("/objects/#{uuid}")
136 |> response(404)
137 end
138
139 test "404s on nonexisting objects", %{conn: conn} do
140 conn
141 |> get("/objects/123")
142 |> response(404)
143 end
144 end
145
146 describe "GET activity/2" do
147 test "gets an activity in xml format", %{conn: conn} do
148 note_activity = insert(:note_activity)
149 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
150
151 conn
152 |> put_req_header("accept", "application/xml")
153 |> get("/activities/#{uuid}")
154 |> response(200)
155 end
156
157 test "redirects to /notice/id for html format", %{conn: conn} do
158 note_activity = insert(:note_activity)
159 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
160
161 conn =
162 conn
163 |> put_req_header("accept", "text/html")
164 |> get("/activities/#{uuid}")
165
166 assert redirected_to(conn) == "/notice/#{note_activity.id}"
167 end
168
169 test "505s when user not found", %{conn: conn} do
170 note_activity = insert(:note_activity)
171 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
172 user = User.get_cached_by_ap_id(note_activity.data["actor"])
173 User.invalidate_cache(user)
174 Pleroma.Repo.delete(user)
175
176 conn =
177 conn
178 |> put_req_header("accept", "text/html")
179 |> get("/activities/#{uuid}")
180
181 assert response(conn, 500) == ~S({"error":"Something went wrong"})
182 end
183
184 test "404s on deleted objects", %{conn: conn} do
185 note_activity = insert(:note_activity)
186 object = Object.normalize(note_activity)
187 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
188
189 conn
190 |> put_req_header("accept", "application/xml")
191 |> get("/objects/#{uuid}")
192 |> response(200)
193
194 Object.delete(object)
195
196 conn
197 |> put_req_header("accept", "application/xml")
198 |> get("/objects/#{uuid}")
199 |> response(404)
200 end
201
202 test "404s on private activities", %{conn: conn} do
203 note_activity = insert(:direct_note_activity)
204 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
205
206 conn
207 |> get("/activities/#{uuid}")
208 |> response(404)
209 end
210
211 test "404s on nonexistent activities", %{conn: conn} do
212 conn
213 |> get("/activities/123")
214 |> response(404)
215 end
216
217 test "gets an activity in AS2 format", %{conn: conn} do
218 note_activity = insert(:note_activity)
219 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
220 url = "/activities/#{uuid}"
221
222 conn =
223 conn
224 |> put_req_header("accept", "application/activity+json")
225 |> get(url)
226
227 assert json_response(conn, 200)
228 end
229 end
230
231 describe "GET notice/2" do
232 test "gets a notice in xml format", %{conn: conn} do
233 note_activity = insert(:note_activity)
234
235 conn
236 |> get("/notice/#{note_activity.id}")
237 |> response(200)
238 end
239
240 test "gets a notice in AS2 format", %{conn: conn} do
241 note_activity = insert(:note_activity)
242
243 conn
244 |> put_req_header("accept", "application/activity+json")
245 |> get("/notice/#{note_activity.id}")
246 |> json_response(200)
247 end
248
249 test "500s when actor not found", %{conn: conn} do
250 note_activity = insert(:note_activity)
251 user = User.get_cached_by_ap_id(note_activity.data["actor"])
252 User.invalidate_cache(user)
253 Pleroma.Repo.delete(user)
254
255 conn =
256 conn
257 |> get("/notice/#{note_activity.id}")
258
259 assert response(conn, 500) == ~S({"error":"Something went wrong"})
260 end
261
262 test "only gets a notice in AS2 format for Create messages", %{conn: conn} do
263 note_activity = insert(:note_activity)
264 url = "/notice/#{note_activity.id}"
265
266 conn =
267 conn
268 |> put_req_header("accept", "application/activity+json")
269 |> get(url)
270
271 assert json_response(conn, 200)
272
273 user = insert(:user)
274
275 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
276 url = "/notice/#{like_activity.id}"
277
278 assert like_activity.data["type"] == "Like"
279
280 conn =
281 build_conn()
282 |> put_req_header("accept", "application/activity+json")
283 |> get(url)
284
285 assert response(conn, 404)
286 end
287
288 test "render html for redirect for html format", %{conn: conn} do
289 note_activity = insert(:note_activity)
290
291 resp =
292 conn
293 |> put_req_header("accept", "text/html")
294 |> get("/notice/#{note_activity.id}")
295 |> response(200)
296
297 assert resp =~
298 "<meta content=\"#{Pleroma.Web.base_url()}/notice/#{note_activity.id}\" property=\"og:url\">"
299
300 user = insert(:user)
301
302 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
303
304 assert like_activity.data["type"] == "Like"
305
306 resp =
307 conn
308 |> put_req_header("accept", "text/html")
309 |> get("/notice/#{like_activity.id}")
310 |> response(200)
311
312 assert resp =~ "<!--server-generated-meta-->"
313 end
314
315 test "404s a private notice", %{conn: conn} do
316 note_activity = insert(:direct_note_activity)
317 url = "/notice/#{note_activity.id}"
318
319 conn =
320 conn
321 |> get(url)
322
323 assert response(conn, 404)
324 end
325
326 test "404s a nonexisting notice", %{conn: conn} do
327 url = "/notice/123"
328
329 conn =
330 conn
331 |> get(url)
332
333 assert response(conn, 404)
334 end
335 end
336
337 describe "GET /notice/:id/embed_player" do
338 test "render embed player", %{conn: conn} do
339 note_activity = insert(:note_activity)
340 object = Pleroma.Object.normalize(note_activity)
341
342 object_data =
343 Map.put(object.data, "attachment", [
344 %{
345 "url" => [
346 %{
347 "href" =>
348 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
349 "mediaType" => "video/mp4",
350 "type" => "Link"
351 }
352 ]
353 }
354 ])
355
356 object
357 |> Ecto.Changeset.change(data: object_data)
358 |> Pleroma.Repo.update()
359
360 conn =
361 conn
362 |> get("/notice/#{note_activity.id}/embed_player")
363
364 assert Plug.Conn.get_resp_header(conn, "x-frame-options") == ["ALLOW"]
365
366 assert Plug.Conn.get_resp_header(
367 conn,
368 "content-security-policy"
369 ) == [
370 "default-src 'none';style-src 'self' 'unsafe-inline';img-src 'self' data: https:; media-src 'self' https:;"
371 ]
372
373 assert response(conn, 200) =~
374 "<video controls loop><source src=\"https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4\" type=\"video/mp4\">Your browser does not support video/mp4 playback.</video>"
375 end
376
377 test "404s when activity isn't create", %{conn: conn} do
378 note_activity = insert(:note_activity, data_attrs: %{"type" => "Like"})
379
380 assert conn
381 |> get("/notice/#{note_activity.id}/embed_player")
382 |> response(404)
383 end
384
385 test "404s when activity is direct message", %{conn: conn} do
386 note_activity = insert(:note_activity, data_attrs: %{"directMessage" => true})
387
388 assert conn
389 |> get("/notice/#{note_activity.id}/embed_player")
390 |> response(404)
391 end
392
393 test "404s when attachment is empty", %{conn: conn} do
394 note_activity = insert(:note_activity)
395 object = Pleroma.Object.normalize(note_activity)
396 object_data = Map.put(object.data, "attachment", [])
397
398 object
399 |> Ecto.Changeset.change(data: object_data)
400 |> Pleroma.Repo.update()
401
402 assert conn
403 |> get("/notice/#{note_activity.id}/embed_player")
404 |> response(404)
405 end
406
407 test "404s when attachment isn't audio or video", %{conn: conn} do
408 note_activity = insert(:note_activity)
409 object = Pleroma.Object.normalize(note_activity)
410
411 object_data =
412 Map.put(object.data, "attachment", [
413 %{
414 "url" => [
415 %{
416 "href" => "https://peertube.moe/static/webseed/480.jpg",
417 "mediaType" => "image/jpg",
418 "type" => "Link"
419 }
420 ]
421 }
422 ])
423
424 object
425 |> Ecto.Changeset.change(data: object_data)
426 |> Pleroma.Repo.update()
427
428 assert conn
429 |> get("/notice/#{note_activity.id}/embed_player")
430 |> response(404)
431 end
432 end
433 end