1 defmodule Pleroma.Web.ActivityPub.UtilsTest do
6 alias Pleroma.Web.ActivityPub.ActivityPub
7 alias Pleroma.Web.ActivityPub.Utils
8 alias Pleroma.Web.CommonAPI
10 import Pleroma.Factory
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"])
18 assert activity == Utils.fetch_latest_follow(follower, followed)
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)
28 assert activity == Utils.fetch_latest_block(blocker, blocked)
32 describe "determine_explicit_mentions()" do
33 test "works with an object that has mentions" do
38 "href" => "https://example.com/~alyssa",
39 "name" => "Alyssa P. Hacker"
44 assert Utils.determine_explicit_mentions(object) == ["https://example.com/~alyssa"]
47 test "works with an object that does not have mentions" do
50 %{"type" => "Hashtag", "href" => "https://example.com/tag/2hu", "name" => "2hu"}
54 assert Utils.determine_explicit_mentions(object) == []
57 test "works with an object that has mentions and other tags" do
62 "href" => "https://example.com/~alyssa",
63 "name" => "Alyssa P. Hacker"
65 %{"type" => "Hashtag", "href" => "https://example.com/tag/2hu", "name" => "2hu"}
69 assert Utils.determine_explicit_mentions(object) == ["https://example.com/~alyssa"]
72 test "works with an object that has no tags" do
75 assert Utils.determine_explicit_mentions(object) == []
78 test "works with an object that has only IR tags" do
79 object = %{"tag" => ["2hu"]}
81 assert Utils.determine_explicit_mentions(object) == []
85 describe "make_like_data" do
88 other_user = insert(:user)
89 third_user = insert(:user)
90 [user: user, other_user: other_user, third_user: third_user]
93 test "addresses actor's follower address if the activity is public", %{
95 other_user: other_user,
96 third_user: third_user
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])
102 CommonAPI.post(user, %{
104 "hey @#{other_user.nickname}, @#{third_user.nickname} how about beering together this weekend?"
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
112 test "does not adress actor's follower address if the activity is not public", %{
114 other_user: other_user,
115 third_user: third_user
117 expected_to = Enum.sort([user.ap_id])
118 expected_cc = [third_user.ap_id]
121 CommonAPI.post(user, %{
122 "status" => "@#{other_user.nickname} @#{third_user.nickname} bought a new swimsuit!",
123 "visibility" => "private"
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
132 describe "fetch_ordered_collection" do
135 test "fetches the first OrderedCollectionPage when an OrderedCollection is encountered" do
137 %{method: :get, url: "http://mastodon.com/outbox"} ->
138 json(%{"type" => "OrderedCollection", "first" => "http://mastodon.com/outbox?page=true"})
140 %{method: :get, url: "http://mastodon.com/outbox?page=true"} ->
141 json(%{"type" => "OrderedCollectionPage", "orderedItems" => ["ok"]})
144 assert Utils.fetch_ordered_collection("http://mastodon.com/outbox", 1) == ["ok"]
147 test "fetches several pages in the right order one after another, but only the specified amount" do
149 %{method: :get, url: "http://example.com/outbox"} ->
151 "type" => "OrderedCollectionPage",
152 "orderedItems" => [0],
153 "next" => "http://example.com/outbox?page=1"
156 %{method: :get, url: "http://example.com/outbox?page=1"} ->
158 "type" => "OrderedCollectionPage",
159 "orderedItems" => [1],
160 "next" => "http://example.com/outbox?page=2"
163 %{method: :get, url: "http://example.com/outbox?page=2"} ->
164 json(%{"type" => "OrderedCollectionPage", "orderedItems" => [2]})
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]
171 test "returns an error if the url doesn't have an OrderedCollection/Page" do
173 %{method: :get, url: "http://example.com/not-an-outbox"} ->
174 json(%{"type" => "NotAnOutbox"})
177 assert {:error, _} = Utils.fetch_ordered_collection("http://example.com/not-an-outbox", 1)
180 test "returns the what was collected if there are less pages than specified" do
182 %{method: :get, url: "http://example.com/outbox"} ->
184 "type" => "OrderedCollectionPage",
185 "orderedItems" => [0],
186 "next" => "http://example.com/outbox?page=1"
189 %{method: :get, url: "http://example.com/outbox?page=1"} ->
190 json(%{"type" => "OrderedCollectionPage", "orderedItems" => [1]})
193 assert Utils.fetch_ordered_collection("http://example.com/outbox", 5) == [0, 1]
197 test "make_json_ld_header/0" do
198 assert Utils.make_json_ld_header() == %{
200 "https://www.w3.org/ns/activitystreams",
201 "http://localhost:4001/schemas/litepub-0.1.jsonld",