Merge develop
[akkoma] / test / object / fetcher_test.exs
1 defmodule Pleroma.Object.FetcherTest do
2 use Pleroma.DataCase
3
4 alias Pleroma.Activity
5 alias Pleroma.Object
6 alias Pleroma.Object.Fetcher
7 import Tesla.Mock
8
9 setup do
10 mock(fn
11 %{method: :get, url: "https://mastodon.example.org/users/userisgone"} ->
12 %Tesla.Env{status: 410}
13
14 %{method: :get, url: "https://mastodon.example.org/users/userisgone404"} ->
15 %Tesla.Env{status: 404}
16
17 env ->
18 apply(HttpRequestMock, :request, [env])
19 end)
20
21 :ok
22 end
23
24 describe "actor origin containment" do
25 test "it rejects objects with a bogus origin" do
26 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity.json")
27 end
28
29 test "it rejects objects when attributedTo is wrong (variant 1)" do
30 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity2.json")
31 end
32
33 test "it rejects objects when attributedTo is wrong (variant 2)" do
34 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity3.json")
35 end
36 end
37
38 describe "fetching an object" do
39 test "it fetches an object" do
40 {:ok, object} =
41 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
42
43 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
44 assert activity.data["id"]
45
46 {:ok, object_again} =
47 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
48
49 assert [attachment] = object.data["attachment"]
50 assert is_list(attachment["url"])
51
52 assert object == object_again
53 end
54
55 test "it works with objects only available via Ostatus" do
56 {:ok, object} = Fetcher.fetch_object_from_id("https://shitposter.club/notice/2827873")
57 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
58 assert activity.data["id"]
59
60 {:ok, object_again} = Fetcher.fetch_object_from_id("https://shitposter.club/notice/2827873")
61
62 assert object == object_again
63 end
64
65 test "it correctly stitches up conversations between ostatus and ap" do
66 last = "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
67 {:ok, object} = Fetcher.fetch_object_from_id(last)
68
69 object = Object.get_by_ap_id(object.data["inReplyTo"])
70 assert object
71 end
72 end
73
74 describe "implementation quirks" do
75 test "it can fetch plume articles" do
76 {:ok, object} =
77 Fetcher.fetch_object_from_id(
78 "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
79 )
80
81 assert object
82 end
83
84 test "it can fetch peertube videos" do
85 {:ok, object} =
86 Fetcher.fetch_object_from_id(
87 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
88 )
89
90 assert object
91 end
92
93 test "all objects with fake directions are rejected by the object fetcher" do
94 assert {:error, _} =
95 Fetcher.fetch_and_contain_remote_object_from_id(
96 "https://info.pleroma.site/activity4.json"
97 )
98 end
99
100 test "handle HTTP 410 Gone response" do
101 assert {:error, "Object has been deleted"} ==
102 Fetcher.fetch_and_contain_remote_object_from_id(
103 "https://mastodon.example.org/users/userisgone"
104 )
105 end
106
107 test "handle HTTP 404 response" do
108 assert {:error, "Object has been deleted"} ==
109 Fetcher.fetch_and_contain_remote_object_from_id(
110 "https://mastodon.example.org/users/userisgone404"
111 )
112 end
113 end
114
115 describe "pruning" do
116 test "it can refetch pruned objects" do
117 object_id = "http://mastodon.example.org/@admin/99541947525187367"
118
119 {:ok, object} = Fetcher.fetch_object_from_id(object_id)
120
121 assert object
122
123 {:ok, _object} = Object.prune(object)
124
125 refute Object.get_by_ap_id(object_id)
126
127 {:ok, %Object{} = object_two} = Fetcher.fetch_object_from_id(object_id)
128
129 assert object.data["id"] == object_two.data["id"]
130 assert object.id != object_two.id
131 end
132 end
133 end