From afb3b811a2970debeed2c246d1dea462a035247c Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Wed, 28 Sep 2022 11:34:36 +0300 Subject: [PATCH 1/2] Ensure form errors can be rendered on /settings --- .../controllers/auth_controller.ex | 36 +++++++++---------- .../controllers/auth_controller_test.exs | 6 ++++ 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/lib/plausible_web/controllers/auth_controller.ex b/lib/plausible_web/controllers/auth_controller.ex index c6e55b8af673..a4cffe1a50a3 100644 --- a/lib/plausible_web/controllers/auth_controller.ex +++ b/lib/plausible_web/controllers/auth_controller.ex @@ -437,20 +437,8 @@ defmodule PlausibleWeb.AuthController do end def user_settings(conn, _params) do - user = conn.assigns[:current_user] - changeset = Auth.User.changeset(user) - - {usage_pageviews, usage_custom_events} = Plausible.Billing.usage_breakdown(user) - - render(conn, "user_settings.html", - user: user |> Repo.preload(:api_keys), - changeset: changeset, - subscription: user.subscription, - invoices: Plausible.Billing.paddle_api().get_invoices(user.subscription), - theme: user.theme || "system", - usage_pageviews: usage_pageviews, - usage_custom_events: usage_custom_events - ) + changeset = Auth.User.changeset(conn.assigns[:current_user]) + render_settings(conn, changeset) end def save_settings(conn, %{"user" => user_params}) do @@ -463,13 +451,25 @@ defmodule PlausibleWeb.AuthController do |> redirect(to: Routes.auth_path(conn, :user_settings)) {:error, changeset} -> - render(conn, "user_settings.html", - changeset: changeset, - subscription: conn.assigns[:current_user].subscription - ) + render_settings(conn, changeset) end end + defp render_settings(conn, changeset) do + user = conn.assigns[:current_user] + {usage_pageviews, usage_custom_events} = Plausible.Billing.usage_breakdown(user) + + render(conn, "user_settings.html", + user: user |> Repo.preload(:api_keys), + changeset: changeset, + subscription: user.subscription, + invoices: Plausible.Billing.paddle_api().get_invoices(user.subscription), + theme: user.theme || "system", + usage_pageviews: usage_pageviews, + usage_custom_events: usage_custom_events + ) + end + def new_api_key(conn, _params) do key = :crypto.strong_rand_bytes(64) |> Base.url_encode64() |> binary_part(0, 64) changeset = Auth.ApiKey.changeset(%Auth.ApiKey{}, %{key: key}) diff --git a/test/plausible_web/controllers/auth_controller_test.exs b/test/plausible_web/controllers/auth_controller_test.exs index 2aaa0c482aa5..dc3d140885dc 100644 --- a/test/plausible_web/controllers/auth_controller_test.exs +++ b/test/plausible_web/controllers/auth_controller_test.exs @@ -584,6 +584,12 @@ defmodule PlausibleWeb.AuthControllerTest do assert redirected_to(conn, 302) == "/settings" end + + test "renders form with error if form validations fail", %{conn: conn, user: user} do + conn = put(conn, "/settings", %{"user" => %{"name" => ""}}) + + assert html_response(conn, 200) =~ "can't be blank" + end end describe "DELETE /me" do From 85c21573ffceb8c4b738f8f29fc056e8241636bb Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Wed, 28 Sep 2022 11:36:20 +0300 Subject: [PATCH 2/2] Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 144394dc1f8a..a1a4ff0fd93a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ All notable changes to this project will be documented in this file. - Timezone offset labels now update with time changes - Render 404 if shared link auth cannot be verified [plausible/analytics#2225](https://github.com/plausible/analytics/pull/2225) - Restore compatibility with older format of shared links [plausible/analytics#2225](https://github.com/plausible/analytics/pull/2225) +- Ensure settings page can be rendered after a form error [plausible/analytics#2278](https://github.com/plausible/analytics/pull/2278) ### Changed - Cache the tracking script for 24 hours