1 defmodule Pleroma.Web.ActivityPub.UtilsTest do
5 alias Pleroma.Web.ActivityPub.ActivityPub
6 alias Pleroma.Web.ActivityPub.Utils
7 alias Pleroma.Web.CommonAPI
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"])
17 assert activity == Utils.fetch_latest_follow(follower, followed)
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)
27 assert activity == Utils.fetch_latest_block(blocker, blocked)
31 describe "determine_explicit_mentions()" do
32 test "works with an object that has mentions" do
37 "href" => "https://example.com/~alyssa",
38 "name" => "Alyssa P. Hacker"
43 assert Utils.determine_explicit_mentions(object) == ["https://example.com/~alyssa"]
46 test "works with an object that does not have mentions" do
49 %{"type" => "Hashtag", "href" => "https://example.com/tag/2hu", "name" => "2hu"}
53 assert Utils.determine_explicit_mentions(object) == []
56 test "works with an object that has mentions and other tags" do
61 "href" => "https://example.com/~alyssa",
62 "name" => "Alyssa P. Hacker"
64 %{"type" => "Hashtag", "href" => "https://example.com/tag/2hu", "name" => "2hu"}
68 assert Utils.determine_explicit_mentions(object) == ["https://example.com/~alyssa"]
71 test "works with an object that has no tags" do
74 assert Utils.determine_explicit_mentions(object) == []
77 test "works with an object that has only IR tags" do
78 object = %{"tag" => ["2hu"]}
80 assert Utils.determine_explicit_mentions(object) == []
84 describe "make_like_data" do
87 other_user = insert(:user)
88 third_user = insert(:user)
89 [user: user, other_user: other_user, third_user: third_user]
92 test "addresses actor's follower address if the activity is public", %{
94 other_user: other_user,
95 third_user: third_user
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])
101 CommonAPI.post(user, %{
103 "hey @#{other_user.nickname}, @#{third_user.nickname} how about beering together this weekend?"
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
111 test "does not adress actor's follower address if the activity is not public", %{
113 other_user: other_user,
114 third_user: third_user
116 expected_to = Enum.sort([user.ap_id])
117 expected_cc = [third_user.ap_id]
120 CommonAPI.post(user, %{
121 "status" => "@#{other_user.nickname} @#{third_user.nickname} bought a new swimsuit!",
122 "visibility" => "private"
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
131 describe "fetch_ordered_collection" do
134 test "fetches the first OrderedCollectionPage when an OrderedCollection is encountered" do
136 %{method: :get, url: "http://mastodon.com/outbox"} ->
137 json(%{"type" => "OrderedCollection", "first" => "http://mastodon.com/outbox?page=true"})
139 %{method: :get, url: "http://mastodon.com/outbox?page=true"} ->
140 json(%{"type" => "OrderedCollectionPage", "orderedItems" => ["ok"]})
143 assert Utils.fetch_ordered_collection("http://mastodon.com/outbox", 1) == ["ok"]
146 test "fetches several pages in the right order one after another, but only the specified amount" do
148 %{method: :get, url: "http://example.com/outbox"} ->
150 "type" => "OrderedCollectionPage",
151 "orderedItems" => [0],
152 "next" => "http://example.com/outbox?page=1"
155 %{method: :get, url: "http://example.com/outbox?page=1"} ->
157 "type" => "OrderedCollectionPage",
158 "orderedItems" => [1],
159 "next" => "http://example.com/outbox?page=2"
162 %{method: :get, url: "http://example.com/outbox?page=2"} ->
163 json(%{"type" => "OrderedCollectionPage", "orderedItems" => [2]})
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]
170 test "returns an error if the url doesn't have an OrderedCollection/Page" do
172 %{method: :get, url: "http://example.com/not-an-outbox"} ->
173 json(%{"type" => "NotAnOutbox"})
176 assert {:error, _} = Utils.fetch_ordered_collection("http://example.com/not-an-outbox", 1)
179 test "returns the what was collected if there are less pages than specified" do
181 %{method: :get, url: "http://example.com/outbox"} ->
183 "type" => "OrderedCollectionPage",
184 "orderedItems" => [0],
185 "next" => "http://example.com/outbox?page=1"
188 %{method: :get, url: "http://example.com/outbox?page=1"} ->
189 json(%{"type" => "OrderedCollectionPage", "orderedItems" => [1]})
192 assert Utils.fetch_ordered_collection("http://example.com/outbox", 5) == [0, 1]
196 test "make_json_ld_header/0" do
197 assert Utils.make_json_ld_header() == %{
199 "https://www.w3.org/ns/activitystreams",
200 "http://localhost:4001/schemas/litepub-0.1.jsonld",