Files
KlipperAnywhere/cmd/server/users_test.go
T
2026-05-14 02:27:26 +01:00

143 lines
4.3 KiB
Go

package main
import (
"path/filepath"
"testing"
)
func TestUserStoreBootstrapAndAuthenticate(t *testing.T) {
store, err := newUserStore(filepath.Join(t.TempDir(), "users.json"), "admin", "admin1234")
if err != nil {
t.Fatal(err)
}
users := store.listPublic()
if len(users) != 1 {
t.Fatalf("users = %d, want 1", len(users))
}
if users[0].Username != "admin" || users[0].Role != "admin" {
t.Fatalf("bootstrap user = %+v", users[0])
}
user, ok, err := store.authenticate("admin", "admin1234")
if err != nil {
t.Fatal(err)
}
if !ok || user.Username != "admin" {
t.Fatalf("auth failed: ok=%t user=%+v", ok, user)
}
}
func TestUserStoreCreateUpdateDelete(t *testing.T) {
store, err := newUserStore(filepath.Join(t.TempDir(), "users.json"), "admin", "admin1234")
if err != nil {
t.Fatal(err)
}
user, err := store.create("alice", "password1", "user", []printerGrant{{PrinterID: "real-printer", Access: "read"}})
if err != nil {
t.Fatal(err)
}
if user.Username != "alice" || user.Role != "user" {
t.Fatalf("created user = %+v", user)
}
updated, err := store.update(user.ID, "password2", "admin", []printerGrant{{PrinterID: "real-printer", Access: "control"}})
if err != nil {
t.Fatal(err)
}
if updated.Role != "admin" {
t.Fatalf("updated role = %q, want admin", updated.Role)
}
if len(updated.PrinterGrants) != 1 || updated.PrinterGrants[0].Access != "control" {
t.Fatalf("updated grants = %+v", updated.PrinterGrants)
}
if _, ok, err := store.authenticate("alice", "password2"); err != nil || !ok {
t.Fatalf("password update failed: ok=%t err=%v", ok, err)
}
if err := store.delete(user.ID, "admin-user"); err != nil {
t.Fatal(err)
}
if _, ok := store.getByID(user.ID); ok {
t.Fatal("expected user to be deleted")
}
}
func TestUserStoreLastAdminProtection(t *testing.T) {
store, err := newUserStore(filepath.Join(t.TempDir(), "users.json"), "admin", "admin1234")
if err != nil {
t.Fatal(err)
}
if err := store.delete(store.listPublic()[0].ID, "someone-else"); err == nil {
t.Fatal("expected last admin deletion to be blocked")
}
if _, err := store.update(store.listPublic()[0].ID, "", "user", nil); err == nil {
t.Fatal("expected last admin demotion to be blocked")
}
}
func TestCanAccessPrinter(t *testing.T) {
store, err := newUserStore(filepath.Join(t.TempDir(), "users.json"), "admin", "admin1234")
if err != nil {
t.Fatal(err)
}
user, err := store.create("bob", "password1", "user", []printerGrant{{PrinterID: "real-printer", Access: "read"}})
if err != nil {
t.Fatal(err)
}
if !store.canAccessPrinter(user.ID, "real-printer", false) {
t.Fatal("expected read access")
}
if store.canAccessPrinter(user.ID, "real-printer", true) {
t.Fatal("read-only grant should not allow control")
}
var adminID string
for _, item := range store.listPublic() {
if item.Role == "admin" {
adminID = item.ID
break
}
}
if adminID == "" {
t.Fatal("missing admin user")
}
if !store.canAccessPrinter(adminID, "any-printer", true) {
t.Fatal("admin should access all printers")
}
}
func TestNormalizePrinterGrantsStrict(t *testing.T) {
grants, err := normalizePrinterGrantsStrict([]printerGrant{
{PrinterID: "real-printer", Access: "read"},
{PrinterID: "spare-printer", Access: "control"},
})
if err != nil {
t.Fatal(err)
}
if len(grants) != 2 {
t.Fatalf("grants = %+v", grants)
}
if _, err := normalizePrinterGrantsStrict([]printerGrant{{PrinterID: "real-printer", Access: "execute"}}); err == nil {
t.Fatal("expected invalid access to fail")
}
}
func TestChangePassword(t *testing.T) {
store, err := newUserStore(filepath.Join(t.TempDir(), "users.json"), "admin", "admin1234")
if err != nil {
t.Fatal(err)
}
user, err := store.create("alice", "password1", "user", []printerGrant{{PrinterID: "real-printer", Access: "read"}})
if err != nil {
t.Fatal(err)
}
if err := store.changePassword(user.ID, "password1", "password2"); err != nil {
t.Fatal(err)
}
if _, ok, err := store.authenticate("alice", "password1"); err != nil || ok {
t.Fatalf("old password should fail: ok=%t err=%v", ok, err)
}
if _, ok, err := store.authenticate("alice", "password2"); err != nil || !ok {
t.Fatalf("new password should work: ok=%t err=%v", ok, err)
}
if err := store.changePassword(user.ID, "wrong", "password3"); err == nil {
t.Fatal("expected current password check to fail")
}
}