Merge branch 'linkify' into 'develop'

Fix Linkify

See merge request pleroma/pleroma!2792
This commit is contained in:
lain 2020-07-23 10:28:52 +00:00
commit a81581472f
15 changed files with 291 additions and 52 deletions

View file

@ -10,6 +10,7 @@ defmodule Pleroma.FormatterTest do
import Pleroma.Factory
setup_all do
clear_config(Pleroma.Formatter)
Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
:ok
end
@ -255,6 +256,36 @@ defmodule Pleroma.FormatterTest do
assert {_text, ^expected_mentions, []} = Formatter.linkify(text)
end
test "it parses URL containing local mention" do
_user = insert(:user, %{nickname: "lain"})
text = "https://example.com/@lain"
expected = ~S(<a href="https://example.com/@lain" rel="ugc">https://example.com/@lain</a>)
assert {^expected, [], []} = Formatter.linkify(text)
end
test "it correctly parses angry face D:< with mention" do
lain =
insert(:user, %{
nickname: "lain@lain.com",
ap_id: "https://lain.com/users/lain",
id: "9qrWmR0cKniB0YU0TA"
})
text = "@lain@lain.com D:<"
expected_text =
~S(<span class="h-card"><a class="u-url mention" data-user="9qrWmR0cKniB0YU0TA" href="https://lain.com/users/lain" rel="ugc">@<span>lain</span></a></span> D:<)
expected_mentions = [
{"@lain@lain.com", lain}
]
assert {^expected_text, ^expected_mentions, []} = Formatter.linkify(text)
end
end
describe ".parse_tags" do

View file

@ -0,0 +1,68 @@
defmodule Pleroma.Repo.Migrations.AutolinkerToLinkifyTest do
use Pleroma.DataCase
import Pleroma.Factory
import Pleroma.Tests.Helpers
alias Pleroma.ConfigDB
setup do: clear_config(Pleroma.Formatter)
setup_all do: require_migration("20200716195806_autolinker_to_linkify")
test "change/0 converts auto_linker opts for Pleroma.Formatter", %{migration: migration} do
autolinker_opts = [
extra: true,
validate_tld: true,
class: false,
strip_prefix: false,
new_window: false,
rel: "testing"
]
insert(:config, group: :auto_linker, key: :opts, value: autolinker_opts)
migration.change()
assert nil == ConfigDB.get_by_params(%{group: :auto_linker, key: :opts})
%{value: new_opts} = ConfigDB.get_by_params(%{group: :pleroma, key: Pleroma.Formatter})
assert new_opts == [
class: false,
extra: true,
new_window: false,
rel: "testing",
strip_prefix: false
]
Pleroma.Config.put(Pleroma.Formatter, new_opts)
assert new_opts == Pleroma.Config.get(Pleroma.Formatter)
{text, _mentions, []} =
Pleroma.Formatter.linkify(
"https://www.businessinsider.com/walmart-will-close-stores-on-thanksgiving-ending-black-friday-tradition-2020-7\n\nOmg will COVID finally end Black Friday???"
)
assert text ==
"<a href=\"https://www.businessinsider.com/walmart-will-close-stores-on-thanksgiving-ending-black-friday-tradition-2020-7\" rel=\"testing\">https://www.businessinsider.com/walmart-will-close-stores-on-thanksgiving-ending-black-friday-tradition-2020-7</a>\n\nOmg will COVID finally end Black Friday???"
end
test "transform_opts/1 returns a list of compatible opts", %{migration: migration} do
old_opts = [
extra: true,
validate_tld: true,
class: false,
strip_prefix: false,
new_window: false,
rel: "qqq"
]
expected_opts = [
class: false,
extra: true,
new_window: false,
rel: "qqq",
strip_prefix: false
]
assert migration.transform_opts(old_opts) == expected_opts
end
end

View file

@ -0,0 +1,66 @@
defmodule Pleroma.Repo.Migrations.FixMalformedFormatterConfigTest do
use Pleroma.DataCase
import Pleroma.Factory
import Pleroma.Tests.Helpers
alias Pleroma.ConfigDB
setup do: clear_config(Pleroma.Formatter)
setup_all do: require_migration("20200722185515_fix_malformed_formatter_config")
test "change/0 converts a map into a list", %{migration: migration} do
incorrect_opts = %{
class: false,
extra: true,
new_window: false,
rel: "F",
strip_prefix: false
}
insert(:config, group: :pleroma, key: Pleroma.Formatter, value: incorrect_opts)
assert :ok == migration.change()
%{value: new_opts} = ConfigDB.get_by_params(%{group: :pleroma, key: Pleroma.Formatter})
assert new_opts == [
class: false,
extra: true,
new_window: false,
rel: "F",
strip_prefix: false
]
Pleroma.Config.put(Pleroma.Formatter, new_opts)
assert new_opts == Pleroma.Config.get(Pleroma.Formatter)
{text, _mentions, []} =
Pleroma.Formatter.linkify(
"https://www.businessinsider.com/walmart-will-close-stores-on-thanksgiving-ending-black-friday-tradition-2020-7\n\nOmg will COVID finally end Black Friday???"
)
assert text ==
"<a href=\"https://www.businessinsider.com/walmart-will-close-stores-on-thanksgiving-ending-black-friday-tradition-2020-7\" rel=\"F\">https://www.businessinsider.com/walmart-will-close-stores-on-thanksgiving-ending-black-friday-tradition-2020-7</a>\n\nOmg will COVID finally end Black Friday???"
end
test "change/0 skips if Pleroma.Formatter config is already a list", %{migration: migration} do
opts = [
class: false,
extra: true,
new_window: false,
rel: "ugc",
strip_prefix: false
]
insert(:config, group: :pleroma, key: Pleroma.Formatter, value: opts)
assert :skipped == migration.change()
%{value: new_opts} = ConfigDB.get_by_params(%{group: :pleroma, key: Pleroma.Formatter})
assert new_opts == opts
end
test "change/0 skips if Pleroma.Formatter is empty", %{migration: migration} do
assert :skipped == migration.change()
end
end

View file

@ -32,6 +32,11 @@ defmodule Pleroma.Tests.Helpers do
end
end
def require_migration(migration_name) do
[{module, _}] = Code.require_file("#{migration_name}.exs", "priv/repo/migrations")
{:ok, %{migration: module}}
end
defmacro __using__(_opts) do
quote do
import Pleroma.Tests.Helpers,