2019-07-10 01:13:23 -04:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-02-24 14:39:27 -05:00
|
|
|
defmodule Pleroma.Web.RichMedia.HelpersTest do
|
|
|
|
use Pleroma.DataCase
|
|
|
|
|
2019-06-25 10:54:03 -04:00
|
|
|
alias Pleroma.Config
|
2019-05-17 14:49:43 -04:00
|
|
|
alias Pleroma.Object
|
2019-02-24 14:39:27 -05:00
|
|
|
alias Pleroma.Web.CommonAPI
|
2019-06-25 10:54:03 -04:00
|
|
|
alias Pleroma.Web.RichMedia.Helpers
|
2019-02-24 14:39:27 -05:00
|
|
|
|
|
|
|
import Pleroma.Factory
|
|
|
|
import Tesla.Mock
|
|
|
|
|
|
|
|
setup do
|
|
|
|
mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
|
2019-06-25 10:54:03 -04:00
|
|
|
rich_media = Config.get([:rich_media, :enabled])
|
|
|
|
on_exit(fn -> Config.put([:rich_media, :enabled], rich_media) end)
|
|
|
|
|
2019-02-24 14:39:27 -05:00
|
|
|
: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"
|
|
|
|
})
|
|
|
|
|
2019-06-25 10:54:03 -04:00
|
|
|
Config.put([:rich_media, :enabled], true)
|
2019-03-02 07:22:02 -05: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, %{
|
|
|
|
"status" => "[test](example.com[]/ogp)",
|
|
|
|
"content_type" => "text/markdown"
|
|
|
|
})
|
|
|
|
|
2019-06-25 10:54:03 -04:00
|
|
|
Config.put([:rich_media, :enabled], true)
|
2019-03-02 07:22:02 -05:00
|
|
|
|
2019-02-24 14:39:27 -05: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, %{
|
2019-06-25 15:25:37 -04:00
|
|
|
"status" => "[test](https://example.com/ogp)",
|
2019-02-24 14:39:27 -05:00
|
|
|
"content_type" => "text/markdown"
|
|
|
|
})
|
|
|
|
|
2019-06-25 10:54:03 -04:00
|
|
|
Config.put([:rich_media, :enabled], true)
|
2019-02-24 14:39:27 -05:00
|
|
|
|
2019-06-25 15:25:37 -04:00
|
|
|
assert %{page_url: "https://example.com/ogp", rich_media: _} =
|
2019-02-24 14:39:27 -05:00
|
|
|
Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
|
|
|
|
end
|
2019-05-17 14:49:43 -04:00
|
|
|
|
|
|
|
test "refuses to crawl URLs from posts marked sensitive" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
|
|
|
"status" => "http://example.com/ogp",
|
2019-05-17 16:42:51 -04:00
|
|
|
"sensitive" => true
|
|
|
|
})
|
|
|
|
|
|
|
|
%Object{} = object = Object.normalize(activity)
|
|
|
|
|
|
|
|
assert object.data["sensitive"]
|
|
|
|
|
2019-06-25 10:54:03 -04:00
|
|
|
Config.put([:rich_media, :enabled], true)
|
2019-05-17 16:42:51 -04:00
|
|
|
|
|
|
|
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"
|
2019-05-17 14:49:43 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
%Object{} = object = Object.normalize(activity)
|
|
|
|
|
|
|
|
assert object.data["sensitive"]
|
|
|
|
|
2019-06-25 10:54:03 -04:00
|
|
|
Config.put([:rich_media, :enabled], true)
|
2019-05-17 14:49:43 -04:00
|
|
|
|
|
|
|
assert %{} = Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
|
2019-06-25 10:54:03 -04:00
|
|
|
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"})
|
2019-06-25 15:25:37 -04:00
|
|
|
{:ok, activity5} = CommonAPI.post(user, %{"status" => "https://pleroma.local/notice/9kCP7V"})
|
|
|
|
|
|
|
|
Config.put([:rich_media, :enabled], true)
|
2019-05-17 14:49:43 -04:00
|
|
|
|
2019-06-25 10:54:03 -04: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 15:25:37 -04:00
|
|
|
assert %{} = Helpers.fetch_data_for_activity(activity5)
|
2019-05-17 14:49:43 -04:00
|
|
|
end
|
2019-02-24 14:39:27 -05:00
|
|
|
end
|