UndoValidator: Add UndoValidator.
[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 "Undos" do
13 setup do
14 user = insert(:user)
15 {:ok, post_activity} = CommonAPI.post(user, %{"status" => "uguu"})
16 {:ok, like} = CommonAPI.favorite(user, post_activity.id)
17 {:ok, valid_like_undo, []} = Builder.undo(user, like)
18
19 %{user: user, like: like, valid_like_undo: valid_like_undo}
20 end
21
22 test "it validates a basic like undo", %{valid_like_undo: valid_like_undo} do
23 assert {:ok, _, _} = ObjectValidator.validate(valid_like_undo, [])
24 end
25
26 test "it does not validate if the actor of the undo is not the actor of the object", %{
27 valid_like_undo: valid_like_undo
28 } do
29 other_user = insert(:user, ap_id: "https://gensokyo.2hu/users/raymoo")
30
31 bad_actor =
32 valid_like_undo
33 |> Map.put("actor", other_user.ap_id)
34
35 {:error, cng} = ObjectValidator.validate(bad_actor, [])
36
37 assert {:actor, {"not the same as object actor", []}} in cng.errors
38 end
39
40 test "it does not validate if the object is missing", %{valid_like_undo: valid_like_undo} do
41 missing_object =
42 valid_like_undo
43 |> Map.put("object", "https://gensokyo.2hu/objects/1")
44
45 {:error, cng} = ObjectValidator.validate(missing_object, [])
46
47 assert {:object, {"can't find object", []}} in cng.errors
48 assert length(cng.errors) == 1
49 end
50 end
51
52 describe "likes" do
53 setup do
54 user = insert(:user)
55 {:ok, post_activity} = CommonAPI.post(user, %{"status" => "uguu"})
56
57 valid_like = %{
58 "to" => [user.ap_id],
59 "cc" => [],
60 "type" => "Like",
61 "id" => Utils.generate_activity_id(),
62 "object" => post_activity.data["object"],
63 "actor" => user.ap_id,
64 "context" => "a context"
65 }
66
67 %{valid_like: valid_like, user: user, post_activity: post_activity}
68 end
69
70 test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
71 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
72
73 assert "id" in Map.keys(object)
74 end
75
76 test "is valid for a valid object", %{valid_like: valid_like} do
77 assert LikeValidator.cast_and_validate(valid_like).valid?
78 end
79
80 test "sets the 'to' field to the object actor if no recipients are given", %{
81 valid_like: valid_like,
82 user: user
83 } do
84 without_recipients =
85 valid_like
86 |> Map.delete("to")
87
88 {:ok, object, _meta} = ObjectValidator.validate(without_recipients, [])
89
90 assert object["to"] == [user.ap_id]
91 end
92
93 test "sets the context field to the context of the object if no context is given", %{
94 valid_like: valid_like,
95 post_activity: post_activity
96 } do
97 without_context =
98 valid_like
99 |> Map.delete("context")
100
101 {:ok, object, _meta} = ObjectValidator.validate(without_context, [])
102
103 assert object["context"] == post_activity.data["context"]
104 end
105
106 test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
107 without_actor = Map.delete(valid_like, "actor")
108
109 refute LikeValidator.cast_and_validate(without_actor).valid?
110
111 with_invalid_actor = Map.put(valid_like, "actor", "invalidactor")
112
113 refute LikeValidator.cast_and_validate(with_invalid_actor).valid?
114 end
115
116 test "it errors when the object is missing or not known", %{valid_like: valid_like} do
117 without_object = Map.delete(valid_like, "object")
118
119 refute LikeValidator.cast_and_validate(without_object).valid?
120
121 with_invalid_object = Map.put(valid_like, "object", "invalidobject")
122
123 refute LikeValidator.cast_and_validate(with_invalid_object).valid?
124 end
125
126 test "it errors when the actor has already like the object", %{
127 valid_like: valid_like,
128 user: user,
129 post_activity: post_activity
130 } do
131 _like = CommonAPI.favorite(user, post_activity.id)
132
133 refute LikeValidator.cast_and_validate(valid_like).valid?
134 end
135
136 test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
137 wrapped_like =
138 valid_like
139 |> Map.put("actor", %{"id" => valid_like["actor"]})
140 |> Map.put("object", %{"id" => valid_like["object"]})
141
142 validated = LikeValidator.cast_and_validate(wrapped_like)
143
144 assert validated.valid?
145
146 assert {:actor, valid_like["actor"]} in validated.changes
147 assert {:object, valid_like["object"]} in validated.changes
148 end
149 end
150 end