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