1 defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do
4 alias Pleroma.Web.ActivityPub.Builder
5 alias Pleroma.Web.ActivityPub.ObjectValidator
6 alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
7 alias Pleroma.Web.ActivityPub.Utils
8 alias Pleroma.Web.CommonAPI
10 import Pleroma.Factory
12 describe "chat messages" do
14 clear_config([:instance, :remote_limit])
16 recipient = insert(:user, local: false)
18 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey")
20 %{user: user, recipient: recipient, valid_chat_message: valid_chat_message}
23 test "validates for a basic object we build", %{valid_chat_message: valid_chat_message} do
24 assert {:ok, _object, _meta} = ObjectValidator.validate(valid_chat_message, [])
27 test "does not validate if the message is longer than the remote_limit", %{
28 valid_chat_message: valid_chat_message
30 Pleroma.Config.put([:instance, :remote_limit], 2)
31 refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
34 test "does not validate if the actor or the recipient is not in our system", %{
35 valid_chat_message: valid_chat_message
39 |> Map.put("actor", "https://raymoo.com/raymoo")
41 {:error, _} = ObjectValidator.validate(chat_message, [])
45 |> Map.put("to", ["https://raymoo.com/raymoo"])
47 {:error, _} = ObjectValidator.validate(chat_message, [])
50 test "does not validate for a message with multiple recipients", %{
51 valid_chat_message: valid_chat_message,
57 |> Map.put("to", [user.ap_id, recipient.ap_id])
59 assert {:error, _} = ObjectValidator.validate(chat_message, [])
62 test "does not validate if it doesn't concern local users" do
63 user = insert(:user, local: false)
64 recipient = insert(:user, local: false)
66 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey")
67 assert {:error, _} = ObjectValidator.validate(valid_chat_message, [])
74 {:ok, post_activity} = CommonAPI.post(user, %{"status" => "uguu"})
80 "id" => Utils.generate_activity_id(),
81 "object" => post_activity.data["object"],
82 "actor" => user.ap_id,
83 "context" => "a context"
86 %{valid_like: valid_like, user: user, post_activity: post_activity}
89 test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
90 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
92 assert "id" in Map.keys(object)
95 test "is valid for a valid object", %{valid_like: valid_like} do
96 assert LikeValidator.cast_and_validate(valid_like).valid?
99 test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
100 without_actor = Map.delete(valid_like, "actor")
102 refute LikeValidator.cast_and_validate(without_actor).valid?
104 with_invalid_actor = Map.put(valid_like, "actor", "invalidactor")
106 refute LikeValidator.cast_and_validate(with_invalid_actor).valid?
109 test "it errors when the object is missing or not known", %{valid_like: valid_like} do
110 without_object = Map.delete(valid_like, "object")
112 refute LikeValidator.cast_and_validate(without_object).valid?
114 with_invalid_object = Map.put(valid_like, "object", "invalidobject")
116 refute LikeValidator.cast_and_validate(with_invalid_object).valid?
119 test "it errors when the actor has already like the object", %{
120 valid_like: valid_like,
122 post_activity: post_activity
124 _like = CommonAPI.favorite(user, post_activity.id)
126 refute LikeValidator.cast_and_validate(valid_like).valid?
129 test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
132 |> Map.put("actor", %{"id" => valid_like["actor"]})
133 |> Map.put("object", %{"id" => valid_like["object"]})
135 validated = LikeValidator.cast_and_validate(wrapped_like)
137 assert validated.valid?
139 assert {:actor, valid_like["actor"]} in validated.changes
140 assert {:object, valid_like["object"]} in validated.changes