feat(auth): keep unsaved passwords in session memory

This commit is contained in:
Abeautifulsnow authored and GitHub committed 2026-08-14 18:37:53 +08:00
1 parent bcec60ebef
commit 1ff24aaeb4
23 files changed
+1265 -65

No files matched your search

@@ -44,12 +44,15 @@ function installApiMocks(extra: Record<string, unknown> = {}) {
vi.doMock("@/lib/backend/api", () => ({
checkConnectionHealth: vi.fn().mockResolvedValue(undefined),
connectDb: vi.fn().mockResolvedValue("pg-1"),
disconnectDb: vi.fn().mockResolvedValue(undefined),
deleteSchemaCachePrefix: vi.fn().mockResolvedValue(undefined),
loadSchemaCache: vi.fn().mockResolvedValue(null),
saveConnections: vi.fn().mockResolvedValue(undefined),
saveSidebarLayout: vi.fn().mockResolvedValue(undefined),
connectionDatabaseInfo: vi.fn().mockResolvedValue(undefined),
listInstalledAgents: vi.fn().mockResolvedValue([]),
sessionCredentialStatus: vi.fn().mockResolvedValue(false),
forgetSessionCredential: vi.fn().mockResolvedValue(undefined),
...extra,
}));
}
@@ -157,6 +160,58 @@ describe("connectionStore save_password opt-out", () => {
expect(requestPassword).not.toHaveBeenCalled();
});
it("connect skips the prompt when the backend already has a session credential", async () => {
installApiMocks({ sessionCredentialStatus: vi.fn().mockResolvedValue(true) });
installPasswordPromptMock();
requestPassword.mockResolvedValue({ password: "never-used", rememberPassword: false });
const { useConnectionStore } = await import("@/stores/connectionStore");
const store = useConnectionStore();
const connection = postgresConnection({ id: "pg-1", save_password: false, password: "" });
store.connections = [connection];
await store.connect(connection);
// 本次运行期已输入过密码:不再弹窗,直接以空密码 connectDb(后端补主密码)。
expect(requestPassword).not.toHaveBeenCalled();
const { connectDb } = await import("@/lib/backend/api");
expect(connectDb).toHaveBeenCalledWith(expect.objectContaining({ id: "pg-1", password: "" }), expect.any(Number));
});
it("disconnectAndForgetConnectionPassword clears the backend session credential", async () => {
const forgetSessionCredential = vi.fn().mockResolvedValue(undefined);
installApiMocks({
forgetSessionCredential,
sessionCredentialStatus: vi.fn().mockResolvedValue(true),
});
installPasswordPromptMock();
const { useConnectionStore } = await import("@/stores/connectionStore");
const store = useConnectionStore();
const connection = postgresConnection({ id: "pg-1", save_password: false, password: "" });
store.connections = [connection];
store.connectedIds.add("pg-1");
await store.disconnectAndForgetConnectionPassword("pg-1");
expect(forgetSessionCredential).toHaveBeenCalledWith("pg-1");
expect(store.connectedIds.has("pg-1")).toBe(false);
});
it("disconnectAndForgetConnectionPassword propagates forget failures", async () => {
const forgetSessionCredential = vi.fn().mockRejectedValue(new Error("no session credential"));
installApiMocks({
forgetSessionCredential,
sessionCredentialStatus: vi.fn().mockResolvedValue(true),
});
installPasswordPromptMock();
const { useConnectionStore } = await import("@/stores/connectionStore");
const store = useConnectionStore();
const connection = postgresConnection({ id: "pg-1", save_password: false, password: "" });
store.connections = [connection];
store.connectedIds.add("pg-1");
await expect(store.disconnectAndForgetConnectionPassword("pg-1")).rejects.toThrow("no session credential");
});
it("prompts and retries when MySQL rejects a synced connection that sent no password", async () => {
const connectDb = vi.fn().mockRejectedValueOnce(new Error("MySQL connection failed: Access denied for user 'root'@'192.168.100.133' (using password: NO)")).mockResolvedValueOnce("mysql-1");
installApiMocks({ connectDb });