41f67964a7d2cc928fc9ffb7d0a54ed25a367e13
[akkoma] / test / web / activity_pub / object_validator_test.exs
1 defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do
2 use Pleroma.DataCase
3
4 alias Pleroma.Object
5 alias Pleroma.Web.ActivityPub.Builder
6 alias Pleroma.Web.ActivityPub.ObjectValidator
7 alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
8 alias Pleroma.Web.ActivityPub.Utils
9 alias Pleroma.Web.CommonAPI
10
11 import Pleroma.Factory
12
13 describe "chat message create activities" do
14 test "it is invalid if the object already exists" do
15 user = insert(:user)
16 recipient = insert(:user)
17 {:ok, activity} = CommonAPI.post_chat_message(user, recipient, "hey")
18 object = Object.normalize(activity, false)
19
20 {:ok, create_data, _} = Builder.create(user, object.data, [recipient.ap_id])
21
22 {:error, cng} = ObjectValidator.validate(create_data, [])
23
24 assert {:object, {"The object to create already exists", []}} in cng.errors
25 end
26 end
27
28 describe "chat messages" do
29 setup do
30 clear_config([:instance, :remote_limit])
31 user = insert(:user)
32 recipient = insert(:user, local: false)
33
34 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey :firefox:")
35
36 %{user: user, recipient: recipient, valid_chat_message: valid_chat_message}
37 end
38
39 test "validates for a basic object we build", %{valid_chat_message: valid_chat_message} do
40 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
41
42 assert object == valid_chat_message
43 end
44
45 test "does not validate if the message is longer than the remote_limit", %{
46 valid_chat_message: valid_chat_message
47 } do
48 Pleroma.Config.put([:instance, :remote_limit], 2)
49 refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
50 end
51
52 test "does not validate if the recipient is blocking the actor", %{
53 valid_chat_message: valid_chat_message,
54 user: user,
55 recipient: recipient
56 } do
57 Pleroma.User.block(recipient, user)
58 refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
59 end
60
61 test "does not validate if the actor or the recipient is not in our system", %{
62 valid_chat_message: valid_chat_message
63 } do
64 chat_message =
65 valid_chat_message
66 |> Map.put("actor", "https://raymoo.com/raymoo")
67
68 {:error, _} = ObjectValidator.validate(chat_message, [])
69
70 chat_message =
71 valid_chat_message
72 |> Map.put("to", ["https://raymoo.com/raymoo"])
73
74 {:error, _} = ObjectValidator.validate(chat_message, [])
75 end
76
77 test "does not validate for a message with multiple recipients", %{
78 valid_chat_message: valid_chat_message,
79 user: user,
80 recipient: recipient
81 } do
82 chat_message =
83 valid_chat_message
84 |> Map.put("to", [user.ap_id, recipient.ap_id])
85
86 assert {:error, _} = ObjectValidator.validate(chat_message, [])
87 end
88
89 test "does not validate if it doesn't concern local users" do
90 user = insert(:user, local: false)
91 recipient = insert(:user, local: false)
92
93 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey")
94 assert {:error, _} = ObjectValidator.validate(valid_chat_message, [])
95 end
96 end
97
98 describe "likes" do
99 setup do
100 user = insert(:user)
101 {:ok, post_activity} = CommonAPI.post(user, %{"status" => "uguu"})
102
103 valid_like = %{
104 "to" => [user.ap_id],
105 "cc" => [],
106 "type" => "Like",
107 "id" => Utils.generate_activity_id(),
108 "object" => post_activity.data["object"],
109 "actor" => user.ap_id,
110 "context" => "a context"
111 }
112
113 %{valid_like: valid_like, user: user, post_activity: post_activity}
114 end
115
116 test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
117 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
118
119 assert "id" in Map.keys(object)
120 end
121
122 test "is valid for a valid object", %{valid_like: valid_like} do
123 assert LikeValidator.cast_and_validate(valid_like).valid?
124 end
125
126 test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
127 without_actor = Map.delete(valid_like, "actor")
128
129 refute LikeValidator.cast_and_validate(without_actor).valid?
130
131 with_invalid_actor = Map.put(valid_like, "actor", "invalidactor")
132
133 refute LikeValidator.cast_and_validate(with_invalid_actor).valid?
134 end
135
136 test "it errors when the object is missing or not known", %{valid_like: valid_like} do
137 without_object = Map.delete(valid_like, "object")
138
139 refute LikeValidator.cast_and_validate(without_object).valid?
140
141 with_invalid_object = Map.put(valid_like, "object", "invalidobject")
142
143 refute LikeValidator.cast_and_validate(with_invalid_object).valid?
144 end
145
146 test "it errors when the actor has already like the object", %{
147 valid_like: valid_like,
148 user: user,
149 post_activity: post_activity
150 } do
151 _like = CommonAPI.favorite(user, post_activity.id)
152
153 refute LikeValidator.cast_and_validate(valid_like).valid?
154 end
155
156 test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
157 wrapped_like =
158 valid_like
159 |> Map.put("actor", %{"id" => valid_like["actor"]})
160 |> Map.put("object", %{"id" => valid_like["object"]})
161
162 validated = LikeValidator.cast_and_validate(wrapped_like)
163
164 assert validated.valid?
165
166 assert {:actor, valid_like["actor"]} in validated.changes
167 assert {:object, valid_like["object"]} in validated.changes
168 end
169 end
170 end