ChatMessageValidator: Don't validate messages that are too long.
[akkoma] / test / web / activity_pub / object_validator_test.exs
1 defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do
2 use Pleroma.DataCase
3
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
9
10 import Pleroma.Factory
11
12 describe "chat messages" do
13 setup do
14 clear_config([:instance, :remote_limit])
15 user = insert(:user)
16 recipient = insert(:user, local: false)
17
18 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey")
19
20 %{user: user, recipient: recipient, valid_chat_message: valid_chat_message}
21 end
22
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, [])
25 end
26
27 test "does not validate if the message is longer than the remote_limit", %{
28 valid_chat_message: valid_chat_message
29 } do
30 Pleroma.Config.put([:instance, :remote_limit], 2)
31 refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
32 end
33
34 test "does not validate if the actor or the recipient is not in our system", %{
35 valid_chat_message: valid_chat_message
36 } do
37 chat_message =
38 valid_chat_message
39 |> Map.put("actor", "https://raymoo.com/raymoo")
40
41 {:error, _} = ObjectValidator.validate(chat_message, [])
42
43 chat_message =
44 valid_chat_message
45 |> Map.put("to", ["https://raymoo.com/raymoo"])
46
47 {:error, _} = ObjectValidator.validate(chat_message, [])
48 end
49
50 test "does not validate for a message with multiple recipients", %{
51 valid_chat_message: valid_chat_message,
52 user: user,
53 recipient: recipient
54 } do
55 chat_message =
56 valid_chat_message
57 |> Map.put("to", [user.ap_id, recipient.ap_id])
58
59 assert {:error, _} = ObjectValidator.validate(chat_message, [])
60 end
61
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)
65
66 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey")
67 assert {:error, _} = ObjectValidator.validate(valid_chat_message, [])
68 end
69 end
70
71 describe "likes" do
72 setup do
73 user = insert(:user)
74 {:ok, post_activity} = CommonAPI.post(user, %{"status" => "uguu"})
75
76 valid_like = %{
77 "to" => [user.ap_id],
78 "cc" => [],
79 "type" => "Like",
80 "id" => Utils.generate_activity_id(),
81 "object" => post_activity.data["object"],
82 "actor" => user.ap_id,
83 "context" => "a context"
84 }
85
86 %{valid_like: valid_like, user: user, post_activity: post_activity}
87 end
88
89 test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
90 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
91
92 assert "id" in Map.keys(object)
93 end
94
95 test "is valid for a valid object", %{valid_like: valid_like} do
96 assert LikeValidator.cast_and_validate(valid_like).valid?
97 end
98
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")
101
102 refute LikeValidator.cast_and_validate(without_actor).valid?
103
104 with_invalid_actor = Map.put(valid_like, "actor", "invalidactor")
105
106 refute LikeValidator.cast_and_validate(with_invalid_actor).valid?
107 end
108
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")
111
112 refute LikeValidator.cast_and_validate(without_object).valid?
113
114 with_invalid_object = Map.put(valid_like, "object", "invalidobject")
115
116 refute LikeValidator.cast_and_validate(with_invalid_object).valid?
117 end
118
119 test "it errors when the actor has already like the object", %{
120 valid_like: valid_like,
121 user: user,
122 post_activity: post_activity
123 } do
124 _like = CommonAPI.favorite(user, post_activity.id)
125
126 refute LikeValidator.cast_and_validate(valid_like).valid?
127 end
128
129 test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
130 wrapped_like =
131 valid_like
132 |> Map.put("actor", %{"id" => valid_like["actor"]})
133 |> Map.put("object", %{"id" => valid_like["object"]})
134
135 validated = LikeValidator.cast_and_validate(wrapped_like)
136
137 assert validated.valid?
138
139 assert {:actor, valid_like["actor"]} in validated.changes
140 assert {:object, valid_like["object"]} in validated.changes
141 end
142 end
143 end