Add compressed background
[akkoma] / test / web / activity_pub / utils_test.exs
1 defmodule Pleroma.Web.ActivityPub.UtilsTest do
2 use Pleroma.DataCase
3 alias Pleroma.Activity
4 alias Pleroma.Repo
5 alias Pleroma.User
6 alias Pleroma.Web.ActivityPub.ActivityPub
7 alias Pleroma.Web.ActivityPub.Utils
8 alias Pleroma.Web.CommonAPI
9
10 import Pleroma.Factory
11
12 describe "fetch the latest Follow" do
13 test "fetches the latest Follow activity" do
14 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
15 follower = Repo.get_by(User, ap_id: activity.data["actor"])
16 followed = Repo.get_by(User, ap_id: activity.data["object"])
17
18 assert activity == Utils.fetch_latest_follow(follower, followed)
19 end
20 end
21
22 describe "fetch the latest Block" do
23 test "fetches the latest Block activity" do
24 blocker = insert(:user)
25 blocked = insert(:user)
26 {:ok, activity} = ActivityPub.block(blocker, blocked)
27
28 assert activity == Utils.fetch_latest_block(blocker, blocked)
29 end
30 end
31
32 describe "determine_explicit_mentions()" do
33 test "works with an object that has mentions" do
34 object = %{
35 "tag" => [
36 %{
37 "type" => "Mention",
38 "href" => "https://example.com/~alyssa",
39 "name" => "Alyssa P. Hacker"
40 }
41 ]
42 }
43
44 assert Utils.determine_explicit_mentions(object) == ["https://example.com/~alyssa"]
45 end
46
47 test "works with an object that does not have mentions" do
48 object = %{
49 "tag" => [
50 %{"type" => "Hashtag", "href" => "https://example.com/tag/2hu", "name" => "2hu"}
51 ]
52 }
53
54 assert Utils.determine_explicit_mentions(object) == []
55 end
56
57 test "works with an object that has mentions and other tags" do
58 object = %{
59 "tag" => [
60 %{
61 "type" => "Mention",
62 "href" => "https://example.com/~alyssa",
63 "name" => "Alyssa P. Hacker"
64 },
65 %{"type" => "Hashtag", "href" => "https://example.com/tag/2hu", "name" => "2hu"}
66 ]
67 }
68
69 assert Utils.determine_explicit_mentions(object) == ["https://example.com/~alyssa"]
70 end
71
72 test "works with an object that has no tags" do
73 object = %{}
74
75 assert Utils.determine_explicit_mentions(object) == []
76 end
77
78 test "works with an object that has only IR tags" do
79 object = %{"tag" => ["2hu"]}
80
81 assert Utils.determine_explicit_mentions(object) == []
82 end
83 end
84
85 describe "make_like_data" do
86 setup do
87 user = insert(:user)
88 other_user = insert(:user)
89 third_user = insert(:user)
90 [user: user, other_user: other_user, third_user: third_user]
91 end
92
93 test "addresses actor's follower address if the activity is public", %{
94 user: user,
95 other_user: other_user,
96 third_user: third_user
97 } do
98 expected_to = Enum.sort([user.ap_id, other_user.follower_address])
99 expected_cc = Enum.sort(["https://www.w3.org/ns/activitystreams#Public", third_user.ap_id])
100
101 {:ok, activity} =
102 CommonAPI.post(user, %{
103 "status" =>
104 "hey @#{other_user.nickname}, @#{third_user.nickname} how about beering together this weekend?"
105 })
106
107 %{"to" => to, "cc" => cc} = Utils.make_like_data(other_user, activity, nil)
108 assert Enum.sort(to) == expected_to
109 assert Enum.sort(cc) == expected_cc
110 end
111
112 test "does not adress actor's follower address if the activity is not public", %{
113 user: user,
114 other_user: other_user,
115 third_user: third_user
116 } do
117 expected_to = Enum.sort([user.ap_id])
118 expected_cc = [third_user.ap_id]
119
120 {:ok, activity} =
121 CommonAPI.post(user, %{
122 "status" => "@#{other_user.nickname} @#{third_user.nickname} bought a new swimsuit!",
123 "visibility" => "private"
124 })
125
126 %{"to" => to, "cc" => cc} = Utils.make_like_data(other_user, activity, nil)
127 assert Enum.sort(to) == expected_to
128 assert Enum.sort(cc) == expected_cc
129 end
130 end
131
132 describe "fetch_ordered_collection" do
133 import Tesla.Mock
134
135 test "fetches the first OrderedCollectionPage when an OrderedCollection is encountered" do
136 mock(fn
137 %{method: :get, url: "http://mastodon.com/outbox"} ->
138 json(%{"type" => "OrderedCollection", "first" => "http://mastodon.com/outbox?page=true"})
139
140 %{method: :get, url: "http://mastodon.com/outbox?page=true"} ->
141 json(%{"type" => "OrderedCollectionPage", "orderedItems" => ["ok"]})
142 end)
143
144 assert Utils.fetch_ordered_collection("http://mastodon.com/outbox", 1) == ["ok"]
145 end
146
147 test "fetches several pages in the right order one after another, but only the specified amount" do
148 mock(fn
149 %{method: :get, url: "http://example.com/outbox"} ->
150 json(%{
151 "type" => "OrderedCollectionPage",
152 "orderedItems" => [0],
153 "next" => "http://example.com/outbox?page=1"
154 })
155
156 %{method: :get, url: "http://example.com/outbox?page=1"} ->
157 json(%{
158 "type" => "OrderedCollectionPage",
159 "orderedItems" => [1],
160 "next" => "http://example.com/outbox?page=2"
161 })
162
163 %{method: :get, url: "http://example.com/outbox?page=2"} ->
164 json(%{"type" => "OrderedCollectionPage", "orderedItems" => [2]})
165 end)
166
167 assert Utils.fetch_ordered_collection("http://example.com/outbox", 0) == [0]
168 assert Utils.fetch_ordered_collection("http://example.com/outbox", 1) == [0, 1]
169 end
170
171 test "returns an error if the url doesn't have an OrderedCollection/Page" do
172 mock(fn
173 %{method: :get, url: "http://example.com/not-an-outbox"} ->
174 json(%{"type" => "NotAnOutbox"})
175 end)
176
177 assert {:error, _} = Utils.fetch_ordered_collection("http://example.com/not-an-outbox", 1)
178 end
179
180 test "returns the what was collected if there are less pages than specified" do
181 mock(fn
182 %{method: :get, url: "http://example.com/outbox"} ->
183 json(%{
184 "type" => "OrderedCollectionPage",
185 "orderedItems" => [0],
186 "next" => "http://example.com/outbox?page=1"
187 })
188
189 %{method: :get, url: "http://example.com/outbox?page=1"} ->
190 json(%{"type" => "OrderedCollectionPage", "orderedItems" => [1]})
191 end)
192
193 assert Utils.fetch_ordered_collection("http://example.com/outbox", 5) == [0, 1]
194 end
195 end
196
197 test "make_json_ld_header/0" do
198 assert Utils.make_json_ld_header() == %{
199 "@context" => [
200 "https://www.w3.org/ns/activitystreams",
201 "http://localhost:4001/schemas/litepub-0.1.jsonld",
202 %{
203 "@language" => "und"
204 }
205 ]
206 }
207 end
208 end