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