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