diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..ff79231 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +.git +admin/node_modules +internal/adminui/dist/* +!internal/adminui/dist/.gitkeep +parsimony +*.db +*.db-* diff --git a/Dockerfile b/Dockerfile index 76631fe..af1781b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,17 @@ +FROM node:24 AS admin +WORKDIR /src/admin +COPY admin/package.json admin/package-lock.json ./ +RUN npm ci +COPY admin/ ./ +# The build writes into ../internal/adminui/dist +RUN mkdir -p /src/internal/adminui/dist && npm run build + FROM golang:1.27 AS build WORKDIR /src COPY go.mod go.sum ./ RUN go mod download COPY . . +COPY --from=admin /src/internal/adminui/dist ./internal/adminui/dist RUN CGO_ENABLED=0 go build -o /parsimony ./cmd/parsimony FROM gcr.io/distroless/static-debian12 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4a3efd5 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +.PHONY: all admin test build clean + +all: admin build + +# Build the admin UI into internal/adminui/dist, which the Go binary embeds. +admin: + cd admin && npm ci && npm run build + +build: + go build -o parsimony ./cmd/parsimony + +test: + go test ./... + cd admin && npm test + +clean: + rm -f parsimony + node admin/scripts/clean.mjs diff --git a/README.md b/README.md index 2d989c8..795a466 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ A modern, single-binary mailing list manager: an evolution of Mailman. - Per-recipient one-click unsubscribe links (RFC 8058) with signed tokens. - SQLite storage, one YAML config file. - Admin REST API under `/api/v1` (see `docs/openapi.yaml`). +- Admin web UI (Vite, React, shadcn/ui) served by the API listener at `/`: lists, settings, + members, held queue and archive. ## Run @@ -40,6 +42,21 @@ the unsubscribe links in every message use it. Keep the `api` listener private. curl -H "Authorization: Bearer $PARSIMONY_API_TOKEN" -d '{"name":"dev","domain":"example.org","title":"Dev"}' localhost:8080/api/v1/lists ``` +## Admin UI + +The UI is a single-page app in `admin/`, built into `internal/adminui/dist` and embedded in the binary. +It is served on the API listener together with `/api`, so it needs no CORS, and only when +`PARSIMONY_API_TOKEN` is set. You sign in with that token (kept in `sessionStorage`, gone when the tab closes). + +``` +make admin # npm ci && npm run build +make build # go build, embeds the UI +make test # go test + vitest +``` + +Without `make admin` the binary still builds and the UI answers 503 with a hint. +For frontend work run the daemon, then `cd admin && npm run dev` (Vite proxies `/api` to `127.0.0.1:8080`). + ## Postfix Deliver the list domains to parsimony over LMTP and keep the `+` extensions intact, for example: diff --git a/admin/src/App.test.tsx b/admin/src/App.test.tsx index 3f0b236..d5df5e4 100644 --- a/admin/src/App.test.tsx +++ b/admin/src/App.test.tsx @@ -4,6 +4,36 @@ import { aList, installFakeApi, newState } from '@/test/fakeApi' import { renderApp } from '@/test/render' describe('authentication', () => { + it('does not keep the token when the check fails for another reason than 401', async () => { + installFakeApi(newState({ fail: { 'GET /lists': { status: 500, error: 'database is locked' } } })) + const { user } = renderApp('/login') + await user.type(await screen.findByLabelText(/api token/i), 'good') + await user.click(screen.getByRole('button', { name: /sign in/i })) + expect(await screen.findByText('database is locked')).toBeInTheDocument() + expect(sessionStorage.getItem('parsimony.token')).toBeNull() + }) + + it('drops cached data when the session ends', async () => { + const state = installFakeApi(newState({ lists: [aList()] })) + const { client } = renderApp('/', { token: 'good', keepCache: true }) + await screen.findByText('dev@example.org') + expect(client.getQueryData(['lists'])).toBeDefined() + state.token = 'rotated' + const { api } = await import('@/lib/api') + await api.lists.list().catch(() => undefined) + await screen.findByRole('heading', { name: /sign in/i }) + expect(client.getQueryData(['lists'])).toBeUndefined() + }) + + it('drops cached data on sign out', async () => { + installFakeApi(newState({ lists: [aList()] })) + const { user, client } = renderApp('/', { token: 'good', keepCache: true }) + await screen.findByText('dev@example.org') + await user.click(screen.getByRole('button', { name: /sign out/i })) + await screen.findByRole('heading', { name: /sign in/i }) + expect(client.getQueryData(['lists'])).toBeUndefined() + }) + it('sends anonymous visitors to the login page', async () => { installFakeApi(newState()) renderApp('/') diff --git a/admin/src/tabs/ArchiveTab.test.tsx b/admin/src/tabs/ArchiveTab.test.tsx index a5927b3..b255286 100644 --- a/admin/src/tabs/ArchiveTab.test.tsx +++ b/admin/src/tabs/ArchiveTab.test.tsx @@ -90,6 +90,9 @@ describe('archive tab', () => { await user.click(screen.getByRole('button', { name: /next/i })) expect(await screen.findByText('26–50 of 60')).toBeInTheDocument() + // The rows really are the second page, not just the label. + expect(await screen.findByRole('button', { name: 'Subject 26' })).toBeInTheDocument() + expect(screen.queryByRole('button', { name: 'Subject 1' })).not.toBeInTheDocument() expect(state.calls).toContain('GET /lists/1/archive') await user.click(screen.getByRole('button', { name: /next/i })) expect(await screen.findByText('51–60 of 60')).toBeInTheDocument() diff --git a/admin/src/tabs/HeldTab.test.tsx b/admin/src/tabs/HeldTab.test.tsx index 6758144..939a393 100644 --- a/admin/src/tabs/HeldTab.test.tsx +++ b/admin/src/tabs/HeldTab.test.tsx @@ -106,6 +106,15 @@ describe('held tab', () => { }) }) + it('marks the archive stale after an approval, because the message is archived on delivery', async () => { + const state = installFakeApi(newState({ lists: [aList({ id: 1, post_policy: 'moderated' })], held: { 1: [held(1)] } })) + const { user, client } = renderApp('/lists/1/held', { token: 'good', keepCache: true }) + client.setQueryData(['archive', 1, '', 0], { total: 0, messages: [] }) + await user.click(within((await screen.findByText('Subject 1')).closest('tr')!).getByRole('button', { name: /approve/i })) + await waitFor(() => expect(state.held[1]).toHaveLength(0)) + await waitFor(() => expect(client.getQueryState(['archive', 1, '', 0])?.isInvalidated).toBe(true)) + }) + it('updates the badge on the tab when the queue changes', async () => { const { user } = await setup([held(1), held(2)]) expect(await screen.findByRole('tab', { name: /^held\s*2$/i })).toBeInTheDocument() diff --git a/admin/src/tabs/MembersTab.test.tsx b/admin/src/tabs/MembersTab.test.tsx index baf5cb8..f69ca62 100644 --- a/admin/src/tabs/MembersTab.test.tsx +++ b/admin/src/tabs/MembersTab.test.tsx @@ -54,6 +54,14 @@ describe('members tab', () => { expect(await screen.findByText(/no members match/i)).toBeInTheDocument() }) + it('filters by display name too', async () => { + const { user } = await setup([member('x@example.com', { display_name: 'Zoe Quinn' }), member('y@example.com')]) + await screen.findByText('y@example.com') + await user.type(screen.getByRole('searchbox', { name: /filter members/i }), 'zoe') + expect(screen.getByText('x@example.com')).toBeInTheDocument() + expect(screen.queryByText('y@example.com')).not.toBeInTheDocument() + }) + it('adds a member and clears the form', async () => { const { user, state } = await setup([member('alice@example.com')]) await user.type(screen.getByLabelText(/email address/i), 'Dave@Example.com') diff --git a/admin/src/test/render.tsx b/admin/src/test/render.tsx index 103c765..e6f8949 100644 --- a/admin/src/test/render.tsx +++ b/admin/src/test/render.tsx @@ -7,9 +7,9 @@ import { AuthProvider } from '@/lib/auth' import { session } from '@/lib/api' /** Renders the whole app at `path`. Pass `token` to start logged in. */ -export function renderApp(path = '/', opts: { token?: string } = {}) { +export function renderApp(path = '/', opts: { token?: string; keepCache?: boolean } = {}) { if (opts.token) session.set(opts.token) - const client = new QueryClient({ defaultOptions: { queries: { retry: false, gcTime: 0 }, mutations: { retry: false } } }) + const client = new QueryClient({ defaultOptions: { queries: { retry: false, gcTime: opts.keepCache ? Infinity : 0 }, mutations: { retry: false } } }) const user = userEvent.setup() const utils = render(