1 defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
3 alias Pleroma.Web.ActivityPub.ActivityPub
4 alias Pleroma.{Activity, Object, User}
5 alias Pleroma.Builders.ActivityBuilder
9 describe "insertion" do
10 test "inserts a given map into the activity database, giving it an id if it has none." do
15 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
16 assert activity.data["ok"] == data["ok"]
17 assert is_binary(activity.data["id"])
25 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
26 assert activity.data["ok"] == data["ok"]
27 assert activity.data["id"] == given_id
30 test "adds an id to a given object if it lacks one and inserts it to the object database" do
37 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
38 assert is_binary(activity.data["object"]["id"])
39 assert %Object{} = Object.get_by_ap_id(activity.data["object"]["id"])
43 describe "fetch activities for recipients" do
44 test "retrieve the activities for certain recipients" do
45 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
46 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
47 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
49 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
50 assert length(activities) == 2
51 assert activities == [activity_one, activity_two]
55 describe "fetch activities in context" do
56 test "retrieves activities that have a given context" do
57 {:ok, activity} = ActivityBuilder.insert(%{"context" => "2hu"})
58 {:ok, activity_two} = ActivityBuilder.insert(%{"context" => "2hu"})
59 {:ok, _activity_three} = ActivityBuilder.insert(%{"context" => "3hu"})
61 activities = ActivityPub.fetch_activities_for_context("2hu")
63 assert activities == [activity, activity_two]
67 describe "public fetch activities" do
68 test "retrieves public activities" do
69 %{public: public} = ActivityBuilder.public_and_non_public
71 activities = ActivityPub.fetch_public_activities
72 assert length(activities) == 1
73 assert Enum.at(activities, 0) == public
76 test "retrieves a maximum of 20 activities" do
77 activities = ActivityBuilder.insert_list(30)
78 last_expected = List.last(activities)
80 activities = ActivityPub.fetch_public_activities
81 last = List.last(activities)
83 assert length(activities) == 20
84 assert last == last_expected
87 test "retrieves ids starting from a since_id" do
88 activities = ActivityBuilder.insert_list(30)
89 later_activities = ActivityBuilder.insert_list(10)
90 since_id = List.last(activities).id
91 last_expected = List.last(later_activities)
93 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
94 last = List.last(activities)
96 assert length(activities) == 10
97 assert last == last_expected
100 test "retrieves ids up to max_id" do
101 _first_activities = ActivityBuilder.insert_list(10)
102 activities = ActivityBuilder.insert_list(20)
103 later_activities = ActivityBuilder.insert_list(10)
104 max_id = List.first(later_activities).id
105 last_expected = List.last(activities)
107 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
108 last = List.last(activities)
110 assert length(activities) == 20
111 assert last == last_expected
115 describe "like an object" do
116 test "adds a like activity to the db" do
117 note_activity = insert(:note_activity)
118 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
120 user_two = insert(:user)
122 {:ok, like_activity, object} = ActivityPub.like(user, object)
124 assert like_activity.data["actor"] == user.ap_id
125 assert like_activity.data["type"] == "Like"
126 assert like_activity.data["object"] == object.data["id"]
127 assert like_activity.data["to"] == [User.ap_followers(user)]
128 assert object.data["like_count"] == 1
129 assert object.data["likes"] == [user.ap_id]
131 # Just return the original activity if the user already liked it.
132 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
134 assert like_activity == same_like_activity
135 assert object.data["likes"] == [user.ap_id]
137 [note_activity] = Activity.all_by_object_ap_id(object.data["id"])
138 assert note_activity.data["object"]["like_count"] == 1
140 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
141 assert object.data["like_count"] == 2
145 describe "unliking" do
146 test "unliking a previously liked object" do
147 note_activity = insert(:note_activity)
148 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
151 # Unliking something that hasn't been liked does nothing
152 {:ok, object} = ActivityPub.unlike(user, object)
153 assert object.data["like_count"] == 0
155 {:ok, like_activity, object} = ActivityPub.like(user, object)
156 assert object.data["like_count"] == 1
158 {:ok, object} = ActivityPub.unlike(user, object)
159 assert object.data["like_count"] == 0
161 assert Repo.get(Activity, like_activity.id) == nil
165 describe "uploading files" do
166 test "copies the file to the configured folder" do
167 file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
169 {:ok, %Object{} = object} = ActivityPub.upload(file)
170 assert object.data["name"] == "an_image.jpg"