1 defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do
4 alias Pleroma.Web.ActivityPub.Builder
5 alias Pleroma.Web.ActivityPub.ObjectValidator
6 alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
7 alias Pleroma.Web.ActivityPub.Utils
8 alias Pleroma.Web.CommonAPI
10 import Pleroma.Factory
15 {:ok, post_activity} = CommonAPI.post(user, %{"status" => "uguu"})
16 {:ok, like} = CommonAPI.favorite(user, post_activity.id)
17 {:ok, valid_like_undo, []} = Builder.undo(user, like)
19 %{user: user, like: like, valid_like_undo: valid_like_undo}
22 test "it validates a basic like undo", %{valid_like_undo: valid_like_undo} do
23 assert {:ok, _, _} = ObjectValidator.validate(valid_like_undo, [])
26 test "it does not validate if the actor of the undo is not the actor of the object", %{
27 valid_like_undo: valid_like_undo
29 other_user = insert(:user, ap_id: "https://gensokyo.2hu/users/raymoo")
33 |> Map.put("actor", other_user.ap_id)
35 {:error, cng} = ObjectValidator.validate(bad_actor, [])
37 assert {:actor, {"not the same as object actor", []}} in cng.errors
40 test "it does not validate if the object is missing", %{valid_like_undo: valid_like_undo} do
43 |> Map.put("object", "https://gensokyo.2hu/objects/1")
45 {:error, cng} = ObjectValidator.validate(missing_object, [])
47 assert {:object, {"can't find object", []}} in cng.errors
48 assert length(cng.errors) == 1
55 {:ok, post_activity} = CommonAPI.post(user, %{"status" => "uguu"})
61 "id" => Utils.generate_activity_id(),
62 "object" => post_activity.data["object"],
63 "actor" => user.ap_id,
64 "context" => "a context"
67 %{valid_like: valid_like, user: user, post_activity: post_activity}
70 test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
71 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
73 assert "id" in Map.keys(object)
76 test "is valid for a valid object", %{valid_like: valid_like} do
77 assert LikeValidator.cast_and_validate(valid_like).valid?
80 test "sets the 'to' field to the object actor if no recipients are given", %{
81 valid_like: valid_like,
88 {:ok, object, _meta} = ObjectValidator.validate(without_recipients, [])
90 assert object["to"] == [user.ap_id]
93 test "sets the context field to the context of the object if no context is given", %{
94 valid_like: valid_like,
95 post_activity: post_activity
99 |> Map.delete("context")
101 {:ok, object, _meta} = ObjectValidator.validate(without_context, [])
103 assert object["context"] == post_activity.data["context"]
106 test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
107 without_actor = Map.delete(valid_like, "actor")
109 refute LikeValidator.cast_and_validate(without_actor).valid?
111 with_invalid_actor = Map.put(valid_like, "actor", "invalidactor")
113 refute LikeValidator.cast_and_validate(with_invalid_actor).valid?
116 test "it errors when the object is missing or not known", %{valid_like: valid_like} do
117 without_object = Map.delete(valid_like, "object")
119 refute LikeValidator.cast_and_validate(without_object).valid?
121 with_invalid_object = Map.put(valid_like, "object", "invalidobject")
123 refute LikeValidator.cast_and_validate(with_invalid_object).valid?
126 test "it errors when the actor has already like the object", %{
127 valid_like: valid_like,
129 post_activity: post_activity
131 _like = CommonAPI.favorite(user, post_activity.id)
133 refute LikeValidator.cast_and_validate(valid_like).valid?
136 test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
139 |> Map.put("actor", %{"id" => valid_like["actor"]})
140 |> Map.put("object", %{"id" => valid_like["object"]})
142 validated = LikeValidator.cast_and_validate(wrapped_like)
144 assert validated.valid?
146 assert {:actor, valid_like["actor"]} in validated.changes
147 assert {:object, valid_like["object"]} in validated.changes