1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidationTest do
6 use Pleroma.DataCase, async: true
8 alias Pleroma.Web.ActivityPub.ObjectValidator
9 alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
10 alias Pleroma.Web.ActivityPub.Utils
11 alias Pleroma.Web.CommonAPI
13 import Pleroma.Factory
18 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
24 "id" => Utils.generate_activity_id(),
25 "object" => post_activity.data["object"],
26 "actor" => user.ap_id,
27 "context" => "a context"
30 %{valid_like: valid_like, user: user, post_activity: post_activity}
33 test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
34 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
36 assert "id" in Map.keys(object)
39 test "is valid for a valid object", %{valid_like: valid_like} do
40 assert LikeValidator.cast_and_validate(valid_like).valid?
43 test "Add object actor from 'to' field if it doesn't owns the like", %{valid_like: valid_like} do
46 object_actor = valid_like["actor"]
50 |> Map.put("actor", user.ap_id)
53 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
54 assert object_actor in object["to"]
57 test "Removes object actor from 'to' field if it owns the like", %{
58 valid_like: valid_like,
63 |> Map.put("to", [user.ap_id])
65 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
66 refute user.ap_id in object["to"]
69 test "sets the context field to the context of the object if no context is given", %{
70 valid_like: valid_like,
71 post_activity: post_activity
75 |> Map.delete("context")
77 {:ok, object, _meta} = ObjectValidator.validate(without_context, [])
79 assert object["context"] == post_activity.data["context"]
82 test "it errors when the object is missing or not known", %{valid_like: valid_like} do
83 without_object = Map.delete(valid_like, "object")
85 refute LikeValidator.cast_and_validate(without_object).valid?
87 with_invalid_object = Map.put(valid_like, "object", "invalidobject")
89 refute LikeValidator.cast_and_validate(with_invalid_object).valid?
92 test "it errors when the actor has already like the object", %{
93 valid_like: valid_like,
95 post_activity: post_activity
97 _like = CommonAPI.favorite(user, post_activity.id)
99 refute LikeValidator.cast_and_validate(valid_like).valid?
102 test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
105 |> Map.put("actor", %{"id" => valid_like["actor"]})
106 |> Map.put("object", %{"id" => valid_like["object"]})
108 validated = LikeValidator.cast_and_validate(wrapped_like)
110 assert validated.valid?
112 assert {:actor, valid_like["actor"]} in validated.changes
113 assert {:object, valid_like["object"]} in validated.changes