From 70a85c3f4c2b1e5abecea78dde3e65281c83401f Mon Sep 17 00:00:00 2001 From: hq1 Date: Tue, 4 Apr 2023 08:23:31 +0200 Subject: [PATCH] Revert "Temporarily disable site creation (#2813)" (#2817) This reverts commit b626329ec570899df7d1cfcacf3f1073f010dcdf. --- .../api/external_sites_controller.ex | 57 +++--- lib/plausible_web/templates/site/new.html.eex | 22 +-- .../api/external_sites_controller_test.exs | 168 +++++++++--------- 3 files changed, 110 insertions(+), 137 deletions(-) diff --git a/lib/plausible_web/controllers/api/external_sites_controller.ex b/lib/plausible_web/controllers/api/external_sites_controller.ex index 78ba4b783..cbb1eac57 100644 --- a/lib/plausible_web/controllers/api/external_sites_controller.ex +++ b/lib/plausible_web/controllers/api/external_sites_controller.ex @@ -6,33 +6,26 @@ defmodule PlausibleWeb.Api.ExternalSitesController do alias Plausible.Goals alias PlausibleWeb.Api.Helpers, as: H - def create_site(conn, _params) do - # user = conn.assigns[:current_user] - - conn - |> put_status(503) - |> json(%{ - error: - "Creating sites is currently unavailable due to regular database maintenance. Please try again after 1 hour." - }) - - # case Sites.create(user, params) do - # {:ok, %{site: site}} -> - # json(conn, site) - - # {:error, :limit, limit, _} -> - # conn - # |> put_status(403) - # |> json(%{ - # error: - # "Your account has reached the limit of #{limit} sites per account. Please contact hello@plausible.io to unlock more sites." - # }) - - # {:error, _, changeset, _} -> - # conn - # |> put_status(400) - # |> json(serialize_errors(changeset)) - # end + def create_site(conn, params) do + user = conn.assigns[:current_user] + + case Sites.create(user, params) do + {:ok, %{site: site}} -> + json(conn, site) + + {:error, :limit, limit, _} -> + conn + |> put_status(403) + |> json(%{ + error: + "Your account has reached the limit of #{limit} sites per account. Please contact hello@plausible.io to unlock more sites." + }) + + {:error, _, changeset, _} -> + conn + |> put_status(400) + |> json(serialize_errors(changeset)) + end end def get_site(conn, %{"site_id" => site_id}) do @@ -144,11 +137,11 @@ defmodule PlausibleWeb.Api.ExternalSitesController do end end - # defp serialize_errors(changeset) do - # {field, {msg, _opts}} = List.first(changeset.errors) - # error_msg = Atom.to_string(field) <> ": " <> msg - # %{"error" => error_msg} - # end + defp serialize_errors(changeset) do + {field, {msg, _opts}} = List.first(changeset.errors) + error_msg = Atom.to_string(field) <> ": " <> msg + %{"error" => error_msg} + end def handle_errors(conn, %{kind: kind, reason: reason}) do json(conn, %{error: Exception.format_banner(kind, reason)}) diff --git a/lib/plausible_web/templates/site/new.html.eex b/lib/plausible_web/templates/site/new.html.eex index eea3ad20e..14cf241f9 100644 --- a/lib/plausible_web/templates/site/new.html.eex +++ b/lib/plausible_web/templates/site/new.html.eex @@ -2,26 +2,6 @@ <%= form_for @changeset, "/sites", [class: "max-w-lg w-full mx-auto bg-white dark:bg-gray-800 shadow-lg rounded px-8 pt-6 pb-8 mb-4 mt-8"], fn f -> %>

Your website details

-
-
-
- -
-
-

- Site creation disabled -

-
-

- Creating sites is currently unavailable due to regular database maintenance. Please try again after 1 hour. -

-
-
-
-
- <%= if @is_at_limit do %>
@@ -98,7 +78,7 @@ if (option) { option.selected = "selected"} - <%= submit "Add snippet →", class: "button mt-4 w-full", disabled: true %> + <%= submit "Add snippet →", class: "button mt-4 w-full", disabled: @is_at_limit %> <% end %> <%= if @is_first_site do %> 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 155e5b411..25e5b15ea 100644 --- a/test/plausible_web/controllers/api/external_sites_controller_test.exs +++ b/test/plausible_web/controllers/api/external_sites_controller_test.exs @@ -9,90 +9,90 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest do {:ok, user: user, 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" - # }) - # - # assert json_response(conn, 200) == %{ - # "domain" => "some-site.domain", - # "timezone" => "Europe/Tallinn" - # } - # end - # - # test "timezone defaults to Etc/UTC", %{conn: conn} do - # conn = - # post(conn, "/api/v1/sites", %{ - # "domain" => "some-site.domain" - # }) - # - # assert json_response(conn, 200) == %{ - # "domain" => "some-site.domain", - # "timezone" => "Etc/UTC" - # } - # 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 "does not allow creating more sites than the limit", %{conn: conn, user: user} do - # patch_env(:site_limit, 3) - # insert(:site, members: [user]) - # insert(:site, members: [user]) - # insert(:site, members: [user]) - # - # conn = - # post(conn, "/api/v1/sites", %{ - # "domain" => "some-site.domain", - # "timezone" => "Europe/Tallinn" - # }) - # - # assert json_response(conn, 403) == %{ - # "error" => - # "Your account has reached the limit of 3 sites per account. Please contact hello@plausible.io to unlock more sites." - # } - # 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 "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" + }) + + assert json_response(conn, 200) == %{ + "domain" => "some-site.domain", + "timezone" => "Europe/Tallinn" + } + end + + test "timezone defaults to Etc/UTC", %{conn: conn} do + conn = + post(conn, "/api/v1/sites", %{ + "domain" => "some-site.domain" + }) + + assert json_response(conn, 200) == %{ + "domain" => "some-site.domain", + "timezone" => "Etc/UTC" + } + 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 "does not allow creating more sites than the limit", %{conn: conn, user: user} do + patch_env(:site_limit, 3) + insert(:site, members: [user]) + insert(:site, members: [user]) + insert(:site, members: [user]) + + conn = + post(conn, "/api/v1/sites", %{ + "domain" => "some-site.domain", + "timezone" => "Europe/Tallinn" + }) + + assert json_response(conn, 403) == %{ + "error" => + "Your account has reached the limit of 3 sites per account. Please contact hello@plausible.io to unlock more sites." + } + 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_new_site