2019-07-10 05:13:23 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-07-10 05:13:23 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-02-24 19:39:27 +00:00
|
|
|
defmodule Pleroma.Web.RichMedia.HelpersTest do
|
|
|
|
use Pleroma.DataCase
|
|
|
|
|
|
|
|
alias Pleroma.Web.CommonAPI
|
2019-06-25 14:54:03 +00:00
|
|
|
alias Pleroma.Web.RichMedia.Helpers
|
2019-02-24 19:39:27 +00:00
|
|
|
|
|
|
|
import Pleroma.Factory
|
|
|
|
import Tesla.Mock
|
|
|
|
|
|
|
|
setup do
|
2022-12-30 20:11:53 +00:00
|
|
|
mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
|
2019-06-25 14:54:03 +00:00
|
|
|
|
2019-02-24 19:39:27 +00:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2020-03-20 15:33:00 +00:00
|
|
|
setup do: clear_config([:rich_media, :enabled])
|
2019-08-19 15:34:29 +00:00
|
|
|
|
2019-02-24 19:39:27 +00:00
|
|
|
test "refuses to crawl incomplete URLs" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
2020-05-12 19:59:26 +00:00
|
|
|
status: "[test](example.com/ogp)",
|
|
|
|
content_type: "text/markdown"
|
2019-02-24 19:39:27 +00:00
|
|
|
})
|
|
|
|
|
2021-01-26 17:58:43 +00:00
|
|
|
clear_config([:rich_media, :enabled], true)
|
2019-03-02 12:22:02 +00:00
|
|
|
|
|
|
|
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, %{
|
2020-05-12 19:59:26 +00:00
|
|
|
status: "[test](example.com[]/ogp)",
|
|
|
|
content_type: "text/markdown"
|
2019-03-02 12:22:02 +00:00
|
|
|
})
|
|
|
|
|
2021-01-26 17:58:43 +00:00
|
|
|
clear_config([:rich_media, :enabled], true)
|
2019-03-02 12:22:02 +00:00
|
|
|
|
2019-02-24 19:39:27 +00:00
|
|
|
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, %{
|
2020-05-12 19:59:26 +00:00
|
|
|
status: "[test](https://example.com/ogp)",
|
|
|
|
content_type: "text/markdown"
|
2019-02-24 19:39:27 +00:00
|
|
|
})
|
|
|
|
|
2021-01-26 17:58:43 +00:00
|
|
|
clear_config([:rich_media, :enabled], true)
|
2019-02-24 19:39:27 +00:00
|
|
|
|
2019-06-25 19:25:37 +00:00
|
|
|
assert %{page_url: "https://example.com/ogp", rich_media: _} =
|
2019-02-24 19:39:27 +00:00
|
|
|
Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
|
|
|
|
end
|
2019-05-17 18:49:43 +00:00
|
|
|
|
2019-06-25 14:54:03 +00:00
|
|
|
test "refuses to crawl URLs of private network from posts" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
{:ok, activity} =
|
2020-05-12 19:59:26 +00:00
|
|
|
CommonAPI.post(user, %{status: "http://127.0.0.1:4000/notice/9kCP7VNyPJXFOXDrgO"})
|
2019-06-25 14:54:03 +00:00
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{: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"})
|
2019-06-25 19:25:37 +00:00
|
|
|
|
2021-01-26 17:58:43 +00:00
|
|
|
clear_config([:rich_media, :enabled], true)
|
2019-05-17 18:49:43 +00:00
|
|
|
|
2019-06-25 14:54:03 +00:00
|
|
|
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)
|
2019-06-25 19:25:37 +00:00
|
|
|
assert %{} = Helpers.fetch_data_for_activity(activity5)
|
2019-05-17 18:49:43 +00:00
|
|
|
end
|
2019-02-24 19:39:27 +00:00
|
|
|
end
|