Disallow goal creation starting with `!`

not-goals
Adam Rutkowski 3 years ago
parent 5bcd7fe814
commit 48accb7774

@ -57,23 +57,29 @@ defmodule Plausible.Goal do
end
defp validate_event_name_and_page_path(changeset) do
if validate_page_path(changeset) || validate_event_name(changeset) do
changeset
else
changeset
|> add_error(:event_name, "this field is required and cannot be blank")
|> add_error(:page_path, "this field is required and must start with a /")
end
end
page_path = get_field(changeset, :page_path)
event_name = get_field(changeset, :event_name)
defp validate_page_path(changeset) do
value = get_field(changeset, :page_path)
value && String.match?(value, ~r/^\/.*/)
end
cond do
page_path && String.match?(page_path, ~r/^\/.*/) ->
changeset
defp validate_event_name(changeset) do
value = get_field(changeset, :event_name)
value && String.match?(value, ~r/^.+/)
event_name && String.match?(event_name, ~r/^[^!].+/) ->
changeset
page_path ->
changeset
|> add_error(:page_path, "this field is required and must start with a /")
event_name ->
changeset
|> add_error(:event_name, "this field is required and must not start with a !")
true ->
changeset
|> add_error(:event_name, "this field is required and cannot be blank")
|> add_error(:page_path, "this field is required and cannot be blank")
end
end
defp maybe_drop_currency(changeset) do

@ -48,6 +48,14 @@ defmodule Plausible.GoalsTest do
assert {"has already been taken", _} = changeset.errors[:event_name]
end
test "create/2 fails to create goal name starting with !" do
site = insert(:site)
{:error, changeset} = Goals.create(site, %{"event_name" => "!foo bar"})
assert {"this field is required and must not start with a !", _} =
changeset.errors[:event_name]
end
@tag :full_build_only
test "create/2 sets site.updated_at for revenue goal" do
site_1 = insert(:site, updated_at: DateTime.add(DateTime.utc_now(), -3600))

@ -71,7 +71,7 @@ defmodule PlausibleWeb.Live.GoalSettings.FormTest do
assert html =~ "this field is required and cannot be blank"
pageview_tab = lv |> element(~s/a#pageview-tab/) |> render_click()
assert pageview_tab =~ "this field is required and must start with a /"
assert pageview_tab =~ "this field is required and cannot be blank"
end
test "creates a custom event", %{conn: conn, site: site} do

Loading…
Cancel
Save