Compare commits

...
Author SHA1 Message Date
l0ng-ai 3195f9cad8 fix(database): keep Save password ticked in the connect prompt
The password prompt shown on connect stored the password but left the data
source's savePassword off, and saved passwords are only read when it is on, so
the next connection asked again. Ticking or clearing Save password in the
prompt now updates the data source setting; the SSH secret prompt does the same
for a data source's own SSH settings.
2026-11-18 22:32:33 +08:00
2 changed files with 96 additions and 3 deletions

No files matched your search

+86
View File
@@ -0,0 +1,86 @@
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { DataSource } from "@/platform/ipc";
const backend = vi.hoisted(() => ({
save: vi.fn(),
password: vi.fn(),
sshSecret: vi.fn(),
sources: vi.fn(),
connected: vi.fn(),
}));
vi.mock("@/platform/ipc", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/platform/ipc")>();
return {
...actual,
commands: {
...actual.commands,
dbSaveSources: backend.save,
dbSetPassword: backend.password,
dbSetSshSecret: backend.sshSecret,
dbSources: backend.sources,
dbConnected: backend.connected,
},
};
});
import { DatabaseDialogs } from "./Dialogs";
import { useDatabase } from "./store";
const ok = <T,>(data: T) => ({ status: "ok" as const, data });
const mysql: DataSource = { id: "my", name: "MySQL", driver: "mysql", user: "manage" };
const tunnelled: DataSource = {
...mysql,
ssh: { enabled: true, host: "bastion", port: 22, user: "u", auth: "password", saveSecret: false },
} as DataSource;
function ask(kind: "password" | "sshSecret", source: DataSource) {
const resolve = vi.fn();
useDatabase.setState({
sources: [source],
dialog: kind === "password" ? { kind, source, resolve } : { kind, source, prompt: "password required", resolve },
});
render(<DatabaseDialogs />);
return resolve;
}
function answer(secret: string) {
fireEvent.change(document.querySelector("input[type=password]")!, { target: { value: secret } });
fireEvent.click(screen.getByRole("checkbox", { name: "Save password" }));
fireEvent.click(screen.getByRole("button", { name: "Connect" }));
}
beforeEach(() => {
for (const m of Object.values(backend)) m.mockReset();
backend.save.mockResolvedValue(ok(null));
backend.password.mockResolvedValue(ok(null));
backend.sshSecret.mockResolvedValue(ok(null));
backend.sources.mockImplementation(async () => ok(backend.save.mock.calls.at(-1)?.[0] ?? [mysql]));
backend.connected.mockResolvedValue(ok([]));
});
describe("connect-time secret prompts", () => {
it("turns on the data source's Save password when it is ticked in the prompt", async () => {
const resolve = ask("password", mysql);
answer("secret");
await waitFor(() => expect(resolve).toHaveBeenCalledWith(true));
expect(backend.save).toHaveBeenCalledWith([{ ...mysql, savePassword: true }]);
expect(backend.password).toHaveBeenCalledWith("my", "secret", true);
});
it("only stores the password when the setting already matches", async () => {
const resolve = ask("password", { ...mysql, savePassword: true });
fireEvent.change(document.querySelector("input[type=password]")!, { target: { value: "secret" } });
fireEvent.click(screen.getByRole("button", { name: "Connect" }));
await waitFor(() => expect(resolve).toHaveBeenCalledWith(true));
expect(backend.save).not.toHaveBeenCalled();
expect(backend.password).toHaveBeenCalledWith("my", "secret", true);
});
it("turns on the SSH tunnel's Save password of the data source's own SSH settings", async () => {
const resolve = ask("sshSecret", tunnelled);
answer("ssh-secret");
await waitFor(() => expect(resolve).toHaveBeenCalledWith(true));
expect(backend.save).toHaveBeenCalledWith([{ ...tunnelled, ssh: { ...tunnelled.ssh, saveSecret: true } }]);
expect(backend.sshSecret).toHaveBeenCalledWith("my", "ssh-secret", true);
});
});
+10 -3
View File
@@ -4,7 +4,7 @@ import { call, commands, type DataSource } from "@/platform/ipc";
import { Checkbox, Dialog, Input, lazyComponent, T } from "@/ui";
import { sshLabel } from "./model";
import { SourceDialog, snowflakeSecretLabel } from "./SourceDialog";
import { closeDialog, useDatabase } from "./store";
import { closeDialog, saveSource, sourceById, useDatabase } from "./store";
import styles from "./Database.module.css";
import { isImeKey } from "@/platform/ime";
import { tx } from "@/platform/i18n";
@@ -27,7 +27,9 @@ function PasswordDialog({ source, resolve }: { source: DataSource; resolve: (ok:
resolve(ok);
};
const submit = async () => {
await call(commands.dbSetPassword(source.id ?? "", password, save));
// The checkbox is the data source's Save password setting: saved passwords are read only when it is on.
if (save !== !!source.savePassword) await saveSource({ ...source, savePassword: save }, password);
else await call(commands.dbSetPassword(source.id ?? "", password, save));
done(true);
};
return (
@@ -81,7 +83,12 @@ function SshSecretDialog({
resolve(ok);
};
const submit = async () => {
await call(commands.dbSetSshSecret(source.id ?? "", secret, save));
// As for the password: the data source's own SSH settings read a saved secret only when Save is on.
// An SSH configuration keeps its own setting.
const stored = sourceById(source.id);
if (stored?.ssh && !stored.ssh.config?.trim() && save !== !!stored.ssh.saveSecret)
await saveSource({ ...stored, ssh: { ...stored.ssh, saveSecret: save } }, undefined, secret);
else await call(commands.dbSetSshSecret(source.id ?? "", secret, save));
done(true);
};
return (