X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=test%2Fweb%2Factivity_pub%2Factivity_pub_test.exs;h=d5dd44cc31cd067816ff4d4cbf8814300cdd7b86;hb=3c2c32b460c2d942d085725d14e77a96c4a01e4c;hp=ad1fb6d02ff7c2161d7a08d040ffb8367e35d8d9;hpb=3ecf131511afc1fc366be6402ca94cf0e6c30e11;p=akkoma diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index ad1fb6d02..d5dd44cc3 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -1,5 +1,5 @@ # Pleroma: A lightweight social networking server -# Copyright © 2017-2019 Pleroma Authors +# Copyright © 2017-2020 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.ActivityPub.ActivityPubTest do @@ -8,6 +8,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do alias Pleroma.Activity alias Pleroma.Builders.ActivityBuilder + alias Pleroma.Config alias Pleroma.Notification alias Pleroma.Object alias Pleroma.User @@ -15,6 +16,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Web.AdminAPI.AccountView alias Pleroma.Web.CommonAPI + alias Pleroma.Web.Federator import Pleroma.Factory import Tesla.Mock @@ -224,7 +226,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do describe "insertion" do test "drops activities beyond a certain limit" do - limit = Pleroma.Config.get([:instance, :remote_limit]) + limit = Config.get([:instance, :remote_limit]) random_text = :crypto.strong_rand_bytes(limit + 1) @@ -385,6 +387,27 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do end describe "create activities" do + test "it reverts create" do + user = insert(:user) + + with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do + assert {:error, :reverted} = + ActivityPub.create(%{ + to: ["user1", "user2"], + actor: user, + context: "", + object: %{ + "to" => ["user1", "user2"], + "type" => "Note", + "content" => "testing" + } + }) + end + + assert Repo.aggregate(Activity, :count, :id) == 0 + assert Repo.aggregate(Object, :count, :id) == 0 + end + test "removes doubled 'to' recipients" do user = insert(:user) @@ -852,8 +875,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do end describe "react to an object" do - test_with_mock "sends an activity to federation", Pleroma.Web.Federator, [:passthrough], [] do - Pleroma.Config.put([:instance, :federating], true) + test_with_mock "sends an activity to federation", Federator, [:passthrough], [] do + Config.put([:instance, :federating], true) user = insert(:user) reactor = insert(:user) {:ok, activity} = CommonAPI.post(user, %{"status" => "YASSSS queen slay"}) @@ -861,12 +884,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do {:ok, reaction_activity, _object} = ActivityPub.react_with_emoji(reactor, object, "🔥") - assert called(Pleroma.Web.Federator.publish(reaction_activity)) + assert called(Federator.publish(reaction_activity)) end test "adds an emoji reaction activity to the db" do user = insert(:user) reactor = insert(:user) + third_user = insert(:user) + fourth_user = insert(:user) {:ok, activity} = CommonAPI.post(user, %{"status" => "YASSSS queen slay"}) assert object = Object.normalize(activity) @@ -875,19 +900,48 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do assert reaction_activity assert reaction_activity.data["actor"] == reactor.ap_id - assert reaction_activity.data["type"] == "EmojiReaction" + assert reaction_activity.data["type"] == "EmojiReact" assert reaction_activity.data["content"] == "🔥" assert reaction_activity.data["object"] == object.data["id"] assert reaction_activity.data["to"] == [User.ap_followers(reactor), activity.data["actor"]] assert reaction_activity.data["context"] == object.data["context"] assert object.data["reaction_count"] == 1 - assert object.data["reactions"]["🔥"] == [reactor.ap_id] + assert object.data["reactions"] == [["🔥", [reactor.ap_id]]] + + {:ok, _reaction_activity, object} = ActivityPub.react_with_emoji(third_user, object, "☕") + + assert object.data["reaction_count"] == 2 + assert object.data["reactions"] == [["🔥", [reactor.ap_id]], ["☕", [third_user.ap_id]]] + + {:ok, _reaction_activity, object} = ActivityPub.react_with_emoji(fourth_user, object, "🔥") + + assert object.data["reaction_count"] == 3 + + assert object.data["reactions"] == [ + ["🔥", [fourth_user.ap_id, reactor.ap_id]], + ["☕", [third_user.ap_id]] + ] + end + + test "reverts emoji reaction on error" do + [user, reactor] = insert_list(2, :user) + + {:ok, activity} = CommonAPI.post(user, %{"status" => "Status"}) + object = Object.normalize(activity) + + with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do + assert {:error, :reverted} = ActivityPub.react_with_emoji(reactor, object, "😀") + end + + object = Object.get_by_ap_id(object.data["id"]) + refute object.data["reaction_count"] + refute object.data["reactions"] end end describe "unreacting to an object" do - test_with_mock "sends an activity to federation", Pleroma.Web.Federator, [:passthrough], [] do - Pleroma.Config.put([:instance, :federating], true) + test_with_mock "sends an activity to federation", Federator, [:passthrough], [] do + Config.put([:instance, :federating], true) user = insert(:user) reactor = insert(:user) {:ok, activity} = CommonAPI.post(user, %{"status" => "YASSSS queen slay"}) @@ -895,12 +949,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do {:ok, reaction_activity, _object} = ActivityPub.react_with_emoji(reactor, object, "🔥") - assert called(Pleroma.Web.Federator.publish(reaction_activity)) + assert called(Federator.publish(reaction_activity)) {:ok, unreaction_activity, _object} = ActivityPub.unreact_with_emoji(reactor, reaction_activity.data["id"]) - assert called(Pleroma.Web.Federator.publish(unreaction_activity)) + assert called(Federator.publish(unreaction_activity)) end test "adds an undo activity to the db" do @@ -919,20 +973,38 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do object = Object.get_by_ap_id(object.data["id"]) assert object.data["reaction_count"] == 0 - assert object.data["reactions"] == %{} + assert object.data["reactions"] == [] + end + + test "reverts emoji unreact on error" do + [user, reactor] = insert_list(2, :user) + {:ok, activity} = CommonAPI.post(user, %{"status" => "Status"}) + object = Object.normalize(activity) + + {:ok, reaction_activity, _object} = ActivityPub.react_with_emoji(reactor, object, "😀") + + with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do + assert {:error, :reverted} = + ActivityPub.unreact_with_emoji(reactor, reaction_activity.data["id"]) + end + + object = Object.get_by_ap_id(object.data["id"]) + + assert object.data["reaction_count"] == 1 + assert object.data["reactions"] == [["😀", [reactor.ap_id]]] end end describe "like an object" do - test_with_mock "sends an activity to federation", Pleroma.Web.Federator, [:passthrough], [] do - Pleroma.Config.put([:instance, :federating], true) + test_with_mock "sends an activity to federation", Federator, [:passthrough], [] do + Config.put([:instance, :federating], true) note_activity = insert(:note_activity) assert object_activity = Object.normalize(note_activity) user = insert(:user) {:ok, like_activity, _object} = ActivityPub.like(user, object_activity) - assert called(Pleroma.Web.Federator.publish(like_activity)) + assert called(Federator.publish(like_activity)) end test "returns exist activity if object already liked" do @@ -947,6 +1019,19 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do assert like_activity == like_activity_exist end + test "reverts like activity on error" do + note_activity = insert(:note_activity) + object = Object.normalize(note_activity) + user = insert(:user) + + with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do + assert {:error, :reverted} = ActivityPub.like(user, object) + end + + assert Repo.aggregate(Activity, :count, :id) == 1 + assert Repo.get(Object, object.id) == object + end + test "adds a like activity to the db" do note_activity = insert(:note_activity) assert object = Object.normalize(note_activity) @@ -977,15 +1062,15 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do end describe "unliking" do - test_with_mock "sends an activity to federation", Pleroma.Web.Federator, [:passthrough], [] do - Pleroma.Config.put([:instance, :federating], true) + test_with_mock "sends an activity to federation", Federator, [:passthrough], [] do + Config.put([:instance, :federating], true) note_activity = insert(:note_activity) object = Object.normalize(note_activity) user = insert(:user) {:ok, object} = ActivityPub.unlike(user, object) - refute called(Pleroma.Web.Federator.publish()) + refute called(Federator.publish()) {:ok, _like_activity, object} = ActivityPub.like(user, object) assert object.data["like_count"] == 1 @@ -993,7 +1078,24 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do {:ok, unlike_activity, _, object} = ActivityPub.unlike(user, object) assert object.data["like_count"] == 0 - assert called(Pleroma.Web.Federator.publish(unlike_activity)) + assert called(Federator.publish(unlike_activity)) + end + + test "reverts unliking on error" do + note_activity = insert(:note_activity) + object = Object.normalize(note_activity) + user = insert(:user) + + {:ok, like_activity, object} = ActivityPub.like(user, object) + assert object.data["like_count"] == 1 + + with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do + assert {:error, :reverted} = ActivityPub.unlike(user, object) + end + + assert Object.get_by_ap_id(object.data["id"]) == object + assert object.data["like_count"] == 1 + assert Activity.get_by_id(like_activity.id) end test "unliking a previously liked object" do @@ -1035,6 +1137,21 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do assert announce_activity.data["actor"] == user.ap_id assert announce_activity.data["context"] == object.data["context"] end + + test "reverts annouce from object on error" do + note_activity = insert(:note_activity) + object = Object.normalize(note_activity) + user = insert(:user) + + with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do + assert {:error, :reverted} = ActivityPub.announce(user, object) + end + + reloaded_object = Object.get_by_ap_id(object.data["id"]) + assert reloaded_object == object + refute reloaded_object.data["announcement_count"] + refute reloaded_object.data["announcements"] + end end describe "announcing a private object" do @@ -1077,8 +1194,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do 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, object} = ActivityPub.unannounce(user, object) + refute object.data["announcement_count"] {:ok, announce_activity, object} = ActivityPub.announce(user, object) assert object.data["announcement_count"] == 1 @@ -1098,6 +1215,22 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do assert Activity.get_by_id(announce_activity.id) == nil end + + test "reverts unannouncing on error" do + note_activity = insert(:note_activity) + object = Object.normalize(note_activity) + user = insert(:user) + + {:ok, _announce_activity, object} = ActivityPub.announce(user, object) + assert object.data["announcement_count"] == 1 + + with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do + assert {:error, :reverted} = ActivityPub.unannounce(user, object) + end + + object = Object.get_by_ap_id(object.data["id"]) + assert object.data["announcement_count"] == 1 + end end describe "uploading files" do @@ -1132,6 +1265,35 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do end describe "following / unfollowing" do + test "it reverts follow activity" do + follower = insert(:user) + followed = insert(:user) + + with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do + assert {:error, :reverted} = ActivityPub.follow(follower, followed) + end + + assert Repo.aggregate(Activity, :count, :id) == 0 + assert Repo.aggregate(Object, :count, :id) == 0 + end + + test "it reverts unfollow activity" do + follower = insert(:user) + followed = insert(:user) + + {:ok, follow_activity} = ActivityPub.follow(follower, followed) + + with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do + assert {:error, :reverted} = ActivityPub.unfollow(follower, followed) + end + + activity = Activity.get_by_id(follow_activity.id) + assert activity.data["type"] == "Follow" + assert activity.data["actor"] == follower.ap_id + + assert activity.data["object"] == followed.ap_id + end + test "creates a follow activity" do follower = insert(:user) followed = insert(:user) @@ -1158,9 +1320,37 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do assert embedded_object["object"] == followed.ap_id assert embedded_object["id"] == follow_activity.data["id"] end + + test "creates an undo activity for a pending follow request" do + follower = insert(:user) + followed = insert(:user, %{locked: true}) + + {:ok, follow_activity} = ActivityPub.follow(follower, followed) + {:ok, activity} = ActivityPub.unfollow(follower, followed) + + assert activity.data["type"] == "Undo" + assert activity.data["actor"] == follower.ap_id + + embedded_object = activity.data["object"] + assert is_map(embedded_object) + assert embedded_object["type"] == "Follow" + assert embedded_object["object"] == followed.ap_id + assert embedded_object["id"] == follow_activity.data["id"] + end end describe "blocking / unblocking" do + test "reverts block activity on error" do + [blocker, blocked] = insert_list(2, :user) + + with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do + assert {:error, :reverted} = ActivityPub.block(blocker, blocked) + end + + assert Repo.aggregate(Activity, :count, :id) == 0 + assert Repo.aggregate(Object, :count, :id) == 0 + end + test "creates a block activity" do blocker = insert(:user) blocked = insert(:user) @@ -1172,6 +1362,21 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do assert activity.data["object"] == blocked.ap_id end + test "reverts unblock activity on error" do + [blocker, blocked] = insert_list(2, :user) + {:ok, block_activity} = ActivityPub.block(blocker, blocked) + + with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do + assert {:error, :reverted} = ActivityPub.unblock(blocker, blocked) + end + + assert block_activity.data["type"] == "Block" + assert block_activity.data["actor"] == blocker.ap_id + + assert Repo.aggregate(Activity, :count, :id) == 1 + assert Repo.aggregate(Object, :count, :id) == 1 + end + test "creates an undo activity for the last block" do blocker = insert(:user) blocked = insert(:user) @@ -1191,6 +1396,21 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do end describe "deletion" do + clear_config([:instance, :rewrite_policy]) + + test "it reverts deletion on error" do + note = insert(:note_activity) + object = Object.normalize(note) + + with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do + assert {:error, :reverted} = ActivityPub.delete(object) + end + + assert Repo.aggregate(Activity, :count, :id) == 1 + assert Repo.get(Object, object.id) == object + assert Activity.get_by_id(note.id) == note + end + test "it creates a delete activity and deletes the original object" do note = insert(:note_activity) object = Object.normalize(note) @@ -1294,11 +1514,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do end test "it passes delete activity through MRF before deleting the object" do - rewrite_policy = Pleroma.Config.get([:instance, :rewrite_policy]) Pleroma.Config.put([:instance, :rewrite_policy], Pleroma.Web.ActivityPub.MRF.DropPolicy) - on_exit(fn -> Pleroma.Config.put([:instance, :rewrite_policy], rewrite_policy) end) - note = insert(:note_activity) object = Object.normalize(note) @@ -1363,6 +1580,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do end describe "update" do + clear_config([:instance, :max_pinned_statuses]) + test "it creates an update activity with the new user data" do user = insert(:user) {:ok, user} = User.ensure_keys_present(user) @@ -1385,7 +1604,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do end test "returned pinned statuses" do - Pleroma.Config.put([:instance, :max_pinned_statuses], 3) + Config.put([:instance, :max_pinned_statuses], 3) user = insert(:user) {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"}) @@ -1623,6 +1842,44 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do assert follow_info.following_count == 32 assert follow_info.hide_follows == true end + + test "doesn't crash when follower and following counters are hidden" do + mock(fn env -> + case env.url do + "http://localhost:4001/users/masto_hidden_counters/following" -> + json(%{ + "@context" => "https://www.w3.org/ns/activitystreams", + "id" => "http://localhost:4001/users/masto_hidden_counters/followers" + }) + + "http://localhost:4001/users/masto_hidden_counters/following?page=1" -> + %Tesla.Env{status: 403, body: ""} + + "http://localhost:4001/users/masto_hidden_counters/followers" -> + json(%{ + "@context" => "https://www.w3.org/ns/activitystreams", + "id" => "http://localhost:4001/users/masto_hidden_counters/following" + }) + + "http://localhost:4001/users/masto_hidden_counters/followers?page=1" -> + %Tesla.Env{status: 403, body: ""} + end + end) + + user = + insert(:user, + local: false, + follower_address: "http://localhost:4001/users/masto_hidden_counters/followers", + following_address: "http://localhost:4001/users/masto_hidden_counters/following" + ) + + {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user) + + assert follow_info.hide_followers == true + assert follow_info.follower_count == 0 + assert follow_info.hide_follows == true + assert follow_info.following_count == 0 + end end describe "fetch_favourites/3" do @@ -1637,17 +1894,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do {:ok, a4} = CommonAPI.post(user2, %{"status" => "Agent Smith "}) {:ok, a5} = CommonAPI.post(user1, %{"status" => "Red or Blue "}) - {:ok, _, _} = CommonAPI.favorite(a4.id, user) - {:ok, _, _} = CommonAPI.favorite(a3.id, other_user) - Process.sleep(1000) - {:ok, _, _} = CommonAPI.favorite(a3.id, user) - {:ok, _, _} = CommonAPI.favorite(a5.id, other_user) - Process.sleep(1000) - {:ok, _, _} = CommonAPI.favorite(a5.id, user) - {:ok, _, _} = CommonAPI.favorite(a4.id, other_user) - Process.sleep(1000) - {:ok, _, _} = CommonAPI.favorite(a1.id, user) - {:ok, _, _} = CommonAPI.favorite(a1.id, other_user) + {:ok, _} = CommonAPI.favorite(user, a4.id) + {:ok, _} = CommonAPI.favorite(other_user, a3.id) + {:ok, _} = CommonAPI.favorite(user, a3.id) + {:ok, _} = CommonAPI.favorite(other_user, a5.id) + {:ok, _} = CommonAPI.favorite(user, a5.id) + {:ok, _} = CommonAPI.favorite(other_user, a4.id) + {:ok, _} = CommonAPI.favorite(user, a1.id) + {:ok, _} = CommonAPI.favorite(other_user, a1.id) result = ActivityPub.fetch_favourites(user) assert Enum.map(result, & &1.id) == [a1.id, a5.id, a3.id, a4.id]