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" => "cancel me daddy"})
17 {:ok, valid_post_delete, _} = Builder.delete(user, post_activity.data["object"])
19 %{user: user, valid_post_delete: valid_post_delete}
22 test "it is valid for a post deletion", %{valid_post_delete: valid_post_delete} do
23 assert match?({:ok, _, _}, ObjectValidator.validate(valid_post_delete, []))
26 test "it's invalid if the id is missing", %{valid_post_delete: valid_post_delete} do
31 {:error, cng} = ObjectValidator.validate(no_id, [])
33 assert {:id, {"can't be blank", [validation: :required]}} in cng.errors
36 test "it's invalid if the object doesn't exist", %{valid_post_delete: valid_post_delete} do
39 |> Map.put("object", "http://does.not/exist")
41 {:error, cng} = ObjectValidator.validate(missing_object, [])
43 assert {:object, {"can't find object", []}} in cng.errors
46 test "it's invalid if the actor of the object and the actor of delete are from different domains",
47 %{valid_post_delete: valid_post_delete} do
50 |> Map.put("actor", valid_post_delete["actor"] <> "1")
52 assert match?({:ok, _, _}, ObjectValidator.validate(valid_other_actor, []))
56 |> Map.put("actor", "https://gensokyo.2hu/users/raymoo")
58 {:error, cng} = ObjectValidator.validate(invalid_other_actor, [])
60 assert {:actor, {"is not allowed to delete object", []}} in cng.errors
63 test "it's invalid if all the recipient fields are empty", %{
64 valid_post_delete: valid_post_delete
71 {:error, cng} = ObjectValidator.validate(empty_recipients, [])
73 assert {:to, {"no recipients in any field", []}} in cng.errors
74 assert {:cc, {"no recipients in any field", []}} in cng.errors
81 {:ok, post_activity} = CommonAPI.post(user, %{"status" => "uguu"})
87 "id" => Utils.generate_activity_id(),
88 "object" => post_activity.data["object"],
89 "actor" => user.ap_id,
90 "context" => "a context"
93 %{valid_like: valid_like, user: user, post_activity: post_activity}
96 test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
97 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
99 assert "id" in Map.keys(object)
102 test "is valid for a valid object", %{valid_like: valid_like} do
103 assert LikeValidator.cast_and_validate(valid_like).valid?
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