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