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
18 "mediaType" => "image/jpeg",
19 "name" => "298p3RG7j27tfsZ9RQ.jpg",
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)
30 test "it turns mastodon attachments into our attachments" do
33 "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
36 "mediaType" => "image/jpeg"
40 AttachmentValidator.cast_and_validate(attachment)
41 |> Ecto.Changeset.apply_action(:insert)
46 "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
48 mediaType: "image/jpeg"
54 describe "chat message create activities" do
55 test "it is invalid if the object already exists" do
57 recipient = insert(:user)
58 {:ok, activity} = CommonAPI.post_chat_message(user, recipient, "hey")
59 object = Object.normalize(activity, false)
61 {:ok, create_data, _} = Builder.create(user, object.data, [recipient.ap_id])
63 {:error, cng} = ObjectValidator.validate(create_data, [])
65 assert {:object, {"The object to create already exists", []}} in cng.errors
68 test "it is invalid if the object data has a different `to` or `actor` field" do
70 recipient = insert(:user)
71 {:ok, object_data, _} = Builder.chat_message(recipient, user.ap_id, "Hey")
73 {:ok, create_data, _} = Builder.create(user, object_data, [recipient.ap_id])
75 {:error, cng} = ObjectValidator.validate(create_data, [])
77 assert {:to, {"Recipients don't match with object recipients", []}} in cng.errors
78 assert {:actor, {"Actor doesn't match with object actor", []}} in cng.errors
82 describe "chat messages" do
84 clear_config([:instance, :remote_limit])
86 recipient = insert(:user, local: false)
88 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey :firefox:")
90 %{user: user, recipient: recipient, valid_chat_message: valid_chat_message}
93 test "validates for a basic object we build", %{valid_chat_message: valid_chat_message} do
94 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
96 assert Map.put(valid_chat_message, "attachment", nil) == object
99 test "validates for a basic object with an attachment", %{
100 valid_chat_message: valid_chat_message,
104 content_type: "image/jpg",
105 path: Path.absname("test/fixtures/image.jpg"),
106 filename: "an_image.jpg"
109 {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
113 |> Map.put("attachment", attachment.data)
115 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
117 assert object["attachment"]
120 test "validates for a basic object with an attachment in an array", %{
121 valid_chat_message: valid_chat_message,
125 content_type: "image/jpg",
126 path: Path.absname("test/fixtures/image.jpg"),
127 filename: "an_image.jpg"
130 {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
134 |> Map.put("attachment", [attachment.data])
136 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
138 assert object["attachment"]
141 test "validates for a basic object with an attachment but without content", %{
142 valid_chat_message: valid_chat_message,
146 content_type: "image/jpg",
147 path: Path.absname("test/fixtures/image.jpg"),
148 filename: "an_image.jpg"
151 {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
155 |> Map.put("attachment", attachment.data)
156 |> Map.delete("content")
158 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
160 assert object["attachment"]
163 test "does not validate if the message has no content", %{
164 valid_chat_message: valid_chat_message
168 |> Map.delete("content")
170 refute match?({:ok, _object, _meta}, ObjectValidator.validate(contentless, []))
173 test "does not validate if the message is longer than the remote_limit", %{
174 valid_chat_message: valid_chat_message
176 Pleroma.Config.put([:instance, :remote_limit], 2)
177 refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
180 test "does not validate if the recipient is blocking the actor", %{
181 valid_chat_message: valid_chat_message,
185 Pleroma.User.block(recipient, user)
186 refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
189 test "does not validate if the actor or the recipient is not in our system", %{
190 valid_chat_message: valid_chat_message
194 |> Map.put("actor", "https://raymoo.com/raymoo")
196 {:error, _} = ObjectValidator.validate(chat_message, [])
200 |> Map.put("to", ["https://raymoo.com/raymoo"])
202 {:error, _} = ObjectValidator.validate(chat_message, [])
205 test "does not validate for a message with multiple recipients", %{
206 valid_chat_message: valid_chat_message,
212 |> Map.put("to", [user.ap_id, recipient.ap_id])
214 assert {:error, _} = ObjectValidator.validate(chat_message, [])
217 test "does not validate if it doesn't concern local users" do
218 user = insert(:user, local: false)
219 recipient = insert(:user, local: false)
221 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey")
222 assert {:error, _} = ObjectValidator.validate(valid_chat_message, [])
226 describe "EmojiReacts" do
229 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
231 object = Pleroma.Object.get_by_ap_id(post_activity.data["object"])
233 {:ok, valid_emoji_react, []} = Builder.emoji_react(user, object, "👌")
235 %{user: user, post_activity: post_activity, valid_emoji_react: valid_emoji_react}
238 test "it validates a valid EmojiReact", %{valid_emoji_react: valid_emoji_react} do
239 assert {:ok, _, _} = ObjectValidator.validate(valid_emoji_react, [])
242 test "it is not valid without a 'content' field", %{valid_emoji_react: valid_emoji_react} do
245 |> Map.delete("content")
247 {:error, cng} = ObjectValidator.validate(without_content, [])
250 assert {:content, {"can't be blank", [validation: :required]}} in cng.errors
253 test "it is not valid with a non-emoji content field", %{valid_emoji_react: valid_emoji_react} do
254 without_emoji_content =
256 |> Map.put("content", "x")
258 {:error, cng} = ObjectValidator.validate(without_emoji_content, [])
262 assert {:content, {"must be a single character emoji", []}} in cng.errors
269 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
270 {:ok, like} = CommonAPI.favorite(user, post_activity.id)
271 {:ok, valid_like_undo, []} = Builder.undo(user, like)
273 %{user: user, like: like, valid_like_undo: valid_like_undo}
276 test "it validates a basic like undo", %{valid_like_undo: valid_like_undo} do
277 assert {:ok, _, _} = ObjectValidator.validate(valid_like_undo, [])
280 test "it does not validate if the actor of the undo is not the actor of the object", %{
281 valid_like_undo: valid_like_undo
283 other_user = insert(:user, ap_id: "https://gensokyo.2hu/users/raymoo")
287 |> Map.put("actor", other_user.ap_id)
289 {:error, cng} = ObjectValidator.validate(bad_actor, [])
291 assert {:actor, {"not the same as object actor", []}} in cng.errors
294 test "it does not validate if the object is missing", %{valid_like_undo: valid_like_undo} do
297 |> Map.put("object", "https://gensokyo.2hu/objects/1")
299 {:error, cng} = ObjectValidator.validate(missing_object, [])
301 assert {:object, {"can't find object", []}} in cng.errors
302 assert length(cng.errors) == 1
306 describe "deletes" do
309 {:ok, post_activity} = CommonAPI.post(user, %{status: "cancel me daddy"})
311 {:ok, valid_post_delete, _} = Builder.delete(user, post_activity.data["object"])
312 {:ok, valid_user_delete, _} = Builder.delete(user, user.ap_id)
314 %{user: user, valid_post_delete: valid_post_delete, valid_user_delete: valid_user_delete}
317 test "it is valid for a post deletion", %{valid_post_delete: valid_post_delete} do
318 {:ok, valid_post_delete, _} = ObjectValidator.validate(valid_post_delete, [])
320 assert valid_post_delete["deleted_activity_id"]
323 test "it is invalid if the object isn't in a list of certain types", %{
324 valid_post_delete: valid_post_delete
326 object = Object.get_by_ap_id(valid_post_delete["object"])
330 |> Map.put("type", "Like")
334 |> Ecto.Changeset.change(%{data: data})
335 |> Object.update_and_set_cache()
337 {:error, cng} = ObjectValidator.validate(valid_post_delete, [])
338 assert {:object, {"object not in allowed types", []}} in cng.errors
341 test "it is valid for a user deletion", %{valid_user_delete: valid_user_delete} do
342 assert match?({:ok, _, _}, ObjectValidator.validate(valid_user_delete, []))
345 test "it's invalid if the id is missing", %{valid_post_delete: valid_post_delete} do
350 {:error, cng} = ObjectValidator.validate(no_id, [])
352 assert {:id, {"can't be blank", [validation: :required]}} in cng.errors
355 test "it's invalid if the object doesn't exist", %{valid_post_delete: valid_post_delete} do
358 |> Map.put("object", "http://does.not/exist")
360 {:error, cng} = ObjectValidator.validate(missing_object, [])
362 assert {:object, {"can't find object", []}} in cng.errors
365 test "it's invalid if the actor of the object and the actor of delete are from different domains",
366 %{valid_post_delete: valid_post_delete} do
367 valid_user = insert(:user)
371 |> Map.put("actor", valid_user.ap_id)
373 assert match?({:ok, _, _}, ObjectValidator.validate(valid_other_actor, []))
375 invalid_other_actor =
377 |> Map.put("actor", "https://gensokyo.2hu/users/raymoo")
379 {:error, cng} = ObjectValidator.validate(invalid_other_actor, [])
381 assert {:actor, {"is not allowed to delete object", []}} in cng.errors
384 test "it's valid if the actor of the object is a local superuser",
385 %{valid_post_delete: valid_post_delete} do
387 insert(:user, local: true, is_moderator: true, ap_id: "https://gensokyo.2hu/users/raymoo")
391 |> Map.put("actor", user.ap_id)
393 {:ok, _, meta} = ObjectValidator.validate(valid_other_actor, [])
394 assert meta[:do_not_federate]
401 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
404 "to" => [user.ap_id],
407 "id" => Utils.generate_activity_id(),
408 "object" => post_activity.data["object"],
409 "actor" => user.ap_id,
410 "context" => "a context"
413 %{valid_like: valid_like, user: user, post_activity: post_activity}
416 test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
417 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
419 assert "id" in Map.keys(object)
422 test "is valid for a valid object", %{valid_like: valid_like} do
423 assert LikeValidator.cast_and_validate(valid_like).valid?
426 test "sets the 'to' field to the object actor if no recipients are given", %{
427 valid_like: valid_like,
434 {:ok, object, _meta} = ObjectValidator.validate(without_recipients, [])
436 assert object["to"] == [user.ap_id]
439 test "sets the context field to the context of the object if no context is given", %{
440 valid_like: valid_like,
441 post_activity: post_activity
445 |> Map.delete("context")
447 {:ok, object, _meta} = ObjectValidator.validate(without_context, [])
449 assert object["context"] == post_activity.data["context"]
452 test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
453 without_actor = Map.delete(valid_like, "actor")
455 refute LikeValidator.cast_and_validate(without_actor).valid?
457 with_invalid_actor = Map.put(valid_like, "actor", "invalidactor")
459 refute LikeValidator.cast_and_validate(with_invalid_actor).valid?
462 test "it errors when the object is missing or not known", %{valid_like: valid_like} do
463 without_object = Map.delete(valid_like, "object")
465 refute LikeValidator.cast_and_validate(without_object).valid?
467 with_invalid_object = Map.put(valid_like, "object", "invalidobject")
469 refute LikeValidator.cast_and_validate(with_invalid_object).valid?
472 test "it errors when the actor has already like the object", %{
473 valid_like: valid_like,
475 post_activity: post_activity
477 _like = CommonAPI.favorite(user, post_activity.id)
479 refute LikeValidator.cast_and_validate(valid_like).valid?
482 test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
485 |> Map.put("actor", %{"id" => valid_like["actor"]})
486 |> Map.put("object", %{"id" => valid_like["object"]})
488 validated = LikeValidator.cast_and_validate(wrapped_like)
490 assert validated.valid?
492 assert {:actor, valid_like["actor"]} in validated.changes
493 assert {:object, valid_like["object"]} in validated.changes