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 actor or the recipient is not in our system", %{
37 valid_chat_message: valid_chat_message
41 |> Map.put("actor", "https://raymoo.com/raymoo")
43 {:error, _} = ObjectValidator.validate(chat_message, [])
47 |> Map.put("to", ["https://raymoo.com/raymoo"])
49 {:error, _} = ObjectValidator.validate(chat_message, [])
52 test "does not validate for a message with multiple recipients", %{
53 valid_chat_message: valid_chat_message,
59 |> Map.put("to", [user.ap_id, recipient.ap_id])
61 assert {:error, _} = ObjectValidator.validate(chat_message, [])
64 test "does not validate if it doesn't concern local users" do
65 user = insert(:user, local: false)
66 recipient = insert(:user, local: false)
68 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey")
69 assert {:error, _} = ObjectValidator.validate(valid_chat_message, [])
76 {:ok, post_activity} = CommonAPI.post(user, %{"status" => "uguu"})
82 "id" => Utils.generate_activity_id(),
83 "object" => post_activity.data["object"],
84 "actor" => user.ap_id,
85 "context" => "a context"
88 %{valid_like: valid_like, user: user, post_activity: post_activity}
91 test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
92 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
94 assert "id" in Map.keys(object)
97 test "is valid for a valid object", %{valid_like: valid_like} do
98 assert LikeValidator.cast_and_validate(valid_like).valid?
101 test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
102 without_actor = Map.delete(valid_like, "actor")
104 refute LikeValidator.cast_and_validate(without_actor).valid?
106 with_invalid_actor = Map.put(valid_like, "actor", "invalidactor")
108 refute LikeValidator.cast_and_validate(with_invalid_actor).valid?
111 test "it errors when the object is missing or not known", %{valid_like: valid_like} do
112 without_object = Map.delete(valid_like, "object")
114 refute LikeValidator.cast_and_validate(without_object).valid?
116 with_invalid_object = Map.put(valid_like, "object", "invalidobject")
118 refute LikeValidator.cast_and_validate(with_invalid_object).valid?
121 test "it errors when the actor has already like the object", %{
122 valid_like: valid_like,
124 post_activity: post_activity
126 _like = CommonAPI.favorite(user, post_activity.id)
128 refute LikeValidator.cast_and_validate(valid_like).valid?
131 test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
134 |> Map.put("actor", %{"id" => valid_like["actor"]})
135 |> Map.put("object", %{"id" => valid_like["object"]})
137 validated = LikeValidator.cast_and_validate(wrapped_like)
139 assert validated.valid?
141 assert {:actor, valid_like["actor"]} in validated.changes
142 assert {:object, valid_like["object"]} in validated.changes