Merge branch 'ejabberd-pleroma-auth' into 'develop'
[akkoma] / test / pleroma / web / rich_media / helpers_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.RichMedia.HelpersTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Config
9 alias Pleroma.Web.CommonAPI
10 alias Pleroma.Web.RichMedia.Helpers
11
12 import Pleroma.Factory
13 import Tesla.Mock
14
15 setup do
16 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
17
18 :ok
19 end
20
21 setup do: clear_config([:rich_media, :enabled])
22
23 test "refuses to crawl incomplete URLs" do
24 user = insert(:user)
25
26 {:ok, activity} =
27 CommonAPI.post(user, %{
28 status: "[test](example.com/ogp)",
29 content_type: "text/markdown"
30 })
31
32 Config.put([:rich_media, :enabled], true)
33
34 assert %{} == Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
35 end
36
37 test "refuses to crawl malformed URLs" do
38 user = insert(:user)
39
40 {:ok, activity} =
41 CommonAPI.post(user, %{
42 status: "[test](example.com[]/ogp)",
43 content_type: "text/markdown"
44 })
45
46 Config.put([:rich_media, :enabled], true)
47
48 assert %{} == Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
49 end
50
51 test "crawls valid, complete URLs" do
52 user = insert(:user)
53
54 {:ok, activity} =
55 CommonAPI.post(user, %{
56 status: "[test](https://example.com/ogp)",
57 content_type: "text/markdown"
58 })
59
60 Config.put([:rich_media, :enabled], true)
61
62 assert %{page_url: "https://example.com/ogp", rich_media: _} =
63 Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
64 end
65
66 test "refuses to crawl URLs of private network from posts" do
67 user = insert(:user)
68
69 {:ok, activity} =
70 CommonAPI.post(user, %{status: "http://127.0.0.1:4000/notice/9kCP7VNyPJXFOXDrgO"})
71
72 {:ok, activity2} = CommonAPI.post(user, %{status: "https://10.111.10.1/notice/9kCP7V"})
73 {:ok, activity3} = CommonAPI.post(user, %{status: "https://172.16.32.40/notice/9kCP7V"})
74 {:ok, activity4} = CommonAPI.post(user, %{status: "https://192.168.10.40/notice/9kCP7V"})
75 {:ok, activity5} = CommonAPI.post(user, %{status: "https://pleroma.local/notice/9kCP7V"})
76
77 Config.put([:rich_media, :enabled], true)
78
79 assert %{} = Helpers.fetch_data_for_activity(activity)
80 assert %{} = Helpers.fetch_data_for_activity(activity2)
81 assert %{} = Helpers.fetch_data_for_activity(activity3)
82 assert %{} = Helpers.fetch_data_for_activity(activity4)
83 assert %{} = Helpers.fetch_data_for_activity(activity5)
84 end
85 end