Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel
[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 wedistribute articles" do
81 {:ok, object} =
82 Fetcher.fetch_object_from_id("https://wedistribute.org/wp-json/pterotype/v1/object/85810")
83
84 assert object
85 end
86
87 test "all objects with fake directions are rejected by the object fetcher" do
88 assert {:error, _} =
89 Fetcher.fetch_and_contain_remote_object_from_id(
90 "https://info.pleroma.site/activity4.json"
91 )
92 end
93
94 test "handle HTTP 410 Gone response" do
95 assert {:error, "Object has been deleted"} ==
96 Fetcher.fetch_and_contain_remote_object_from_id(
97 "https://mastodon.example.org/users/userisgone"
98 )
99 end
100
101 test "handle HTTP 404 response" do
102 assert {:error, "Object has been deleted"} ==
103 Fetcher.fetch_and_contain_remote_object_from_id(
104 "https://mastodon.example.org/users/userisgone404"
105 )
106 end
107 end
108
109 describe "pruning" do
110 test "it can refetch pruned objects" do
111 object_id = "http://mastodon.example.org/@admin/99541947525187367"
112
113 {:ok, object} = Fetcher.fetch_object_from_id(object_id)
114
115 assert object
116
117 {:ok, _object} = Object.prune(object)
118
119 refute Object.get_by_ap_id(object_id)
120
121 {:ok, %Object{} = object_two} = Fetcher.fetch_object_from_id(object_id)
122
123 assert object.data["id"] == object_two.data["id"]
124 assert object.id != object_two.id
125 end
126 end
127
128 describe "signed fetches" do
129 clear_config([:activitypub, :sign_object_fetches])
130
131 test_with_mock "it signs fetches when configured to do so",
132 Pleroma.Signature,
133 [:passthrough],
134 [] do
135 Pleroma.Config.put([:activitypub, :sign_object_fetches], true)
136
137 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
138
139 assert called(Pleroma.Signature.sign(:_, :_))
140 end
141
142 test_with_mock "it doesn't sign fetches when not configured to do so",
143 Pleroma.Signature,
144 [:passthrough],
145 [] do
146 Pleroma.Config.put([:activitypub, :sign_object_fetches], false)
147
148 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
149
150 refute called(Pleroma.Signature.sign(:_, :_))
151 end
152 end
153 end