summaryrefslogtreecommitdiff
path: root/test/pleroma/web/activity_pub/mrf/inline_quote_policy_test.exs
blob: 44ee91d4b1a8bf0bf85daa443761320f6484276d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.ActivityPub.MRF.InlineQuotePolicyTest do
  alias Pleroma.Web.ActivityPub.MRF.InlineQuotePolicy
  use Pleroma.DataCase

  test "adds quote URL to post content" do
    quote_url = "https://gleasonator.com/objects/1234"

    activity = %{
      "type" => "Create",
      "actor" => "https://gleasonator.com/users/alex",
      "object" => %{
        "type" => "Note",
        "content" => "Nice post",
        "quoteUrl" => quote_url
      }
    }

    {:ok, %{"object" => %{"content" => filtered}}} = InlineQuotePolicy.filter(activity)

    assert filtered ==
             "Nice post<span class=\"quote-inline\"><br/><br/>RT: <a href=\"https://gleasonator.com/objects/1234\">https://gleasonator.com/objects/1234</a></span>"
  end

  test "doesn't add line breaks to markdown posts" do
    quote_url = "https://gleasonator.com/objects/1234"

    activity = %{
      "type" => "Create",
      "actor" => "https://gleasonator.com/users/alex",
      "object" => %{
        "type" => "Note",
        "content" => "<p>Nice post</p>",
        "quoteUrl" => quote_url
      }
    }

    {:ok, %{"object" => %{"content" => filtered}}} = InlineQuotePolicy.filter(activity)

    assert filtered ==
             "<p>Nice post<span class=\"quote-inline\"><br/><br/>RT: <a href=\"https://gleasonator.com/objects/1234\">https://gleasonator.com/objects/1234</a></span></p>"
  end

  test "ignores Misskey quote posts" do
    object = File.read!("test/fixtures/quote_post/misskey_quote_post.json") |> Jason.decode!()

    activity = %{
      "type" => "Create",
      "actor" => "https://misskey.io/users/7rkrarq81i",
      "object" => object
    }

    {:ok, filtered} = InlineQuotePolicy.filter(activity)
    assert filtered == activity
  end

  test "ignores Fedibird quote posts" do
    object = File.read!("test/fixtures/quote_post/fedibird_quote_post.json") |> Jason.decode!()

    # Normally the ObjectValidator will fix this before it reaches MRF
    object = Map.put(object, "quoteUrl", object["quoteURL"])

    activity = %{
      "type" => "Create",
      "actor" => "https://fedibird.com/users/noellabo",
      "object" => object
    }

    {:ok, filtered} = InlineQuotePolicy.filter(activity)
    assert filtered == activity
  end

  test "skips objects which already have an .inline-quote span" do
    object =
      File.read!("test/fixtures/quote_post/fedibird_quote_mismatched.json") |> Jason.decode!()

    # Normally the ObjectValidator will fix this before it reaches MRF
    object = Map.put(object, "quoteUrl", object["quoteUri"])

    activity = %{
      "type" => "Create",
      "actor" => "https://fedibird.com/users/noellabo",
      "object" => object
    }

    {:ok, filtered} = InlineQuotePolicy.filter(activity)
    assert filtered == activity
  end
end