summaryrefslogtreecommitdiff
path: root/test/pleroma/web/rich_media/card_test.exs
blob: 387defc8ce793d294847c43a47167373ffe36a35 (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
# Pleroma: A lightweight social networking server
# Copyright © 2017-2024 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.RichMedia.CardTest do
  use Oban.Testing, repo: Pleroma.Repo
  use Pleroma.DataCase, async: true

  alias Pleroma.Tests.ObanHelpers
  alias Pleroma.UnstubbedConfigMock, as: ConfigMock
  alias Pleroma.Web.CommonAPI
  alias Pleroma.Web.RichMedia.Card
  alias Pleroma.Workers.RichMediaWorker

  import Mox
  import Pleroma.Factory
  import Tesla.Mock

  setup do
    mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)

    ConfigMock
    |> stub_with(Pleroma.Test.StaticConfig)

    :ok
  end

  setup do: clear_config([:rich_media, :enabled], true)

  test "crawls URL in activity" do
    user = insert(:user)

    url = "https://example.com/ogp"
    url_hash = Card.url_to_hash(url)

    {:ok, activity} =
      CommonAPI.post(user, %{
        status: "[test](#{url})",
        content_type: "text/markdown"
      })

    Pleroma.Web.ActivityPub.ActivityPubMock
    |> expect(:stream_out, fn ^activity -> nil end)

    assert_enqueued(
      worker: RichMediaWorker,
      args: %{"url" => url, "activity_id" => activity.id}
    )

    ObanHelpers.perform_all()

    assert %Card{url_hash: ^url_hash, fields: _} = Card.get_by_activity(activity)
  end

  test "recrawls URLs on status edits/updates" do
    original_url = "https://google.com/"
    original_url_hash = Card.url_to_hash(original_url)
    updated_url = "https://yahoo.com/"
    updated_url_hash = Card.url_to_hash(updated_url)

    user = insert(:user)
    {:ok, activity} = CommonAPI.post(user, %{status: "I like this site #{original_url}"})

    # Force a backfill
    Card.get_by_activity(activity)
    ObanHelpers.perform_all()

    assert match?(
             %Card{url_hash: ^original_url_hash, fields: _},
             Card.get_by_activity(activity)
           )

    {:ok, _} = CommonAPI.update(activity, user, %{status: "I like this site #{updated_url}"})

    activity = Pleroma.Activity.get_by_id(activity.id)

    # Force a backfill
    Card.get_by_activity(activity)
    ObanHelpers.perform_all()

    assert match?(
             %Card{url_hash: ^updated_url_hash, fields: _},
             Card.get_by_activity(activity)
           )
  end
end