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 "it turns mastodon attachments into our attachments" do
19 "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
22 "mediaType" => "image/jpeg"
26 AttachmentValidator.cast_and_validate(attachment)
27 |> Ecto.Changeset.apply_action(:insert)
32 "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
34 mediaType: "image/jpeg"
40 describe "chat message create activities" do
41 test "it is invalid if the object already exists" do
43 recipient = insert(:user)
44 {:ok, activity} = CommonAPI.post_chat_message(user, recipient, "hey")
45 object = Object.normalize(activity, false)
47 {:ok, create_data, _} = Builder.create(user, object.data, [recipient.ap_id])
49 {:error, cng} = ObjectValidator.validate(create_data, [])
51 assert {:object, {"The object to create already exists", []}} in cng.errors
54 test "it is invalid if the object data has a different `to` or `actor` field" do
56 recipient = insert(:user)
57 {:ok, object_data, _} = Builder.chat_message(recipient, user.ap_id, "Hey")
59 {:ok, create_data, _} = Builder.create(user, object_data, [recipient.ap_id])
61 {:error, cng} = ObjectValidator.validate(create_data, [])
63 assert {:to, {"Recipients don't match with object recipients", []}} in cng.errors
64 assert {:actor, {"Actor doesn't match with object actor", []}} in cng.errors
68 describe "chat messages" do
70 clear_config([:instance, :remote_limit])
72 recipient = insert(:user, local: false)
74 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey :firefox:")
76 %{user: user, recipient: recipient, valid_chat_message: valid_chat_message}
79 test "validates for a basic object we build", %{valid_chat_message: valid_chat_message} do
80 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
82 assert Map.put(valid_chat_message, "attachment", nil) == object
85 test "validates for a basic object with an attachment", %{
86 valid_chat_message: valid_chat_message,
90 content_type: "image/jpg",
91 path: Path.absname("test/fixtures/image.jpg"),
92 filename: "an_image.jpg"
95 {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
99 |> Map.put("attachment", attachment.data)
101 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
103 assert object["attachment"]
106 test "validates for a basic object with an attachment but without content", %{
107 valid_chat_message: valid_chat_message,
111 content_type: "image/jpg",
112 path: Path.absname("test/fixtures/image.jpg"),
113 filename: "an_image.jpg"
116 {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
120 |> Map.put("attachment", attachment.data)
121 |> Map.delete("content")
123 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
125 assert object["attachment"]
128 test "does not validate if the message has no content", %{
129 valid_chat_message: valid_chat_message
133 |> Map.delete("content")
135 refute match?({:ok, _object, _meta}, ObjectValidator.validate(contentless, []))
138 test "does not validate if the message is longer than the remote_limit", %{
139 valid_chat_message: valid_chat_message
141 Pleroma.Config.put([:instance, :remote_limit], 2)
142 refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
145 test "does not validate if the recipient is blocking the actor", %{
146 valid_chat_message: valid_chat_message,
150 Pleroma.User.block(recipient, user)
151 refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
154 test "does not validate if the actor or the recipient is not in our system", %{
155 valid_chat_message: valid_chat_message
159 |> Map.put("actor", "https://raymoo.com/raymoo")
161 {:error, _} = ObjectValidator.validate(chat_message, [])
165 |> Map.put("to", ["https://raymoo.com/raymoo"])
167 {:error, _} = ObjectValidator.validate(chat_message, [])
170 test "does not validate for a message with multiple recipients", %{
171 valid_chat_message: valid_chat_message,
177 |> Map.put("to", [user.ap_id, recipient.ap_id])
179 assert {:error, _} = ObjectValidator.validate(chat_message, [])
182 test "does not validate if it doesn't concern local users" do
183 user = insert(:user, local: false)
184 recipient = insert(:user, local: false)
186 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey")
187 assert {:error, _} = ObjectValidator.validate(valid_chat_message, [])
191 describe "EmojiReacts" do
194 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
196 object = Pleroma.Object.get_by_ap_id(post_activity.data["object"])
198 {:ok, valid_emoji_react, []} = Builder.emoji_react(user, object, "👌")
200 %{user: user, post_activity: post_activity, valid_emoji_react: valid_emoji_react}
203 test "it validates a valid EmojiReact", %{valid_emoji_react: valid_emoji_react} do
204 assert {:ok, _, _} = ObjectValidator.validate(valid_emoji_react, [])
207 test "it is not valid without a 'content' field", %{valid_emoji_react: valid_emoji_react} do
210 |> Map.delete("content")
212 {:error, cng} = ObjectValidator.validate(without_content, [])
215 assert {:content, {"can't be blank", [validation: :required]}} in cng.errors
218 test "it is not valid with a non-emoji content field", %{valid_emoji_react: valid_emoji_react} do
219 without_emoji_content =
221 |> Map.put("content", "x")
223 {:error, cng} = ObjectValidator.validate(without_emoji_content, [])
227 assert {:content, {"must be a single character emoji", []}} in cng.errors
234 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
235 {:ok, like} = CommonAPI.favorite(user, post_activity.id)
236 {:ok, valid_like_undo, []} = Builder.undo(user, like)
238 %{user: user, like: like, valid_like_undo: valid_like_undo}
241 test "it validates a basic like undo", %{valid_like_undo: valid_like_undo} do
242 assert {:ok, _, _} = ObjectValidator.validate(valid_like_undo, [])
245 test "it does not validate if the actor of the undo is not the actor of the object", %{
246 valid_like_undo: valid_like_undo
248 other_user = insert(:user, ap_id: "https://gensokyo.2hu/users/raymoo")
252 |> Map.put("actor", other_user.ap_id)
254 {:error, cng} = ObjectValidator.validate(bad_actor, [])
256 assert {:actor, {"not the same as object actor", []}} in cng.errors
259 test "it does not validate if the object is missing", %{valid_like_undo: valid_like_undo} do
262 |> Map.put("object", "https://gensokyo.2hu/objects/1")
264 {:error, cng} = ObjectValidator.validate(missing_object, [])
266 assert {:object, {"can't find object", []}} in cng.errors
267 assert length(cng.errors) == 1
271 describe "deletes" do
274 {:ok, post_activity} = CommonAPI.post(user, %{status: "cancel me daddy"})
276 {:ok, valid_post_delete, _} = Builder.delete(user, post_activity.data["object"])
277 {:ok, valid_user_delete, _} = Builder.delete(user, user.ap_id)
279 %{user: user, valid_post_delete: valid_post_delete, valid_user_delete: valid_user_delete}
282 test "it is valid for a post deletion", %{valid_post_delete: valid_post_delete} do
283 {:ok, valid_post_delete, _} = ObjectValidator.validate(valid_post_delete, [])
285 assert valid_post_delete["deleted_activity_id"]
288 test "it is invalid if the object isn't in a list of certain types", %{
289 valid_post_delete: valid_post_delete
291 object = Object.get_by_ap_id(valid_post_delete["object"])
295 |> Map.put("type", "Like")
299 |> Ecto.Changeset.change(%{data: data})
300 |> Object.update_and_set_cache()
302 {:error, cng} = ObjectValidator.validate(valid_post_delete, [])
303 assert {:object, {"object not in allowed types", []}} in cng.errors
306 test "it is valid for a user deletion", %{valid_user_delete: valid_user_delete} do
307 assert match?({:ok, _, _}, ObjectValidator.validate(valid_user_delete, []))
310 test "it's invalid if the id is missing", %{valid_post_delete: valid_post_delete} do
315 {:error, cng} = ObjectValidator.validate(no_id, [])
317 assert {:id, {"can't be blank", [validation: :required]}} in cng.errors
320 test "it's invalid if the object doesn't exist", %{valid_post_delete: valid_post_delete} do
323 |> Map.put("object", "http://does.not/exist")
325 {:error, cng} = ObjectValidator.validate(missing_object, [])
327 assert {:object, {"can't find object", []}} in cng.errors
330 test "it's invalid if the actor of the object and the actor of delete are from different domains",
331 %{valid_post_delete: valid_post_delete} do
332 valid_user = insert(:user)
336 |> Map.put("actor", valid_user.ap_id)
338 assert match?({:ok, _, _}, ObjectValidator.validate(valid_other_actor, []))
340 invalid_other_actor =
342 |> Map.put("actor", "https://gensokyo.2hu/users/raymoo")
344 {:error, cng} = ObjectValidator.validate(invalid_other_actor, [])
346 assert {:actor, {"is not allowed to delete object", []}} in cng.errors
349 test "it's valid if the actor of the object is a local superuser",
350 %{valid_post_delete: valid_post_delete} do
352 insert(:user, local: true, is_moderator: true, ap_id: "https://gensokyo.2hu/users/raymoo")
356 |> Map.put("actor", user.ap_id)
358 {:ok, _, meta} = ObjectValidator.validate(valid_other_actor, [])
359 assert meta[:do_not_federate]
366 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
369 "to" => [user.ap_id],
372 "id" => Utils.generate_activity_id(),
373 "object" => post_activity.data["object"],
374 "actor" => user.ap_id,
375 "context" => "a context"
378 %{valid_like: valid_like, user: user, post_activity: post_activity}
381 test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
382 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
384 assert "id" in Map.keys(object)
387 test "is valid for a valid object", %{valid_like: valid_like} do
388 assert LikeValidator.cast_and_validate(valid_like).valid?
391 test "sets the 'to' field to the object actor if no recipients are given", %{
392 valid_like: valid_like,
399 {:ok, object, _meta} = ObjectValidator.validate(without_recipients, [])
401 assert object["to"] == [user.ap_id]
404 test "sets the context field to the context of the object if no context is given", %{
405 valid_like: valid_like,
406 post_activity: post_activity
410 |> Map.delete("context")
412 {:ok, object, _meta} = ObjectValidator.validate(without_context, [])
414 assert object["context"] == post_activity.data["context"]
417 test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
418 without_actor = Map.delete(valid_like, "actor")
420 refute LikeValidator.cast_and_validate(without_actor).valid?
422 with_invalid_actor = Map.put(valid_like, "actor", "invalidactor")
424 refute LikeValidator.cast_and_validate(with_invalid_actor).valid?
427 test "it errors when the object is missing or not known", %{valid_like: valid_like} do
428 without_object = Map.delete(valid_like, "object")
430 refute LikeValidator.cast_and_validate(without_object).valid?
432 with_invalid_object = Map.put(valid_like, "object", "invalidobject")
434 refute LikeValidator.cast_and_validate(with_invalid_object).valid?
437 test "it errors when the actor has already like the object", %{
438 valid_like: valid_like,
440 post_activity: post_activity
442 _like = CommonAPI.favorite(user, post_activity.id)
444 refute LikeValidator.cast_and_validate(valid_like).valid?
447 test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
450 |> Map.put("actor", %{"id" => valid_like["actor"]})
451 |> Map.put("object", %{"id" => valid_like["object"]})
453 validated = LikeValidator.cast_and_validate(wrapped_like)
455 assert validated.valid?
457 assert {:actor, valid_like["actor"]} in validated.changes
458 assert {:object, valid_like["object"]} in validated.changes