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
13 describe "EmojiReacts" do
16 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
18 object = Pleroma.Object.get_by_ap_id(post_activity.data["object"])
20 {:ok, valid_emoji_react, []} = Builder.emoji_react(user, object, "👌")
22 %{user: user, post_activity: post_activity, valid_emoji_react: valid_emoji_react}
25 test "it validates a valid EmojiReact", %{valid_emoji_react: valid_emoji_react} do
26 assert {:ok, _, _} = ObjectValidator.validate(valid_emoji_react, [])
29 test "it is not valid without a 'content' field", %{valid_emoji_react: valid_emoji_react} do
32 |> Map.delete("content")
34 {:error, cng} = ObjectValidator.validate(without_content, [])
37 assert {:content, {"can't be blank", [validation: :required]}} in cng.errors
40 test "it is not valid with a non-emoji content field", %{valid_emoji_react: valid_emoji_react} do
41 without_emoji_content =
43 |> Map.put("content", "x")
45 {:error, cng} = ObjectValidator.validate(without_emoji_content, [])
49 assert {:content, {"must be a single character emoji", []}} in cng.errors
56 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
57 {:ok, like} = CommonAPI.favorite(user, post_activity.id)
58 {:ok, valid_like_undo, []} = Builder.undo(user, like)
60 %{user: user, like: like, valid_like_undo: valid_like_undo}
63 test "it validates a basic like undo", %{valid_like_undo: valid_like_undo} do
64 assert {:ok, _, _} = ObjectValidator.validate(valid_like_undo, [])
67 test "it does not validate if the actor of the undo is not the actor of the object", %{
68 valid_like_undo: valid_like_undo
70 other_user = insert(:user, ap_id: "https://gensokyo.2hu/users/raymoo")
74 |> Map.put("actor", other_user.ap_id)
76 {:error, cng} = ObjectValidator.validate(bad_actor, [])
78 assert {:actor, {"not the same as object actor", []}} in cng.errors
81 test "it does not validate if the object is missing", %{valid_like_undo: valid_like_undo} do
84 |> Map.put("object", "https://gensokyo.2hu/objects/1")
86 {:error, cng} = ObjectValidator.validate(missing_object, [])
88 assert {:object, {"can't find object", []}} in cng.errors
89 assert length(cng.errors) == 1
96 {:ok, post_activity} = CommonAPI.post(user, %{status: "cancel me daddy"})
98 {:ok, valid_post_delete, _} = Builder.delete(user, post_activity.data["object"])
99 {:ok, valid_user_delete, _} = Builder.delete(user, user.ap_id)
101 %{user: user, valid_post_delete: valid_post_delete, valid_user_delete: valid_user_delete}
104 test "it is valid for a post deletion", %{valid_post_delete: valid_post_delete} do
105 {:ok, valid_post_delete, _} = ObjectValidator.validate(valid_post_delete, [])
107 assert valid_post_delete["deleted_activity_id"]
110 test "it is invalid if the object isn't in a list of certain types", %{
111 valid_post_delete: valid_post_delete
113 object = Object.get_by_ap_id(valid_post_delete["object"])
117 |> Map.put("type", "Like")
121 |> Ecto.Changeset.change(%{data: data})
122 |> Object.update_and_set_cache()
124 {:error, cng} = ObjectValidator.validate(valid_post_delete, [])
125 assert {:object, {"object not in allowed types", []}} in cng.errors
128 test "it is valid for a user deletion", %{valid_user_delete: valid_user_delete} do
129 assert match?({:ok, _, _}, ObjectValidator.validate(valid_user_delete, []))
132 test "it's invalid if the id is missing", %{valid_post_delete: valid_post_delete} do
137 {:error, cng} = ObjectValidator.validate(no_id, [])
139 assert {:id, {"can't be blank", [validation: :required]}} in cng.errors
142 test "it's invalid if the object doesn't exist", %{valid_post_delete: valid_post_delete} do
145 |> Map.put("object", "http://does.not/exist")
147 {:error, cng} = ObjectValidator.validate(missing_object, [])
149 assert {:object, {"can't find object", []}} in cng.errors
152 test "it's invalid if the actor of the object and the actor of delete are from different domains",
153 %{valid_post_delete: valid_post_delete} do
154 valid_user = insert(:user)
158 |> Map.put("actor", valid_user.ap_id)
160 assert match?({:ok, _, _}, ObjectValidator.validate(valid_other_actor, []))
162 invalid_other_actor =
164 |> Map.put("actor", "https://gensokyo.2hu/users/raymoo")
166 {:error, cng} = ObjectValidator.validate(invalid_other_actor, [])
168 assert {:actor, {"is not allowed to delete object", []}} in cng.errors
171 test "it's valid if the actor of the object is a local superuser",
172 %{valid_post_delete: valid_post_delete} do
174 insert(:user, local: true, is_moderator: true, ap_id: "https://gensokyo.2hu/users/raymoo")
178 |> Map.put("actor", user.ap_id)
180 {:ok, _, meta} = ObjectValidator.validate(valid_other_actor, [])
181 assert meta[:do_not_federate]
188 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
191 "to" => [user.ap_id],
194 "id" => Utils.generate_activity_id(),
195 "object" => post_activity.data["object"],
196 "actor" => user.ap_id,
197 "context" => "a context"
200 %{valid_like: valid_like, user: user, post_activity: post_activity}
203 test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
204 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
206 assert "id" in Map.keys(object)
209 test "is valid for a valid object", %{valid_like: valid_like} do
210 assert LikeValidator.cast_and_validate(valid_like).valid?
213 test "sets the 'to' field to the object actor if no recipients are given", %{
214 valid_like: valid_like,
221 {:ok, object, _meta} = ObjectValidator.validate(without_recipients, [])
223 assert object["to"] == [user.ap_id]
226 test "sets the context field to the context of the object if no context is given", %{
227 valid_like: valid_like,
228 post_activity: post_activity
232 |> Map.delete("context")
234 {:ok, object, _meta} = ObjectValidator.validate(without_context, [])
236 assert object["context"] == post_activity.data["context"]
239 test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
240 without_actor = Map.delete(valid_like, "actor")
242 refute LikeValidator.cast_and_validate(without_actor).valid?
244 with_invalid_actor = Map.put(valid_like, "actor", "invalidactor")
246 refute LikeValidator.cast_and_validate(with_invalid_actor).valid?
249 test "it errors when the object is missing or not known", %{valid_like: valid_like} do
250 without_object = Map.delete(valid_like, "object")
252 refute LikeValidator.cast_and_validate(without_object).valid?
254 with_invalid_object = Map.put(valid_like, "object", "invalidobject")
256 refute LikeValidator.cast_and_validate(with_invalid_object).valid?
259 test "it errors when the actor has already like the object", %{
260 valid_like: valid_like,
262 post_activity: post_activity
264 _like = CommonAPI.favorite(user, post_activity.id)
266 refute LikeValidator.cast_and_validate(valid_like).valid?
269 test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
272 |> Map.put("actor", %{"id" => valid_like["actor"]})
273 |> Map.put("object", %{"id" => valid_like["object"]})
275 validated = LikeValidator.cast_and_validate(wrapped_like)
277 assert validated.valid?
279 assert {:actor, valid_like["actor"]} in validated.changes
280 assert {:object, valid_like["object"]} in validated.changes
284 describe "announces" do
287 announcer = insert(:user)
288 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
290 object = Object.normalize(post_activity, false)
291 {:ok, valid_announce, []} = Builder.announce(announcer, object)
294 valid_announce: valid_announce,
296 post_activity: post_activity,
301 test "returns ok for a valid announce", %{valid_announce: valid_announce} do
302 assert {:ok, _object, _meta} = ObjectValidator.validate(valid_announce, [])
305 test "returns an error if the object can't be found", %{valid_announce: valid_announce} do
308 |> Map.delete("object")
310 {:error, cng} = ObjectValidator.validate(without_object, [])
312 assert {:object, {"can't be blank", [validation: :required]}} in cng.errors
316 |> Map.put("object", "https://gensokyo.2hu/objects/99999999")
318 {:error, cng} = ObjectValidator.validate(nonexisting_object, [])
320 assert {:object, {"can't find object", []}} in cng.errors
323 test "returns an error if we don't have the actor", %{valid_announce: valid_announce} do
326 |> Map.put("actor", "https://gensokyo.2hu/users/raymoo")
328 {:error, cng} = ObjectValidator.validate(nonexisting_actor, [])
330 assert {:actor, {"can't find user", []}} in cng.errors
333 test "returns an error if the actor already announced the object", %{
334 valid_announce: valid_announce,
335 announcer: announcer,
336 post_activity: post_activity
338 _announce = CommonAPI.repeat(post_activity.id, announcer)
340 {:error, cng} = ObjectValidator.validate(valid_announce, [])
342 assert {:actor, {"already announced this object", []}} in cng.errors
343 assert {:object, {"already announced by this actor", []}} in cng.errors
346 test "returns an error if the actor can't announce the object", %{
347 announcer: announcer,
350 {:ok, post_activity} =
351 CommonAPI.post(user, %{status: "a secret post", visibility: "private"})
353 object = Object.normalize(post_activity, false)
355 # Another user can't announce it
356 {:ok, announce, []} = Builder.announce(announcer, object, public: false)
358 {:error, cng} = ObjectValidator.validate(announce, [])
360 assert {:actor, {"can not announce this object", []}} in cng.errors
362 # The actor of the object can announce it
363 {:ok, announce, []} = Builder.announce(user, object, public: false)
365 assert {:ok, _, _} = ObjectValidator.validate(announce, [])
367 # The actor of the object can not announce it publicly
368 {:ok, announce, []} = Builder.announce(user, object, public: true)
370 {:error, cng} = ObjectValidator.validate(announce, [])
372 assert {:actor, {"can not announce this object publicly", []}} in cng.errors