From 20ddf28dba90c6916fa176c787ceed21912b7e38 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad <230291+adeelahmad@users.noreply.github.com> Date: Thu, 5 Nov 2026 18:39:10 +1100 Subject: [PATCH 1/2] fix(settings): restore chat credential fields --- changelog.d/fix-chat-settings-secrets.md | 3 + docs/chat-rooms.md | 8 +- .../settings-chat-credentials.test.tsx | 129 ++++++++++++++++++ nextjs-frontend/components/settings/model.ts | 4 + 4 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 changelog.d/fix-chat-settings-secrets.md create mode 100644 nextjs-frontend/__tests__/settings-chat-credentials.test.tsx diff --git a/changelog.d/fix-chat-settings-secrets.md b/changelog.d/fix-chat-settings-secrets.md new file mode 100644 index 000000000..34abc1cd6 --- /dev/null +++ b/changelog.d/fix-chat-settings-secrets.md @@ -0,0 +1,3 @@ +Fixed a crash when opening Telegram, Slack, or Assistant email settings. Their credential fields are now registered +with the shared form, so tokens and the mailbox password can be saved, replaced, or cleared without exposing stored +values. Cancel and Undo preserve the saved credentials. diff --git a/docs/chat-rooms.md b/docs/chat-rooms.md index d8c69fa3c..b32bc649f 100644 --- a/docs/chat-rooms.md +++ b/docs/chat-rooms.md @@ -9,7 +9,7 @@ Lens's assistant can answer in Slack, Discord, Telegram, Matrix, Mattermost, IRC Telegram needs no Matterbridge and no public address: Lens reads its own bot's messages (long polling) and answers. 1. In Telegram, message **@BotFather**, send `/newbot` and copy the token it gives you. -2. Open **Settings → Telegram**, paste the token and turn on **Answer in Telegram**. +2. Open **Settings → Telegram**, paste the token, turn on **Answer in Telegram**, and click **Save**. 3. Message your bot. You show on the settings page under **Wrote to the bot, not let in**: click **Let in**. Anyone can find a bot, so it answers only the people in **People it talks to** (Telegram user ids or usernames); @@ -23,6 +23,9 @@ namespace's assistant with **Chats for a namespace's assistant** (`-100123456789 Give Lens a bot of its own: a bot that Matterbridge also reads answers neither reliably (**Check the bot** says so). +The bot token is write-only: reopening Settings shows whether it is set, never its value. Use **Replace** to change +it or **Clear** to remove it, then **Save**. **Cancel** and **Undo** leave the saved token unchanged. + Telegram is one adapter of the chat-channel layer (`app/domain/channels.py`) that Matterbridge is another of; a new chat app adds an adapter (reading, sending, files) and gets everything in a room for free. The assistant's own email address is one too ([The assistant by email](assistant-email.md)). @@ -37,7 +40,8 @@ and answers there. 2. In the app's page, **Install to Workspace**, then copy the **Bot User OAuth Token** (`xoxb-…`, under OAuth & Permissions). Under **Basic Information → App-Level Tokens**, generate one with `connections:write` and copy it (`xapp-…`). -3. Paste both tokens in Settings → Slack and turn on **Answer in Slack**. **Check the app** says whether both work. +3. Paste both tokens in Settings → Slack, turn on **Answer in Slack**, and click **Save**. **Check the app** says + whether both saved tokens work. Message the app directly (always for Lens), invite it to a channel with `/invite @Lens` and say "Lens, …" or @mention it, or type `/lens what did we decide about the launch?` anywhere (the answer shows in that channel). diff --git a/nextjs-frontend/__tests__/settings-chat-credentials.test.tsx b/nextjs-frontend/__tests__/settings-chat-credentials.test.tsx new file mode 100644 index 000000000..615ae78ad --- /dev/null +++ b/nextjs-frontend/__tests__/settings-chat-credentials.test.tsx @@ -0,0 +1,129 @@ +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { fireEvent, render, screen, waitFor } from "@testing-library/react"; +import "@testing-library/jest-dom"; + +import { Admin } from "@/app/openapi-client"; +import { fieldId, fieldsOf, toUi, type SectionId, type SettingsView } from "@/components/settings/model"; +import { SettingsSection } from "@/components/settings/section"; +import { TooltipProvider } from "@/components/ui/tooltip"; + +jest.mock("@/app/openapi-client", () => ({ + Admin: { + updateSettings: jest.fn(), + getSecrets: jest.fn(), + telegramStatus: jest.fn(), + slackStatus: jest.fn(), + mailboxStatus: jest.fn(), + }, + Metadata: {}, +})); +jest.mock("next-auth/react", () => ({ useSession: () => ({ data: { accessToken: "t" } }) })); +jest.mock("next/navigation", () => ({ useRouter: () => ({ push: jest.fn() }) })); +jest.mock("@/lib/hooks/session", () => ({ useArchive: () => ({ namespaces: [] }) })); +jest.mock("@/components/ui/toast", () => ({ useToast: () => jest.fn() })); + +const ok = (data: unknown) => Promise.resolve({ data, response: { ok: true, status: 200 } }); +const CREDENTIALS: [SectionId, string, string][] = [ + ["telegram", "token", "Bot token"], + ["slack", "bot_token", "Bot token (xoxb-…, from OAuth & Permissions)"], + ["slack", "app_token", "App-level token (xapp-…, from Basic Information, with connections:write)"], + ["mailbox", "password", "Password"], +]; + +function view(section: SectionId, key: string, saved: boolean): SettingsView { + return { + [section]: { + values: { + enabled: false, + account: null, + name: "Lens", + answer: "mention", + users: [], + rooms: [], + approve: "low_risk", + api_url: null, + token: { secret: true, set: false }, + bot_token: { secret: true, set: false }, + app_token: { secret: true, set: false }, + address: null, + password: { secret: true, set: false }, + username: null, + imap_host: null, + imap_port: null, + imap_security: "ssl", + smtp_host: null, + smtp_port: null, + smtp_security: "starttls", + folder: "INBOX", + routes: [], + imports: "auto", + check: "authenticated", + auth_server: null, + poll_seconds: 30, + [key]: { secret: true, set: saved }, + }, + }, + } as SettingsView; +} + +function show(section: SectionId, key: string, saved = false) { + const qc = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } }); + return render( + + + undefined} /> + + , + ); +} + +beforeEach(() => { + (Admin.updateSettings as jest.Mock).mockReset().mockImplementation(() => ok({ ok: true })); + (Admin.getSecrets as jest.Mock).mockReset().mockImplementation(() => ok({ providers: [], references: [], fields: [] })); + for (const status of [Admin.telegramStatus, Admin.slackStatus, Admin.mailboxStatus]) + (status as jest.Mock).mockReset().mockImplementation(() => ok({ state: "off", people: [], chats: [] })); +}); + +describe.each(CREDENTIALS)("%s.%s", (section, key, label) => { + it("registers the custom credential control as a write-only field", () => { + const field = fieldsOf(section).find((f) => fieldId(f) === `${section}.${key}`); + expect(field).toMatchObject({ section, key, kind: "secret" }); + expect(toUi(field!, { secret: true, set: true })).toBeUndefined(); + }); + + it("opens without crashing and saves a new credential", async () => { + show(section, key); + const input = screen.getByLabelText(label); + expect(input).toHaveAttribute("type", "password"); + expect(input).toHaveValue(""); + expect(screen.queryByRole("button", { name: "Save" })).not.toBeInTheDocument(); + fireEvent.change(input, { target: { value: "test-credential" } }); + fireEvent.click(screen.getByRole("button", { name: "Save" })); + await waitFor(() => expect(Admin.updateSettings).toHaveBeenCalledTimes(1)); + expect((Admin.updateSettings as jest.Mock).mock.calls[0][0]).toMatchObject({ + path: { section }, + body: { [key]: "test-credential" }, + }); + expect((Admin.updateSettings as jest.Mock).mock.calls[0][0].body).toEqual({ [key]: "test-credential" }); + }); + + it("keeps a saved credential unchanged after Cancel and Undo", () => { + show(section, key, true); + fireEvent.click(screen.getByRole("button", { name: "Replace" })); + fireEvent.change(screen.getByLabelText(label), { target: { value: "replacement" } }); + fireEvent.click(screen.getByRole("button", { name: "Cancel" })); + expect(screen.queryByRole("button", { name: "Save" })).not.toBeInTheDocument(); + fireEvent.click(screen.getByRole("button", { name: "Clear" })); + fireEvent.click(screen.getByRole("button", { name: "Undo" })); + expect(screen.queryByRole("button", { name: "Save" })).not.toBeInTheDocument(); + expect(Admin.updateSettings).not.toHaveBeenCalled(); + }); + + it("submits an explicit clear without resubmitting masked secrets", async () => { + show(section, key, true); + fireEvent.click(screen.getByRole("button", { name: "Clear" })); + fireEvent.click(screen.getByRole("button", { name: "Save" })); + await waitFor(() => expect(Admin.updateSettings).toHaveBeenCalledTimes(1)); + expect((Admin.updateSettings as jest.Mock).mock.calls[0][0].body).toEqual({ [key]: "" }); + }); +}); diff --git a/nextjs-frontend/components/settings/model.ts b/nextjs-frontend/components/settings/model.ts index 1a0c2fc3d..43ba8fe9f 100644 --- a/nextjs-frontend/components/settings/model.ts +++ b/nextjs-frontend/components/settings/model.ts @@ -1790,6 +1790,7 @@ export const FIELDS: FieldSpec[] = [ }, // Telegram (telegram.py) { section: "telegram", key: "enabled", label: "Answer in Telegram", kind: "switch" }, + { section: "telegram", key: "token", label: "Bot token", kind: "secret" }, { section: "telegram", key: "account", @@ -1849,6 +1850,8 @@ export const FIELDS: FieldSpec[] = [ }, // Slack (slack.py) { section: "slack", key: "enabled", label: "Answer in Slack", kind: "switch" }, + { section: "slack", key: "bot_token", label: "Bot token", kind: "secret" }, + { section: "slack", key: "app_token", label: "App-level token", kind: "secret" }, { section: "slack", key: "account", @@ -1898,6 +1901,7 @@ export const FIELDS: FieldSpec[] = [ }, // The assistant's mailbox (mailbox.py) { section: "mailbox", key: "enabled", label: "Answer email", kind: "switch" }, + { section: "mailbox", key: "password", label: "Password", kind: "secret" }, { section: "mailbox", key: "address", From 010ae46c30592a3f590844f1fae2429ae3f388d1 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad <230291+adeelahmad@users.noreply.github.com> Date: Thu, 5 Nov 2026 18:43:44 +1100 Subject: [PATCH 2/2] style(settings): format credential regression tests --- nextjs-frontend/__tests__/settings-chat-credentials.test.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/nextjs-frontend/__tests__/settings-chat-credentials.test.tsx b/nextjs-frontend/__tests__/settings-chat-credentials.test.tsx index 615ae78ad..db97ab7ad 100644 --- a/nextjs-frontend/__tests__/settings-chat-credentials.test.tsx +++ b/nextjs-frontend/__tests__/settings-chat-credentials.test.tsx @@ -79,7 +79,9 @@ function show(section: SectionId, key: string, saved = false) { beforeEach(() => { (Admin.updateSettings as jest.Mock).mockReset().mockImplementation(() => ok({ ok: true })); - (Admin.getSecrets as jest.Mock).mockReset().mockImplementation(() => ok({ providers: [], references: [], fields: [] })); + (Admin.getSecrets as jest.Mock) + .mockReset() + .mockImplementation(() => ok({ providers: [], references: [], fields: [] })); for (const status of [Admin.telegramStatus, Admin.slackStatus, Admin.mailboxStatus]) (status as jest.Mock).mockReset().mockImplementation(() => ok({ state: "off", people: [], chats: [] })); });