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