1 defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do
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
10 import Pleroma.Factory
12 describe "chat messages" do
14 clear_config([:instance, :remote_limit])
16 recipient = insert(:user, local: false)
18 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey :firefox:")
20 %{user: user, recipient: recipient, valid_chat_message: valid_chat_message}
23 test "validates for a basic object we build", %{valid_chat_message: valid_chat_message} do
24 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
26 assert object == valid_chat_message
29 test "does not validate if the message is longer than the remote_limit", %{
30 valid_chat_message: valid_chat_message
32 Pleroma.Config.put([:instance, :remote_limit], 2)
33 refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
36 test "does not validate if the recipient is blocking the actor", %{
37 valid_chat_message: valid_chat_message,
41 Pleroma.User.block(recipient, user)
42 refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
45 test "does not validate if the actor or the recipient is not in our system", %{
46 valid_chat_message: valid_chat_message
50 |> Map.put("actor", "https://raymoo.com/raymoo")
52 {:error, _} = ObjectValidator.validate(chat_message, [])
56 |> Map.put("to", ["https://raymoo.com/raymoo"])
58 {:error, _} = ObjectValidator.validate(chat_message, [])
61 test "does not validate for a message with multiple recipients", %{
62 valid_chat_message: valid_chat_message,
68 |> Map.put("to", [user.ap_id, recipient.ap_id])
70 assert {:error, _} = ObjectValidator.validate(chat_message, [])
73 test "does not validate if it doesn't concern local users" do
74 user = insert(:user, local: false)
75 recipient = insert(:user, local: false)
77 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey")
78 assert {:error, _} = ObjectValidator.validate(valid_chat_message, [])
85 {:ok, post_activity} = CommonAPI.post(user, %{"status" => "uguu"})
91 "id" => Utils.generate_activity_id(),
92 "object" => post_activity.data["object"],
93 "actor" => user.ap_id,
94 "context" => "a context"
97 %{valid_like: valid_like, user: user, post_activity: post_activity}
100 test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
101 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
103 assert "id" in Map.keys(object)
106 test "is valid for a valid object", %{valid_like: valid_like} do
107 assert LikeValidator.cast_and_validate(valid_like).valid?
110 test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
111 without_actor = Map.delete(valid_like, "actor")
113 refute LikeValidator.cast_and_validate(without_actor).valid?
115 with_invalid_actor = Map.put(valid_like, "actor", "invalidactor")
117 refute LikeValidator.cast_and_validate(with_invalid_actor).valid?
120 test "it errors when the object is missing or not known", %{valid_like: valid_like} do
121 without_object = Map.delete(valid_like, "object")
123 refute LikeValidator.cast_and_validate(without_object).valid?
125 with_invalid_object = Map.put(valid_like, "object", "invalidobject")
127 refute LikeValidator.cast_and_validate(with_invalid_object).valid?
130 test "it errors when the actor has already like the object", %{
131 valid_like: valid_like,
133 post_activity: post_activity
135 _like = CommonAPI.favorite(user, post_activity.id)
137 refute LikeValidator.cast_and_validate(valid_like).valid?
140 test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
143 |> Map.put("actor", %{"id" => valid_like["actor"]})
144 |> Map.put("object", %{"id" => valid_like["object"]})
146 validated = LikeValidator.cast_and_validate(wrapped_like)
148 assert validated.valid?
150 assert {:actor, valid_like["actor"]} in validated.changes
151 assert {:object, valid_like["object"]} in validated.changes