Improve domain cleanup on site creation (#3393)

This commit is contained in:
Adrian Gruntkowski 2023-10-04 12:32:27 +02:00 committed by GitHub
parent 296637dc18
commit d608fec903
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 12 deletions

View File

@ -221,12 +221,12 @@ defmodule Plausible.Site do
defp clean_domain(changeset) do
clean_domain =
(get_field(changeset, :domain) || "")
|> String.downcase()
|> String.trim()
|> String.replace_leading("http://", "")
|> String.replace_leading("https://", "")
|> String.trim("/")
|> String.replace_leading("www.", "")
|> String.replace_trailing("/", "")
|> String.downcase()
change(changeset, %{domain: clean_domain})
end

View File

@ -235,17 +235,19 @@ defmodule PlausibleWeb.SiteControllerTest do
assert Repo.get_by(Plausible.Site, domain: "example.com")
end
test "cleans up the url", %{conn: conn} do
conn =
post(conn, "/sites", %{
"site" => %{
"domain" => "https://www.Example.com/",
"timezone" => "Europe/London"
}
})
for url <- ["https://Example.com/", "HTTPS://EXAMPLE.COM/", "/Example.com/", "//Example.com/"] do
test "cleans up an url like #{url}", %{conn: conn} do
conn =
post(conn, "/sites", %{
"site" => %{
"domain" => unquote(url),
"timezone" => "Europe/London"
}
})
assert redirected_to(conn) == "/example.com/snippet"
assert Repo.get_by(Plausible.Site, domain: "example.com")
assert redirected_to(conn) == "/example.com/snippet"
assert Repo.get_by(Plausible.Site, domain: "example.com")
end
end
test "renders form again when domain is missing", %{conn: conn} do