1 defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do
5 alias Pleroma.Web.ActivityPub.ActivityPub
6 alias Pleroma.Web.ActivityPub.Builder
7 alias Pleroma.Web.ActivityPub.ObjectValidator
8 alias Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator
9 alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
10 alias Pleroma.Web.ActivityPub.Utils
11 alias Pleroma.Web.CommonAPI
13 import Pleroma.Factory
15 describe "attachments" do
16 test "works with honkerific attachments" do
20 "summary" => "298p3RG7j27tfsZ9RQ.jpg",
22 "url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg"
25 assert {:ok, attachment} =
26 AttachmentValidator.cast_and_validate(attachment)
27 |> Ecto.Changeset.apply_action(:insert)
29 assert attachment.mediaType == "application/octet-stream"
32 test "it turns mastodon attachments into our attachments" do
35 "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
38 "mediaType" => "image/jpeg"
42 AttachmentValidator.cast_and_validate(attachment)
43 |> Ecto.Changeset.apply_action(:insert)
48 "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
50 mediaType: "image/jpeg"
54 assert attachment.mediaType == "image/jpeg"
57 test "it handles our own uploads" do
61 content_type: "image/jpg",
62 path: Path.absname("test/fixtures/image.jpg"),
63 filename: "an_image.jpg"
66 {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
70 |> AttachmentValidator.cast_and_validate()
71 |> Ecto.Changeset.apply_action(:insert)
73 assert attachment.mediaType == "image/jpeg"
77 describe "chat message create activities" do
78 test "it is invalid if the object already exists" do
80 recipient = insert(:user)
81 {:ok, activity} = CommonAPI.post_chat_message(user, recipient, "hey")
82 object = Object.normalize(activity, false)
84 {:ok, create_data, _} = Builder.create(user, object.data, [recipient.ap_id])
86 {:error, cng} = ObjectValidator.validate(create_data, [])
88 assert {:object, {"The object to create already exists", []}} in cng.errors
91 test "it is invalid if the object data has a different `to` or `actor` field" do
93 recipient = insert(:user)
94 {:ok, object_data, _} = Builder.chat_message(recipient, user.ap_id, "Hey")
96 {:ok, create_data, _} = Builder.create(user, object_data, [recipient.ap_id])
98 {:error, cng} = ObjectValidator.validate(create_data, [])
100 assert {:to, {"Recipients don't match with object recipients", []}} in cng.errors
101 assert {:actor, {"Actor doesn't match with object actor", []}} in cng.errors
105 describe "chat messages" do
107 clear_config([:instance, :remote_limit])
109 recipient = insert(:user, local: false)
111 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey :firefox:")
113 %{user: user, recipient: recipient, valid_chat_message: valid_chat_message}
116 test "validates for a basic object we build", %{valid_chat_message: valid_chat_message} do
117 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
119 assert Map.put(valid_chat_message, "attachment", nil) == object
122 test "validates for a basic object with an attachment", %{
123 valid_chat_message: valid_chat_message,
127 content_type: "image/jpg",
128 path: Path.absname("test/fixtures/image.jpg"),
129 filename: "an_image.jpg"
132 {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
136 |> Map.put("attachment", attachment.data)
138 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
140 assert object["attachment"]
143 test "validates for a basic object with an attachment in an array", %{
144 valid_chat_message: valid_chat_message,
148 content_type: "image/jpg",
149 path: Path.absname("test/fixtures/image.jpg"),
150 filename: "an_image.jpg"
153 {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
157 |> Map.put("attachment", [attachment.data])
159 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
161 assert object["attachment"]
164 test "validates for a basic object with an attachment but without content", %{
165 valid_chat_message: valid_chat_message,
169 content_type: "image/jpg",
170 path: Path.absname("test/fixtures/image.jpg"),
171 filename: "an_image.jpg"
174 {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
178 |> Map.put("attachment", attachment.data)
179 |> Map.delete("content")
181 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
183 assert object["attachment"]
186 test "does not validate if the message has no content", %{
187 valid_chat_message: valid_chat_message
191 |> Map.delete("content")
193 refute match?({:ok, _object, _meta}, ObjectValidator.validate(contentless, []))
196 test "does not validate if the message is longer than the remote_limit", %{
197 valid_chat_message: valid_chat_message
199 Pleroma.Config.put([:instance, :remote_limit], 2)
200 refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
203 test "does not validate if the recipient is blocking the actor", %{
204 valid_chat_message: valid_chat_message,
208 Pleroma.User.block(recipient, user)
209 refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
212 test "does not validate if the actor or the recipient is not in our system", %{
213 valid_chat_message: valid_chat_message
217 |> Map.put("actor", "https://raymoo.com/raymoo")
219 {:error, _} = ObjectValidator.validate(chat_message, [])
223 |> Map.put("to", ["https://raymoo.com/raymoo"])
225 {:error, _} = ObjectValidator.validate(chat_message, [])
228 test "does not validate for a message with multiple recipients", %{
229 valid_chat_message: valid_chat_message,
235 |> Map.put("to", [user.ap_id, recipient.ap_id])
237 assert {:error, _} = ObjectValidator.validate(chat_message, [])
240 test "does not validate if it doesn't concern local users" do
241 user = insert(:user, local: false)
242 recipient = insert(:user, local: false)
244 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey")
245 assert {:error, _} = ObjectValidator.validate(valid_chat_message, [])
249 describe "EmojiReacts" do
252 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
254 object = Pleroma.Object.get_by_ap_id(post_activity.data["object"])
256 {:ok, valid_emoji_react, []} = Builder.emoji_react(user, object, "👌")
258 %{user: user, post_activity: post_activity, valid_emoji_react: valid_emoji_react}
261 test "it validates a valid EmojiReact", %{valid_emoji_react: valid_emoji_react} do
262 assert {:ok, _, _} = ObjectValidator.validate(valid_emoji_react, [])
265 test "it is not valid without a 'content' field", %{valid_emoji_react: valid_emoji_react} do
268 |> Map.delete("content")
270 {:error, cng} = ObjectValidator.validate(without_content, [])
273 assert {:content, {"can't be blank", [validation: :required]}} in cng.errors
276 test "it is not valid with a non-emoji content field", %{valid_emoji_react: valid_emoji_react} do
277 without_emoji_content =
279 |> Map.put("content", "x")
281 {:error, cng} = ObjectValidator.validate(without_emoji_content, [])
285 assert {:content, {"must be a single character emoji", []}} in cng.errors
292 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
293 {:ok, like} = CommonAPI.favorite(user, post_activity.id)
294 {:ok, valid_like_undo, []} = Builder.undo(user, like)
296 %{user: user, like: like, valid_like_undo: valid_like_undo}
299 test "it validates a basic like undo", %{valid_like_undo: valid_like_undo} do
300 assert {:ok, _, _} = ObjectValidator.validate(valid_like_undo, [])
303 test "it does not validate if the actor of the undo is not the actor of the object", %{
304 valid_like_undo: valid_like_undo
306 other_user = insert(:user, ap_id: "https://gensokyo.2hu/users/raymoo")
310 |> Map.put("actor", other_user.ap_id)
312 {:error, cng} = ObjectValidator.validate(bad_actor, [])
314 assert {:actor, {"not the same as object actor", []}} in cng.errors
317 test "it does not validate if the object is missing", %{valid_like_undo: valid_like_undo} do
320 |> Map.put("object", "https://gensokyo.2hu/objects/1")
322 {:error, cng} = ObjectValidator.validate(missing_object, [])
324 assert {:object, {"can't find object", []}} in cng.errors
325 assert length(cng.errors) == 1
329 describe "deletes" do
332 {:ok, post_activity} = CommonAPI.post(user, %{status: "cancel me daddy"})
334 {:ok, valid_post_delete, _} = Builder.delete(user, post_activity.data["object"])
335 {:ok, valid_user_delete, _} = Builder.delete(user, user.ap_id)
337 %{user: user, valid_post_delete: valid_post_delete, valid_user_delete: valid_user_delete}
340 test "it is valid for a post deletion", %{valid_post_delete: valid_post_delete} do
341 {:ok, valid_post_delete, _} = ObjectValidator.validate(valid_post_delete, [])
343 assert valid_post_delete["deleted_activity_id"]
346 test "it is invalid if the object isn't in a list of certain types", %{
347 valid_post_delete: valid_post_delete
349 object = Object.get_by_ap_id(valid_post_delete["object"])
353 |> Map.put("type", "Like")
357 |> Ecto.Changeset.change(%{data: data})
358 |> Object.update_and_set_cache()
360 {:error, cng} = ObjectValidator.validate(valid_post_delete, [])
361 assert {:object, {"object not in allowed types", []}} in cng.errors
364 test "it is valid for a user deletion", %{valid_user_delete: valid_user_delete} do
365 assert match?({:ok, _, _}, ObjectValidator.validate(valid_user_delete, []))
368 test "it's invalid if the id is missing", %{valid_post_delete: valid_post_delete} do
373 {:error, cng} = ObjectValidator.validate(no_id, [])
375 assert {:id, {"can't be blank", [validation: :required]}} in cng.errors
378 test "it's invalid if the object doesn't exist", %{valid_post_delete: valid_post_delete} do
381 |> Map.put("object", "http://does.not/exist")
383 {:error, cng} = ObjectValidator.validate(missing_object, [])
385 assert {:object, {"can't find object", []}} in cng.errors
388 test "it's invalid if the actor of the object and the actor of delete are from different domains",
389 %{valid_post_delete: valid_post_delete} do
390 valid_user = insert(:user)
394 |> Map.put("actor", valid_user.ap_id)
396 assert match?({:ok, _, _}, ObjectValidator.validate(valid_other_actor, []))
398 invalid_other_actor =
400 |> Map.put("actor", "https://gensokyo.2hu/users/raymoo")
402 {:error, cng} = ObjectValidator.validate(invalid_other_actor, [])
404 assert {:actor, {"is not allowed to delete object", []}} in cng.errors
407 test "it's valid if the actor of the object is a local superuser",
408 %{valid_post_delete: valid_post_delete} do
410 insert(:user, local: true, is_moderator: true, ap_id: "https://gensokyo.2hu/users/raymoo")
414 |> Map.put("actor", user.ap_id)
416 {:ok, _, meta} = ObjectValidator.validate(valid_other_actor, [])
417 assert meta[:do_not_federate]
424 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
427 "to" => [user.ap_id],
430 "id" => Utils.generate_activity_id(),
431 "object" => post_activity.data["object"],
432 "actor" => user.ap_id,
433 "context" => "a context"
436 %{valid_like: valid_like, user: user, post_activity: post_activity}
439 test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
440 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
442 assert "id" in Map.keys(object)
445 test "is valid for a valid object", %{valid_like: valid_like} do
446 assert LikeValidator.cast_and_validate(valid_like).valid?
449 test "sets the 'to' field to the object actor if no recipients are given", %{
450 valid_like: valid_like,
457 {:ok, object, _meta} = ObjectValidator.validate(without_recipients, [])
459 assert object["to"] == [user.ap_id]
462 test "sets the context field to the context of the object if no context is given", %{
463 valid_like: valid_like,
464 post_activity: post_activity
468 |> Map.delete("context")
470 {:ok, object, _meta} = ObjectValidator.validate(without_context, [])
472 assert object["context"] == post_activity.data["context"]
475 test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
476 without_actor = Map.delete(valid_like, "actor")
478 refute LikeValidator.cast_and_validate(without_actor).valid?
480 with_invalid_actor = Map.put(valid_like, "actor", "invalidactor")
482 refute LikeValidator.cast_and_validate(with_invalid_actor).valid?
485 test "it errors when the object is missing or not known", %{valid_like: valid_like} do
486 without_object = Map.delete(valid_like, "object")
488 refute LikeValidator.cast_and_validate(without_object).valid?
490 with_invalid_object = Map.put(valid_like, "object", "invalidobject")
492 refute LikeValidator.cast_and_validate(with_invalid_object).valid?
495 test "it errors when the actor has already like the object", %{
496 valid_like: valid_like,
498 post_activity: post_activity
500 _like = CommonAPI.favorite(user, post_activity.id)
502 refute LikeValidator.cast_and_validate(valid_like).valid?
505 test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
508 |> Map.put("actor", %{"id" => valid_like["actor"]})
509 |> Map.put("object", %{"id" => valid_like["object"]})
511 validated = LikeValidator.cast_and_validate(wrapped_like)
513 assert validated.valid?
515 assert {:actor, valid_like["actor"]} in validated.changes
516 assert {:object, valid_like["object"]} in validated.changes
520 describe "announces" do
523 announcer = insert(:user)
524 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
526 object = Object.normalize(post_activity, false)
527 {:ok, valid_announce, []} = Builder.announce(announcer, object)
530 valid_announce: valid_announce,
532 post_activity: post_activity,
537 test "returns ok for a valid announce", %{valid_announce: valid_announce} do
538 assert {:ok, _object, _meta} = ObjectValidator.validate(valid_announce, [])
541 test "returns an error if the object can't be found", %{valid_announce: valid_announce} do
544 |> Map.delete("object")
546 {:error, cng} = ObjectValidator.validate(without_object, [])
548 assert {:object, {"can't be blank", [validation: :required]}} in cng.errors
552 |> Map.put("object", "https://gensokyo.2hu/objects/99999999")
554 {:error, cng} = ObjectValidator.validate(nonexisting_object, [])
556 assert {:object, {"can't find object", []}} in cng.errors
559 test "returns an error if we don't have the actor", %{valid_announce: valid_announce} do
562 |> Map.put("actor", "https://gensokyo.2hu/users/raymoo")
564 {:error, cng} = ObjectValidator.validate(nonexisting_actor, [])
566 assert {:actor, {"can't find user", []}} in cng.errors
569 test "returns an error if the actor already announced the object", %{
570 valid_announce: valid_announce,
571 announcer: announcer,
572 post_activity: post_activity
574 _announce = CommonAPI.repeat(post_activity.id, announcer)
576 {:error, cng} = ObjectValidator.validate(valid_announce, [])
578 assert {:actor, {"already announced this object", []}} in cng.errors
579 assert {:object, {"already announced by this actor", []}} in cng.errors
582 test "returns an error if the actor can't announce the object", %{
583 announcer: announcer,
586 {:ok, post_activity} =
587 CommonAPI.post(user, %{status: "a secret post", visibility: "private"})
589 object = Object.normalize(post_activity, false)
591 # Another user can't announce it
592 {:ok, announce, []} = Builder.announce(announcer, object, public: false)
594 {:error, cng} = ObjectValidator.validate(announce, [])
596 assert {:actor, {"can not announce this object", []}} in cng.errors
598 # The actor of the object can announce it
599 {:ok, announce, []} = Builder.announce(user, object, public: false)
601 assert {:ok, _, _} = ObjectValidator.validate(announce, [])
603 # The actor of the object can not announce it publicly
604 {:ok, announce, []} = Builder.announce(user, object, public: true)
606 {:error, cng} = ObjectValidator.validate(announce, [])
608 assert {:actor, {"can not announce this object publicly", []}} in cng.errors