Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel-dms
[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.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
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 :firefox:")
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
26 assert object == valid_chat_message
27 end
28
29 test "does not validate if the message is longer than the remote_limit", %{
30 valid_chat_message: valid_chat_message
31 } do
32 Pleroma.Config.put([:instance, :remote_limit], 2)
33 refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
34 end
35
36 test "does not validate if the recipient is blocking the actor", %{
37 valid_chat_message: valid_chat_message,
38 user: user,
39 recipient: recipient
40 } do
41 Pleroma.User.block(recipient, user)
42 refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
43 end
44
45 test "does not validate if the actor or the recipient is not in our system", %{
46 valid_chat_message: valid_chat_message
47 } do
48 chat_message =
49 valid_chat_message
50 |> Map.put("actor", "https://raymoo.com/raymoo")
51
52 {:error, _} = ObjectValidator.validate(chat_message, [])
53
54 chat_message =
55 valid_chat_message
56 |> Map.put("to", ["https://raymoo.com/raymoo"])
57
58 {:error, _} = ObjectValidator.validate(chat_message, [])
59 end
60
61 test "does not validate for a message with multiple recipients", %{
62 valid_chat_message: valid_chat_message,
63 user: user,
64 recipient: recipient
65 } do
66 chat_message =
67 valid_chat_message
68 |> Map.put("to", [user.ap_id, recipient.ap_id])
69
70 assert {:error, _} = ObjectValidator.validate(chat_message, [])
71 end
72
73 test "does not validate if it doesn't concern local users" do
74 user = insert(:user, local: false)
75 recipient = insert(:user, local: false)
76
77 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey")
78 assert {:error, _} = ObjectValidator.validate(valid_chat_message, [])
79 end
80 end
81
82 describe "likes" do
83 setup do
84 user = insert(:user)
85 {:ok, post_activity} = CommonAPI.post(user, %{"status" => "uguu"})
86
87 valid_like = %{
88 "to" => [user.ap_id],
89 "cc" => [],
90 "type" => "Like",
91 "id" => Utils.generate_activity_id(),
92 "object" => post_activity.data["object"],
93 "actor" => user.ap_id,
94 "context" => "a context"
95 }
96
97 %{valid_like: valid_like, user: user, post_activity: post_activity}
98 end
99
100 test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
101 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
102
103 assert "id" in Map.keys(object)
104 end
105
106 test "is valid for a valid object", %{valid_like: valid_like} do
107 assert LikeValidator.cast_and_validate(valid_like).valid?
108 end
109
110 test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
111 without_actor = Map.delete(valid_like, "actor")
112
113 refute LikeValidator.cast_and_validate(without_actor).valid?
114
115 with_invalid_actor = Map.put(valid_like, "actor", "invalidactor")
116
117 refute LikeValidator.cast_and_validate(with_invalid_actor).valid?
118 end
119
120 test "it errors when the object is missing or not known", %{valid_like: valid_like} do
121 without_object = Map.delete(valid_like, "object")
122
123 refute LikeValidator.cast_and_validate(without_object).valid?
124
125 with_invalid_object = Map.put(valid_like, "object", "invalidobject")
126
127 refute LikeValidator.cast_and_validate(with_invalid_object).valid?
128 end
129
130 test "it errors when the actor has already like the object", %{
131 valid_like: valid_like,
132 user: user,
133 post_activity: post_activity
134 } do
135 _like = CommonAPI.favorite(user, post_activity.id)
136
137 refute LikeValidator.cast_and_validate(valid_like).valid?
138 end
139
140 test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
141 wrapped_like =
142 valid_like
143 |> Map.put("actor", %{"id" => valid_like["actor"]})
144 |> Map.put("object", %{"id" => valid_like["object"]})
145
146 validated = LikeValidator.cast_and_validate(wrapped_like)
147
148 assert validated.valid?
149
150 assert {:actor, valid_like["actor"]} in validated.changes
151 assert {:object, valid_like["object"]} in validated.changes
152 end
153 end
154 end