- feat(auth): add argon2id passwords, sessions, rate limiter, authorization and audit builders - feat(cli): add check and audit commands - feat(api): add api framework, login, page routes and account, user, delegation and audit handlers - test(auth): cover passwords, sessions, rate limiter, authorization and audit builders - test(api): cover the api framework, sessions, client origin, headers and cross-origin refusal - test(api): cover account, user, delegation and audit handlers and the authorization matrix - test(ui): cover login, logout, page routes and the page authorization matrix
99 lines
3.1 KiB
Go
99 lines
3.1 KiB
Go
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
// Copyright (C) 2026 Bryan Joshua Pedini
|
|
|
|
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"net/http"
|
|
"slices"
|
|
"testing"
|
|
)
|
|
|
|
func TestAuthorize(t *testing.T) {
|
|
admin := &principal{User: userRow{ID: 1, Role: roleAdmin}}
|
|
user := &principal{User: userRow{ID: 2, Role: roleUser}}
|
|
cases := []struct {
|
|
access string
|
|
p *principal
|
|
delegated bool
|
|
want accessDecision
|
|
}{
|
|
{accessNone, nil, false, accessAllowed},
|
|
{accessSession, nil, false, accessUnauthenticated},
|
|
{accessZone, nil, true, accessUnauthenticated},
|
|
{accessAdmin, nil, false, accessUnauthenticated},
|
|
{accessSession, user, false, accessAllowed},
|
|
{accessSession, admin, false, accessAllowed},
|
|
{accessAdmin, user, true, accessForbidden},
|
|
{accessAdmin, admin, false, accessAllowed},
|
|
{accessZone, user, false, accessNotFound},
|
|
{accessZone, user, true, accessAllowed},
|
|
{accessZone, admin, false, accessAllowed},
|
|
}
|
|
for i, c := range cases {
|
|
if got := authorize(c.access, c.p, c.delegated); got != c.want {
|
|
t.Errorf("case %d: %v", i, got)
|
|
}
|
|
}
|
|
if !needsDelegation(accessZone, *user) || needsDelegation(accessZone, *admin) || needsDelegation(accessSession, *user) || needsDelegation(accessAdmin, *user) {
|
|
t.Fatal("needsDelegation")
|
|
}
|
|
}
|
|
|
|
func TestVisibleZones(t *testing.T) {
|
|
zones := []string{"a.test.", "b.test.", "c.test."}
|
|
delegated := map[string]bool{"b.test.": true, "z.test.": true}
|
|
admin := principal{User: userRow{Role: roleAdmin}}
|
|
user := principal{User: userRow{Role: roleUser}}
|
|
if got := visibleZones(admin, zones, delegated); !slices.Equal(got, zones) {
|
|
t.Fatal(got)
|
|
}
|
|
if got := visibleZones(user, zones, delegated); !slices.Equal(got, []string{"b.test."}) {
|
|
t.Fatal(got)
|
|
}
|
|
if got := visibleZones(user, zones, nil); len(got) != 0 {
|
|
t.Fatal(got)
|
|
}
|
|
if canAccessZone(principal{User: userRow{Role: "other"}}, "b.test.", delegated) {
|
|
t.Fatal("unknown role")
|
|
}
|
|
}
|
|
|
|
func TestOwnAccountAndTargets(t *testing.T) {
|
|
p := principal{User: userRow{ID: 1, Username: "alice", Role: roleAdmin}}
|
|
if !isOwnAccount(p, "alice") || isOwnAccount(p, "bob") {
|
|
t.Fatal("own account")
|
|
}
|
|
if delegationTargetError(userRow{Role: roleUser}) != nil {
|
|
t.Fatal("user refused")
|
|
}
|
|
if e := delegationTargetError(userRow{Role: roleAdmin}); e == nil || e.Status != http.StatusUnprocessableEntity || e.Fields["username"] == "" {
|
|
t.Fatal(e)
|
|
}
|
|
if e := delegationTargetError(userRow{Role: roleUser, DeletedAt: sql.NullTime{Valid: true}}); e == nil || e.Status != http.StatusNotFound || e.Message != "user not found" {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
|
|
func TestLeavesNoActiveAdmin(t *testing.T) {
|
|
a := userRow{ID: 1, Role: roleAdmin}
|
|
b := userRow{ID: 2, Role: roleAdmin}
|
|
u := userRow{ID: 3, Role: roleUser}
|
|
if !leavesNoActiveAdmin(a, []userRow{a}) {
|
|
t.Fatal("last admin")
|
|
}
|
|
if leavesNoActiveAdmin(a, []userRow{a, b}) {
|
|
t.Fatal("two admins")
|
|
}
|
|
if leavesNoActiveAdmin(u, []userRow{a}) || leavesNoActiveAdmin(u, nil) {
|
|
t.Fatal("user target")
|
|
}
|
|
if leavesNoActiveAdmin(userRow{ID: 1, Role: roleAdmin, DeletedAt: sql.NullTime{Valid: true}}, nil) {
|
|
t.Fatal("deleted target")
|
|
}
|
|
if !leavesNoActiveAdmin(a, nil) {
|
|
t.Fatal("admin not in the locked list")
|
|
}
|
|
}
|