BREAKING CHANGE: the users:/config-based collection setup is gone. All
user, calendar, and address-book data now lives in the SQLite DB
(internal/db) and is managed exclusively via nidusctl or the web UI.
Existing deployments must recreate their users after upgrading:
nidusctl user create <username>
nidusctl calendar create <username> <name>
nidusctl addressbook create <username> <name>
- internal/db: new users, calendars, addressbooks tables with FK cascade
delete; foreign_keys pragma enabled; internal/db/users.go implements
full CRUD + bcrypt auth (CreateUser, VerifyPassword, ListUsers,
CreateCalendar/AddressBook, etc).
- internal/config: removed Users/UserConfig entirely.
- internal/auth: Basic Auth now checks credentials via db.DB instead of
cfg.Users.
- internal/caldav, internal/carddav: ListCalendars/ListAddressBooks and
Create/Delete now backed by the DB.
- internal/web: login uses db.VerifyPassword; new resources.go adds
create/delete handlers for calendars/address books at
/web/resources/{calendar,addressbook}; dashboard gained create forms
and per-card delete buttons (templ + htmx, no hyperscript).
- tools/nidusctl: new user create/delete/list/passwd commands (masked
interactive password prompt via golang.org/x/term) plus create/delete/
list subcommands for calendar/addressbook.
- cmd/server/main.go: pre-creates on-disk collections from the DB at
startup instead of cfg.Users; warns when no users exist yet.
- Updated tests to seed data via the DB; added resources_test.go for the
new web UI handlers.
- README.md and .github/copilot-instructions.md updated to document the
new nidusctl commands and the DB-backed architecture.
Verified end-to-end against a live test server: nidusctl user/calendar/
addressbook create, DAV Basic Auth PROPFIND, web login, dashboard
rendering, and web UI create/delete of resources all confirmed working.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
152 lines
4.3 KiB
Go
152 lines
4.3 KiB
Go
package caldav
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
ical "github.com/emersion/go-ical"
|
|
"github.com/yourusername/caldav-server/internal/auth"
|
|
"github.com/yourusername/caldav-server/internal/config"
|
|
"github.com/yourusername/caldav-server/internal/db"
|
|
"github.com/yourusername/caldav-server/internal/store"
|
|
)
|
|
|
|
func newTestBackend(t *testing.T) (*Backend, *db.DB) {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
|
|
st, err := store.NewStore(filepath.Join(dir, "data"))
|
|
if err != nil {
|
|
t.Fatalf("NewStore: %v", err)
|
|
}
|
|
dbase, err := db.Open(filepath.Join(dir, "test.db"))
|
|
if err != nil {
|
|
t.Fatalf("db.Open: %v", err)
|
|
}
|
|
t.Cleanup(func() { dbase.Close() })
|
|
|
|
cfg := &config.Config{}
|
|
if err := dbase.CreateUser("alice", "pw", "", ""); err != nil {
|
|
t.Fatalf("CreateUser alice: %v", err)
|
|
}
|
|
if err := dbase.CreateUser("bob", "pw", "", ""); err != nil {
|
|
t.Fatalf("CreateUser bob: %v", err)
|
|
}
|
|
if err := dbase.CreateCalendar("alice", "work"); err != nil {
|
|
t.Fatalf("CreateCalendar alice/work: %v", err)
|
|
}
|
|
if err := dbase.CreateCalendar("bob", "personal"); err != nil {
|
|
t.Fatalf("CreateCalendar bob/personal: %v", err)
|
|
}
|
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
|
return NewBackend(cfg, st, dbase, logger), dbase
|
|
}
|
|
|
|
func ctxFor(username string) context.Context {
|
|
return auth.NewContext(context.Background(), &auth.Principal{Username: username})
|
|
}
|
|
|
|
// minimalEvent returns a minimal, valid VCALENDAR/VEVENT for use in tests.
|
|
func minimalEvent() *ical.Calendar {
|
|
const raw = "BEGIN:VCALENDAR\r\n" +
|
|
"VERSION:2.0\r\n" +
|
|
"PRODID:-//nidus//test//EN\r\n" +
|
|
"BEGIN:VEVENT\r\n" +
|
|
"UID:event1@nidus.test\r\n" +
|
|
"DTSTAMP:20240101T000000Z\r\n" +
|
|
"DTSTART:20240101T100000Z\r\n" +
|
|
"SUMMARY:Test Event\r\n" +
|
|
"END:VEVENT\r\n" +
|
|
"END:VCALENDAR\r\n"
|
|
cal, err := ical.NewDecoder(strings.NewReader(raw)).Decode()
|
|
if err != nil {
|
|
panic(fmt.Sprintf("minimalEvent: %v", err))
|
|
}
|
|
return cal
|
|
}
|
|
|
|
func TestListCalendarsIncludesSharedCalendar(t *testing.T) {
|
|
b, dbase := newTestBackend(t)
|
|
|
|
if err := dbase.ShareCalendar("alice", "work", "bob", db.PermRead); err != nil {
|
|
t.Fatalf("ShareCalendar: %v", err)
|
|
}
|
|
// The shared calendar must exist on disk for it to be listed; normally
|
|
// this happens when alice's own ListCalendars runs and ensures it.
|
|
if _, err := b.ListCalendars(ctxFor("alice")); err != nil {
|
|
t.Fatalf("ListCalendars(alice): %v", err)
|
|
}
|
|
|
|
cals, err := b.ListCalendars(ctxFor("bob"))
|
|
if err != nil {
|
|
t.Fatalf("ListCalendars: %v", err)
|
|
}
|
|
|
|
var found bool
|
|
wantName := sharedCalendarName("alice", "work")
|
|
for _, c := range cals {
|
|
if c.Name == wantName {
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
t.Errorf("shared calendar %q not found in ListCalendars result: %+v", wantName, cals)
|
|
}
|
|
}
|
|
|
|
func TestSharedCalendarReadOnlyRejectsWrite(t *testing.T) {
|
|
b, dbase := newTestBackend(t)
|
|
|
|
if err := dbase.ShareCalendar("alice", "work", "bob", db.PermRead); err != nil {
|
|
t.Fatalf("ShareCalendar: %v", err)
|
|
}
|
|
|
|
localName := sharedCalendarName("alice", "work")
|
|
objPath := calObjectPath(localName, "event1.ics")
|
|
|
|
_, err := b.PutCalendarObject(ctxFor("bob"), objPath, minimalEvent(), nil)
|
|
if err == nil {
|
|
t.Fatal("expected error writing to read-only shared calendar, got nil")
|
|
}
|
|
}
|
|
|
|
func TestSharedCalendarWriteAllowed(t *testing.T) {
|
|
b, dbase := newTestBackend(t)
|
|
|
|
if err := dbase.ShareCalendar("alice", "work", "bob", db.PermWrite); err != nil {
|
|
t.Fatalf("ShareCalendar: %v", err)
|
|
}
|
|
|
|
localName := sharedCalendarName("alice", "work")
|
|
objPath := calObjectPath(localName, "event1.ics")
|
|
|
|
if _, err := b.PutCalendarObject(ctxFor("bob"), objPath, minimalEvent(), nil); err != nil {
|
|
t.Fatalf("PutCalendarObject with write share: %v", err)
|
|
}
|
|
|
|
// The object should now be visible under alice's own calendar too,
|
|
// since it's stored in her namespace.
|
|
obj, err := b.GetCalendarObject(ctxFor("alice"), calObjectPath("work", "event1.ics"), nil)
|
|
if err != nil {
|
|
t.Fatalf("GetCalendarObject as owner: %v", err)
|
|
}
|
|
if obj == nil {
|
|
t.Fatal("expected non-nil object")
|
|
}
|
|
}
|
|
|
|
func TestUnauthorizedUserCannotAccessUnsharedCalendar(t *testing.T) {
|
|
b, _ := newTestBackend(t)
|
|
|
|
localName := sharedCalendarName("alice", "work")
|
|
_, err := b.GetCalendar(ctxFor("bob"), calHomePath()+localName+"/")
|
|
if err == nil {
|
|
t.Fatal("expected error accessing unshared calendar, got nil")
|
|
}
|
|
}
|