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