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