2023-09-26 14:13:08 +03:00
|
|
|
defmodule PlausibleWeb.Plugs.AuthorizePluginsAPITest do
|
|
|
|
use PlausibleWeb.ConnCase, async: true
|
|
|
|
|
2023-10-18 15:14:30 +03:00
|
|
|
alias Plausible.Plugins.API.{Token, Tokens}
|
2023-09-26 14:13:08 +03:00
|
|
|
alias PlausibleWeb.Plugs.AuthorizePluginsAPI
|
2023-10-18 15:14:30 +03:00
|
|
|
alias Plausible.Repo
|
2023-09-26 14:13:08 +03:00
|
|
|
|
|
|
|
import Plug.Conn
|
|
|
|
|
|
|
|
test "plug passes when a token is found" do
|
|
|
|
%{id: site_id} = site = insert(:site, domain: "pass.example.com")
|
|
|
|
{:ok, _, raw} = Tokens.create(site, "Some token")
|
|
|
|
|
|
|
|
credentials = "Basic " <> Base.encode64("#{site.domain}:#{raw}")
|
|
|
|
|
|
|
|
conn =
|
2023-10-16 14:22:09 +03:00
|
|
|
build_conn()
|
|
|
|
|> put_req_header("authorization", credentials)
|
|
|
|
|> AuthorizePluginsAPI.call()
|
|
|
|
|
|
|
|
refute conn.halted
|
|
|
|
assert %Plausible.Site{id: ^site_id} = conn.assigns.authorized_site
|
|
|
|
end
|
|
|
|
|
|
|
|
test "plug passes when a token is found, no domain provided" do
|
|
|
|
%{id: site_id} = site = insert(:site, domain: "pass.example.com")
|
|
|
|
{:ok, _, raw} = Tokens.create(site, "Some token")
|
|
|
|
|
|
|
|
credentials = "Basic " <> Base.encode64(raw)
|
|
|
|
|
|
|
|
conn =
|
2023-09-26 14:13:08 +03:00
|
|
|
build_conn()
|
|
|
|
|> put_req_header("authorization", credentials)
|
|
|
|
|> AuthorizePluginsAPI.call()
|
|
|
|
|
|
|
|
refute conn.halted
|
|
|
|
assert %Plausible.Site{id: ^site_id} = conn.assigns.authorized_site
|
|
|
|
end
|
|
|
|
|
|
|
|
test "plug halts when a token is not found" do
|
|
|
|
site = insert(:site, domain: "pass.example.com")
|
|
|
|
|
|
|
|
credentials = "Basic " <> Base.encode64("#{site.domain}:invalid-token")
|
|
|
|
|
|
|
|
conn =
|
|
|
|
build_conn()
|
|
|
|
|> put_req_header("authorization", credentials)
|
|
|
|
|> AuthorizePluginsAPI.call()
|
|
|
|
|
|
|
|
assert conn.halted
|
|
|
|
|
|
|
|
assert json_response(conn, 401) == %{
|
|
|
|
"errors" => [
|
|
|
|
%{"detail" => "Plugins API: unauthorized"}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "plug halts when no authorization header is passed" do
|
|
|
|
conn =
|
|
|
|
build_conn()
|
|
|
|
|> AuthorizePluginsAPI.call()
|
|
|
|
|
|
|
|
assert conn.halted
|
|
|
|
|
|
|
|
assert json_response(conn, 401) == %{
|
|
|
|
"errors" => [
|
|
|
|
%{"detail" => "Plugins API: unauthorized"}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
2023-10-18 15:14:30 +03:00
|
|
|
|
2024-02-21 14:41:56 +03:00
|
|
|
test "plug optionally doesn't halt when no authorization header is passed" do
|
|
|
|
conn =
|
|
|
|
build_conn()
|
|
|
|
|> AuthorizePluginsAPI.call(send_error?: false)
|
|
|
|
|
|
|
|
refute conn.halted
|
|
|
|
end
|
|
|
|
|
2023-10-18 15:14:30 +03:00
|
|
|
test "plug updates last seen timestamp" do
|
|
|
|
site = insert(:site, domain: "pass.example.com")
|
|
|
|
{:ok, token, raw} = Tokens.create(site, "Some token")
|
|
|
|
|
|
|
|
refute token.last_used_at
|
|
|
|
assert Token.last_used_humanize(token) == "Not yet"
|
|
|
|
|
|
|
|
credentials = "Basic " <> Base.encode64(raw)
|
|
|
|
|
|
|
|
build_conn()
|
|
|
|
|> put_req_header("authorization", credentials)
|
|
|
|
|> AuthorizePluginsAPI.call()
|
|
|
|
|
|
|
|
token = Repo.reload!(token)
|
|
|
|
assert token.last_used_at
|
|
|
|
assert Token.last_used_humanize(token) == "Just recently"
|
|
|
|
end
|
2023-09-26 14:13:08 +03:00
|
|
|
end
|