85 lines
2.9 KiB
Elixir
85 lines
2.9 KiB
Elixir
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
|
|
|
|
describe "list_all/0" do
|
|
test "returns every host ordered by name" do
|
|
{:ok, {_, _}} = Hosts.create_host("pve-02")
|
|
{:ok, {_, _}} = Hosts.create_host("pve-01")
|
|
names = Hosts.list_all() |> Enum.map(& &1.name)
|
|
assert names == ["pve-01", "pve-02"]
|
|
end
|
|
end
|
|
|
|
describe "delete_host/1" do
|
|
test "deletes the host row" do
|
|
{:ok, {host, _}} = Hosts.create_host("pve-01")
|
|
assert {:ok, _} = Hosts.delete_host(host)
|
|
assert Server.Repo.get(Server.Schema.Host, host.id) == nil
|
|
end
|
|
end
|
|
|
|
describe "rotate_token/1" do
|
|
test "replaces token_hash and returns new plaintext token" do
|
|
{:ok, {host, old_token}} = Hosts.create_host("pve-01")
|
|
assert {:ok, {updated, new_token}} = Hosts.rotate_token(host)
|
|
assert updated.id == host.id
|
|
refute updated.token_hash == host.token_hash
|
|
assert is_binary(new_token)
|
|
refute new_token == old_token
|
|
assert {:error, :invalid_token} = Hosts.authenticate("pve-01", old_token)
|
|
assert {:ok, _} = Hosts.authenticate("pve-01", new_token)
|
|
end
|
|
end
|
|
end
|