fix tests broken by relay defaults changing (#314)
[akkoma] / test / pleroma / collections / collections_fetcher_test.exs
1 defmodule Akkoma.Collections.FetcherTest do
2 use Pleroma.DataCase
3 use Oban.Testing, repo: Pleroma.Repo
4
5 alias Akkoma.Collections.Fetcher
6
7 import Tesla.Mock
8
9 setup do
10 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
11 :ok
12 end
13
14 test "it should extract items from an embedded array in a Collection" do
15 unordered_collection =
16 "test/fixtures/collections/unordered_array.json"
17 |> File.read!()
18
19 ap_id = "https://example.com/collection/ordered_array"
20
21 Tesla.Mock.mock(fn
22 %{
23 method: :get,
24 url: ^ap_id
25 } ->
26 %Tesla.Env{
27 status: 200,
28 body: unordered_collection,
29 headers: [{"content-type", "application/activity+json"}]
30 }
31 end)
32
33 {:ok, objects} = Fetcher.fetch_collection(ap_id)
34 assert [%{"type" => "Create"}, %{"type" => "Like"}] = objects
35 end
36
37 test "it should extract items from an embedded array in an OrderedCollection" do
38 ordered_collection =
39 "test/fixtures/collections/ordered_array.json"
40 |> File.read!()
41
42 ap_id = "https://example.com/collection/ordered_array"
43
44 Tesla.Mock.mock(fn
45 %{
46 method: :get,
47 url: ^ap_id
48 } ->
49 %Tesla.Env{
50 status: 200,
51 body: ordered_collection,
52 headers: [{"content-type", "application/activity+json"}]
53 }
54 end)
55
56 {:ok, objects} = Fetcher.fetch_collection(ap_id)
57 assert [%{"type" => "Create"}, %{"type" => "Like"}] = objects
58 end
59
60 test "it should extract items from an referenced first page in a Collection" do
61 unordered_collection =
62 "test/fixtures/collections/unordered_page_reference.json"
63 |> File.read!()
64
65 first_page =
66 "test/fixtures/collections/unordered_page_first.json"
67 |> File.read!()
68
69 second_page =
70 "test/fixtures/collections/unordered_page_second.json"
71 |> File.read!()
72
73 ap_id = "https://example.com/collection/unordered_page_reference"
74 first_page_id = "https://example.com/collection/unordered_page_reference?page=1"
75 second_page_id = "https://example.com/collection/unordered_page_reference?page=2"
76
77 Tesla.Mock.mock(fn
78 %{
79 method: :get,
80 url: ^ap_id
81 } ->
82 %Tesla.Env{
83 status: 200,
84 body: unordered_collection,
85 headers: [{"content-type", "application/activity+json"}]
86 }
87
88 %{
89 method: :get,
90 url: ^first_page_id
91 } ->
92 %Tesla.Env{
93 status: 200,
94 body: first_page,
95 headers: [{"content-type", "application/activity+json"}]
96 }
97
98 %{
99 method: :get,
100 url: ^second_page_id
101 } ->
102 %Tesla.Env{
103 status: 200,
104 body: second_page,
105 headers: [{"content-type", "application/activity+json"}]
106 }
107 end)
108
109 {:ok, objects} = Fetcher.fetch_collection(ap_id)
110 assert [%{"type" => "Create"}, %{"type" => "Like"}] = objects
111 end
112
113 test "it should stop fetching when we hit :max_collection_objects" do
114 clear_config([:activitypub, :max_collection_objects], 1)
115
116 unordered_collection =
117 "test/fixtures/collections/unordered_page_reference.json"
118 |> File.read!()
119
120 first_page =
121 "test/fixtures/collections/unordered_page_first.json"
122 |> File.read!()
123
124 second_page =
125 "test/fixtures/collections/unordered_page_second.json"
126 |> File.read!()
127
128 ap_id = "https://example.com/collection/unordered_page_reference"
129 first_page_id = "https://example.com/collection/unordered_page_reference?page=1"
130 second_page_id = "https://example.com/collection/unordered_page_reference?page=2"
131
132 Tesla.Mock.mock(fn
133 %{
134 method: :get,
135 url: ^ap_id
136 } ->
137 %Tesla.Env{
138 status: 200,
139 body: unordered_collection,
140 headers: [{"content-type", "application/activity+json"}]
141 }
142
143 %{
144 method: :get,
145 url: ^first_page_id
146 } ->
147 %Tesla.Env{
148 status: 200,
149 body: first_page,
150 headers: [{"content-type", "application/activity+json"}]
151 }
152
153 %{
154 method: :get,
155 url: ^second_page_id
156 } ->
157 %Tesla.Env{
158 status: 200,
159 body: second_page,
160 headers: [{"content-type", "application/activity+json"}]
161 }
162 end)
163
164 {:ok, objects} = Fetcher.fetch_collection(ap_id)
165 assert [%{"type" => "Create"}] = objects
166 end
167
168 test "it should stop fetching when we hit a 404" do
169 clear_config([:activitypub, :max_collection_objects], 1)
170
171 unordered_collection =
172 "test/fixtures/collections/unordered_page_reference.json"
173 |> File.read!()
174
175 first_page =
176 "test/fixtures/collections/unordered_page_first.json"
177 |> File.read!()
178
179 ap_id = "https://example.com/collection/unordered_page_reference"
180 first_page_id = "https://example.com/collection/unordered_page_reference?page=1"
181 second_page_id = "https://example.com/collection/unordered_page_reference?page=2"
182
183 Tesla.Mock.mock(fn
184 %{
185 method: :get,
186 url: ^ap_id
187 } ->
188 %Tesla.Env{
189 status: 200,
190 body: unordered_collection,
191 headers: [{"content-type", "application/activity+json"}]
192 }
193
194 %{
195 method: :get,
196 url: ^first_page_id
197 } ->
198 %Tesla.Env{
199 status: 200,
200 body: first_page,
201 headers: [{"content-type", "application/activity+json"}]
202 }
203
204 %{
205 method: :get,
206 url: ^second_page_id
207 } ->
208 %Tesla.Env{
209 status: 404,
210 body: nil,
211 headers: [{"content-type", "application/activity+json"}]
212 }
213 end)
214
215 {:ok, objects} = Fetcher.fetch_collection(ap_id)
216 assert [%{"type" => "Create"}] = objects
217 end
218 end