9399221278174fdf9256b738d186f596b67f4a2e
[akkoma] / test / pleroma / web / activity_pub / object_validators / announce_validation_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.AnnounceValidationTest do
6 use Pleroma.DataCase, async: true
7
8 alias Pleroma.Object
9 alias Pleroma.Web.ActivityPub.Builder
10 alias Pleroma.Web.ActivityPub.ObjectValidator
11 alias Pleroma.Web.CommonAPI
12
13 import Pleroma.Factory
14
15 describe "announces" do
16 setup do
17 user = insert(:user)
18 announcer = insert(:user)
19 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
20
21 object = Object.normalize(post_activity, fetch: false)
22 {:ok, valid_announce, []} = Builder.announce(announcer, object)
23
24 %{
25 valid_announce: valid_announce,
26 user: user,
27 post_activity: post_activity,
28 announcer: announcer
29 }
30 end
31
32 test "returns ok for a valid announce", %{valid_announce: valid_announce} do
33 assert {:ok, _object, _meta} = ObjectValidator.validate(valid_announce, [])
34 end
35
36 test "returns an error if the object can't be found", %{valid_announce: valid_announce} do
37 without_object =
38 valid_announce
39 |> Map.delete("object")
40
41 {:error, cng} = ObjectValidator.validate(without_object, [])
42
43 assert {:object, {"can't be blank", [validation: :required]}} in cng.errors
44
45 nonexisting_object =
46 valid_announce
47 |> Map.put("object", "https://gensokyo.2hu/objects/99999999")
48
49 {:error, cng} = ObjectValidator.validate(nonexisting_object, [])
50
51 assert {:object, {"can't find object", []}} in cng.errors
52 end
53
54 test "returns an error if we don't have the actor", %{valid_announce: valid_announce} do
55 nonexisting_actor =
56 valid_announce
57 |> Map.put("actor", "https://gensokyo.2hu/users/raymoo")
58
59 {:error, cng} = ObjectValidator.validate(nonexisting_actor, [])
60
61 assert {:actor, {"can't find user", []}} in cng.errors
62 end
63
64 test "returns an error if the actor already announced the object", %{
65 valid_announce: valid_announce,
66 announcer: announcer,
67 post_activity: post_activity
68 } do
69 _announce = CommonAPI.repeat(post_activity.id, announcer)
70
71 {:error, cng} = ObjectValidator.validate(valid_announce, [])
72
73 assert {:actor, {"already announced this object", []}} in cng.errors
74 assert {:object, {"already announced by this actor", []}} in cng.errors
75 end
76
77 test "returns an error if the actor can't announce the object", %{
78 announcer: announcer,
79 user: user
80 } do
81 {:ok, post_activity} =
82 CommonAPI.post(user, %{status: "a secret post", visibility: "private"})
83
84 object = Object.normalize(post_activity, fetch: false)
85
86 # Another user can't announce it
87 {:ok, announce, []} = Builder.announce(announcer, object, public: false)
88
89 {:error, cng} = ObjectValidator.validate(announce, [])
90
91 assert {:actor, {"can not announce this object", []}} in cng.errors
92
93 # The actor of the object can announce it
94 {:ok, announce, []} = Builder.announce(user, object, public: false)
95
96 assert {:ok, _, _} = ObjectValidator.validate(announce, [])
97
98 # The actor of the object can not announce it publicly
99 {:ok, announce, []} = Builder.announce(user, object, public: true)
100
101 {:error, cng} = ObjectValidator.validate(announce, [])
102
103 assert {:actor, {"can not announce this object publicly", []}} in cng.errors
104 end
105 end
106 end