fix(webui): localize missing request secret errors
This commit is contained in:
1 parent
a5e6952cf9
commit
7575269e7a
13 files changed
+56
-2
No files matched your search
@@ -7,6 +7,7 @@ import {
|
||||
type ProviderForm,
|
||||
} from "@/components/settings/models/providerForm";
|
||||
import { SettingsTextEditor } from "@/components/settings/shared/SettingsTextEditor";
|
||||
import { ApiError } from "@/lib/api";
|
||||
|
||||
const PARAMETER_FIELDS = [
|
||||
{ field: "extra_headers", formKey: "extraHeaders", titleKey: "settings.providers.extraHeaders", placeholder: '{"X-Header":"value"}' },
|
||||
@@ -56,8 +57,18 @@ export function ProviderParameterFields({ fields, form, onChange, onSave, inheri
|
||||
throw new Error(t("settings.providers.stringValuesRequired"));
|
||||
}
|
||||
const update = { [item.formKey]: value };
|
||||
if (onSave) await onSave(update);
|
||||
else onChange(update);
|
||||
try {
|
||||
if (onSave) await onSave(update);
|
||||
else onChange(update);
|
||||
} catch (error) {
|
||||
// The gateway alone can tell whether a mask has a saved value to retain.
|
||||
if (item.field !== "extra_body" && error instanceof ApiError && error.status === 400
|
||||
&& Object.entries(parsed).some(([key, entry]) => entry === "••••••••"
|
||||
&& error.message === `${item.field}.${key}: Input should be a valid string`)) {
|
||||
throw new Error(t("settings.providers.maskedSecretMissing"), { cause: error });
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}}
|
||||
trigger={
|
||||
<button type="button" className="settings-hover flex min-h-12 w-full items-center gap-3 border-b border-border/60 px-3 py-3 text-left last:border-b-0">
|
||||
|
||||
@@ -1195,6 +1195,7 @@
|
||||
"draftHint": "Save the new preset to apply these settings.",
|
||||
"invalidObject": "Enter a JSON object, for example: {\"key\":\"value\"}.",
|
||||
"stringValuesRequired": "Use text values for headers and query parameters.",
|
||||
"maskedSecretMissing": "No saved secret matches this mask. Enter the actual value or remove the entry.",
|
||||
"providerGroupOAuth": "OAuth",
|
||||
"providerGroupApiKey": "API Key",
|
||||
"providerGroupLocal": "Local",
|
||||
|
||||
@@ -771,6 +771,7 @@
|
||||
"draftHint": "Guarda el nuevo preajuste para aplicar estos ajustes.",
|
||||
"invalidObject": "Introduce un objeto JSON, por ejemplo: {\"key\":\"value\"}.",
|
||||
"stringValuesRequired": "Usa valores de texto en cabeceras y parámetros de consulta.",
|
||||
"maskedSecretMissing": "No hay un secreto guardado que corresponda a esta máscara. Introduce el valor real o elimina la entrada.",
|
||||
"providerGroupOAuth": "OAuth",
|
||||
"providerGroupApiKey": "API Key",
|
||||
"providerGroupLocal": "Local",
|
||||
|
||||
@@ -771,6 +771,7 @@
|
||||
"draftHint": "Enregistrez le nouveau préréglage pour appliquer ces réglages.",
|
||||
"invalidObject": "Saisissez un objet JSON, par exemple : {\"key\":\"value\"}.",
|
||||
"stringValuesRequired": "Utilisez des valeurs textuelles pour les en-têtes et les paramètres de requête.",
|
||||
"maskedSecretMissing": "Aucun secret enregistré ne correspond à ce masque. Saisissez la valeur réelle ou supprimez l’entrée.",
|
||||
"providerGroupOAuth": "OAuth",
|
||||
"providerGroupApiKey": "API Key",
|
||||
"providerGroupLocal": "Local",
|
||||
|
||||
@@ -771,6 +771,7 @@
|
||||
"draftHint": "Simpan preset baru untuk menerapkan pengaturan ini.",
|
||||
"invalidObject": "Masukkan objek JSON, misalnya: {\"key\":\"value\"}.",
|
||||
"stringValuesRequired": "Gunakan nilai teks untuk header dan parameter kueri.",
|
||||
"maskedSecretMissing": "Tidak ada rahasia tersimpan yang sesuai dengan nilai tersamarkan ini. Masukkan nilai sebenarnya atau hapus entri ini.",
|
||||
"providerGroupOAuth": "OAuth",
|
||||
"providerGroupApiKey": "API Key",
|
||||
"providerGroupLocal": "Lokal",
|
||||
|
||||
@@ -771,6 +771,7 @@
|
||||
"draftHint": "新しいプリセットを保存すると、この設定が適用されます。",
|
||||
"invalidObject": "JSON オブジェクトを入力してください。例:{\"key\":\"value\"}。",
|
||||
"stringValuesRequired": "ヘッダーとクエリパラメーターの値には文字列を使用してください。",
|
||||
"maskedSecretMissing": "このマスクに対応する保存済みのシークレットがありません。実際の値を入力するか、この項目を削除してください。",
|
||||
"providerGroupOAuth": "OAuth",
|
||||
"providerGroupApiKey": "API Key",
|
||||
"providerGroupLocal": "ローカル",
|
||||
|
||||
@@ -771,6 +771,7 @@
|
||||
"draftHint": "새 프리셋을 저장하면 이 설정이 적용됩니다.",
|
||||
"invalidObject": "JSON 객체를 입력하세요. 예: {\"key\":\"value\"}.",
|
||||
"stringValuesRequired": "헤더와 쿼리 매개변수의 값은 문자열로 입력하세요.",
|
||||
"maskedSecretMissing": "이 마스크에 해당하는 저장된 비밀 값이 없습니다. 실제 값을 입력하거나 항목을 삭제하세요.",
|
||||
"providerGroupOAuth": "OAuth",
|
||||
"providerGroupApiKey": "API Key",
|
||||
"providerGroupLocal": "로컬",
|
||||
|
||||
@@ -1009,6 +1009,7 @@
|
||||
"draftHint": "Salve a nova predefinição para aplicar estes ajustes.",
|
||||
"invalidObject": "Insira um objeto JSON, por exemplo: {\"key\":\"value\"}.",
|
||||
"stringValuesRequired": "Use valores de texto nos cabeçalhos e parâmetros de consulta.",
|
||||
"maskedSecretMissing": "Nenhum segredo salvo corresponde a esta máscara. Digite o valor real ou remova a entrada.",
|
||||
"providerGroupOAuth": "OAuth",
|
||||
"providerGroupApiKey": "API Key",
|
||||
"providerGroupLocal": "Local",
|
||||
|
||||
@@ -771,6 +771,7 @@
|
||||
"draftHint": "Lưu cấu hình mẫu mới để áp dụng các thiết lập này.",
|
||||
"invalidObject": "Nhập một đối tượng JSON, ví dụ: {\"key\":\"value\"}.",
|
||||
"stringValuesRequired": "Dùng giá trị văn bản cho tiêu đề và tham số truy vấn.",
|
||||
"maskedSecretMissing": "Không có giá trị bí mật đã lưu tương ứng với phần che này. Nhập giá trị thật hoặc xóa mục này.",
|
||||
"providerGroupOAuth": "OAuth",
|
||||
"providerGroupApiKey": "API Key",
|
||||
"providerGroupLocal": "Cục bộ",
|
||||
|
||||
@@ -1195,6 +1195,7 @@
|
||||
"draftHint": "保存新预设后,这些设置才会生效。",
|
||||
"invalidObject": "请输入 JSON 对象,例如:{\"key\":\"value\"}。",
|
||||
"stringValuesRequired": "请求头和查询参数的值须使用文本。",
|
||||
"maskedSecretMissing": "掩码没有对应的已保存密钥。请输入实际值,或删除该项。",
|
||||
"providerGroupOAuth": "OAuth",
|
||||
"providerGroupApiKey": "API Key",
|
||||
"providerGroupLocal": "本地",
|
||||
|
||||
@@ -771,6 +771,7 @@
|
||||
"draftHint": "儲存新預設後,這些設定才會生效。",
|
||||
"invalidObject": "請輸入 JSON 物件,例如:{\"key\":\"value\"}。",
|
||||
"stringValuesRequired": "請求標頭和查詢參數的值須使用文字。",
|
||||
"maskedSecretMissing": "遮罩沒有對應的已儲存金鑰。請輸入實際值,或刪除該項。",
|
||||
"providerGroupOAuth": "OAuth",
|
||||
"providerGroupApiKey": "API Key",
|
||||
"providerGroupLocal": "本地",
|
||||
|
||||
@@ -49,6 +49,7 @@ const SETTINGS_NAV_KEYS = [
|
||||
"advanced",
|
||||
];
|
||||
const LOCALIZED_SETTINGS_COPY_KEYS = [
|
||||
"settings.providers.maskedSecretMissing",
|
||||
"settings.backToChat",
|
||||
"settings.sidebar.title",
|
||||
"settings.sidebar.ariaLabel",
|
||||
|
||||
@@ -2,12 +2,44 @@ import { act, cleanup, fireEvent, render, screen, within } from "@testing-librar
|
||||
import { afterEach, beforeEach, expect, it, vi } from "vitest";
|
||||
import { ProviderParameterFields } from "@/components/settings/models/ProviderParameterFields";
|
||||
import i18n from "@/i18n";
|
||||
import { ApiError } from "@/lib/api";
|
||||
|
||||
beforeEach(async () => { await i18n.changeLanguage("en"); });
|
||||
afterEach(cleanup);
|
||||
|
||||
const form = { extraHeaders: "", extraQuery: "", extraBody: "{}" };
|
||||
|
||||
it.each([
|
||||
{ field: "extra_headers", formKey: "extraHeaders", title: "请求头" },
|
||||
{ field: "extra_query", formKey: "extraQuery", title: "查询参数" },
|
||||
] as const)("localizes a rejected secret mask in $field and allows a corrected retry", async ({ field, formKey, title }) => {
|
||||
await i18n.changeLanguage("zh-CN");
|
||||
const save = vi.fn().mockRejectedValueOnce(new ApiError(400, `${field}.X-Token: Input should be a valid string`)).mockResolvedValue(undefined);
|
||||
render(<ProviderParameterFields inherit fields={[field]} form={form} onChange={vi.fn()} onSave={save} />);
|
||||
fireEvent.click(screen.getByRole("button", { name: `${title} 使用提供商设置 编辑` }));
|
||||
const dialog = screen.getByRole("dialog", { name: title });
|
||||
const editor = within(dialog).getByRole("textbox");
|
||||
fireEvent.change(editor, { target: { value: '{"X-Token":"••••••••"}' } });
|
||||
fireEvent.click(within(dialog).getByRole("button", { name: "保存", exact: true }));
|
||||
expect(await within(dialog).findByRole("alert")).toHaveTextContent("掩码没有对应的已保存密钥。请输入实际值,或删除该项。");
|
||||
expect(editor).toHaveValue('{"X-Token":"••••••••"}');
|
||||
expect(editor).toHaveFocus();
|
||||
fireEvent.change(editor, { target: { value: '{"X-Token":"fixture-value"}' } });
|
||||
await act(async () => { fireEvent.click(within(dialog).getByRole("button", { name: "保存", exact: true })); });
|
||||
expect(save).toHaveBeenLastCalledWith({ [formKey]: '{"X-Token":"fixture-value"}' });
|
||||
expect(screen.queryByRole("dialog", { name: title })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("lets the gateway preserve an existing masked secret without rejecting it in the editor", async () => {
|
||||
const masked = '{"X-Token":"••••••••"}';
|
||||
const save = vi.fn().mockResolvedValue(undefined);
|
||||
render(<ProviderParameterFields inherit fields={["extra_headers"]} form={{ ...form, extraHeaders: masked }} onChange={vi.fn()} onSave={save} />);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Headers Entries: 1 Edit" }));
|
||||
await act(async () => { fireEvent.click(screen.getByRole("button", { name: "Save", exact: true })); });
|
||||
expect(save).toHaveBeenCalledWith({ extraHeaders: masked });
|
||||
expect(screen.queryByRole("dialog", { name: "Headers" })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("distinguishes a new preset draft from persisted settings and validates before applying", async () => {
|
||||
const change = vi.fn();
|
||||
render(<ProviderParameterFields inherit draft fields={["extra_headers"]} form={form} onChange={change} />);
|
||||
|
||||
Reference in new issue
Block a user