Files
nidus/tools/nidusctl/main_test.go
T
arnefandCopilot 58d74a29cd Add nidusctl admin CLI for managing calendar/address-book shares
- tools/nidusctl: subcommands `calendar {share,unshare,shares}` and
  `addressbook {share,unshare,shares}`, thin wrapper around internal/db.
  Warns (non-fatal) if owner/user isn't in config.yaml.
- internal/db: add SharesOfAddressBook (owner-perspective query, mirrors
  SharesOfCalendar) needed by the CLI.
- internal/db: db.Open now creates the parent data directory itself, so
  the CLI works standalone without requiring the server to have run first.
- Update Makefile (build bin/nidusctl, new 'nidusctl' target), README and
  copilot-instructions with usage docs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-08-18 21:01:28 +02:00

197 lines
5.6 KiB
Go

package main
import (
"bytes"
"errors"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yourusername/caldav-server/internal/db"
)
// writeTestConfig creates a minimal config.yaml in dir and returns its path.
func writeTestConfig(t *testing.T, dir string) string {
t.Helper()
cfgPath := filepath.Join(dir, "config.yaml")
dataDir := filepath.Join(dir, "data")
content := "storage:\n data_dir: " + dataDir + "\n" +
"users:\n" +
" alice:\n" +
" password: \"$2a$10$9.WEs0uz5TaNJLQbSTLrX.Te.BIe8XTTykVRzZbSHQMEkyFb8Sq/O\"\n" +
" bob:\n" +
" password: \"$2a$10$9.WEs0uz5TaNJLQbSTLrX.Te.BIe8XTTykVRzZbSHQMEkyFb8Sq/O\"\n"
if err := os.WriteFile(cfgPath, []byte(content), 0o644); err != nil {
t.Fatalf("writing test config: %v", err)
}
return cfgPath
}
// runCLI runs the CLI's run() function, capturing stdout/stderr, and
// returns (exit code, combined stdout+stderr).
func runCLI(t *testing.T, args ...string) (int, string) {
t.Helper()
oldStdout, oldStderr := os.Stdout, os.Stderr
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("os.Pipe: %v", err)
}
os.Stdout = w
os.Stderr = w
defer func() {
os.Stdout = oldStdout
os.Stderr = oldStderr
}()
code := run(args)
w.Close()
var buf bytes.Buffer
io.Copy(&buf, r)
return code, buf.String()
}
func TestCalendarShareUnshareLifecycle(t *testing.T) {
dir := t.TempDir()
cfgPath := writeTestConfig(t, dir)
code, out := runCLI(t, "-config", cfgPath, "calendar", "share", "alice", "work", "bob", "write")
if code != 0 {
t.Fatalf("share exit code = %d, output: %s", code, out)
}
if !strings.Contains(out, "shared") {
t.Errorf("output = %q, want to contain 'shared'", out)
}
code, out = runCLI(t, "-config", cfgPath, "calendar", "shares", "alice", "work")
if code != 0 {
t.Fatalf("shares exit code = %d, output: %s", code, out)
}
if !strings.Contains(out, "bob") || !strings.Contains(out, "write") {
t.Errorf("output = %q, want to contain bob/write", out)
}
code, out = runCLI(t, "-config", cfgPath, "calendar", "unshare", "alice", "work", "bob")
if code != 0 {
t.Fatalf("unshare exit code = %d, output: %s", code, out)
}
code, out = runCLI(t, "-config", cfgPath, "calendar", "shares", "alice", "work")
if code != 0 {
t.Fatalf("shares (after unshare) exit code = %d, output: %s", code, out)
}
if !strings.Contains(out, "not shared with anyone") {
t.Errorf("output = %q, want 'not shared with anyone'", out)
}
}
func TestCalendarShareInvalidPermission(t *testing.T) {
dir := t.TempDir()
cfgPath := writeTestConfig(t, dir)
code, out := runCLI(t, "-config", cfgPath, "calendar", "share", "alice", "work", "bob", "admin")
if code != 2 {
t.Errorf("exit code = %d, want 2; output: %s", code, out)
}
if !strings.Contains(out, "invalid permission") {
t.Errorf("output = %q, want 'invalid permission'", out)
}
}
func TestCalendarUnshareNotFound(t *testing.T) {
dir := t.TempDir()
cfgPath := writeTestConfig(t, dir)
code, out := runCLI(t, "-config", cfgPath, "calendar", "unshare", "alice", "ghost", "bob")
if code != 1 {
t.Errorf("exit code = %d, want 1; output: %s", code, out)
}
if !strings.Contains(out, "not found") {
t.Errorf("output = %q, want 'not found'", out)
}
}
func TestAddressBookShareUnshareLifecycle(t *testing.T) {
dir := t.TempDir()
cfgPath := writeTestConfig(t, dir)
code, out := runCLI(t, "-config", cfgPath, "addressbook", "share", "alice", "contacts", "bob", "read")
if code != 0 {
t.Fatalf("share exit code = %d, output: %s", code, out)
}
code, out = runCLI(t, "-config", cfgPath, "addressbook", "shares", "alice", "contacts")
if code != 0 {
t.Fatalf("shares exit code = %d, output: %s", code, out)
}
if !strings.Contains(out, "bob") || !strings.Contains(out, "read") {
t.Errorf("output = %q, want to contain bob/read", out)
}
code, out = runCLI(t, "-config", cfgPath, "addressbook", "unshare", "alice", "contacts", "bob")
if code != 0 {
t.Fatalf("unshare exit code = %d, output: %s", code, out)
}
}
func TestUnknownUserWarningDoesNotBlockShare(t *testing.T) {
dir := t.TempDir()
cfgPath := writeTestConfig(t, dir)
code, out := runCLI(t, "-config", cfgPath, "calendar", "share", "alice", "work", "carol", "read")
if code != 0 {
t.Fatalf("exit code = %d, output: %s", code, out)
}
if !strings.Contains(out, "warning") || !strings.Contains(out, "carol") {
t.Errorf("output = %q, want a warning about unknown user carol", out)
}
if !strings.Contains(out, "shared") {
t.Errorf("output = %q, want the share to still succeed", out)
}
}
func TestNoArgsShowsUsage(t *testing.T) {
dir := t.TempDir()
// No config needed since usage() is printed before config.Load for
// missing subcommands.
code, out := runCLI(t, "-config", filepath.Join(dir, "missing.yaml"))
if code != 2 {
t.Errorf("exit code = %d, want 2", code)
}
if !strings.Contains(out, "Usage") {
t.Errorf("output = %q, want usage text", out)
}
}
func TestUnknownCommand(t *testing.T) {
dir := t.TempDir()
cfgPath := writeTestConfig(t, dir)
code, out := runCLI(t, "-config", cfgPath, "bogus")
if code != 2 {
t.Errorf("exit code = %d, want 2", code)
}
if !strings.Contains(out, "unknown command") {
t.Errorf("output = %q, want 'unknown command'", out)
}
}
// Sanity check that the CLI and internal/db agree on ErrShareNotFound
// being surfaced (not swallowed) through the exit code / error message.
func TestUnshareErrorIsShareNotFound(t *testing.T) {
dir := t.TempDir()
dbase, err := db.Open(filepath.Join(dir, "test.db"))
if err != nil {
t.Fatalf("db.Open: %v", err)
}
defer dbase.Close()
err = dbase.UnshareCalendar("alice", "work", "bob")
if !errors.Is(err, db.ErrShareNotFound) {
t.Errorf("err = %v, want ErrShareNotFound", err)
}
}