1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.RichMedia.HelpersTest do
9 alias Pleroma.Web.CommonAPI
10 alias Pleroma.Web.RichMedia.Helpers
12 import Pleroma.Factory
16 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
21 setup do: clear_config([:rich_media, :enabled])
23 test "refuses to crawl incomplete URLs" do
27 CommonAPI.post(user, %{
28 status: "[test](example.com/ogp)",
29 content_type: "text/markdown"
32 Config.put([:rich_media, :enabled], true)
34 assert %{} == Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
37 test "refuses to crawl malformed URLs" do
41 CommonAPI.post(user, %{
42 status: "[test](example.com[]/ogp)",
43 content_type: "text/markdown"
46 Config.put([:rich_media, :enabled], true)
48 assert %{} == Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
51 test "crawls valid, complete URLs" do
55 CommonAPI.post(user, %{
56 status: "[test](https://example.com/ogp)",
57 content_type: "text/markdown"
60 Config.put([:rich_media, :enabled], true)
62 assert %{page_url: "https://example.com/ogp", rich_media: _} =
63 Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
66 test "refuses to crawl URLs of private network from posts" do
70 CommonAPI.post(user, %{status: "http://127.0.0.1:4000/notice/9kCP7VNyPJXFOXDrgO"})
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"})
77 Config.put([:rich_media, :enabled], true)
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)