1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.RichMedia.HelpersTest do
8 alias Pleroma.Web.CommonAPI
9 alias Pleroma.Web.RichMedia.Helpers
11 import Pleroma.Factory
15 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
20 setup do: clear_config([:rich_media, :enabled])
22 test "refuses to crawl incomplete URLs" do
26 CommonAPI.post(user, %{
27 status: "[test](example.com/ogp)",
28 content_type: "text/markdown"
31 clear_config([:rich_media, :enabled], true)
33 assert %{} == Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
36 test "refuses to crawl malformed URLs" do
40 CommonAPI.post(user, %{
41 status: "[test](example.com[]/ogp)",
42 content_type: "text/markdown"
45 clear_config([:rich_media, :enabled], true)
47 assert %{} == Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
50 test "crawls valid, complete URLs" do
54 CommonAPI.post(user, %{
55 status: "[test](https://example.com/ogp)",
56 content_type: "text/markdown"
59 clear_config([:rich_media, :enabled], true)
61 assert %{page_url: "https://example.com/ogp", rich_media: _} =
62 Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
65 test "refuses to crawl URLs of private network from posts" do
69 CommonAPI.post(user, %{status: "http://127.0.0.1:4000/notice/9kCP7VNyPJXFOXDrgO"})
71 {:ok, activity2} = CommonAPI.post(user, %{status: "https://10.111.10.1/notice/9kCP7V"})
72 {:ok, activity3} = CommonAPI.post(user, %{status: "https://172.16.32.40/notice/9kCP7V"})
73 {:ok, activity4} = CommonAPI.post(user, %{status: "https://192.168.10.40/notice/9kCP7V"})
74 {:ok, activity5} = CommonAPI.post(user, %{status: "https://pleroma.local/notice/9kCP7V"})
76 clear_config([:rich_media, :enabled], true)
78 assert %{} = Helpers.fetch_data_for_activity(activity)
79 assert %{} = Helpers.fetch_data_for_activity(activity2)
80 assert %{} = Helpers.fetch_data_for_activity(activity3)
81 assert %{} = Helpers.fetch_data_for_activity(activity4)
82 assert %{} = Helpers.fetch_data_for_activity(activity5)