Merge branch 'refactor/user' into 'develop'
[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 }
401
402 assert response["@context"] == [
403 "https://www.w3.org/ns/activitystreams",
404 "http://localhost:4001/schemas/litepub-0.1.jsonld",
405 %{"@language" => "und"}
406 ]
407
408 assert Map.take(response, [
409 "followers",
410 "following",
411 "id",
412 "inbox",
413 "manuallyApprovesFollowers",
414 "name",
415 "outbox",
416 "preferredUsername",
417 "summary",
418 "tag",
419 "type",
420 "url"
421 ]) == %{
422 "followers" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/followers",
423 "following" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/following",
424 "id" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}",
425 "inbox" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/inbox",
426 "manuallyApprovesFollowers" => false,
427 "name" => user.name,
428 "outbox" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/outbox",
429 "preferredUsername" => user.nickname,
430 "summary" => user.bio,
431 "tag" => [],
432 "type" => "Person",
433 "url" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
434 }
435 end
436
437 test "activity+json format. it returns error whe use not found", %{conn: conn} do
438 response =
439 conn
440 |> put_req_header("accept", "application/activity+json")
441 |> get("/users/jimm")
442 |> json_response(404)
443
444 assert response == "Not found"
445 end
446
447 test "json format. it redirects on actual feed of user", %{conn: conn} do
448 note_activity = insert(:note_activity)
449 user = User.get_cached_by_ap_id(note_activity.data["actor"])
450
451 response =
452 conn
453 |> put_req_header("accept", "application/json")
454 |> get("/users/#{user.nickname}")
455 |> json_response(200)
456
457 assert response["endpoints"] == %{
458 "oauthAuthorizationEndpoint" => "#{Pleroma.Web.base_url()}/oauth/authorize",
459 "oauthRegistrationEndpoint" => "#{Pleroma.Web.base_url()}/api/v1/apps",
460 "oauthTokenEndpoint" => "#{Pleroma.Web.base_url()}/oauth/token",
461 "sharedInbox" => "#{Pleroma.Web.base_url()}/inbox"
462 }
463
464 assert response["@context"] == [
465 "https://www.w3.org/ns/activitystreams",
466 "http://localhost:4001/schemas/litepub-0.1.jsonld",
467 %{"@language" => "und"}
468 ]
469
470 assert Map.take(response, [
471 "followers",
472 "following",
473 "id",
474 "inbox",
475 "manuallyApprovesFollowers",
476 "name",
477 "outbox",
478 "preferredUsername",
479 "summary",
480 "tag",
481 "type",
482 "url"
483 ]) == %{
484 "followers" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/followers",
485 "following" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/following",
486 "id" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}",
487 "inbox" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/inbox",
488 "manuallyApprovesFollowers" => false,
489 "name" => user.name,
490 "outbox" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/outbox",
491 "preferredUsername" => user.nickname,
492 "summary" => user.bio,
493 "tag" => [],
494 "type" => "Person",
495 "url" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
496 }
497 end
498
499 test "json format. it returns error whe use not found", %{conn: conn} do
500 response =
501 conn
502 |> put_req_header("accept", "application/json")
503 |> get("/users/jimm")
504 |> json_response(404)
505
506 assert response == "Not found"
507 end
508
509 test "html format. it redirects on actual feed of user", %{conn: conn} do
510 note_activity = insert(:note_activity)
511 user = User.get_cached_by_ap_id(note_activity.data["actor"])
512
513 response =
514 conn
515 |> get("/users/#{user.nickname}")
516 |> response(200)
517
518 assert response ==
519 Fallback.RedirectController.redirector_with_meta(
520 conn,
521 %{user: user}
522 ).resp_body
523 end
524
525 test "html format. it returns error when user not found", %{conn: conn} do
526 response =
527 conn
528 |> get("/users/jimm")
529 |> json_response(404)
530
531 assert response == %{"error" => "Not found"}
532 end
533 end
534
535 describe "GET /notice/:id/embed_player" do
536 test "render embed player", %{conn: conn} do
537 note_activity = insert(:note_activity)
538 object = Pleroma.Object.normalize(note_activity)
539
540 object_data =
541 Map.put(object.data, "attachment", [
542 %{
543 "url" => [
544 %{
545 "href" =>
546 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
547 "mediaType" => "video/mp4",
548 "type" => "Link"
549 }
550 ]
551 }
552 ])
553
554 object
555 |> Ecto.Changeset.change(data: object_data)
556 |> Pleroma.Repo.update()
557
558 conn =
559 conn
560 |> get("/notice/#{note_activity.id}/embed_player")
561
562 assert Plug.Conn.get_resp_header(conn, "x-frame-options") == ["ALLOW"]
563
564 assert Plug.Conn.get_resp_header(
565 conn,
566 "content-security-policy"
567 ) == [
568 "default-src 'none';style-src 'self' 'unsafe-inline';img-src 'self' data: https:; media-src 'self' https:;"
569 ]
570
571 assert response(conn, 200) =~
572 "<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>"
573 end
574
575 test "404s when activity isn't create", %{conn: conn} do
576 note_activity = insert(:note_activity, data_attrs: %{"type" => "Like"})
577
578 assert conn
579 |> get("/notice/#{note_activity.id}/embed_player")
580 |> response(404)
581 end
582
583 test "404s when activity is direct message", %{conn: conn} do
584 note_activity = insert(:note_activity, data_attrs: %{"directMessage" => true})
585
586 assert conn
587 |> get("/notice/#{note_activity.id}/embed_player")
588 |> response(404)
589 end
590
591 test "404s when attachment is empty", %{conn: conn} do
592 note_activity = insert(:note_activity)
593 object = Pleroma.Object.normalize(note_activity)
594 object_data = Map.put(object.data, "attachment", [])
595
596 object
597 |> Ecto.Changeset.change(data: object_data)
598 |> Pleroma.Repo.update()
599
600 assert conn
601 |> get("/notice/#{note_activity.id}/embed_player")
602 |> response(404)
603 end
604
605 test "404s when attachment isn't audio or video", %{conn: conn} do
606 note_activity = insert(:note_activity)
607 object = Pleroma.Object.normalize(note_activity)
608
609 object_data =
610 Map.put(object.data, "attachment", [
611 %{
612 "url" => [
613 %{
614 "href" => "https://peertube.moe/static/webseed/480.jpg",
615 "mediaType" => "image/jpg",
616 "type" => "Link"
617 }
618 ]
619 }
620 ])
621
622 object
623 |> Ecto.Changeset.change(data: object_data)
624 |> Pleroma.Repo.update()
625
626 assert conn
627 |> get("/notice/#{note_activity.id}/embed_player")
628 |> response(404)
629 end
630 end
631 end