Compare commits

...

3 Commits

Author SHA1 Message Date
Uku Taht 7d44ecd856 Disabled message wording 4 years ago
Uku Taht da5a3a2da8 Fix linter 4 years ago
Uku Taht 60bb92920c Temporarily disable site creation 4 years ago

@ -6,26 +6,33 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
alias Plausible.Goals alias Plausible.Goals
alias PlausibleWeb.Api.Helpers, as: H alias PlausibleWeb.Api.Helpers, as: H
def create_site(conn, params) do def create_site(conn, _params) do
user = conn.assigns[:current_user] # user = conn.assigns[:current_user]
case Sites.create(user, params) do conn
{:ok, %{site: site}} -> |> put_status(503)
json(conn, site) |> json(%{
error:
{:error, :limit, limit, _} -> "Creating sites is currently unavailable due to regular database maintenance. Please try again after 1 hour."
conn })
|> put_status(403)
|> json(%{ # case Sites.create(user, params) do
error: # {:ok, %{site: site}} ->
"Your account has reached the limit of #{limit} sites per account. Please contact hello@plausible.io to unlock more sites." # json(conn, site)
})
# {:error, :limit, limit, _} ->
{:error, _, changeset, _} -> # conn
conn # |> put_status(403)
|> put_status(400) # |> json(%{
|> json(serialize_errors(changeset)) # error:
end # "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 end
def get_site(conn, %{"site_id" => site_id}) do def get_site(conn, %{"site_id" => site_id}) do
@ -137,11 +144,11 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
end end
end end
defp serialize_errors(changeset) do # defp serialize_errors(changeset) do
{field, {msg, _opts}} = List.first(changeset.errors) # {field, {msg, _opts}} = List.first(changeset.errors)
error_msg = Atom.to_string(field) <> ": " <> msg # error_msg = Atom.to_string(field) <> ": " <> msg
%{"error" => error_msg} # %{"error" => error_msg}
end # end
def handle_errors(conn, %{kind: kind, reason: reason}) do def handle_errors(conn, %{kind: kind, reason: reason}) do
json(conn, %{error: Exception.format_banner(kind, reason)}) json(conn, %{error: Exception.format_banner(kind, reason)})

@ -2,6 +2,26 @@
<%= 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 -> %> <%= 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 -> %>
<h2 class="text-xl font-black dark:text-gray-100">Your website details</h2> <h2 class="text-xl font-black dark:text-gray-100">Your website details</h2>
<div class="rounded-md bg-yellow-50 dark:bg-transparent dark:border border-yellow-200 p-4 mt-4">
<div class="flex">
<div class="flex-shrink-0">
<svg class="h-5 w-5 text-yellow-400 dark:text-yellow-300" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z" clip-rule="evenodd" />
</svg>
</div>
<div class="ml-3">
<h3 class="text-sm font-medium text-yellow-800 dark:text-yellow-400">
Site creation disabled
</h3>
<div class="mt-2 text-sm text-yellow-700 dark:text-yellow-300">
<p>
Creating sites is currently unavailable due to regular database maintenance. Please try again after 1 hour.
</p>
</div>
</div>
</div>
</div>
<%= if @is_at_limit do %> <%= if @is_at_limit do %>
<div class="rounded-md bg-yellow-50 dark:bg-transparent dark:border border-yellow-200 p-4 mt-4"> <div class="rounded-md bg-yellow-50 dark:bg-transparent dark:border border-yellow-200 p-4 mt-4">
<div class="flex"> <div class="flex">
@ -78,7 +98,7 @@
if (option) { option.selected = "selected"} if (option) { option.selected = "selected"}
</script> </script>
<%= submit "Add snippet →", class: "button mt-4 w-full", disabled: @is_at_limit %> <%= submit "Add snippet →", class: "button mt-4 w-full", disabled: true %>
<% end %> <% end %>
<%= if @is_first_site do %> <%= if @is_first_site do %>

