Improved in-test `clear_config/n` applicability (setup / setup_all / in-test usage).
[akkoma] / test / object / fetcher_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Object.FetcherTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Activity
9 alias Pleroma.Object
10 alias Pleroma.Object.Fetcher
11 import Tesla.Mock
12 import Mock
13
14 setup do
15 mock(fn
16 %{method: :get, url: "https://mastodon.example.org/users/userisgone"} ->
17 %Tesla.Env{status: 410}
18
19 %{method: :get, url: "https://mastodon.example.org/users/userisgone404"} ->
20 %Tesla.Env{status: 404}
21
22 env ->
23 apply(HttpRequestMock, :request, [env])
24 end)
25
26 :ok
27 end
28
29 describe "max thread distance restriction" do
30 @ap_id "http://mastodon.example.org/@admin/99541947525187367"
31 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
32
33 test "it returns thread depth exceeded error if thread depth is exceeded" do
34 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
35
36 assert {:error, "Max thread distance exceeded."} =
37 Fetcher.fetch_object_from_id(@ap_id, depth: 1)
38 end
39
40 test "it fetches object if max thread depth is restricted to 0 and depth is not specified" do
41 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
42
43 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id)
44 end
45
46 test "it fetches object if requested depth does not exceed max thread depth" do
47 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
48
49 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id, depth: 10)
50 end
51 end
52
53 describe "actor origin containment" do
54 test "it rejects objects with a bogus origin" do
55 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity.json")
56 end
57
58 test "it rejects objects when attributedTo is wrong (variant 1)" do
59 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity2.json")
60 end
61
62 test "it rejects objects when attributedTo is wrong (variant 2)" do
63 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity3.json")
64 end
65 end
66
67 describe "fetching an object" do
68 test "it fetches an object" do
69 {:ok, object} =
70 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
71
72 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
73 assert activity.data["id"]
74
75 {:ok, object_again} =
76 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
77
78 assert [attachment] = object.data["attachment"]
79 assert is_list(attachment["url"])
80
81 assert object == object_again
82 end
83 end
84
85 describe "implementation quirks" do
86 test "it can fetch plume articles" do
87 {:ok, object} =
88 Fetcher.fetch_object_from_id(
89 "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
90 )
91
92 assert object
93 end
94
95 test "it can fetch peertube videos" do
96 {:ok, object} =
97 Fetcher.fetch_object_from_id(
98 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
99 )
100
101 assert object
102 end
103
104 test "it can fetch Mobilizon events" do
105 {:ok, object} =
106 Fetcher.fetch_object_from_id(
107 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
108 )
109
110 assert object
111 end
112
113 test "it can fetch wedistribute articles" do
114 {:ok, object} =
115 Fetcher.fetch_object_from_id("https://wedistribute.org/wp-json/pterotype/v1/object/85810")
116
117 assert object
118 end
119
120 test "all objects with fake directions are rejected by the object fetcher" do
121 assert {:error, _} =
122 Fetcher.fetch_and_contain_remote_object_from_id(
123 "https://info.pleroma.site/activity4.json"
124 )
125 end
126
127 test "handle HTTP 410 Gone response" do
128 assert {:error, "Object has been deleted"} ==
129 Fetcher.fetch_and_contain_remote_object_from_id(
130 "https://mastodon.example.org/users/userisgone"
131 )
132 end
133
134 test "handle HTTP 404 response" do
135 assert {:error, "Object has been deleted"} ==
136 Fetcher.fetch_and_contain_remote_object_from_id(
137 "https://mastodon.example.org/users/userisgone404"
138 )
139 end
140 end
141
142 describe "pruning" do
143 test "it can refetch pruned objects" do
144 object_id = "http://mastodon.example.org/@admin/99541947525187367"
145
146 {:ok, object} = Fetcher.fetch_object_from_id(object_id)
147
148 assert object
149
150 {:ok, _object} = Object.prune(object)
151
152 refute Object.get_by_ap_id(object_id)
153
154 {:ok, %Object{} = object_two} = Fetcher.fetch_object_from_id(object_id)
155
156 assert object.data["id"] == object_two.data["id"]
157 assert object.id != object_two.id
158 end
159 end
160
161 describe "signed fetches" do
162 setup do: clear_config([:activitypub, :sign_object_fetches])
163
164 test_with_mock "it signs fetches when configured to do so",
165 Pleroma.Signature,
166 [:passthrough],
167 [] do
168 Pleroma.Config.put([:activitypub, :sign_object_fetches], true)
169
170 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
171
172 assert called(Pleroma.Signature.sign(:_, :_))
173 end
174
175 test_with_mock "it doesn't sign fetches when not configured to do so",
176 Pleroma.Signature,
177 [:passthrough],
178 [] do
179 Pleroma.Config.put([:activitypub, :sign_object_fetches], false)
180
181 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
182
183 refute called(Pleroma.Signature.sign(:_, :_))
184 end
185 end
186 end