b9f84f5c40551190b391a5130cc620c668bc4e5e
[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_by_ap_id(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_by_ap_id(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_by_ap_id(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_by_ap_id(ap_id)
165 assert [%{"type" => "Create"}] = objects
166 end
167 end