1 defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do
4 alias Pleroma.Web.ActivityPub.ObjectValidator
5 alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
6 alias Pleroma.Web.ActivityPub.Utils
7 alias Pleroma.Web.CommonAPI
8 alias Pleroma.Web.ActivityPub.Builder
10 import Pleroma.Factory
12 describe "chat messages" do
15 recipient = insert(:user, local: false)
17 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey")
19 %{user: user, recipient: recipient, valid_chat_message: valid_chat_message}
22 test "validates for a basic object we build", %{valid_chat_message: valid_chat_message} do
23 assert {:ok, _object, _meta} = ObjectValidator.validate(valid_chat_message, [])
26 test "does not validate if the actor or the recipient is not in our system", %{
27 valid_chat_message: valid_chat_message
31 |> Map.put("actor", "https://raymoo.com/raymoo")
33 {:error, _} = ObjectValidator.validate(chat_message, [])
37 |> Map.put("to", ["https://raymoo.com/raymoo"])
39 {:error, _} = ObjectValidator.validate(chat_message, [])
42 test "does not validate for a message with multiple recipients", %{
43 valid_chat_message: valid_chat_message,
49 |> Map.put("to", [user.ap_id, recipient.ap_id])
51 assert {:error, _} = ObjectValidator.validate(chat_message, [])
54 test "does not validate if it doesn't concern local users" do
55 user = insert(:user, local: false)
56 recipient = insert(:user, local: false)
58 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey")
59 assert {:error, _} = ObjectValidator.validate(valid_chat_message, [])
66 {:ok, post_activity} = CommonAPI.post(user, %{"status" => "uguu"})
72 "id" => Utils.generate_activity_id(),
73 "object" => post_activity.data["object"],
74 "actor" => user.ap_id,
75 "context" => "a context"
78 %{valid_like: valid_like, user: user, post_activity: post_activity}
81 test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
82 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
84 assert "id" in Map.keys(object)
87 test "is valid for a valid object", %{valid_like: valid_like} do
88 assert LikeValidator.cast_and_validate(valid_like).valid?
91 test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
92 without_actor = Map.delete(valid_like, "actor")
94 refute LikeValidator.cast_and_validate(without_actor).valid?
96 with_invalid_actor = Map.put(valid_like, "actor", "invalidactor")
98 refute LikeValidator.cast_and_validate(with_invalid_actor).valid?
101 test "it errors when the object is missing or not known", %{valid_like: valid_like} do
102 without_object = Map.delete(valid_like, "object")
104 refute LikeValidator.cast_and_validate(without_object).valid?
106 with_invalid_object = Map.put(valid_like, "object", "invalidobject")
108 refute LikeValidator.cast_and_validate(with_invalid_object).valid?
111 test "it errors when the actor has already like the object", %{
112 valid_like: valid_like,
114 post_activity: post_activity
116 _like = CommonAPI.favorite(user, post_activity.id)
118 refute LikeValidator.cast_and_validate(valid_like).valid?
121 test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
124 |> Map.put("actor", %{"id" => valid_like["actor"]})
125 |> Map.put("object", %{"id" => valid_like["object"]})
127 validated = LikeValidator.cast_and_validate(wrapped_like)
129 assert validated.valid?
131 assert {:actor, valid_like["actor"]} in validated.changes
132 assert {:object, valid_like["object"]} in validated.changes