aboutsummaryrefslogtreecommitdiff
path: root/test/web/rich_media/helpers_test.exs
blob: c8f442b05e4dd98d126e5a30a3102c76e0d5b404 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
defmodule Pleroma.Web.RichMedia.HelpersTest do
  use Pleroma.DataCase

  alias Pleroma.Config
  alias Pleroma.Object
  alias Pleroma.Web.CommonAPI
  alias Pleroma.Web.RichMedia.Helpers

  import Pleroma.Factory
  import Tesla.Mock

  setup do
    mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
    rich_media = Config.get([:rich_media, :enabled])
    on_exit(fn -> Config.put([:rich_media, :enabled], rich_media) end)

    :ok
  end

  test "refuses to crawl incomplete URLs" do
    user = insert(:user)

    {:ok, activity} =
      CommonAPI.post(user, %{
        "status" => "[test](example.com/ogp)",
        "content_type" => "text/markdown"
      })

    Config.put([:rich_media, :enabled], true)

    assert %{} == Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
  end

  test "refuses to crawl malformed URLs" do
    user = insert(:user)

    {:ok, activity} =
      CommonAPI.post(user, %{
        "status" => "[test](example.com[]/ogp)",
        "content_type" => "text/markdown"
      })

    Config.put([:rich_media, :enabled], true)

    assert %{} == Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
  end

  test "crawls valid, complete URLs" do
    user = insert(:user)

    {:ok, activity} =
      CommonAPI.post(user, %{
        "status" => "[test](https://example.com/ogp)",
        "content_type" => "text/markdown"
      })

    Config.put([:rich_media, :enabled], true)

    assert %{page_url: "https://example.com/ogp", rich_media: _} =
             Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
  end

  test "refuses to crawl URLs from posts marked sensitive" do
    user = insert(:user)

    {:ok, activity} =
      CommonAPI.post(user, %{
        "status" => "http://example.com/ogp",
        "sensitive" => true
      })

    %Object{} = object = Object.normalize(activity)

    assert object.data["sensitive"]

    Config.put([:rich_media, :enabled], true)

    assert %{} = Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
  end

  test "refuses to crawl URLs from posts tagged NSFW" do
    user = insert(:user)

    {:ok, activity} =
      CommonAPI.post(user, %{
        "status" => "http://example.com/ogp #nsfw"
      })

    %Object{} = object = Object.normalize(activity)

    assert object.data["sensitive"]

    Config.put([:rich_media, :enabled], true)

    assert %{} = Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
  end

  test "refuses to crawl URLs of private network from posts" do
    user = insert(:user)

    {:ok, activity} =
      CommonAPI.post(user, %{"status" => "http://127.0.0.1:4000/notice/9kCP7VNyPJXFOXDrgO"})

    {:ok, activity2} = CommonAPI.post(user, %{"status" => "https://10.111.10.1/notice/9kCP7V"})
    {:ok, activity3} = CommonAPI.post(user, %{"status" => "https://172.16.32.40/notice/9kCP7V"})
    {:ok, activity4} = CommonAPI.post(user, %{"status" => "https://192.168.10.40/notice/9kCP7V"})
    {:ok, activity5} = CommonAPI.post(user, %{"status" => "https://pleroma.local/notice/9kCP7V"})

    Config.put([:rich_media, :enabled], true)

    assert %{} = Helpers.fetch_data_for_activity(activity)
    assert %{} = Helpers.fetch_data_for_activity(activity2)
    assert %{} = Helpers.fetch_data_for_activity(activity3)
    assert %{} = Helpers.fetch_data_for_activity(activity4)
    assert %{} = Helpers.fetch_data_for_activity(activity5)
  end
end