@ -9,90 +9,90 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest do
{:ok, user: user, api_key: api_key, conn: conn} {:ok, user: user, api_key: api_key, conn: conn}
end end
describe "POST /api/v1/sites" do # describe "POST /api/v1/sites" do
test "can create a site", %{conn: conn} do # test "can create a site", %{conn: conn} do
conn = # conn =
post(conn, "/api/v1/sites", %{ # post(conn, "/api/v1/sites", %{
"domain" => "some-site.domain", # "domain" => "some-site.domain",
"timezone" => "Europe/Tallinn" # "timezone" => "Europe/Tallinn"
}) # })
#
assert json_response(conn, 200) == %{ # assert json_response(conn, 200) == %{
"domain" => "some-site.domain", # "domain" => "some-site.domain",
"timezone" => "Europe/Tallinn" # "timezone" => "Europe/Tallinn"
} # }
end # end
#
test "timezone defaults to Etc/UTC", %{conn: conn} do # test "timezone defaults to Etc/UTC", %{conn: conn} do
conn = # conn =
post(conn, "/api/v1/sites", %{ # post(conn, "/api/v1/sites", %{
"domain" => "some-site.domain" # "domain" => "some-site.domain"
}) # })
#
assert json_response(conn, 200) == %{ # assert json_response(conn, 200) == %{
"domain" => "some-site.domain", # "domain" => "some-site.domain",
"timezone" => "Etc/UTC" # "timezone" => "Etc/UTC"
} # }
end # end
#
test "domain is required", %{conn: conn} do # test "domain is required", %{conn: conn} do
conn = post(conn, "/api/v1/sites", %{}) # conn = post(conn, "/api/v1/sites", %{})
#
assert json_response(conn, 400) == %{ # assert json_response(conn, 400) == %{
"error" => "domain: can't be blank" # "error" => "domain: can't be blank"
} # }
end # end
#
test "accepts international domain names", %{conn: conn} do # test "accepts international domain names", %{conn: conn} do
["müllers-café.test", "音乐.cn", "до.101домен.рф/pages"] # ["müllers-café.test", "音乐.cn", "до.101домен.рф/pages"]
|> Enum.each(fn idn_domain -> # |> Enum.each(fn idn_domain ->
conn = post(conn, "/api/v1/sites", %{"domain" => idn_domain}) # conn = post(conn, "/api/v1/sites", %{"domain" => idn_domain})
assert %{"domain" => ^idn_domain} = json_response(conn, 200) # assert %{"domain" => ^idn_domain} = json_response(conn, 200)
end) # end)
end # end
#
test "validates uri breaking domains", %{conn: conn} do # test "validates uri breaking domains", %{conn: conn} do
["quero:café.test", "h&llo.test", "iamnotsur&about?this.com"] # ["quero:café.test", "h&llo.test", "iamnotsur&about?this.com"]
|> Enum.each(fn bad_domain -> # |> Enum.each(fn bad_domain ->
conn = post(conn, "/api/v1/sites", %{"domain" => bad_domain}) # conn = post(conn, "/api/v1/sites", %{"domain" => bad_domain})
#
assert %{"error" => error} = json_response(conn, 400) # assert %{"error" => error} = json_response(conn, 400)
assert error =~ "domain: must not contain URI reserved characters" # assert error =~ "domain: must not contain URI reserved characters"
end) # end)
end # end
#
test "does not allow creating more sites than the limit", %{conn: conn, user: user} do # test "does not allow creating more sites than the limit", %{conn: conn, user: user} do
patch_env(:site_limit, 3) # patch_env(:site_limit, 3)
insert(:site, members: [user]) # insert(:site, members: [user])
insert(:site, members: [user]) # insert(:site, members: [user])
insert(:site, members: [user]) # insert(:site, members: [user])
#
conn = # conn =
post(conn, "/api/v1/sites", %{ # post(conn, "/api/v1/sites", %{
"domain" => "some-site.domain", # "domain" => "some-site.domain",
"timezone" => "Europe/Tallinn" # "timezone" => "Europe/Tallinn"
}) # })
#
assert json_response(conn, 403) == %{ # assert json_response(conn, 403) == %{
"error" => # "error" =>
"Your account has reached the limit of 3 sites per account. Please contact hello@plausible.io to unlock more sites." # "Your account has reached the limit of 3 sites per account. Please contact hello@plausible.io to unlock more sites."
} # }
end # end
#
test "cannot access with a bad API key scope", %{conn: conn, user: user} do # test "cannot access with a bad API key scope", %{conn: conn, user: user} do
api_key = insert(:api_key, user: user, scopes: ["stats:read:*"]) # api_key = insert(:api_key, user: user, scopes: ["stats:read:*"])
#
conn = # conn =
conn # conn
|> Plug.Conn.put_req_header("authorization", "Bearer #{api_key.key}") # |> Plug.Conn.put_req_header("authorization", "Bearer #{api_key.key}")
|> post("/api/v1/sites", %{"site" => %{"domain" => "domain.com"}}) # |> post("/api/v1/sites", %{"site" => %{"domain" => "domain.com"}})
#
assert json_response(conn, 401) == %{ # assert json_response(conn, 401) == %{
"error" => # "error" =>
"Invalid API key. Please make sure you're using a valid API key with access to the resource you've requested." # "Invalid API key. Please make sure you're using a valid API key with access to the resource you've requested."
} # }
end # end
end # end
describe "DELETE /api/v1/sites/:site_id" do describe "DELETE /api/v1/sites/:site_id" do
setup :create_new_site setup :create_new_site

Loading…
Cancel
Save