1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.AnnouncValidationTest do
9 alias Pleroma.Web.ActivityPub.Builder
10 alias Pleroma.Web.ActivityPub.ObjectValidator
11 alias Pleroma.Web.CommonAPI
13 import Pleroma.Factory
15 describe "announces" do
18 announcer = insert(:user)
19 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
21 object = Object.normalize(post_activity, false)
22 {:ok, valid_announce, []} = Builder.announce(announcer, object)
25 valid_announce: valid_announce,
27 post_activity: post_activity,
32 test "returns ok for a valid announce", %{valid_announce: valid_announce} do
33 assert {:ok, _object, _meta} = ObjectValidator.validate(valid_announce, [])
36 test "returns an error if the object can't be found", %{valid_announce: valid_announce} do
39 |> Map.delete("object")
41 {:error, cng} = ObjectValidator.validate(without_object, [])
43 assert {:object, {"can't be blank", [validation: :required]}} in cng.errors
47 |> Map.put("object", "https://gensokyo.2hu/objects/99999999")
49 {:error, cng} = ObjectValidator.validate(nonexisting_object, [])
51 assert {:object, {"can't find object", []}} in cng.errors
54 test "returns an error if we don't have the actor", %{valid_announce: valid_announce} do
57 |> Map.put("actor", "https://gensokyo.2hu/users/raymoo")
59 {:error, cng} = ObjectValidator.validate(nonexisting_actor, [])
61 assert {:actor, {"can't find user", []}} in cng.errors
64 test "returns an error if the actor already announced the object", %{
65 valid_announce: valid_announce,
67 post_activity: post_activity
69 _announce = CommonAPI.repeat(post_activity.id, announcer)
71 {:error, cng} = ObjectValidator.validate(valid_announce, [])
73 assert {:actor, {"already announced this object", []}} in cng.errors
74 assert {:object, {"already announced by this actor", []}} in cng.errors
77 test "returns an error if the actor can't announce the object", %{
81 {:ok, post_activity} =
82 CommonAPI.post(user, %{status: "a secret post", visibility: "private"})
84 object = Object.normalize(post_activity, false)
86 # Another user can't announce it
87 {:ok, announce, []} = Builder.announce(announcer, object, public: false)
89 {:error, cng} = ObjectValidator.validate(announce, [])
91 assert {:actor, {"can not announce this object", []}} in cng.errors
93 # The actor of the object can announce it
94 {:ok, announce, []} = Builder.announce(user, object, public: false)
96 assert {:ok, _, _} = ObjectValidator.validate(announce, [])
98 # The actor of the object can not announce it publicly
99 {:ok, announce, []} = Builder.announce(user, object, public: true)
101 {:error, cng} = ObjectValidator.validate(announce, [])
103 assert {:actor, {"can not announce this object publicly", []}} in cng.errors