Merge branch 'develop' into test/activity_pub/transmogrifier.ex
[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 # Set a wrong magic-key for a user so it has to refetch
54 salmon_user = User.get_cached_by_ap_id("http://gs.example.org:4040/index.php/user/1")
55
56 # Wrong key
57 info_cng =
58 User.Info.remote_user_creation(salmon_user.info, %{
59 magic_key:
60 "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwrong1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB"
61 })
62
63 salmon_user
64 |> Ecto.Changeset.change()
65 |> Ecto.Changeset.put_embed(:info, info_cng)
66 |> User.update_and_set_cache()
67
68 assert capture_log(fn ->
69 conn =
70 build_conn()
71 |> put_req_header("content-type", "application/atom+xml")
72 |> post("/users/#{user.nickname}/salmon", salmon)
73
74 assert response(conn, 200)
75 end) =~ "[error]"
76 end
77 end
78
79 test "gets a feed", %{conn: conn} do
80 note_activity = insert(:note_activity)
81 object = Object.normalize(note_activity)
82 user = User.get_cached_by_ap_id(note_activity.data["actor"])
83
84 conn =
85 conn
86 |> put_req_header("content-type", "application/atom+xml")
87 |> get("/users/#{user.nickname}/feed.atom")
88
89 assert response(conn, 200) =~ object.data["content"]
90 end
91
92 test "returns 404 for a missing feed", %{conn: conn} do
93 conn =
94 conn
95 |> put_req_header("content-type", "application/atom+xml")
96 |> get("/users/nonexisting/feed.atom")
97
98 assert response(conn, 404)
99 end
100
101 describe "GET object/2" do
102 test "gets an object", %{conn: conn} do
103 note_activity = insert(:note_activity)
104 object = Object.normalize(note_activity)
105 user = User.get_cached_by_ap_id(note_activity.data["actor"])
106 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["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 "redirects to /notice/id for html format", %{conn: conn} do
124 note_activity = insert(:note_activity)
125 object = Object.normalize(note_activity)
126 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
127 url = "/objects/#{uuid}"
128
129 conn =
130 conn
131 |> put_req_header("accept", "text/html")
132 |> get(url)
133
134 assert redirected_to(conn) == "/notice/#{note_activity.id}"
135 end
136
137 test "500s when user not found", %{conn: conn} do
138 note_activity = insert(:note_activity)
139 object = Object.normalize(note_activity)
140 user = User.get_cached_by_ap_id(note_activity.data["actor"])
141 User.invalidate_cache(user)
142 Pleroma.Repo.delete(user)
143 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
144 url = "/objects/#{uuid}"
145
146 conn =
147 conn
148 |> put_req_header("accept", "application/xml")
149 |> get(url)
150
151 assert response(conn, 500) == ~S({"error":"Something went wrong"})
152 end
153
154 test "404s on private objects", %{conn: conn} do
155 note_activity = insert(:direct_note_activity)
156 object = Object.normalize(note_activity)
157 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
158
159 conn
160 |> get("/objects/#{uuid}")
161 |> response(404)
162 end
163
164 test "404s on nonexisting objects", %{conn: conn} do
165 conn
166 |> get("/objects/123")
167 |> response(404)
168 end
169 end
170
171 describe "GET activity/2" do
172 test "gets an activity in xml format", %{conn: conn} do
173 note_activity = insert(:note_activity)
174 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
175
176 conn
177 |> put_req_header("accept", "application/xml")
178 |> get("/activities/#{uuid}")
179 |> response(200)
180 end
181
182 test "redirects to /notice/id for html format", %{conn: conn} do
183 note_activity = insert(:note_activity)
184 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
185
186 conn =
187 conn
188 |> put_req_header("accept", "text/html")
189 |> get("/activities/#{uuid}")
190
191 assert redirected_to(conn) == "/notice/#{note_activity.id}"
192 end
193
194 test "505s when user not found", %{conn: conn} do
195 note_activity = insert(:note_activity)
196 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
197 user = User.get_cached_by_ap_id(note_activity.data["actor"])
198 User.invalidate_cache(user)
199 Pleroma.Repo.delete(user)
200
201 conn =
202 conn
203 |> put_req_header("accept", "text/html")
204 |> get("/activities/#{uuid}")
205
206 assert response(conn, 500) == ~S({"error":"Something went wrong"})
207 end
208
209 test "404s on deleted objects", %{conn: conn} do
210 note_activity = insert(:note_activity)
211 object = Object.normalize(note_activity)
212 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
213
214 conn
215 |> put_req_header("accept", "application/xml")
216 |> get("/objects/#{uuid}")
217 |> response(200)
218
219 Object.delete(object)
220
221 conn
222 |> put_req_header("accept", "application/xml")
223 |> get("/objects/#{uuid}")
224 |> response(404)
225 end
226
227 test "404s on private activities", %{conn: conn} do
228 note_activity = insert(:direct_note_activity)
229 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
230
231 conn
232 |> get("/activities/#{uuid}")
233 |> response(404)
234 end
235
236 test "404s on nonexistent activities", %{conn: conn} do
237 conn
238 |> get("/activities/123")
239 |> response(404)
240 end
241
242 test "gets an activity in AS2 format", %{conn: conn} do
243 note_activity = insert(:note_activity)
244 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
245 url = "/activities/#{uuid}"
246
247 conn =
248 conn
249 |> put_req_header("accept", "application/activity+json")
250 |> get(url)
251
252 assert json_response(conn, 200)
253 end
254 end
255
256 describe "GET notice/2" do
257 test "gets a notice in xml format", %{conn: conn} do
258 note_activity = insert(:note_activity)
259
260 conn
261 |> get("/notice/#{note_activity.id}")
262 |> response(200)
263 end
264
265 test "gets a notice in AS2 format", %{conn: conn} do
266 note_activity = insert(:note_activity)
267
268 conn
269 |> put_req_header("accept", "application/activity+json")
270 |> get("/notice/#{note_activity.id}")
271 |> json_response(200)
272 end
273
274 test "500s when actor not found", %{conn: conn} do
275 note_activity = insert(:note_activity)
276 user = User.get_cached_by_ap_id(note_activity.data["actor"])
277 User.invalidate_cache(user)
278 Pleroma.Repo.delete(user)
279
280 conn =
281 conn
282 |> get("/notice/#{note_activity.id}")
283
284 assert response(conn, 500) == ~S({"error":"Something went wrong"})
285 end
286
287 test "only gets a notice in AS2 format for Create messages", %{conn: conn} do
288 note_activity = insert(:note_activity)
289 url = "/notice/#{note_activity.id}"
290
291 conn =
292 conn
293 |> put_req_header("accept", "application/activity+json")
294 |> get(url)
295
296 assert json_response(conn, 200)
297
298 user = insert(:user)
299
300 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
301 url = "/notice/#{like_activity.id}"
302
303 assert like_activity.data["type"] == "Like"
304
305 conn =
306 build_conn()
307 |> put_req_header("accept", "application/activity+json")
308 |> get(url)
309
310 assert response(conn, 404)
311 end
312
313 test "render html for redirect for html format", %{conn: conn} do
314 note_activity = insert(:note_activity)
315
316 resp =
317 conn
318 |> put_req_header("accept", "text/html")
319 |> get("/notice/#{note_activity.id}")
320 |> response(200)
321
322 assert resp =~
323 "<meta content=\"#{Pleroma.Web.base_url()}/notice/#{note_activity.id}\" property=\"og:url\">"
324
325 user = insert(:user)
326
327 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
328
329 assert like_activity.data["type"] == "Like"
330
331 resp =
332 conn
333 |> put_req_header("accept", "text/html")
334 |> get("/notice/#{like_activity.id}")
335 |> response(200)
336
337 assert resp =~ "<!--server-generated-meta-->"
338 end
339
340 test "404s a private notice", %{conn: conn} do
341 note_activity = insert(:direct_note_activity)
342 url = "/notice/#{note_activity.id}"
343
344 conn =
345 conn
346 |> get(url)
347
348 assert response(conn, 404)
349 end
350
351 test "404s a nonexisting notice", %{conn: conn} do
352 url = "/notice/123"
353
354 conn =
355 conn
356 |> get(url)
357
358 assert response(conn, 404)
359 end
360 end
361
362 describe "feed_redirect" do
363 test "undefined format. it redirects to feed", %{conn: conn} do
364 note_activity = insert(:note_activity)
365 user = User.get_cached_by_ap_id(note_activity.data["actor"])
366
367 response =
368 conn
369 |> put_req_header("accept", "application/xml")
370 |> get("/users/#{user.nickname}")
371 |> response(302)
372
373 assert response ==
374 "<html><body>You are being <a href=\"#{Pleroma.Web.base_url()}/users/#{
375 user.nickname
376 }/feed.atom\">redirected</a>.</body></html>"
377 end
378
379 test "undefined format. it returns error when user not found", %{conn: conn} do
380 response =
381 conn
382 |> put_req_header("accept", "application/xml")
383 |> get("/users/jimm")
384 |> response(404)
385
386 assert response == ~S({"error":"Not found"})
387 end
388
389 test "activity+json format. it redirects on actual feed of user", %{conn: conn} do
390 note_activity = insert(:note_activity)
391 user = User.get_cached_by_ap_id(note_activity.data["actor"])
392
393 response =
394 conn
395 |> put_req_header("accept", "application/activity+json")
396 |> get("/users/#{user.nickname}")
397 |> json_response(200)
398
399 assert response["endpoints"] == %{
400 "oauthAuthorizationEndpoint" => "#{Pleroma.Web.base_url()}/oauth/authorize",
401 "oauthRegistrationEndpoint" => "#{Pleroma.Web.base_url()}/api/v1/apps",
402 "oauthTokenEndpoint" => "#{Pleroma.Web.base_url()}/oauth/token",
403 "sharedInbox" => "#{Pleroma.Web.base_url()}/inbox"
404 }
405
406 assert response["@context"] == [
407 "https://www.w3.org/ns/activitystreams",
408 "http://localhost:4001/schemas/litepub-0.1.jsonld",
409 %{"@language" => "und"}
410 ]
411
412 assert Map.take(response, [
413 "followers",
414 "following",
415 "id",
416 "inbox",
417 "manuallyApprovesFollowers",
418 "name",
419 "outbox",
420 "preferredUsername",
421 "summary",
422 "tag",
423 "type",
424 "url"
425 ]) == %{
426 "followers" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/followers",
427 "following" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/following",
428 "id" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}",
429 "inbox" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/inbox",
430 "manuallyApprovesFollowers" => false,
431 "name" => user.name,
432 "outbox" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/outbox",
433 "preferredUsername" => user.nickname,
434 "summary" => user.bio,
435 "tag" => [],
436 "type" => "Person",
437 "url" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
438 }
439 end
440
441 test "activity+json format. it returns error whe use not found", %{conn: conn} do
442 response =
443 conn
444 |> put_req_header("accept", "application/activity+json")
445 |> get("/users/jimm")
446 |> json_response(404)
447
448 assert response == "Not found"
449 end
450
451 test "json format. it redirects on actual feed of user", %{conn: conn} do
452 note_activity = insert(:note_activity)
453 user = User.get_cached_by_ap_id(note_activity.data["actor"])
454
455 response =
456 conn
457 |> put_req_header("accept", "application/json")
458 |> get("/users/#{user.nickname}")
459 |> json_response(200)
460
461 assert response["endpoints"] == %{
462 "oauthAuthorizationEndpoint" => "#{Pleroma.Web.base_url()}/oauth/authorize",
463 "oauthRegistrationEndpoint" => "#{Pleroma.Web.base_url()}/api/v1/apps",
464 "oauthTokenEndpoint" => "#{Pleroma.Web.base_url()}/oauth/token",
465 "sharedInbox" => "#{Pleroma.Web.base_url()}/inbox"
466 }
467
468 assert response["@context"] == [
469 "https://www.w3.org/ns/activitystreams",
470 "http://localhost:4001/schemas/litepub-0.1.jsonld",
471 %{"@language" => "und"}
472 ]
473
474 assert Map.take(response, [
475 "followers",
476 "following",
477 "id",
478 "inbox",
479 "manuallyApprovesFollowers",
480 "name",
481 "outbox",
482 "preferredUsername",
483 "summary",
484 "tag",
485 "type",
486 "url"
487 ]) == %{
488 "followers" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/followers",
489 "following" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/following",
490 "id" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}",
491 "inbox" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/inbox",
492 "manuallyApprovesFollowers" => false,
493 "name" => user.name,
494 "outbox" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/outbox",
495 "preferredUsername" => user.nickname,
496 "summary" => user.bio,
497 "tag" => [],
498 "type" => "Person",
499 "url" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
500 }
501 end
502
503 test "json format. it returns error whe use not found", %{conn: conn} do
504 response =
505 conn
506 |> put_req_header("accept", "application/json")
507 |> get("/users/jimm")
508 |> json_response(404)
509
510 assert response == "Not found"
511 end
512
513 test "html format. it redirects on actual feed of user", %{conn: conn} do
514 note_activity = insert(:note_activity)
515 user = User.get_cached_by_ap_id(note_activity.data["actor"])
516
517 response =
518 conn
519 |> get("/users/#{user.nickname}")
520 |> response(200)
521
522 assert response ==
523 Fallback.RedirectController.redirector_with_meta(
524 conn,
525 %{user: user}
526 ).resp_body
527 end
528
529 test "html format. it returns error when user not found", %{conn: conn} do
530 response =
531 conn
532 |> get("/users/jimm")
533 |> json_response(404)
534
535 assert response == %{"error" => "Not found"}
536 end
537 end
538
539 describe "GET /notice/:id/embed_player" do
540 test "render embed player", %{conn: conn} do
541 note_activity = insert(:note_activity)
542 object = Pleroma.Object.normalize(note_activity)
543
544 object_data =
545 Map.put(object.data, "attachment", [
546 %{
547 "url" => [
548 %{
549 "href" =>
550 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
551 "mediaType" => "video/mp4",
552 "type" => "Link"
553 }
554 ]
555 }
556 ])
557
558 object
559 |> Ecto.Changeset.change(data: object_data)
560 |> Pleroma.Repo.update()
561
562 conn =
563 conn
564 |> get("/notice/#{note_activity.id}/embed_player")
565
566 assert Plug.Conn.get_resp_header(conn, "x-frame-options") == ["ALLOW"]
567
568 assert Plug.Conn.get_resp_header(
569 conn,
570 "content-security-policy"
571 ) == [
572 "default-src 'none';style-src 'self' 'unsafe-inline';img-src 'self' data: https:; media-src 'self' https:;"
573 ]
574
575 assert response(conn, 200) =~
576 "<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>"
577 end
578
579 test "404s when activity isn't create", %{conn: conn} do
580 note_activity = insert(:note_activity, data_attrs: %{"type" => "Like"})
581
582 assert conn
583 |> get("/notice/#{note_activity.id}/embed_player")
584 |> response(404)
585 end
586
587 test "404s when activity is direct message", %{conn: conn} do
588 note_activity = insert(:note_activity, data_attrs: %{"directMessage" => true})
589
590 assert conn
591 |> get("/notice/#{note_activity.id}/embed_player")
592 |> response(404)
593 end
594
595 test "404s when attachment is empty", %{conn: conn} do
596 note_activity = insert(:note_activity)
597 object = Pleroma.Object.normalize(note_activity)
598 object_data = Map.put(object.data, "attachment", [])
599
600 object
601 |> Ecto.Changeset.change(data: object_data)
602 |> Pleroma.Repo.update()
603
604 assert conn
605 |> get("/notice/#{note_activity.id}/embed_player")
606 |> response(404)
607 end
608
609 test "404s when attachment isn't audio or video", %{conn: conn} do
610 note_activity = insert(:note_activity)
611 object = Pleroma.Object.normalize(note_activity)
612
613 object_data =
614 Map.put(object.data, "attachment", [
615 %{
616 "url" => [
617 %{
618 "href" => "https://peertube.moe/static/webseed/480.jpg",
619 "mediaType" => "image/jpg",
620 "type" => "Link"
621 }
622 ]
623 }
624 ])
625
626 object
627 |> Ecto.Changeset.change(data: object_data)
628 |> Pleroma.Repo.update()
629
630 assert conn
631 |> get("/notice/#{note_activity.id}/embed_player")
632 |> response(404)
633 end
634 end
635 end