|
|
|
|
@ -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
|
|
|
|
|
|