1 defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do
4 alias Pleroma.Web.ActivityPub.ObjectValidator
5 alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
6 alias Pleroma.Web.ActivityPub.Utils
7 alias Pleroma.Web.CommonAPI
14 {:ok, post_activity} = CommonAPI.post(user, %{"status" => "uguu"})
20 "id" => Utils.generate_activity_id(),
21 "object" => post_activity.data["object"],
22 "actor" => user.ap_id,
23 "context" => "a context"
26 %{valid_like: valid_like, user: user, post_activity: post_activity}
29 test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
30 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
32 assert "id" in Map.keys(object)
35 test "is valid for a valid object", %{valid_like: valid_like} do
36 assert LikeValidator.cast_and_validate(valid_like).valid?
39 test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
40 without_actor = Map.delete(valid_like, "actor")
42 refute LikeValidator.cast_and_validate(without_actor).valid?
44 with_invalid_actor = Map.put(valid_like, "actor", "invalidactor")
46 refute LikeValidator.cast_and_validate(with_invalid_actor).valid?
49 test "it errors when the object is missing or not known", %{valid_like: valid_like} do
50 without_object = Map.delete(valid_like, "object")
52 refute LikeValidator.cast_and_validate(without_object).valid?
54 with_invalid_object = Map.put(valid_like, "object", "invalidobject")
56 refute LikeValidator.cast_and_validate(with_invalid_object).valid?
59 test "it errors when the actor has already like the object", %{
60 valid_like: valid_like,
62 post_activity: post_activity
64 _like = CommonAPI.favorite(user, post_activity.id)
66 refute LikeValidator.cast_and_validate(valid_like).valid?
69 test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
72 |> Map.put("actor", %{"id" => valid_like["actor"]})
73 |> Map.put("object", %{"id" => valid_like["object"]})
75 validated = LikeValidator.cast_and_validate(wrapped_like)
77 assert validated.valid?
79 assert {:actor, valid_like["actor"]} in validated.changes
80 assert {:object, valid_like["object"]} in validated.changes