use Pleroma.DataCase
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.Utils
+ alias Pleroma.Web.CommonAPI
alias Pleroma.{Activity, Object, User}
alias Pleroma.Builders.ActivityBuilder
describe "insertion" do
test "returns the activity if one with the same id is already in" do
activity = insert(:note_activity)
- {:ok, new_activity}= ActivityPub.insert(activity.data)
+ {:ok, new_activity} = ActivityPub.insert(activity.data)
assert activity == new_activity
end
assert is_binary(activity.data["id"])
given_id = "bla"
+
data = %{
"ok" => true,
- "id" => given_id
+ "id" => given_id,
+ "context" => "blabla"
}
{:ok, %Activity{} = activity} = ActivityPub.insert(data)
assert activity.data["ok"] == data["ok"]
assert activity.data["id"] == given_id
+ assert activity.data["context"] == "blabla"
+ assert activity.data["context_id"]
end
- test "adds an id to a given object if it lacks one and inserts it to the object database" do
+ test "adds a context when none is there" do
data = %{
+ "id" => "some_id",
"object" => %{
+ "id" => "object_id"
+ }
+ }
+
+ {:ok, %Activity{} = activity} = ActivityPub.insert(data)
+
+ assert is_binary(activity.data["context"])
+ assert is_binary(activity.data["object"]["context"])
+ assert activity.data["context_id"]
+ assert activity.data["object"]["context_id"]
+ end
+
+ test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
+ data = %{
+ "object" => %{
+ "type" => "Note",
"ok" => true
}
}
describe "create activities" do
test "removes doubled 'to' recipients" do
- {:ok, activity} = ActivityPub.create(%{to: ["user1", "user1", "user2"], actor: %User{ap_id: "1"}, context: "", object: %{}})
+ user = insert(:user)
+
+ {:ok, activity} =
+ ActivityPub.create(%{
+ to: ["user1", "user1", "user2"],
+ actor: user,
+ context: "",
+ object: %{}
+ })
+
assert activity.data["to"] == ["user1", "user2"]
- assert activity.actor == "1"
+ assert activity.actor == user.ap_id
assert activity.recipients == ["user1", "user2"]
end
end
test "doesn't return blocked activities" do
activity_one = insert(:note_activity)
activity_two = insert(:note_activity)
+ activity_three = insert(:note_activity)
user = insert(:user)
+ booster = insert(:user)
{:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})
activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
assert Enum.member?(activities, activity_two)
+ assert Enum.member?(activities, activity_three)
refute Enum.member?(activities, activity_one)
{:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
assert Enum.member?(activities, activity_two)
+ assert Enum.member?(activities, activity_three)
+ assert Enum.member?(activities, activity_one)
+
+ {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]})
+ {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
+ %Activity{} = boost_activity = Activity.get_create_activity_by_object_ap_id(id)
+ activity_three = Repo.get(Activity, activity_three.id)
+
+ activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
+
+ assert Enum.member?(activities, activity_two)
+ refute Enum.member?(activities, activity_three)
+ refute Enum.member?(activities, boost_activity)
assert Enum.member?(activities, activity_one)
activities = ActivityPub.fetch_activities([], %{"blocking_user" => nil})
assert Enum.member?(activities, activity_two)
+ assert Enum.member?(activities, activity_three)
+ assert Enum.member?(activities, boost_activity)
assert Enum.member?(activities, activity_one)
end
describe "public fetch activities" do
+ test "doesn't retrieve unlisted activities" do
+ user = insert(:user)
+
+ {:ok, unlisted_activity} =
+ CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
+
+ {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
+
+ [activity] = ActivityPub.fetch_public_activities()
+
+ assert activity == listed_activity
+ end
+
test "retrieves public activities" do
- _activities = ActivityPub.fetch_public_activities
+ _activities = ActivityPub.fetch_public_activities()
- %{public: public} = ActivityBuilder.public_and_non_public
+ %{public: public} = ActivityBuilder.public_and_non_public()
- activities = ActivityPub.fetch_public_activities
+ activities = ActivityPub.fetch_public_activities()
assert length(activities) == 1
assert Enum.at(activities, 0) == public
end
activities = ActivityBuilder.insert_list(30)
last_expected = List.last(activities)
- activities = ActivityPub.fetch_public_activities
+ activities = ActivityPub.fetch_public_activities()
last = List.last(activities)
assert length(activities) == 20
{:ok, like_activity, object} = ActivityPub.like(user, object)
assert object.data["like_count"] == 1
- {:ok, object} = ActivityPub.unlike(user, object)
+ {:ok, _, _, object} = ActivityPub.unlike(user, object)
assert object.data["like_count"] == 0
assert Repo.get(Activity, like_activity.id) == nil
{:ok, announce_activity, object} = ActivityPub.announce(user, object)
assert object.data["announcement_count"] == 1
assert object.data["announcements"] == [user.ap_id]
- assert announce_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
+
+ assert announce_activity.data["to"] == [
+ User.ap_followers(user),
+ note_activity.data["actor"]
+ ]
+
assert announce_activity.data["object"] == object.data["id"]
assert announce_activity.data["actor"] == user.ap_id
assert announce_activity.data["context"] == object.data["context"]
end
end
+ describe "unannouncing an object" do
+ test "unannouncing a previously announced object" do
+ note_activity = insert(:note_activity)
+ object = Object.get_by_ap_id(note_activity.data["object"]["id"])
+ user = insert(:user)
+
+ # Unannouncing an object that is not announced does nothing
+ # {:ok, object} = ActivityPub.unannounce(user, object)
+ # assert object.data["announcement_count"] == 0
+
+ {:ok, announce_activity, object} = ActivityPub.announce(user, object)
+ assert object.data["announcement_count"] == 1
+
+ {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
+ assert object.data["announcement_count"] == 0
+
+ assert unannounce_activity.data["to"] == [
+ User.ap_followers(user),
+ announce_activity.data["actor"]
+ ]
+
+ assert unannounce_activity.data["type"] == "Undo"
+ assert unannounce_activity.data["object"] == announce_activity.data
+ assert unannounce_activity.data["actor"] == user.ap_id
+ assert unannounce_activity.data["context"] == announce_activity.data["context"]
+
+ assert Repo.get(Activity, announce_activity.id) == nil
+ end
+ end
+
describe "uploading files" do
test "copies the file to the configured folder" do
- file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
+ file = %Plug.Upload{
+ content_type: "image/jpg",
+ path: Path.absname("test/fixtures/image.jpg"),
+ filename: "an_image.jpg"
+ }
{:ok, %Object{} = object} = ActivityPub.upload(file)
assert object.data["name"] == "an_image.jpg"
describe "fetching an object" do
test "it fetches an object" do
- {:ok, object} = ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
+ {:ok, object} =
+ ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
+
assert activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
assert activity.data["id"]
- {:ok, object_again} = ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
+ {:ok, object_again} =
+ ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
assert [attachment] = object.data["attachment"]
assert is_list(attachment["url"])
assert activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
assert activity.data["id"]
- {:ok, object_again} = ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
+ {:ok, object_again} =
+ ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
assert object == object_again
end
assert activity.data["type"] == "Undo"
assert activity.data["actor"] == follower.ap_id
- assert activity.data["object"] == follow_activity.data["id"]
+
+ assert is_map(activity.data["object"])
+ assert activity.data["object"]["type"] == "Follow"
+ assert activity.data["object"]["object"] == followed.ap_id
+ assert activity.data["object"]["id"] == follow_activity.data["id"]
+ end
+ end
+
+ describe "blocking / unblocking" do
+ test "creates a block activity" do
+ blocker = insert(:user)
+ blocked = insert(:user)
+
+ {:ok, activity} = ActivityPub.block(blocker, blocked)
+
+ assert activity.data["type"] == "Block"
+ assert activity.data["actor"] == blocker.ap_id
+ assert activity.data["object"] == blocked.ap_id
+ end
+
+ test "creates an undo activity for the last block" do
+ blocker = insert(:user)
+ blocked = insert(:user)
+
+ {:ok, block_activity} = ActivityPub.block(blocker, blocked)
+ {:ok, activity} = ActivityPub.unblock(blocker, blocked)
+
+ assert activity.data["type"] == "Undo"
+ assert activity.data["actor"] == blocker.ap_id
+
+ assert is_map(activity.data["object"])
+ assert activity.data["object"]["type"] == "Block"
+ assert activity.data["object"]["object"] == blocked.ap_id
+ assert activity.data["object"]["id"] == block_activity.data["id"]
end
end
user = insert(:user)
{:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
- {:ok, update} = ActivityPub.update(%{actor: user_data["id"], to: [user.follower_address], cc: [], object: user_data})
+
+ {:ok, update} =
+ ActivityPub.update(%{
+ actor: user_data["id"],
+ to: [user.follower_address],
+ cc: [],
+ object: user_data
+ })
assert update.data["actor"] == user.ap_id
assert update.data["to"] == [user.follower_address]