Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Refactor HS callback (#6375)

Adrian Gruntkowskib177e45df3c0
diff --git a/extra/lib/plausible_web/controllers/help_scout_controller.ex b/extra/lib/plausible_web/controllers/help_scout_controller.ex
index 6397426786..92c6129791 100644
--- a/extra/lib/plausible_web/controllers/help_scout_controller.ex
+++ b/extra/lib/plausible_web/controllers/help_scout_controller.ex
@@ -8,11 +8,12 @@ defmodule PlausibleWeb.HelpScoutController do
plug :make_iframe_friendly
def callback(conn, %{"customer-id" => customer_id, "conversation-id" => conversation_id}) do
- token = sign_token(conversation_id)
- assigns = %{conversation_id: conversation_id, customer_id: customer_id, token: token}
+ assigns = %{conversation_id: conversation_id, customer_id: customer_id, token: ""}
with :ok <- HelpScout.validate_signature(conn),
{:ok, details} <- HelpScout.get_details_for_customer(customer_id, conversation_id) do
+ assigns = Map.put(assigns, :token, sign_token(conversation_id))
+
conn
|> render("callback.html", Map.merge(assigns, details))
else
diff --git a/test/plausible_web/controllers/help_scout_controller_test.exs b/test/plausible_web/controllers/help_scout_controller_test.exs
index 91caf4637d..5bb115f238 100644
--- a/test/plausible_web/controllers/help_scout_controller_test.exs
+++ b/test/plausible_web/controllers/help_scout_controller_test.exs
@@ -49,8 +49,9 @@ defmodule PlausibleWeb.HelpScoutControllerTest do
assert [] = Plug.Conn.get_resp_header(conn, "content-security-policy")
- assert html_response(conn, 200) =~
- Routes.customer_support_user_path(PlausibleWeb.Endpoint, :show, user.id)
+ html = html_response(conn, 200)
+ assert html =~ Routes.customer_support_user_path(PlausibleWeb.Endpoint, :show, user.id)
+ assert text_of_attr("input[name=token]", "value") != ""
end
test "returns error on failure", %{conn: conn} do
@@ -60,7 +61,10 @@ defmodule PlausibleWeb.HelpScoutControllerTest do
"/helpscout/callback?conversation-id=123&customer-id=500&X-HelpScout-Signature=invalid"
)
- assert html_response(conn, 200) =~ "bad_signature"
+ html = html_response(conn, 200)
+
+ assert html =~ "bad_signature"
+ refute text_of_attr("input[name=token]", "value")
end
test "handles invalid parameters gracefully", %{conn: conn} do