Merge branch 'bugfix/repeating-announce' into 'develop'
[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
9 import Pleroma.Factory
10
11 describe "likes" do
12 setup do
13 user = insert(:user)
14 {:ok, post_activity} = CommonAPI.post(user, %{"status" => "uguu"})
15
16 valid_like = %{
17 "to" => [user.ap_id],
18 "cc" => [],
19 "type" => "Like",
20 "id" => Utils.generate_activity_id(),
21 "object" => post_activity.data["object"],
22 "actor" => user.ap_id,
23 "context" => "a context"
24 }
25
26 %{valid_like: valid_like, user: user, post_activity: post_activity}
27 end
28
29 test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
30 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
31
32 assert "id" in Map.keys(object)
33 end
34
35 test "is valid for a valid object", %{valid_like: valid_like} do
36 assert LikeValidator.cast_and_validate(valid_like).valid?
37 end
38
39 test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
40 without_actor = Map.delete(valid_like, "actor")
41
42 refute LikeValidator.cast_and_validate(without_actor).valid?
43
44 with_invalid_actor = Map.put(valid_like, "actor", "invalidactor")
45
46 refute LikeValidator.cast_and_validate(with_invalid_actor).valid?
47 end
48
49 test "it errors when the object is missing or not known", %{valid_like: valid_like} do
50 without_object = Map.delete(valid_like, "object")
51
52 refute LikeValidator.cast_and_validate(without_object).valid?
53
54 with_invalid_object = Map.put(valid_like, "object", "invalidobject")
55
56 refute LikeValidator.cast_and_validate(with_invalid_object).valid?
57 end
58
59 test "it errors when the actor has already like the object", %{
60 valid_like: valid_like,
61 user: user,
62 post_activity: post_activity
63 } do
64 _like = CommonAPI.favorite(user, post_activity.id)
65
66 refute LikeValidator.cast_and_validate(valid_like).valid?
67 end
68
69 test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
70 wrapped_like =
71 valid_like
72 |> Map.put("actor", %{"id" => valid_like["actor"]})
73 |> Map.put("object", %{"id" => valid_like["object"]})
74
75 validated = LikeValidator.cast_and_validate(wrapped_like)
76
77 assert validated.valid?
78
79 assert {:actor, valid_like["actor"]} in validated.changes
80 assert {:object, valid_like["object"]} in validated.changes
81 end
82 end
83 end