1 defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do
5 alias Pleroma.Web.ActivityPub.Builder
6 alias Pleroma.Web.ActivityPub.ObjectValidator
7 alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
8 alias Pleroma.Web.ActivityPub.Utils
9 alias Pleroma.Web.CommonAPI
11 import Pleroma.Factory
16 {:ok, post_activity} = CommonAPI.post(user, %{"status" => "uguu"})
17 {:ok, like} = CommonAPI.favorite(user, post_activity.id)
18 {:ok, valid_like_undo, []} = Builder.undo(user, like)
20 %{user: user, like: like, valid_like_undo: valid_like_undo}
23 test "it validates a basic like undo", %{valid_like_undo: valid_like_undo} do
24 assert {:ok, _, _} = ObjectValidator.validate(valid_like_undo, [])
27 test "it does not validate if the actor of the undo is not the actor of the object", %{
28 valid_like_undo: valid_like_undo
30 other_user = insert(:user, ap_id: "https://gensokyo.2hu/users/raymoo")
34 |> Map.put("actor", other_user.ap_id)
36 {:error, cng} = ObjectValidator.validate(bad_actor, [])
38 assert {:actor, {"not the same as object actor", []}} in cng.errors
41 test "it does not validate if the object is missing", %{valid_like_undo: valid_like_undo} do
44 |> Map.put("object", "https://gensokyo.2hu/objects/1")
46 {:error, cng} = ObjectValidator.validate(missing_object, [])
48 assert {:object, {"can't find object", []}} in cng.errors
49 assert length(cng.errors) == 1
56 {:ok, post_activity} = CommonAPI.post(user, %{"status" => "cancel me daddy"})
58 {:ok, valid_post_delete, _} = Builder.delete(user, post_activity.data["object"])
59 {:ok, valid_user_delete, _} = Builder.delete(user, user.ap_id)
61 %{user: user, valid_post_delete: valid_post_delete, valid_user_delete: valid_user_delete}
64 test "it is valid for a post deletion", %{valid_post_delete: valid_post_delete} do
65 {:ok, valid_post_delete, _} = ObjectValidator.validate(valid_post_delete, [])
67 assert valid_post_delete["deleted_activity_id"]
70 test "it is invalid if the object isn't in a list of certain types", %{
71 valid_post_delete: valid_post_delete
73 object = Object.get_by_ap_id(valid_post_delete["object"])
77 |> Map.put("type", "Like")
81 |> Ecto.Changeset.change(%{data: data})
82 |> Object.update_and_set_cache()
84 {:error, cng} = ObjectValidator.validate(valid_post_delete, [])
85 assert {:object, {"object not in allowed types", []}} in cng.errors
88 test "it is valid for a user deletion", %{valid_user_delete: valid_user_delete} do
89 assert match?({:ok, _, _}, ObjectValidator.validate(valid_user_delete, []))
92 test "it's invalid if the id is missing", %{valid_post_delete: valid_post_delete} do
97 {:error, cng} = ObjectValidator.validate(no_id, [])
99 assert {:id, {"can't be blank", [validation: :required]}} in cng.errors
102 test "it's invalid if the object doesn't exist", %{valid_post_delete: valid_post_delete} do
105 |> Map.put("object", "http://does.not/exist")
107 {:error, cng} = ObjectValidator.validate(missing_object, [])
109 assert {:object, {"can't find object", []}} in cng.errors
112 test "it's invalid if the actor of the object and the actor of delete are from different domains",
113 %{valid_post_delete: valid_post_delete} do
114 valid_user = insert(:user)
118 |> Map.put("actor", valid_user.ap_id)
120 assert match?({:ok, _, _}, ObjectValidator.validate(valid_other_actor, []))
122 invalid_other_actor =
124 |> Map.put("actor", "https://gensokyo.2hu/users/raymoo")
126 {:error, cng} = ObjectValidator.validate(invalid_other_actor, [])
128 assert {:actor, {"is not allowed to delete object", []}} in cng.errors
131 test "it's valid if the actor of the object is a local superuser",
132 %{valid_post_delete: valid_post_delete} do
134 insert(:user, local: true, is_moderator: true, ap_id: "https://gensokyo.2hu/users/raymoo")
138 |> Map.put("actor", user.ap_id)
140 {:ok, _, meta} = ObjectValidator.validate(valid_other_actor, [])
141 assert meta[:do_not_federate]
148 {:ok, post_activity} = CommonAPI.post(user, %{"status" => "uguu"})
151 "to" => [user.ap_id],
154 "id" => Utils.generate_activity_id(),
155 "object" => post_activity.data["object"],
156 "actor" => user.ap_id,
157 "context" => "a context"
160 %{valid_like: valid_like, user: user, post_activity: post_activity}
163 test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
164 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
166 assert "id" in Map.keys(object)
169 test "is valid for a valid object", %{valid_like: valid_like} do
170 assert LikeValidator.cast_and_validate(valid_like).valid?
173 test "sets the 'to' field to the object actor if no recipients are given", %{
174 valid_like: valid_like,
181 {:ok, object, _meta} = ObjectValidator.validate(without_recipients, [])
183 assert object["to"] == [user.ap_id]
186 test "sets the context field to the context of the object if no context is given", %{
187 valid_like: valid_like,
188 post_activity: post_activity
192 |> Map.delete("context")
194 {:ok, object, _meta} = ObjectValidator.validate(without_context, [])
196 assert object["context"] == post_activity.data["context"]
199 test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
200 without_actor = Map.delete(valid_like, "actor")
202 refute LikeValidator.cast_and_validate(without_actor).valid?
204 with_invalid_actor = Map.put(valid_like, "actor", "invalidactor")
206 refute LikeValidator.cast_and_validate(with_invalid_actor).valid?
209 test "it errors when the object is missing or not known", %{valid_like: valid_like} do
210 without_object = Map.delete(valid_like, "object")
212 refute LikeValidator.cast_and_validate(without_object).valid?
214 with_invalid_object = Map.put(valid_like, "object", "invalidobject")
216 refute LikeValidator.cast_and_validate(with_invalid_object).valid?
219 test "it errors when the actor has already like the object", %{
220 valid_like: valid_like,
222 post_activity: post_activity
224 _like = CommonAPI.favorite(user, post_activity.id)
226 refute LikeValidator.cast_and_validate(valid_like).valid?
229 test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
232 |> Map.put("actor", %{"id" => valid_like["actor"]})
233 |> Map.put("object", %{"id" => valid_like["object"]})
235 validated = LikeValidator.cast_and_validate(wrapped_like)
237 assert validated.valid?
239 assert {:actor, valid_like["actor"]} in validated.changes
240 assert {:object, valid_like["object"]} in validated.changes