Delete a site through the API

pull/1511/head
Uku Taht 5 years ago
parent 7da138e99e
commit 7cfd9f95f1

@ -27,6 +27,17 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
end
end
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) do
case Map.fetch(params, key) do
:error -> {:missing, key}

@ -86,6 +86,7 @@ defmodule PlausibleWeb.Router do
pipe_through [:public_api, PlausibleWeb.AuthorizeSitesApiPlug]
post "/", ExternalSitesController, :create_site
delete "/:site_id", ExternalSitesController, :delete_site
put "/shared-links", ExternalSitesController, :find_or_create_shared_link
end

@ -77,6 +77,44 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest do
end
end
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" do
setup :create_site

Loading…
Cancel
Save