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