Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Delete a site through the API
Uku Taht7cfd9f95f142
diff --git a/lib/plausible_web/controllers/api/external_sites_controller.ex b/lib/plausible_web/controllers/api/external_sites_controller.exindex bfff82719d..388429c21a 100644--- a/lib/plausible_web/controllers/api/external_sites_controller.ex+++ b/lib/plausible_web/controllers/api/external_sites_controller.ex@@ -27,6 +27,17 @@ defmodule PlausibleWeb.Api.ExternalSitesController doendend+ def delete_site(conn, %{"site_id" => site_id}) do+ site = Sites.get_for_user(conn.assigns[:current_user].id, site_id, [:owner])++ if site do+ Sites.delete!(site)+ json(conn, %{"deleted" => true})+ else+ H.not_found(conn, "Site could not be found")+ end+ end+defp expect_param_key(params, key) docase Map.fetch(params, key) do:error -> {:missing, key}diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.exindex fbe432997b..b4457c53f3 100644--- a/lib/plausible_web/router.ex+++ b/lib/plausible_web/router.ex@@ -86,6 +86,7 @@ defmodule PlausibleWeb.Router dopipe_through [:public_api, PlausibleWeb.AuthorizeSitesApiPlug]post "/", ExternalSitesController, :create_site+ delete "/:site_id", ExternalSitesController, :delete_siteput "/shared-links", ExternalSitesController, :find_or_create_shared_linkenddiff --git a/test/plausible_web/controllers/api/external_sites_controller_test.exs b/test/plausible_web/controllers/api/external_sites_controller_test.exsindex 2f05fceb90..57b4b8c610 100644--- a/test/plausible_web/controllers/api/external_sites_controller_test.exs+++ b/test/plausible_web/controllers/api/external_sites_controller_test.exs@@ -77,6 +77,44 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest doendend+ describe "DELETE /api/v1/sites/:site_id" do+ setup :create_new_site++ test "delete a site by it's domain", %{conn: conn, site: site} do+ conn = delete(conn, "/api/v1/sites/" <> site.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++ test "cannot delete a site that the user does not own", %{conn: conn, user: user} do+ site = insert(:site, members: [])+ insert(:site_membership, user: user, site: site, role: :admin)+ 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 "PUT /api/v1/sites/shared-links" dosetup :create_site