feat(server): host schema, context, auth, status transitions

This commit is contained in:
Carsten 2026-04-21 22:02:24 +02:00
parent bab31b7c4e
commit b141ee7816
5 changed files with 174 additions and 0 deletions

View file

@ -0,0 +1,55 @@
defmodule Server.HostsTest do
use Server.DataCase, async: true
alias Server.Hosts
describe "create_host/1" do
test "returns host and a plaintext token on success" do
assert {:ok, {host, token}} = Hosts.create_host("pve-01")
assert host.name == "pve-01"
assert host.status == "never_connected"
assert is_binary(token) and byte_size(token) >= 32
refute host.token_hash == token
end
test "rejects duplicate names" do
{:ok, _} = Hosts.create_host("pve-01")
assert {:error, changeset} = Hosts.create_host("pve-01")
assert %{name: ["has already been taken"]} = errors_on(changeset)
end
end
describe "authenticate/2" do
test "returns host for valid name+token" do
{:ok, {host, token}} = Hosts.create_host("pve-01")
assert {:ok, found} = Hosts.authenticate("pve-01", token)
assert found.id == host.id
end
test "returns :invalid_token for wrong token" do
{:ok, {_host, _token}} = Hosts.create_host("pve-01")
assert {:error, :invalid_token} = Hosts.authenticate("pve-01", "wrong")
end
test "returns :unknown_host when name does not exist" do
assert {:error, :unknown_host} = Hosts.authenticate("nope", "whatever")
end
end
describe "mark_online/2 and mark_offline/1" do
test "mark_online stamps status, last_seen_at, agent_version" do
{:ok, {host, _}} = Hosts.create_host("pve-01")
assert {:ok, updated} = Hosts.mark_online(host, "0.1.0")
assert updated.status == "online"
assert updated.agent_version == "0.1.0"
assert updated.last_seen_at != nil
end
test "mark_offline sets status to offline" do
{:ok, {host, _}} = Hosts.create_host("pve-01")
{:ok, online} = Hosts.mark_online(host, "0.1.0")
assert {:ok, offline} = Hosts.mark_offline(online)
assert offline.status == "offline"
end
end
end