Skip to content

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

Script v2: Allow changing tracker config over Sites API site endpoints (#5607)

Artur Patae8ef3f83d060
diff --git a/extra/lib/plausible_web/controllers/api/external_sites_controller.ex b/extra/lib/plausible_web/controllers/api/external_sites_controller.ex
index 11f05c4534..d68ca5b865 100644
--- a/extra/lib/plausible_web/controllers/api/external_sites_controller.ex
+++ b/extra/lib/plausible_web/controllers/api/external_sites_controller.ex
@@ -23,7 +23,7 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
|> paginate(params, @pagination_opts)
json(conn, %{
- sites: page.entries,
+ sites: page.entries |> Enum.map(&get_site_response_for_index/1),
meta: pagination_meta(page.metadata)
})
end
@@ -33,7 +33,7 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
team = conn.assigns.current_team
with {:ok, site_id} <- expect_param_key(params, "site_id"),
- {:ok, site} <- get_site(user, team, site_id, [:owner, :admin, :editor, :viewer]) do
+ {:ok, site} <- find_site(user, team, site_id, [:owner, :admin, :editor, :viewer]) do
opts = [cursor_fields: [inserted_at: :desc, id: :desc], limit: 100, maximum_limit: 1000]
page = site |> Sites.list_guests_query() |> paginate(params, opts)
@@ -83,7 +83,7 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
team = conn.assigns.current_team
with {:ok, site_id} <- expect_param_key(params, "site_id"),
- {:ok, site} <- get_site(user, team, site_id, [:owner, :admin, :editor, :viewer]) do
+ {:ok, site} <- find_site(user, team, site_id, [:owner, :admin, :editor, :viewer]) do
page =
site
|> Plausible.Goals.for_site_query()
@@ -115,11 +115,28 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
user = conn.assigns.current_user
team = conn.assigns.current_team || Teams.get(params["team_id"])
- case Sites.create(user, params, team) do
- {:ok, %{site: site}} ->
- json(conn, site)
+ case Repo.transact(fn ->
+ with {:ok, %{site: site}} <- Sites.create(user, params, team),
+ {:ok, tracker_script_configuration} <-
+ get_or_create_config(site, params["tracker_script_configuration"] || %{}, user) do
+ {:ok,
+ struct(site,
+ tracker_script_configuration: tracker_script_configuration
+ )}
+ else
+ # Translates Multi error format to Repo.transact error format
+ {:error, step_id, output, context} ->
+ {:error, {step_id, output, context}}
+
+ # already in Repo.transact error format
+ {:error, reason} ->
+ {:error, reason}
+ end
+ end) do
+ {:ok, site} ->
+ json(conn, get_site_response(site, user))
- {:error, _, {:over_limit, limit}, _} ->
+ {:error, {_, {:over_limit, limit}, _}} ->
conn
|> put_status(402)
|> json(%{
@@ -127,21 +144,26 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
"Your account has reached the limit of #{limit} sites. To unlock more sites, please upgrade your subscription."
})
- {:error, _, :permission_denied, _} ->
+ {:error, {_, :permission_denied, _}} ->
conn
|> put_status(403)
|> json(%{
error: "You can't add sites to the selected team."
})
- {:error, _, :multiple_teams, _} ->
+ {:error, {_, :multiple_teams, _}} ->
conn
|> put_status(400)
|> json(%{
error: "You must select a team with 'team_id' parameter."
})
- {:error, _, changeset, _} ->
+ {:error, {:tracker_script_configuration_invalid, %Ecto.Changeset{} = changeset}} ->
+ conn
+ |> put_status(400)
+ |> json(serialize_errors(changeset, "tracker_script_configuration."))
+
+ {:error, {_, %Ecto.Changeset{} = changeset, _}} ->
conn
|> put_status(400)
|> json(serialize_errors(changeset))
@@ -152,14 +174,18 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
user = conn.assigns.current_user
team = conn.assigns.current_team
- case get_site(user, team, site_id, [:owner, :admin, :editor, :viewer]) do
- {:ok, site} ->
- json(conn, %{
- domain: site.domain,
- timezone: site.timezone,
- custom_properties: site.allowed_event_props || []
- })
+ with {:ok, site} <- find_site(user, team, site_id, [:owner, :admin, :editor, :viewer]),
+ {:ok, tracker_script_configuration} <- get_or_create_config(site, %{}, user) do
+ site = struct(site, tracker_script_configuration: tracker_script_configuration)
+ json(
+ conn,
+ get_site_response(
+ site,
+ user
+ )
+ )
+ else
{:error, :site_not_found} ->
H.not_found(conn, "Site could not be found")
end
@@ -169,7 +195,7 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
user = conn.assigns.current_user
team = conn.assigns.current_team
- case get_site(user, team, site_id, [:owner]) do
+ case find_site(user, team, site_id, [:owner]) do
{:ok, site} ->
{:ok, _} = Plausible.Site.Removal.run(site)
json(conn, %{"deleted" => true})
@@ -183,18 +209,66 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
user = conn.assigns.current_user
team = conn.assigns.current_team
- # for now this only allows to change the domain
- with {:ok, site} <- get_site(user, team, site_id, [:owner, :admin, :editor]),
- {:ok, site} <- Plausible.Site.Domain.change(site, params["domain"]) do
- json(conn, site)
+ with {:ok, params} <- validate_update_payload(params),
+ {:ok, site} <- find_site(user, team, site_id, [:owner, :admin, :editor]),
+ {:ok, site} <- do_update_site(site, params, user) do
+ json(conn, get_site_response(site, user))
else
{:error, :site_not_found} ->
H.not_found(conn, "Site could not be found")
- {:error, %Ecto.Changeset{} = changeset} ->
+ {:error, :no_changes} ->
+ H.bad_request(
+ conn,
+ "Payload must contain at least one of the parameters 'domain', 'tracker_script_configuration'"
+ )
+
+ {:error, {:domain_change_invalid, %Ecto.Changeset{} = changeset}} ->
conn
|> put_status(400)
|> json(serialize_errors(changeset))
+
+ {:error, {:tracker_script_configuration_invalid, %Ecto.Changeset{} = changeset}} ->
+ conn
+ |> put_status(400)
+ |> json(serialize_errors(changeset, "tracker_script_configuration."))
+ end
+ end
+
+ defp validate_update_payload(params) do
+ params = params |> Map.take(["domain", "tracker_script_configuration"]) |> Map.drop([nil])
+
+ if map_size(params) == 0 do
+ {:error, :no_changes}
+ else
+ {:ok, params}
+ end
+ end
+
+ defp do_update_site(site, params, user) do
+ Repo.transact(fn ->
+ with {:ok, site} <-
+ if(params["domain"],
+ do: change_domain(site, params["domain"]),
+ else: {:ok, site}
+ ),
+ {:ok, tracker_script_configuration} <-
+ if(params["tracker_script_configuration"],
+ do: update_config(site, params["tracker_script_configuration"], user),
+ else: get_or_create_config(site, %{}, user)
+ ) do
+ {:ok, struct(site, tracker_script_configuration: tracker_script_configuration)}
+ end
+ end)
+ end
+
+ defp change_domain(site, domain) do
+ case Plausible.Site.Domain.change(site, domain) do
+ {:ok, site} ->
+ {:ok, site}
+
+ {:error, %Ecto.Changeset{} = changeset} ->
+ {:error, {:domain_change_invalid, changeset}}
end
end
@@ -205,7 +279,7 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
with {:ok, site_id} <- expect_param_key(params, "site_id"),
{:ok, email} <- expect_param_key(params, "email"),
{:ok, role} <- expect_param_key(params, "role", ["viewer", "editor"]),
- {:ok, site} <- get_site(user, team, site_id, [:owner, :admin]) do
+ {:ok, site} <- find_site(user, team, site_id, [:owner, :admin]) do
existing = Repo.one(Sites.list_guests_query(site, email: email))
if existing do
@@ -250,7 +324,7 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
with {:ok, site_id} <- expect_param_key(params, "site_id"),
{:ok, email} <- expect_param_key(params, "email"),
- {:ok, site} <- get_site(user, team, site_id, [:owner, :admin]) do
+ {:ok, site} <- find_site(user, team, site_id, [:owner, :admin]) do
existing = Repo.one(Sites.list_guests_query(site, email: email))
case existing do
@@ -285,7 +359,7 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
with {:ok, site_id} <- expect_param_key(params, "site_id"),
{:ok, link_name} <- expect_param_key(params, "name"),
- {:ok, site} <- get_site(user, team, site_id, [:owner, :admin, :editor]) do
+ {:ok, site} <- find_site(user, team, site_id, [:owner, :admin, :editor]) do
shared_link = Repo.get_by(Plausible.Site.SharedLink, site_id: site.id, name: link_name)
shared_link =
@@ -329,7 +403,7 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
with {:ok, site_id} <- expect_param_key(params, "site_id"),
{:ok, _} <- expect_param_key(params, "goal_type"),
- {:ok, site} <- get_site(user, team, site_id, [:owner, :admin, :editor]),
+ {:ok, site} <- find_site(user, team, site_id, [:owner, :admin, :editor]),
{:ok, goal} <- Goals.find_or_create(site, params) do
json(conn, goal)
else
@@ -350,7 +424,7 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
with {:ok, site_id} <- expect_param_key(params, "site_id"),
{:ok, goal_id} <- expect_param_key(params, "goal_id"),
- {:ok, site} <- get_site(user, team, site_id, [:owner, :admin, :editor]),
+ {:ok, site} <- find_site(user, team, site_id, [:owner, :admin, :editor]),
:ok <- Goals.delete(goal_id, site) do
json(conn, %{"deleted" => true})
else
@@ -379,7 +453,7 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
}
end
- defp get_site(user, team, site_id, roles) do
+ defp find_site(user, team, site_id, roles) do
case Plausible.Sites.get_for_user(user, site_id, roles) do
nil ->
{:error, :site_not_found}
@@ -395,9 +469,9 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
end
end
- defp serialize_errors(changeset) do
+ defp serialize_errors(changeset, field_prefix \\ "") do
{field, {msg, _opts}} = List.first(changeset.errors)
- error_msg = Atom.to_string(field) <> ": " <> msg
+ error_msg = field_prefix <> Atom.to_string(field) <> ": " <> msg
%{"error" => error_msg}
end
@@ -419,4 +493,53 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
{:missing, key}
end
end
+
+ defp get_or_create_config(site, params, user) do
+ if scriptv2?(site, user) do
+ case PlausibleWeb.Tracker.get_or_create_tracker_script_configuration(site, params) do
+ {:ok, tracker_script_configuration} ->
+ {:ok, tracker_script_configuration}
+
+ {:error, %Ecto.Changeset{} = changeset} ->
+ {:error, {:tracker_script_configuration_invalid, changeset}}
+ end
+ else
+ {:ok, %{}}
+ end
+ end
+
+ defp update_config(site, params, user) do
+ if scriptv2?(site, user) do
+ case PlausibleWeb.Tracker.update_script_configuration(site, params, :installation) do
+ {:ok, tracker_script_configuration} ->
+ {:ok, tracker_script_configuration}
+
+ {:error, %Ecto.Changeset{} = changeset} ->
+ {:error, {:tracker_script_configuration_invalid, changeset}}
+ end
+ else
+ {:ok, %{}}
+ end
+ end
+
+ defp get_site_response_for_index(site) do
+ site |> Map.take([:domain, :timezone])
+ end
+
+ defp get_site_response(site, user) do
+ serializable_properties =
+ if(scriptv2?(site, user),
+ do: [:domain, :timezone, :tracker_script_configuration],
+ else: [:domain, :timezone]
+ )
+
+ site
+ |> Map.take(serializable_properties)
+ # remap to `custom_properties`
+ |> Map.put(:custom_properties, site.allowed_event_props || [])
+ end
+
+ defp scriptv2?(site, user) do
+ FunWithFlags.enabled?(:scriptv2, for: site) or FunWithFlags.enabled?(:scriptv2, for: user)
+ end
end
diff --git a/lib/plausible/site/tracker_script_configuration.ex b/lib/plausible/site/tracker_script_configuration.ex
index 2b0c1d20c3..5571b5356c 100644
--- a/lib/plausible/site/tracker_script_configuration.ex
+++ b/lib/plausible/site/tracker_script_configuration.ex
@@ -6,6 +6,19 @@ defmodule Plausible.Site.TrackerScriptConfiguration do
import Ecto.Changeset
@type t() :: %__MODULE__{}
+ @derive {Jason.Encoder,
+ only: [
+ :id,
+ :installation_type,
+ :track_404_pages,
+ :hash_based_routing,
+ :outbound_links,
+ :file_downloads,
+ :revenue_tracking,
+ :tagged_events,
+ :form_submissions,
+ :pageview_props
+ ]}
@primary_key {:id, Plausible.Ecto.Types.Nanoid, autogenerate: true}
schema "tracker_script_configuration" do
diff --git a/lib/plausible_web/live/installation.ex b/lib/plausible_web/live/installation.ex
index 0b175e6743..1c15be63b5 100644
--- a/lib/plausible_web/live/installation.ex
+++ b/lib/plausible_web/live/installation.ex
@@ -505,7 +505,7 @@ defmodule PlausibleWeb.Live.Installation do
"installation_type" => socket.assigns.installation_type
})
- PlausibleWeb.Tracker.update_script_configuration(
+ PlausibleWeb.Tracker.update_script_configuration!(
socket.assigns.site,
tracker_script_config_update,
:installation
diff --git a/lib/plausible_web/live/installationv2.ex b/lib/plausible_web/live/installationv2.ex
index fce10dd2b0..bc8805866b 100644
--- a/lib/plausible_web/live/installationv2.ex
+++ b/lib/plausible_web/live/installationv2.ex
@@ -186,7 +186,7 @@ defmodule PlausibleWeb.Live.InstallationV2 do
end
def handle_event("submit", %{"tracker_script_configuration" => params}, socket) do
- PlausibleWeb.Tracker.update_script_configuration(
+ PlausibleWeb.Tracker.update_script_configuration!(
socket.assigns.site,
params,
:installation
diff --git a/lib/plausible_web/plugins/api/controllers/tracker_script_configuration.ex b/lib/plausible_web/plugins/api/controllers/tracker_script_configuration.ex
index dc6d8d8152..b37da2ef52 100644
--- a/lib/plausible_web/plugins/api/controllers/tracker_script_configuration.ex
+++ b/lib/plausible_web/plugins/api/controllers/tracker_script_configuration.ex
@@ -44,7 +44,7 @@ defmodule PlausibleWeb.Plugins.API.Controllers.TrackerScriptConfiguration do
update_params = Map.put(update_params, "site_id", site.id)
updated_config =
- PlausibleWeb.Tracker.update_script_configuration(site, update_params, :plugins_api)
+ PlausibleWeb.Tracker.update_script_configuration!(site, update_params, :plugins_api)
conn
|> put_view(Views.TrackerScriptConfiguration)
diff --git a/lib/plausible_web/tracker.ex b/lib/plausible_web/tracker.ex
index 9e28e080b4..6a79af3dd9 100644
--- a/lib/plausible_web/tracker.ex
+++ b/lib/plausible_web/tracker.ex
@@ -43,57 +43,71 @@ defmodule PlausibleWeb.Tracker do
end
def update_script_configuration(site, config_update, changeset_type) do
- {:ok, updated_config} =
- Repo.transaction(fn ->
- original_config = get_or_create_tracker_script_configuration!(site)
- changeset = changeset(original_config, config_update, changeset_type)
-
- updated_config = Repo.update!(changeset)
-
+ Repo.transact(fn ->
+ with {:ok, original_config} <- get_or_create_tracker_script_configuration(site),
+ changeset <- changeset(original_config, config_update, changeset_type),
+ {:ok, updated_config} <-
+ Repo.update(changeset) do
sync_goals(site, original_config, updated_config)
on_ee do
- if Map.keys(changeset.changes) != [:installation_type] do
- Plausible.Workers.PurgeCDNCache.new(
- %{id: updated_config.id},
- # See PurgeCDNCache.ex for more details
- schedule_in: 10,
- replace: [scheduled: [:scheduled_at]]
- )
- |> Oban.insert!()
+ if should_purge_cache?(changeset) do
+ purge_cache!(updated_config.id)
end
end
- updated_config
- end)
+ {:ok, updated_config}
+ end
+ end)
+ end
+
+ on_ee do
+ defp should_purge_cache?(changeset) do
+ Map.keys(changeset.changes) != [:installation_type]
+ end
+
+ defp purge_cache!(config_id) do
+ Plausible.Workers.PurgeCDNCache.new(
+ %{id: config_id},
+ # See PurgeCDNCache.ex for more details
+ schedule_in: 10,
+ replace: [scheduled: [:scheduled_at]]
+ )
+ |> Oban.insert!()
+ end
+ end
+ def update_script_configuration!(site, config_update, changeset_type) do
+ {:ok, updated_config} = update_script_configuration(site, config_update, changeset_type)
updated_config
end
- def get_or_create_tracker_script_configuration!(site, params \\ %{}) do
+ def get_or_create_tracker_script_configuration(site, params \\ %{}) do
configuration = Repo.get_by(TrackerScriptConfiguration, site_id: site.id)
if configuration do
- configuration
+ {:ok, configuration}
else
- {:ok, created_config} =
- Repo.transaction(fn ->
- created_config =
- TrackerScriptConfiguration.installation_changeset(
- %TrackerScriptConfiguration{site_id: site.id},
- params
- )
- |> Repo.insert!()
-
+ Repo.transact(fn ->
+ with {:ok, created_config} <-
+ Repo.insert(
+ TrackerScriptConfiguration.installation_changeset(
+ %TrackerScriptConfiguration{site_id: site.id},
+ params
+ )
+ ) do
sync_goals(site, %{}, created_config)
-
- created_config
- end)
-
- created_config
+ {:ok, created_config}
+ end
+ end)
end
end
+ def get_or_create_tracker_script_configuration!(site, params \\ %{}) do
+ {:ok, config} = get_or_create_tracker_script_configuration(site, params)
+ config
+ end
+
# Sync plausible goals with the updated script config
defp sync_goals(site, original_config, updated_config) do
[:track_404_pages, :outbound_links, :file_downloads, :form_submissions]
diff --git a/test/plausible_web/controllers/api/external_sites_controller_sites_crud_api_test.exs b/test/plausible_web/controllers/api/external_sites_controller_sites_crud_api_test.exs
new file mode 100644
index 0000000000..6953e2b28b
--- /dev/null
+++ b/test/plausible_web/controllers/api/external_sites_controller_sites_crud_api_test.exs
@@ -0,0 +1,893 @@
+defmodule PlausibleWeb.Api.ExternalSitesControllerSitesCrudApiTest do
+ @moduledoc """
+ Tests for Sites create/read/update/delete API with `scriptv2` feature flag enabled.
+ It has overlap with some of the tests in `PlausibleWeb.Api.ExternalSitesControllerTest` test suite.
+ The overlapped tests from that suite can be deleted once the feature flag is enabled globally.
+ """
+ use Plausible
+ use PlausibleWeb.ConnCase, async: false
+ use Plausible.Repo
+ use Plausible.Teams.Test
+ use Bamboo.Test
+
+ on_ee do
+ setup :create_user
+
+ setup %{conn: conn, user: user} do
+ FunWithFlags.enable(:scriptv2, for_actor: user)
+ api_key = insert(:api_key, user: user, scopes: ["sites:provision:*"])
+ conn = Plug.Conn.put_req_header(conn, "authorization", "Bearer #{api_key.key}")
+ {:ok, api_key: api_key, conn: conn}
+ end
+
+ describe "POST /api/v1/sites" do
+ test "can create a site", %{conn: conn} do
+ conn =
+ post(conn, "/api/v1/sites", %{
+ "domain" => "some-site.domain",
+ "timezone" => "Europe/Tallinn"
+ })
+
+ response = json_response(conn, 200)
+
+ assert_matches ^strict_map(%{
+ "domain" => "some-site.domain",
+ "timezone" => "Europe/Tallinn",
+ "custom_properties" => [],
+ "tracker_script_configuration" =>
+ ^strict_map(%{
+ "id" => ^any(:string),
+ "installation_type" => nil,
+ "track_404_pages" => false,
+ "hash_based_routing" => false,
+ "outbound_links" => false,
+ "file_downloads" => false,
+ "revenue_tracking" => false,
+ "tagged_events" => false,
+ "form_submissions" => false,
+ "pageview_props" => false
+ })
+ }) = response
+ end
+
+ test "can create a site with a specific tracker script configuration", %{conn: conn} do
+ payload = %{
+ "domain" => "some-site.domain",
+ "timezone" => "Europe/Tallinn",
+ "tracker_script_configuration" => %{
+ "installation_type" => "wordpress",
+ "track_404_pages" => false,
+ "hash_based_routing" => false,
+ "outbound_links" => false,
+ "file_downloads" => true,
+ "revenue_tracking" => false,
+ "tagged_events" => false,
+ "form_submissions" => true,
+ "pageview_props" => false
+ }
+ }
+
+ conn =
+ post(conn, "/api/v1/sites", payload)
+
+ response = json_response(conn, 200)
+
+ assert_matches ^strict_map(%{
+ "domain" => ^payload["domain"],
+ "timezone" => ^payload["timezone"],
+ "custom_properties" => [],
+ "tracker_script_configuration" =>
+ ^strict_map(%{
+ "id" => ^any(:string),
+ "installation_type" =>
+ ^payload["tracker_script_configuration"]["installation_type"],
+ "track_404_pages" =>
+ ^payload["tracker_script_configuration"]["track_404_pages"],
+ "hash_based_routing" =>
+ ^payload["tracker_script_configuration"]["hash_based_routing"],
+ "outbound_links" =>
+ ^payload["tracker_script_configuration"]["outbound_links"],
+ "file_downloads" =>
+ ^payload["tracker_script_configuration"]["file_downloads"],
+ "revenue_tracking" =>
+ ^payload["tracker_script_configuration"]["revenue_tracking"],
+ "tagged_events" =>
+ ^payload["tracker_script_configuration"]["tagged_events"],
+ "form_submissions" =>
+ ^payload["tracker_script_configuration"]["form_submissions"],
+ "pageview_props" =>
+ ^payload["tracker_script_configuration"]["pageview_props"]
+ })
+ }) = response
+ end
+
+ test "can't create site in a team where not permitted to", %{conn: conn, user: user} do
+ owner = new_user() |> subscribe_to_growth_plan()
+ team = owner |> team_of() |> Plausible.Teams.complete_setup()
+ add_member(team, user: user, role: :viewer)
+
+ conn =
+ post(conn, "/api/v1/sites", %{
+ "team_id" => team.identifier,
+ "domain" => "some-site.domain",
+ "timezone" => "Europe/Tallinn"
+ })
+
+ assert json_response(conn, 403) == %{
+ "error" => "You can't add sites to the selected team."
+ }
+ end
+
+ test "can create a site under a specific team if permitted", %{conn: conn, user: user} do
+ _site = new_site(owner: user)
+
+ owner = new_user() |> subscribe_to_growth_plan()
+ team = owner |> team_of() |> Plausible.Teams.complete_setup()
+ add_member(team, user: user, role: :owner)
+
+ conn =
+ post(conn, "/api/v1/sites", %{
+ "team_id" => team.identifier,
+ "domain" => "some-site.domain",
+ "timezone" => "Europe/Tallinn"
+ })
+
+ response = json_response(conn, 200)
+
+ assert_matches ^strict_map(%{
+ "domain" => "some-site.domain",
+ "timezone" => "Europe/Tallinn",
+ "custom_properties" => [],
+ "tracker_script_configuration" =>
+ ^strict_map(%{
+ "id" => ^any(:string),
+ "installation_type" => nil,
+ "track_404_pages" => false,
+ "hash_based_routing" => false,
+ "outbound_links" => false,
+ "file_downloads" => false,
+ "revenue_tracking" => false,
+ "tagged_events" => false,
+ "form_submissions" => false,
+ "pageview_props" => false
+ })
+ }) = response
+
+ assert Repo.get_by(Plausible.Site, domain: "some-site.domain").team_id == team.id
+ end
+
+ test "creates under a particular team when team-scoped key used", %{conn: conn, user: user} do
+ personal_team = user |> subscribe_to_business_plan() |> team_of()
+
+ another_team = new_user() |> subscribe_to_business_plan() |> team_of()
+ add_member(another_team, user: user, role: :admin)
+
+ api_key = insert(:api_key, user: user, team: another_team, scopes: ["sites:provision:*"])
+ conn = Plug.Conn.put_req_header(conn, "authorization", "Bearer #{api_key.key}")
+
+ conn =
+ post(conn, "/api/v1/sites", %{
+ # is ignored
+ "team_id" => personal_team.identifier,
+ "domain" => "some-site.domain",
+ "timezone" => "Europe/Tallinn"
+ })
+
+ response = json_response(conn, 200)
+
+ assert_matches ^strict_map(%{
+ "domain" => "some-site.domain",
+ "timezone" => "Europe/Tallinn",
+ "custom_properties" => [],
+ "tracker_script_configuration" =>
+ ^strict_map(%{
+ "id" => ^any(:string),
+ "installation_type" => nil,
+ "track_404_pages" => false,
+ "hash_based_routing" => false,
+ "outbound_links" => false,
+ "file_downloads" => false,
+ "revenue_tracking" => false,
+ "tagged_events" => false,
+ "form_submissions" => false,
+ "pageview_props" => false
+ })
+ }) = response
+
+ assert Repo.get_by(Plausible.Site, domain: "some-site.domain").team_id == another_team.id
+ end
+
+ test "timezone is validated", %{conn: conn} do
+ conn =
+ post(conn, "/api/v1/sites", %{
+ "domain" => "some-site.domain",
+ "timezone" => "d"
+ })
+
+ assert json_response(conn, 400) == %{
+ "error" => "timezone: is invalid"
+ }
+ end
+
+ test "timezone defaults to Etc/UTC", %{conn: conn} do
+ conn =
+ post(conn, "/api/v1/sites", %{
+ "domain" => "some-site.domain"
+ })
+
+ response = json_response(conn, 200)
+
+ assert_matches ^strict_map(%{
+ "domain" => "some-site.domain",
+ "timezone" => "Etc/UTC",
+ "custom_properties" => [],
+ "tracker_script_configuration" => %{}
+ }) = response
+ end
+
+ test "domain is required", %{conn: conn} do
+ conn = post(conn, "/api/v1/sites", %{})
+
+ assert json_response(conn, 400) == %{
+ "error" => "domain: can't be blank"
+ }
+ end
+
+ test "accepts international domain names", %{conn: conn} do
+ ["müllers-café.test", "音乐.cn", "до.101домен.рф/pages"]
+ |> Enum.each(fn idn_domain ->
+ conn = post(conn, "/api/v1/sites", %{"domain" => idn_domain})
+ assert %{"domain" => ^idn_domain} = json_response(conn, 200)
+ end)
+ end
+
+ test "validates uri breaking domains", %{conn: conn} do
+ ["quero:café.test", "h&llo.test", "iamnotsur&about?this.com"]
+ |> Enum.each(fn bad_domain ->
+ conn = post(conn, "/api/v1/sites", %{"domain" => bad_domain})
+
+ assert %{"error" => error} = json_response(conn, 400)
+ assert error =~ "domain: must not contain URI reserved characters"
+ end)
+ end
+
+ test "validates tracker script configuration if it's not nil, serializing the first error",
+ %{conn: conn} do
+ conn =
+ post(conn, "/api/v1/sites", %{
+ "domain" => "some-site.domain",
+ "timezone" => "Europe/Tallinn",
+ "tracker_script_configuration" => %{
+ "installation_type" => "an invalid value",
+ "track_404_pages" => "an invalid value"
+ }
+ })
+
+ assert json_response(conn, 400) == %{
+ "error" => "tracker_script_configuration.installation_type: is invalid"
+ }
+
+ conn2 =
+ post(conn, "/api/v1/sites", %{
+ "domain" => "some-site.domain",
+ "timezone" => "Europe/Tallinn",
+ "tracker_script_configuration" => %{
+ "installation_type" => "wordpress",
+ "track_404_pages" => "an invalid value"
+ }
+ })
+
+ assert json_response(conn2, 400) == %{
+ "error" => "tracker_script_configuration.track_404_pages: is invalid"
+ }
+ end
+
+ test "creating the site and creating the tracker script configuration are run as a transaction: if creating the tracker script configuration fails, the site is not inserted",
+ %{conn: conn} do
+ conn =
+ post(conn, "/api/v1/sites", %{
+ "domain" => "some-site.domain",
+ "timezone" => "Europe/Tallinn",
+ "tracker_script_configuration" => %{
+ "installation_type" => "an invalid value"
+ }
+ })
+
+ assert json_response(conn, 400) == %{
+ "error" => "tracker_script_configuration.installation_type: is invalid"
+ }
+
+ assert Repo.get_by(Plausible.Site, domain: "some-site.domain") == nil
+
+ # try again with a valid tracker script configuration
+ conn2 =
+ post(conn, "/api/v1/sites", %{
+ "domain" => "some-site.domain",
+ "timezone" => "Europe/Tallinn",
+ "tracker_script_configuration" => %{
+ "installation_type" => "manual"
+ }
+ })
+
+ response = json_response(conn2, 200)
+
+ assert_matches ^strict_map(%{
+ "domain" => "some-site.domain",
+ "timezone" => "Europe/Tallinn",
+ "custom_properties" => [],
+ "tracker_script_configuration" =>
+ ^strict_map(%{
+ "id" => ^any(:string),
+ "installation_type" => "manual",
+ "track_404_pages" => false,
+ "hash_based_routing" => false,
+ "outbound_links" => false,
+ "file_downloads" => false,
+ "revenue_tracking" => false,
+ "tagged_events" => false,
+ "form_submissions" => false,
+ "pageview_props" => false
+ })
+ }) = response
+ end
+
+ test "does not allow creating more sites than the limit", %{conn: conn, user: user} do
+ for _ <- 1..10, do: new_site(owner: user)
+
+ conn =
+ post(conn, "/api/v1/sites", %{
+ "domain" => "some-site.domain",
+ "timezone" => "Europe/Tallinn"
+ })
+
+ assert json_response(conn, 402) == %{
+ "error" =>
+ "Your account has reached the limit of 10 sites. To unlock more sites, please upgrade your subscription."
+ }
+ end
+
+ test "cannot access with a bad API key scope", %{conn: conn, user: user} do
+ api_key = insert(:api_key, user: user, scopes: ["stats:read:*"])
+
+ conn =
+ conn
+ |> Plug.Conn.put_req_header("authorization", "Bearer #{api_key.key}")
+ |> post("/api/v1/sites", %{"site" => %{"domain" => "domain.com"}})
+
+ assert json_response(conn, 401) == %{
+ "error" =>
+ "Invalid API key. Please make sure you're using a valid API key with access to the resource you've requested."
+ }
+ end
+ end
+
+ describe "DELETE /api/v1/sites/:site_id" do
+ setup :create_site
+
+ test "delete a site by its domain", %{conn: conn, site: site} do
+ conn = delete(conn, "/api/v1/sites/" <> site.domain)
+
+ assert json_response(conn, 200) == %{"deleted" => true}
+ end
+
+ test "delete a site by its old domain after domain change", %{conn: conn, site: site} do
+ old_domain = site.domain
+ new_domain = "new.example.com"
+
+ Plausible.Site.Domain.change(site, new_domain)
+
+ conn = delete(conn, "/api/v1/sites/" <> old_domain)
+
+ assert json_response(conn, 200) == %{"deleted" => true}
+ end
+
+ test "is 404 when site cannot be found", %{conn: conn} do
+ conn = delete(conn, "/api/v1/sites/foobar.baz")
+
+ assert json_response(conn, 404) == %{"error" => "Site could not be found"}
+ end
+
+ @tag :capture_log
+ test "cannot delete a site that the user does not own", %{conn: conn, user: user} do
+ site = new_site()
+ add_guest(site, user: user, role: :editor)
+ conn = delete(conn, "/api/v1/sites/" <> site.domain)
+
+ assert json_response(conn, 404) == %{"error" => "Site could not be found"}
+ end
+
+ test "cannot delete if team not matching team-scoped API key", %{
+ conn: conn,
+ user: user,
+ site: site
+ } do
+ another_team = new_user() |> subscribe_to_business_plan() |> team_of()
+ add_member(another_team, user: user, role: :admin)
+ api_key = insert(:api_key, user: user, team: another_team, scopes: ["sites:provision:*"])
+ conn = Plug.Conn.put_req_header(conn, "authorization", "Bearer #{api_key.key}")
+
+ conn = delete(conn, "/api/v1/sites/" <> site.domain)
+
+ assert json_response(conn, 404) == %{"error" => "Site could not be found"}
+ end
+
+ test "cannot access with a bad API key scope", %{conn: conn, site: site, user: user} do
+ api_key = insert(:api_key, user: user, scopes: ["stats:read:*"])
+
+ conn =
+ conn
+ |> Plug.Conn.put_req_header("authorization", "Bearer #{api_key.key}")
+ |> delete("/api/v1/sites/" <> site.domain)
+
+ assert json_response(conn, 401) == %{
+ "error" =>
+ "Invalid API key. Please make sure you're using a valid API key with access to the resource you've requested."
+ }
+ end
+ end
+
+ describe "GET /api/v1/sites" do
+ test "returns empty when there are no sites for user", %{conn: conn} do
+ conn = get(conn, "/api/v1/sites")
+
+ assert json_response(conn, 200) == %{
+ "sites" => [],
+ "meta" => %{
+ "before" => nil,
+ "after" => nil,
+ "limit" => 100
+ }
+ }
+ end
+
+ test "returns sites when present", %{conn: conn, user: user} do
+ site1 = new_site(owner: user)
+ site2 = new_site(owner: user)
+
+ _unrelated_site = new_site()
+
+ conn = get(conn, "/api/v1/sites")
+
+ assert json_response(conn, 200) == %{
+ "sites" => [
+ %{"domain" => site2.domain, "timezone" => site2.timezone},
+ %{"domain" => site1.domain, "timezone" => site1.timezone}
+ ],
+ "meta" => %{
+ "before" => nil,
+ "after" => nil,
+ "limit" => 100
+ }
+ }
+ end
+
+ test "returns sites where user is only a viewer", %{conn: conn, user: user} do
+ %{domain: owned_site_domain} = new_site(owner: user)
+ other_site = %{domain: other_site_domain} = new_site()
+ add_guest(other_site, user: user, role: :viewer)
+
+ conn = get(conn, "/api/v1/sites")
+
+ assert %{
+ "sites" => [
+ %{"domain" => ^other_site_domain},
+ %{"domain" => ^owned_site_domain}
+ ]
+ } = json_response(conn, 200)
+ end
+
+ test "returns sites scoped to a given team for full memberships", %{conn: conn, user: user} do
+ _owned_site = new_site(owner: user)
+ other_site = new_site()
+ add_guest(other_site, user: user, role: :viewer)
+ other_team_site = new_site()
+ add_member(other_team_site.team, user: user, role: :viewer)
+
+ conn = get(conn, "/api/v1/sites?team_id=" <> other_team_site.team.identifier)
+
+ assert_matches %{
+ "sites" => [
+ %{"domain" => ^other_team_site.domain}
+ ]
+ } = json_response(conn, 200)
+ end
+
+ test "implicitly scopes to a team for a team-scoped key", %{
+ conn: conn,
+ user: user
+ } do
+ another_team = new_user() |> subscribe_to_business_plan() |> team_of()
+ add_member(another_team, user: user, role: :admin)
+ site = new_site(team: another_team)
+ api_key = insert(:api_key, user: user, team: another_team, scopes: ["sites:provision:*"])
+ conn = Plug.Conn.put_req_header(conn, "authorization", "Bearer #{api_key.key}")
+
+ _owned_site = new_site(owner: user)
+ other_site = new_site()
+ add_guest(other_site, user: user, role: :viewer)
+ other_team_site = new_site()
+ add_member(other_team_site.team, user: user, role: :viewer)
+
+ # `team_id` paramaeter is ignored
+ conn = get(conn, "/api/v1/sites?team_id=" <> other_team_site.team.identifier)
+
+ assert_matches %{
+ "sites" => [
+ %{"domain" => ^site.domain}
+ ]
+ } = json_response(conn, 200)
+ end
+
+ test "handles pagination correctly", %{conn: conn, user: user} do
+ [
+ %{domain: site1_domain},
+ %{domain: site2_domain},
+ %{domain: site3_domain}
+ ] = for _ <- 1..3, do: new_site(owner: user)
+
+ conn1 = get(conn, "/api/v1/sites?limit=2")
+
+ assert %{
+ "sites" => [
+ %{"domain" => ^site3_domain},
+ %{"domain" => ^site2_domain}
+ ],
+ "meta" => %{
+ "before" => nil,
+ "after" => after_cursor,
+ "limit" => 2
+ }
+ } = json_response(conn1, 200)
+
+ conn2 = get(conn, "/api/v1/sites?limit=2&after=" <> after_cursor)
+
+ assert %{
+ "sites" => [
+ %{"domain" => ^site1_domain}
+ ],
+ "meta" => %{
+ "before" => before_cursor,
+ "after" => nil,
+ "limit" => 2
+ }
+ } = json_response(conn2, 200)
+
+ assert is_binary(before_cursor)
+ end
+
+ test "lists sites for user with read-only scope", %{conn: conn, user: user} do
+ %{domain: site_domain} = new_site(owner: user)
+ api_key = insert(:api_key, user: user, scopes: ["stats:read:*"])
+
+ conn =
+ conn
+ |> Plug.Conn.put_req_header("authorization", "Bearer #{api_key.key}")
+ |> get("/api/v1/sites")
+
+ assert %{"sites" => [%{"domain" => ^site_domain}]} = json_response(conn, 200)
+ end
+ end
+
+ describe "GET /api/v1/sites/:site_id" do
+ setup :create_site
+
+ test "get a site by its domain", %{conn: conn, site: site} do
+ site =
+ site
+ |> Ecto.Changeset.change(allowed_event_props: ["logged_in", "author"])
+ |> Repo.update!()
+
+ conn = get(conn, "/api/v1/sites/" <> site.domain)
+
+ response = json_response(conn, 200)
+
+ assert_matches ^strict_map(%{
+ "domain" => ^site.domain,
+ "timezone" => ^site.timezone,
+ "custom_properties" => ["logged_in", "author"],
+ "tracker_script_configuration" =>
+ ^strict_map(%{
+ "id" => ^any(:string),
+ "installation_type" => nil,
+ "track_404_pages" => false,
+ "hash_based_routing" => false,
+ "outbound_links" => false,
+ "file_downloads" => false,
+ "revenue_tracking" => false,
+ "tagged_events" => false,
+ "form_submissions" => false,
+ "pageview_props" => false
+ })
+ }) = response
+ end
+
+ test "get a site by old site_id after domain change", %{conn: conn, site: site} do
+ old_domain = site.domain
+ new_domain = "new.example.com"
+
+ Plausible.Site.Domain.change(site, new_domain)
+
+ conn = get(conn, "/api/v1/sites/" <> old_domain)
+
+ response = json_response(conn, 200)
+
+ assert_matches ^strict_map(%{
+ "domain" => ^new_domain,
+ "timezone" => ^site.timezone,
+ "custom_properties" => [],
+ "tracker_script_configuration" =>
+ ^strict_map(%{
+ "id" => ^any(:string),
+ "installation_type" => nil,
+ "track_404_pages" => false,
+ "hash_based_routing" => false,
+ "outbound_links" => false,
+ "file_downloads" => false,
+ "revenue_tracking" => false,
+ "tagged_events" => false,
+ "form_submissions" => false,
+ "pageview_props" => false
+ })
+ }) = response
+ end
+
+ test "get a site for user with read-only scope", %{conn: conn, user: user, site: site} do
+ api_key = insert(:api_key, user: user, scopes: ["stats:read:*"])
+
+ conn =
+ conn
+ |> Plug.Conn.put_req_header("authorization", "Bearer #{api_key.key}")
+ |> get("/api/v1/sites/" <> site.domain)
+
+ response = json_response(conn, 200)
+
+ assert_matches ^strict_map(%{
+ "domain" => ^site.domain,
+ "timezone" => ^site.timezone,
+ "custom_properties" => [],
+ "tracker_script_configuration" =>
+ ^strict_map(%{
+ "id" => ^any(:string),
+ "installation_type" => nil,
+ "track_404_pages" => false,
+ "hash_based_routing" => false,
+ "outbound_links" => false,
+ "file_downloads" => false,
+ "revenue_tracking" => false,
+ "tagged_events" => false,
+ "form_submissions" => false,
+ "pageview_props" => false
+ })
+ }) = response
+ end
+
+ test "fails when team does not match team-scoped key", %{conn: conn, user: user, site: site} do
+ another_team = new_user() |> subscribe_to_business_plan() |> team_of()
+ add_member(another_team, user: user, role: :admin)
+ api_key = insert(:api_key, user: user, team: another_team, scopes: ["sites:provision:*"])
+ conn = Plug.Conn.put_req_header(conn, "authorization", "Bearer #{api_key.key}")
+
+ conn = get(conn, "/api/v1/sites/" <> site.domain)
+
+ res = json_response(conn, 404)
+ assert res["error"] == "Site could not be found"
+ end
+
+ test "is 404 when site cannot be found", %{conn: conn} do
+ conn = get(conn, "/api/v1/sites/foobar.baz")
+
+ assert json_response(conn, 404) == %{"error" => "Site could not be found"}
+ end
+
+ test "is 404 when user is not a member of the site", %{conn: conn} do
+ site = insert(:site)
+
+ conn = get(conn, "/api/v1/sites/" <> site.domain)
+
+ assert json_response(conn, 404) == %{"error" => "Site could not be found"}
+ end
+ end
+
+ describe "PUT /api/v1/sites/:site_id" do
+ setup :create_site
+
+ test "can change domain name", %{conn: conn, site: site} do
+ old_domain = site.domain
+ assert old_domain != "new.example.com"
+
+ conn =
+ put(conn, "/api/v1/sites/#{old_domain}", %{
+ "domain" => "new.example.com"
+ })
+
+ response = json_response(conn, 200)
+
+ assert_matches ^strict_map(%{
+ "domain" => "new.example.com",
+ "timezone" => "UTC",
+ "custom_properties" => [],
+ "tracker_script_configuration" =>
+ ^strict_map(%{
+ "id" => ^any(:string),
+ "installation_type" => nil,
+ "track_404_pages" => false,
+ "hash_based_routing" => false,
+ "outbound_links" => false,
+ "file_downloads" => false,
+ "revenue_tracking" => false,
+ "tagged_events" => false,
+ "form_submissions" => false,
+ "pageview_props" => false
+ })
+ }) = response
+
+ site = Repo.reload!(site)
+
+ assert site.domain == "new.example.com"
+ assert site.domain_changed_from == old_domain
+ end
+
+ test "can change tracker script configuration (merging updated keys with previous configuration)",
+ %{conn: conn, site: site} do
+ conn =
+ put(conn, "/api/v1/sites/#{site.domain}", %{
+ "tracker_script_configuration" => %{
+ "form_submissions" => true
+ }
+ })
+
+ response = json_response(conn, 200)
+
+ assert_matches ^strict_map(%{
+ "domain" => ^site.domain,
+ "timezone" => "UTC",
+ "custom_properties" => [],
+ "tracker_script_configuration" =>
+ ^strict_map(%{
+ "id" => ^any(:string),
+ "installation_type" => nil,
+ "track_404_pages" => false,
+ "hash_based_routing" => false,
+ "outbound_links" => false,
+ "file_downloads" => false,
+ "revenue_tracking" => false,
+ "tagged_events" => false,
+ "form_submissions" => true,
+ "pageview_props" => false
+ })
+ }) = response
+ end
+
+ test "can change domain name and tracker script configuration together (merging updated keys with previous configuration)",
+ %{conn: conn, site: site} do
+ old_domain = site.domain
+ assert old_domain != "new.example.com"
+
+ conn =
+ put(conn, "/api/v1/sites/#{site.domain}", %{
+ "domain" => "new.example.com",
+ "tracker_script_configuration" => %{
+ "form_submissions" => true
+ }
+ })
+
+ response = json_response(conn, 200)
+
+ assert_matches ^strict_map(%{
+ "domain" => "new.example.com",
+ "timezone" => "UTC",
+ "custom_properties" => [],
+ "tracker_script_configuration" =>
+ ^strict_map(%{
+ "id" => ^any(:string),
+ "installation_type" => nil,
+ "track_404_pages" => false,
+ "hash_based_routing" => false,
+ "outbound_links" => false,
+ "file_downloads" => false,
+ "revenue_tracking" => false,
+ "tagged_events" => false,
+ "form_submissions" => true,
+ "pageview_props" => false
+ })
+ }) = response
+
+ site = Repo.reload!(site)
+
+ assert site.domain == "new.example.com"
+ assert site.domain_changed_from == old_domain
+ end
+
+ test "domain name change and tracker script configuration update are run as a transaction: if updating the configuration fails, the domain change is rolled back",
+ %{conn: conn, site: site} do
+ old_domain = site.domain
+ assert old_domain != "new.example.com"
+
+ conn =
+ put(conn, "/api/v1/sites/#{site.domain}", %{
+ "domain" => "new.example.com",
+ "tracker_script_configuration" => %{
+ "installation_type" => "an invalid value",
+ "form_submissions" => true
+ }
+ })
+
+ assert json_response(conn, 400) == %{
+ "error" => "tracker_script_configuration.installation_type: is invalid"
+ }
+
+ site = Repo.reload!(site)
+
+ assert site.domain == old_domain
+ assert site.domain_changed_from == nil
+ end
+
+ test "fails when team does not match team-scoped key", %{conn: conn, user: user, site: site} do
+ another_team = new_user() |> subscribe_to_business_plan() |> team_of()
+ add_member(another_team, user: user, role: :admin)
+ api_key = insert(:api_key, user: user, team: another_team, scopes: ["sites:provision:*"])
+ conn = Plug.Conn.put_req_header(conn, "authorization", "Bearer #{api_key.key}")
+
+ old_domain = site.domain
+ assert old_domain != "new.example.com"
+
+ conn =
+ put(conn, "/api/v1/sites/#{old_domain}", %{
+ "domain" => "new.example.com"
+ })
+
+ assert json_response(conn, 404) == %{
+ "error" => "Site could not be found"
+ }
+ end
+
+ test "fails when neither 'domain' nor 'tracker_script_configuration' is provided", %{
+ conn: conn,
+ site: site
+ } do
+ conn =
+ put(conn, "/api/v1/sites/#{site.domain}", %{"foo" => "bar"})
+
+ assert json_response(conn, 400) == %{
+ "error" =>
+ "Payload must contain at least one of the parameters 'domain', 'tracker_script_configuration'"
+ }
+ end
+
+ test "fails when domain parameter is invalid", %{conn: conn, site: site} do
+ conn = put(conn, "/api/v1/sites/#{site.domain}", %{"domain" => 123})
+
+ assert json_response(conn, 400) == %{
+ "error" => "domain: is invalid"
+ }
+ end
+
+ test "can't make a no-op domain change", %{conn: conn, site: site} do
+ conn =
+ put(conn, "/api/v1/sites/#{site.domain}", %{
+ "domain" => site.domain
+ })
+
+ assert json_response(conn, 400) == %{
+ "error" => "domain: New domain must be different than the current one"
+ }
+ end
+
+ test "fails when tracker script configuration parameter is invalid", %{
+ conn: conn,
+ site: site
+ } do
+ conn =
+ put(conn, "/api/v1/sites/#{site.domain}", %{
+ "tracker_script_configuration" => %{
+ "form_submissions" => "an invalid value"
+ }
+ })
+
+ assert json_response(conn, 400) == %{
+ "error" => "tracker_script_configuration.form_submissions: is invalid"
+ }
+ end
+ end
+ end
+end
diff --git a/test/plausible_web/controllers/api/external_sites_controller_test.exs b/test/plausible_web/controllers/api/external_sites_controller_test.exs
index 9523c22350..a987a086dd 100644
--- a/test/plausible_web/controllers/api/external_sites_controller_test.exs
+++ b/test/plausible_web/controllers/api/external_sites_controller_test.exs
@@ -115,7 +115,8 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest do
assert json_response(conn, 200) == %{
"domain" => "some-site.domain",
- "timezone" => "Europe/Tallinn"
+ "timezone" => "Europe/Tallinn",
+ "custom_properties" => []
}
end
@@ -152,7 +153,8 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest do
assert json_response(conn, 200) == %{
"domain" => "some-site.domain",
- "timezone" => "Europe/Tallinn"
+ "timezone" => "Europe/Tallinn",
+ "custom_properties" => []
}
assert Repo.get_by(Plausible.Site, domain: "some-site.domain").team_id == team.id
@@ -177,7 +179,8 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest do
assert json_response(conn, 200) == %{
"domain" => "some-site.domain",
- "timezone" => "Europe/Tallinn"
+ "timezone" => "Europe/Tallinn",
+ "custom_properties" => []
}
assert Repo.get_by(Plausible.Site, domain: "some-site.domain").team_id == another_team.id
@@ -203,7 +206,8 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest do
assert json_response(conn, 200) == %{
"domain" => "some-site.domain",
- "timezone" => "Etc/UTC"
+ "timezone" => "Etc/UTC",
+ "custom_properties" => []
}
end
@@ -1464,7 +1468,8 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest do
assert json_response(conn, 200) == %{
"domain" => "new.example.com",
- "timezone" => "UTC"
+ "timezone" => "UTC",
+ "custom_properties" => []
}
site = Repo.reload!(site)
@@ -1506,7 +1511,8 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest do
conn = put(conn, "/api/v1/sites/#{site.domain}", %{})
assert json_response(conn, 400) == %{
- "error" => "domain: can't be blank"
+ "error" =>
+ "Payload must contain at least one of the parameters 'domain', 'tracker_script_configuration'"
}
end
end
diff --git a/test/plausible_web/plugs/tracker_plug_test.exs b/test/plausible_web/plugs/tracker_plug_test.exs
index 33aeb7fa08..e315732936 100644
--- a/test/plausible_web/plugs/tracker_plug_test.exs
+++ b/test/plausible_web/plugs/tracker_plug_test.exs
@@ -36,7 +36,7 @@ defmodule PlausibleWeb.TrackerPlugTest do
site = new_site()
tracker_script_configuration =
- Tracker.update_script_configuration(
+ Tracker.update_script_configuration!(
site,
Map.put(@example_config, :site_id, site.id),
:installation
@@ -56,7 +56,7 @@ defmodule PlausibleWeb.TrackerPlugTest do
site = new_site()
tracker_script_configuration =
- Tracker.update_script_configuration(
+ Tracker.update_script_configuration!(
site,
Map.put(@example_config, :site_id, site.id),
:installation
diff --git a/test/plausible_web/tracker_test.exs b/test/plausible_web/tracker_test.exs
index 848cdcc9cb..d727d610f3 100644
--- a/test/plausible_web/tracker_test.exs
+++ b/test/plausible_web/tracker_test.exs
@@ -67,7 +67,7 @@ defmodule PlausibleWeb.TrackerTest do
assert tracker_script_configuration.installation_type == :manual
- Tracker.update_script_configuration(
+ Tracker.update_script_configuration!(
site,
%{installation_type: :wordpress, outbound_links: true},
:installation
@@ -86,7 +86,7 @@ defmodule PlausibleWeb.TrackerTest do
tracker_script_configuration =
Tracker.get_or_create_tracker_script_configuration!(site)
- Tracker.update_script_configuration(
+ Tracker.update_script_configuration!(
site,
%{installation_type: :wordpress, outbound_links: true},
:installation
@@ -104,7 +104,11 @@ defmodule PlausibleWeb.TrackerTest do
tracker_script_configuration =
Tracker.get_or_create_tracker_script_configuration!(site)
- Tracker.update_script_configuration(site, %{installation_type: :wordpress}, :installation)
+ Tracker.update_script_configuration!(
+ site,
+ %{installation_type: :wordpress},
+ :installation
+ )
refute_enqueued(
worker: Plausible.Workers.PurgeCDNCache,
@@ -132,7 +136,7 @@ defmodule PlausibleWeb.TrackerTest do
end
def create_config(site) do
- Tracker.update_script_configuration(
+ Tracker.update_script_configuration!(
site,
Map.put(@example_config, :site_id, site.id),
:installation