summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Gleason <alex@alexgleason.me>2020-07-22 14:18:09 -0500
committerAlex Gleason <alex@alexgleason.me>2020-07-22 14:32:45 -0500
commitc7a0016f9f4731c58a7989c7ee10e19d3f90d2eb (patch)
treec04ac473e6d76088ef9caf029b7e355cecec0dbb
parentb87a1f8eaff7e5663fd4b84b43be350754eb37d2 (diff)
Migration to fix malformed Pleroma.Formatter config
-rw-r--r--priv/repo/migrations/20200722185515_fix_malformed_formatter_config.exs26
-rw-r--r--test/migrations/20200722185515_fix_malformed_formatter_config_test.exs62
2 files changed, 88 insertions, 0 deletions
diff --git a/priv/repo/migrations/20200722185515_fix_malformed_formatter_config.exs b/priv/repo/migrations/20200722185515_fix_malformed_formatter_config.exs
new file mode 100644
index 000000000..77b760825
--- /dev/null
+++ b/priv/repo/migrations/20200722185515_fix_malformed_formatter_config.exs
@@ -0,0 +1,26 @@
+defmodule Pleroma.Repo.Migrations.FixMalformedFormatterConfig do
+ use Ecto.Migration
+ alias Pleroma.ConfigDB
+
+ @config_path %{group: :pleroma, key: Pleroma.Formatter}
+
+ def change do
+ with %ConfigDB{value: %{} = opts} <- ConfigDB.get_by_params(@config_path),
+ fixed_opts <- Map.to_list(opts) do
+ fix_config(fixed_opts)
+ else
+ _ -> :skipped
+ end
+ end
+
+ defp fix_config(fixed_opts) when is_list(fixed_opts) do
+ {:ok, _} =
+ ConfigDB.update_or_create(%{
+ group: :pleroma,
+ key: Pleroma.Formatter,
+ value: fixed_opts
+ })
+
+ :ok
+ end
+end
diff --git a/test/migrations/20200722185515_fix_malformed_formatter_config_test.exs b/test/migrations/20200722185515_fix_malformed_formatter_config_test.exs
new file mode 100644
index 000000000..9e8f997a0
--- /dev/null
+++ b/test/migrations/20200722185515_fix_malformed_formatter_config_test.exs
@@ -0,0 +1,62 @@
+defmodule Pleroma.Repo.Migrations.FixMalformedFormatterConfigTest do
+ use Pleroma.DataCase
+ import Pleroma.Factory
+ import Pleroma.Tests.Helpers, only: [require_migration: 1]
+ alias Pleroma.ConfigDB
+
+ 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: "ugc",
+ 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: "ugc",
+ strip_prefix: false
+ ]
+
+ {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=\"ugc\">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