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