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 "let's through some basic html", %{user: user, recipient: recipient} do
117 {:ok, valid_chat_message, _} =
118 Builder.chat_message(
121 "hey <a href='https://example.org'>example</a> <script>alert('uguu')</script>"
124 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
126 assert object["content"] ==
127 "hey <a href=\"https://example.org\">example</a> alert('uguu')"
130 test "validates for a basic object we build", %{valid_chat_message: valid_chat_message} do
131 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
133 assert Map.put(valid_chat_message, "attachment", nil) == object
136 test "validates for a basic object with an attachment", %{
137 valid_chat_message: valid_chat_message,
141 content_type: "image/jpg",
142 path: Path.absname("test/fixtures/image.jpg"),
143 filename: "an_image.jpg"
146 {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
150 |> Map.put("attachment", attachment.data)
152 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
154 assert object["attachment"]
157 test "validates for a basic object with an attachment in an array", %{
158 valid_chat_message: valid_chat_message,
162 content_type: "image/jpg",
163 path: Path.absname("test/fixtures/image.jpg"),
164 filename: "an_image.jpg"
167 {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
171 |> Map.put("attachment", [attachment.data])
173 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
175 assert object["attachment"]
178 test "validates for a basic object with an attachment but without content", %{
179 valid_chat_message: valid_chat_message,
183 content_type: "image/jpg",
184 path: Path.absname("test/fixtures/image.jpg"),
185 filename: "an_image.jpg"
188 {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
192 |> Map.put("attachment", attachment.data)
193 |> Map.delete("content")
195 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
197 assert object["attachment"]
200 test "does not validate if the message has no content", %{
201 valid_chat_message: valid_chat_message
205 |> Map.delete("content")
207 refute match?({:ok, _object, _meta}, ObjectValidator.validate(contentless, []))
210 test "does not validate if the message is longer than the remote_limit", %{
211 valid_chat_message: valid_chat_message
213 Pleroma.Config.put([:instance, :remote_limit], 2)
214 refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
217 test "does not validate if the recipient is blocking the actor", %{
218 valid_chat_message: valid_chat_message,
222 Pleroma.User.block(recipient, user)
223 refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
226 test "does not validate if the actor or the recipient is not in our system", %{
227 valid_chat_message: valid_chat_message
231 |> Map.put("actor", "https://raymoo.com/raymoo")
233 {:error, _} = ObjectValidator.validate(chat_message, [])
237 |> Map.put("to", ["https://raymoo.com/raymoo"])
239 {:error, _} = ObjectValidator.validate(chat_message, [])
242 test "does not validate for a message with multiple recipients", %{
243 valid_chat_message: valid_chat_message,
249 |> Map.put("to", [user.ap_id, recipient.ap_id])
251 assert {:error, _} = ObjectValidator.validate(chat_message, [])
254 test "does not validate if it doesn't concern local users" do
255 user = insert(:user, local: false)
256 recipient = insert(:user, local: false)
258 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey")
259 assert {:error, _} = ObjectValidator.validate(valid_chat_message, [])
263 describe "EmojiReacts" do
266 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
268 object = Pleroma.Object.get_by_ap_id(post_activity.data["object"])
270 {:ok, valid_emoji_react, []} = Builder.emoji_react(user, object, "👌")
272 %{user: user, post_activity: post_activity, valid_emoji_react: valid_emoji_react}
275 test "it validates a valid EmojiReact", %{valid_emoji_react: valid_emoji_react} do
276 assert {:ok, _, _} = ObjectValidator.validate(valid_emoji_react, [])
279 test "it is not valid without a 'content' field", %{valid_emoji_react: valid_emoji_react} do
282 |> Map.delete("content")
284 {:error, cng} = ObjectValidator.validate(without_content, [])
287 assert {:content, {"can't be blank", [validation: :required]}} in cng.errors
290 test "it is not valid with a non-emoji content field", %{valid_emoji_react: valid_emoji_react} do
291 without_emoji_content =
293 |> Map.put("content", "x")
295 {:error, cng} = ObjectValidator.validate(without_emoji_content, [])
299 assert {:content, {"must be a single character emoji", []}} in cng.errors
306 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
307 {:ok, like} = CommonAPI.favorite(user, post_activity.id)
308 {:ok, valid_like_undo, []} = Builder.undo(user, like)
310 %{user: user, like: like, valid_like_undo: valid_like_undo}
313 test "it validates a basic like undo", %{valid_like_undo: valid_like_undo} do
314 assert {:ok, _, _} = ObjectValidator.validate(valid_like_undo, [])
317 test "it does not validate if the actor of the undo is not the actor of the object", %{
318 valid_like_undo: valid_like_undo
320 other_user = insert(:user, ap_id: "https://gensokyo.2hu/users/raymoo")
324 |> Map.put("actor", other_user.ap_id)
326 {:error, cng} = ObjectValidator.validate(bad_actor, [])
328 assert {:actor, {"not the same as object actor", []}} in cng.errors
331 test "it does not validate if the object is missing", %{valid_like_undo: valid_like_undo} do
334 |> Map.put("object", "https://gensokyo.2hu/objects/1")
336 {:error, cng} = ObjectValidator.validate(missing_object, [])
338 assert {:object, {"can't find object", []}} in cng.errors
339 assert length(cng.errors) == 1
343 describe "deletes" do
346 {:ok, post_activity} = CommonAPI.post(user, %{status: "cancel me daddy"})
348 {:ok, valid_post_delete, _} = Builder.delete(user, post_activity.data["object"])
349 {:ok, valid_user_delete, _} = Builder.delete(user, user.ap_id)
351 %{user: user, valid_post_delete: valid_post_delete, valid_user_delete: valid_user_delete}
354 test "it is valid for a post deletion", %{valid_post_delete: valid_post_delete} do
355 {:ok, valid_post_delete, _} = ObjectValidator.validate(valid_post_delete, [])
357 assert valid_post_delete["deleted_activity_id"]
360 test "it is invalid if the object isn't in a list of certain types", %{
361 valid_post_delete: valid_post_delete
363 object = Object.get_by_ap_id(valid_post_delete["object"])
367 |> Map.put("type", "Like")
371 |> Ecto.Changeset.change(%{data: data})
372 |> Object.update_and_set_cache()
374 {:error, cng} = ObjectValidator.validate(valid_post_delete, [])
375 assert {:object, {"object not in allowed types", []}} in cng.errors
378 test "it is valid for a user deletion", %{valid_user_delete: valid_user_delete} do
379 assert match?({:ok, _, _}, ObjectValidator.validate(valid_user_delete, []))
382 test "it's invalid if the id is missing", %{valid_post_delete: valid_post_delete} do
387 {:error, cng} = ObjectValidator.validate(no_id, [])
389 assert {:id, {"can't be blank", [validation: :required]}} in cng.errors
392 test "it's invalid if the object doesn't exist", %{valid_post_delete: valid_post_delete} do
395 |> Map.put("object", "http://does.not/exist")
397 {:error, cng} = ObjectValidator.validate(missing_object, [])
399 assert {:object, {"can't find object", []}} in cng.errors
402 test "it's invalid if the actor of the object and the actor of delete are from different domains",
403 %{valid_post_delete: valid_post_delete} do
404 valid_user = insert(:user)
408 |> Map.put("actor", valid_user.ap_id)
410 assert match?({:ok, _, _}, ObjectValidator.validate(valid_other_actor, []))
412 invalid_other_actor =
414 |> Map.put("actor", "https://gensokyo.2hu/users/raymoo")
416 {:error, cng} = ObjectValidator.validate(invalid_other_actor, [])
418 assert {:actor, {"is not allowed to delete object", []}} in cng.errors
421 test "it's valid if the actor of the object is a local superuser",
422 %{valid_post_delete: valid_post_delete} do
424 insert(:user, local: true, is_moderator: true, ap_id: "https://gensokyo.2hu/users/raymoo")
428 |> Map.put("actor", user.ap_id)
430 {:ok, _, meta} = ObjectValidator.validate(valid_other_actor, [])
431 assert meta[:do_not_federate]
438 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
441 "to" => [user.ap_id],
444 "id" => Utils.generate_activity_id(),
445 "object" => post_activity.data["object"],
446 "actor" => user.ap_id,
447 "context" => "a context"
450 %{valid_like: valid_like, user: user, post_activity: post_activity}
453 test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
454 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
456 assert "id" in Map.keys(object)
459 test "is valid for a valid object", %{valid_like: valid_like} do
460 assert LikeValidator.cast_and_validate(valid_like).valid?
463 test "sets the 'to' field to the object actor if no recipients are given", %{
464 valid_like: valid_like,
471 {:ok, object, _meta} = ObjectValidator.validate(without_recipients, [])
473 assert object["to"] == [user.ap_id]
476 test "sets the context field to the context of the object if no context is given", %{
477 valid_like: valid_like,
478 post_activity: post_activity
482 |> Map.delete("context")
484 {:ok, object, _meta} = ObjectValidator.validate(without_context, [])
486 assert object["context"] == post_activity.data["context"]
489 test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
490 without_actor = Map.delete(valid_like, "actor")
492 refute LikeValidator.cast_and_validate(without_actor).valid?
494 with_invalid_actor = Map.put(valid_like, "actor", "invalidactor")
496 refute LikeValidator.cast_and_validate(with_invalid_actor).valid?
499 test "it errors when the object is missing or not known", %{valid_like: valid_like} do
500 without_object = Map.delete(valid_like, "object")
502 refute LikeValidator.cast_and_validate(without_object).valid?
504 with_invalid_object = Map.put(valid_like, "object", "invalidobject")
506 refute LikeValidator.cast_and_validate(with_invalid_object).valid?
509 test "it errors when the actor has already like the object", %{
510 valid_like: valid_like,
512 post_activity: post_activity
514 _like = CommonAPI.favorite(user, post_activity.id)
516 refute LikeValidator.cast_and_validate(valid_like).valid?
519 test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
522 |> Map.put("actor", %{"id" => valid_like["actor"]})
523 |> Map.put("object", %{"id" => valid_like["object"]})
525 validated = LikeValidator.cast_and_validate(wrapped_like)
527 assert validated.valid?
529 assert {:actor, valid_like["actor"]} in validated.changes
530 assert {:object, valid_like["object"]} in validated.changes
534 describe "announces" do
537 announcer = insert(:user)
538 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
540 object = Object.normalize(post_activity, false)
541 {:ok, valid_announce, []} = Builder.announce(announcer, object)
544 valid_announce: valid_announce,
546 post_activity: post_activity,
551 test "returns ok for a valid announce", %{valid_announce: valid_announce} do
552 assert {:ok, _object, _meta} = ObjectValidator.validate(valid_announce, [])
555 test "returns an error if the object can't be found", %{valid_announce: valid_announce} do
558 |> Map.delete("object")
560 {:error, cng} = ObjectValidator.validate(without_object, [])
562 assert {:object, {"can't be blank", [validation: :required]}} in cng.errors
566 |> Map.put("object", "https://gensokyo.2hu/objects/99999999")
568 {:error, cng} = ObjectValidator.validate(nonexisting_object, [])
570 assert {:object, {"can't find object", []}} in cng.errors
573 test "returns an error if we don't have the actor", %{valid_announce: valid_announce} do
576 |> Map.put("actor", "https://gensokyo.2hu/users/raymoo")
578 {:error, cng} = ObjectValidator.validate(nonexisting_actor, [])
580 assert {:actor, {"can't find user", []}} in cng.errors
583 test "returns an error if the actor already announced the object", %{
584 valid_announce: valid_announce,
585 announcer: announcer,
586 post_activity: post_activity
588 _announce = CommonAPI.repeat(post_activity.id, announcer)
590 {:error, cng} = ObjectValidator.validate(valid_announce, [])
592 assert {:actor, {"already announced this object", []}} in cng.errors
593 assert {:object, {"already announced by this actor", []}} in cng.errors
596 test "returns an error if the actor can't announce the object", %{
597 announcer: announcer,
600 {:ok, post_activity} =
601 CommonAPI.post(user, %{status: "a secret post", visibility: "private"})
603 object = Object.normalize(post_activity, false)
605 # Another user can't announce it
606 {:ok, announce, []} = Builder.announce(announcer, object, public: false)
608 {:error, cng} = ObjectValidator.validate(announce, [])
610 assert {:actor, {"can not announce this object", []}} in cng.errors
612 # The actor of the object can announce it
613 {:ok, announce, []} = Builder.announce(user, object, public: false)
615 assert {:ok, _, _} = ObjectValidator.validate(announce, [])
617 # The actor of the object can not announce it publicly
618 {:ok, announce, []} = Builder.announce(user, object, public: true)
620 {:error, cng} = ObjectValidator.validate(announce, [])
622 assert {:actor, {"can not announce this object publicly", []}} in cng.errors