2019-09-02 14:29:19 +03:00
|
|
|
defmodule PlausibleWeb.Api.InternalControllerTest do
|
2022-12-26 16:20:29 +03:00
|
|
|
use PlausibleWeb.ConnCase, async: true
|
2019-09-02 14:29:19 +03:00
|
|
|
use Plausible.Repo
|
|
|
|
|
2020-08-13 13:45:18 +03:00
|
|
|
describe "GET /api/:domain/status" do
|
2020-05-22 12:33:17 +03:00
|
|
|
setup [:create_user, :log_in]
|
2019-09-02 14:29:19 +03:00
|
|
|
|
2020-05-22 12:33:17 +03:00
|
|
|
test "is WAITING when site has no pageviews", %{conn: conn, user: user} do
|
|
|
|
site = insert(:site, members: [user])
|
2019-09-02 14:29:19 +03:00
|
|
|
conn = get(conn, "/api/#{site.domain}/status")
|
|
|
|
|
|
|
|
assert json_response(conn, 200) == "WAITING"
|
|
|
|
end
|
|
|
|
|
2020-05-22 12:33:17 +03:00
|
|
|
test "is READY when site has at least 1 pageview", %{conn: conn, user: user} do
|
|
|
|
site = insert(:site, members: [user])
|
2023-03-27 14:52:42 +03:00
|
|
|
Plausible.TestUtils.create_pageviews([%{site: site}])
|
2019-09-02 14:29:19 +03:00
|
|
|
|
|
|
|
conn = get(conn, "/api/#{site.domain}/status")
|
|
|
|
|
|
|
|
assert json_response(conn, 200) == "READY"
|
|
|
|
end
|
|
|
|
end
|
2020-08-13 13:45:18 +03:00
|
|
|
|
2021-03-03 12:36:19 +03:00
|
|
|
describe "GET /api/sites" do
|
2020-08-13 13:45:18 +03:00
|
|
|
setup [:create_user, :log_in]
|
|
|
|
|
|
|
|
test "returns a list of site domains for the current user", %{conn: conn, user: user} do
|
|
|
|
site = insert(:site, members: [user])
|
|
|
|
site2 = insert(:site, members: [user])
|
|
|
|
conn = get(conn, "/api/sites")
|
|
|
|
|
2022-04-18 12:32:01 +03:00
|
|
|
%{"data" => sites} = json_response(conn, 200)
|
|
|
|
|
|
|
|
assert Enum.map(sites, & &1["domain"]) == [site.domain, site2.domain]
|
2020-08-13 13:45:18 +03:00
|
|
|
end
|
|
|
|
end
|
2021-03-03 12:36:19 +03:00
|
|
|
|
|
|
|
describe "GET /api/sites - user not logged in" do
|
|
|
|
test "returns 401 unauthorized", %{conn: conn} do
|
|
|
|
conn = get(conn, "/api/sites")
|
|
|
|
|
|
|
|
assert json_response(conn, 401) == %{
|
|
|
|
"error" => "You need to be logged in to request a list of sites"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|