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