From 181581dd6b87f7bc26c2f8d16cc8131e848b610f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Fri, 18 Oct 2024 19:50:32 +0200 Subject: [PATCH] Add test for BiteValidator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- .../web/activity_pub/transmogrifier.ex | 7 ++- .../object_validators/bite_validator_test.exs | 51 +++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 test/pleroma/web/activity_pub/object_validators/bite_validator_test.exs diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 8f242c58e2..55f4fcfc0b 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -489,18 +489,17 @@ def handle_incoming( def handle_incoming( %{"type" => "Bite", "target" => target_id} = data, - options + _options ) do target_id = cond do %User{ap_id: actor_id} = User.get_by_ap_id(target_id) -> actor_id - %Object{data: data} = Object.get_by_ap_id(target_id) - when is_binary(data["actor"]) or is_binary(data["attributedTo"]) -> + %Object{data: data} = Object.get_by_ap_id(target_id) -> data["actor"] || data["attributedTo"] - _ -> + true -> target_id end diff --git a/test/pleroma/web/activity_pub/object_validators/bite_validator_test.exs b/test/pleroma/web/activity_pub/object_validators/bite_validator_test.exs new file mode 100644 index 0000000000..94e433d5af --- /dev/null +++ b/test/pleroma/web/activity_pub/object_validators/bite_validator_test.exs @@ -0,0 +1,51 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2024 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.ActivityPub.ObjectValidators.BiteValidationTest do + use Pleroma.DataCase, async: true + + alias Pleroma.Web.ActivityPub.ObjectValidator + alias Pleroma.Web.ActivityPub.ObjectValidators.BiteValidator + alias Pleroma.Web.ActivityPub.Utils + alias Pleroma.Web.CommonAPI + + import Pleroma.Factory + + describe "bites" do + setup do + biting = insert(:user) + bitten = insert(:user) + + valid_bite = %{ + "id" => Utils.generate_activity_id(), + "type" => "Bite", + "actor" => biting.ap_id, + "target" => bitten.ap_id, + "to" => [bitten.ap_id] + } + + %{valid_bite: valid_bite, biting: biting, bitten: bitten} + end + + test "returns ok when called in the ObjectValidator", %{valid_bite: valid_bite} do + {:ok, object, _meta} = ObjectValidator.validate(valid_bite, []) + + assert "id" in Map.keys(object) + end + + test "is valid for a valid object", %{valid_bite: valid_bite} do + assert BiteValidator.cast_and_validate(valid_bite).valid? + end + + test "is valid when biting an object", %{valid_bite: valid_bite, bitten: bitten} do + {:ok, activity} = CommonAPI.post(bitten, %{status: "uguu"}) + + valid_bite = + valid_bite + |> Map.put("target", activity.data["object"]) + + assert BiteValidator.cast_and_validate(valid_bite).valid? + end + end +end