From ff2c3346d253035be212ce36ac19fd181f6929a2 Mon Sep 17 00:00:00 2001 From: RobertJoonas <56999674+RobertJoonas@users.noreply.github.com> Date: Tue, 28 Nov 2023 09:30:35 +0000 Subject: [PATCH] Bugfix: Allow breakdown by internally used prop keys for Growth plans (#3562) * fix bug - allow internally used prop key breakdown for Growth plans * use case instead of with --- lib/plausible/props.ex | 8 +++ .../controllers/api/stats_controller.ex | 3 +- .../custom_prop_breakdown_test.exs | 49 ++++++++++++++----- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/lib/plausible/props.ex b/lib/plausible/props.ex index 726da24fe..dde886f64 100644 --- a/lib/plausible/props.ex +++ b/lib/plausible/props.ex @@ -96,6 +96,14 @@ defmodule Plausible.Props do """ def internal_keys, do: @internal_keys + def ensure_prop_key_accessible(prop_key, user) do + if prop_key in @internal_keys do + :ok + else + Plausible.Billing.Feature.Props.check_availability(user) + end + end + @spec suggest_keys_to_allow(Plausible.Site.t(), non_neg_integer()) :: [String.t()] @doc """ Queries the events table to fetch the #{@max_props} most frequent prop keys diff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.ex index db0dbc3c3..c419fbad7 100644 --- a/lib/plausible_web/controllers/api/stats_controller.ex +++ b/lib/plausible_web/controllers/api/stats_controller.ex @@ -1185,8 +1185,9 @@ defmodule PlausibleWeb.Api.StatsController do def custom_prop_values(conn, params) do site = Plausible.Repo.preload(conn.assigns.site, :owner) + prop_key = Map.fetch!(params, "prop_key") - case Plausible.Billing.Feature.Props.check_availability(site.owner) do + case Plausible.Props.ensure_prop_key_accessible(prop_key, site.owner) do :ok -> props = breakdown_custom_prop_values(site, params) json(conn, props) diff --git a/test/plausible_web/controllers/api/stats_controller/custom_prop_breakdown_test.exs b/test/plausible_web/controllers/api/stats_controller/custom_prop_breakdown_test.exs index 7f26685bd..46c6b1c61 100644 --- a/test/plausible_web/controllers/api/stats_controller/custom_prop_breakdown_test.exs +++ b/test/plausible_web/controllers/api/stats_controller/custom_prop_breakdown_test.exs @@ -177,17 +177,6 @@ defmodule PlausibleWeb.Api.StatsController.CustomPropBreakdownTest do } ] end - - test "errors when site owner is on a growth plan", %{conn: conn, site: site, user: user} do - insert(:growth_subscription, user: user) - - conn = get(conn, "/api/stats/#{site.domain}/custom-prop-values/prop?period=day") - - assert json_response(conn, 402) == %{ - "error" => - "Custom Properties is part of the Plausible Business plan. To get access to this feature, please upgrade your account." - } - end end describe "GET /api/stats/:domain/custom-prop-values/:prop_key - with goal filter" do @@ -1058,4 +1047,42 @@ defmodule PlausibleWeb.Api.StatsController.CustomPropBreakdownTest do ] end end + + describe "GET /api/stats/:domain/custom-prop-values/:prop_key - for a Growth subscription" do + setup [:create_user, :log_in, :create_new_site] + + setup %{user: user, site: site} do + insert(:growth_subscription, user: user) + + populate_stats(site, [ + build(:pageview, + "meta.key": ["url", "path", "author"], + "meta.value": ["one", "two", "three"] + ) + ]) + + :ok + end + + test "returns breakdown for internally used prop keys", %{conn: conn, site: site} do + [%{"visitors" => 1, "name" => "one"}] = + conn + |> get("/api/stats/#{site.domain}/custom-prop-values/url?period=day") + |> json_response(200) + + [%{"visitors" => 1, "name" => "two"}] = + conn + |> get("/api/stats/#{site.domain}/custom-prop-values/path?period=day") + |> json_response(200) + end + + test "returns 402 'upgrade required' for any other prop key", %{conn: conn, site: site} do + conn = get(conn, "/api/stats/#{site.domain}/custom-prop-values/prop?period=day") + + assert json_response(conn, 402) == %{ + "error" => + "Custom Properties is part of the Plausible Business plan. To get access to this feature, please upgrade your account." + } + end + end end