mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-04 16:34:52 +03:00
52f5fb5205
Keep the existing node API token unless an operator explicitly requests a replacement. Initialize edit sessions with an empty replacement draft and keep the credential field unmounted by default. Omit apiToken from Test Connection, Save, fingerprint, and inbound-loading requests in keep mode, while preserving explicit replacement, token clearing, and tokenless mTLS behavior. Accept historyUndo/historyRedo input events so undoing and redoing an explicit replacement remains synchronized with form state. Add regression coverage for the default keep path, replacement flow, and Undo/Redo. The first edit after a page refresh could otherwise submit an unintended non-empty password value as a replacement token, causing Test Connection and Save to fail. The exact browser/password-manager trigger was not independently reproduced; this change fixes the unsafe form/payload behavior at its source. Addresses #7349.
201 lines
8.3 KiB
TypeScript
201 lines
8.3 KiB
TypeScript
import { useState } from 'react';
|
|
import { fireEvent, screen, waitFor, within } from '@testing-library/react';
|
|
import { expect, it, vi } from 'vitest';
|
|
|
|
import NodeFormModal from '@/pages/nodes/NodeFormModal';
|
|
import type { NodeRecord, ProbeResult } from '@/schemas/node';
|
|
import { Msg } from '@/utils';
|
|
import { chooseSelectOption, renderWithProviders } from './test-utils';
|
|
|
|
const stored: NodeRecord = {
|
|
id: 11,
|
|
name: 'Primary',
|
|
address: 'primary.example.test',
|
|
port: 2053,
|
|
status: 'online',
|
|
enable: true,
|
|
hasApiToken: true,
|
|
};
|
|
const other: NodeRecord = { ...stored, id: 22, name: 'Secondary', tlsVerifyMode: 'pin' };
|
|
const unexpectedValue = 'unrelated-fixture';
|
|
const apiTokenLabel = { selector: 'input' };
|
|
|
|
function setup() {
|
|
const probe = vi.fn(async (payload: Partial<NodeRecord>) =>
|
|
payload.apiToken === 'incorrect-fixture'
|
|
? new Msg<ProbeResult>(false, 'Credential rejected')
|
|
: new Msg<ProbeResult>(true, '', { status: 'online', latencyMs: 1 }),
|
|
);
|
|
const save = vi.fn(async (_payload: Partial<NodeRecord>) => new Msg(true));
|
|
const fingerprint = vi.fn(
|
|
async (_payload: Partial<NodeRecord>) => new Msg(true, '', 'fixture-pin'),
|
|
);
|
|
const inbounds = vi.fn(async (_payload: Partial<NodeRecord>) => new Msg(true, '', []));
|
|
|
|
function Page() {
|
|
const [node, setNode] = useState<NodeRecord | null>(null);
|
|
const [open, setOpen] = useState(false);
|
|
return (
|
|
<>
|
|
<button
|
|
onClick={() => {
|
|
setNode(stored);
|
|
setOpen(true);
|
|
}}
|
|
>
|
|
Edit primary
|
|
</button>
|
|
<button
|
|
onClick={() => {
|
|
setNode(other);
|
|
setOpen(true);
|
|
}}
|
|
>
|
|
Edit secondary
|
|
</button>
|
|
<button
|
|
onClick={() => {
|
|
setNode(null);
|
|
setOpen(true);
|
|
}}
|
|
>
|
|
Add node
|
|
</button>
|
|
<NodeFormModal
|
|
open={open}
|
|
mode={node ? 'edit' : 'add'}
|
|
node={node}
|
|
testConnection={probe}
|
|
fetchFingerprint={fingerprint}
|
|
fetchInbounds={inbounds}
|
|
save={save}
|
|
onOpenChange={setOpen}
|
|
/>
|
|
</>
|
|
);
|
|
}
|
|
|
|
const view = renderWithProviders(<Page />);
|
|
return { ...view, probe, save, fingerprint, inbounds };
|
|
}
|
|
|
|
function openForm(name: string) {
|
|
fireEvent.click(screen.getByRole('button', { name }));
|
|
return within(screen.getByRole('dialog'));
|
|
}
|
|
|
|
it('keeps the stored token on first open even if a password input receives an unexpected value', async () => {
|
|
for (let i = 0; i < 2; i++) {
|
|
const { probe, save, unmount } = setup();
|
|
const dialog = openForm('Edit primary');
|
|
const unexpectedInput = dialog.queryByLabelText('API Token', apiTokenLabel);
|
|
if (unexpectedInput) {
|
|
fireEvent.change(unexpectedInput, { target: { value: unexpectedValue } });
|
|
}
|
|
fireEvent.click(dialog.getByRole('button', { name: 'Test Connection' }));
|
|
await waitFor(() => expect(probe).toHaveBeenCalledTimes(1));
|
|
expect(probe.mock.calls[0]?.[0]).toMatchObject({ id: 11 });
|
|
expect(probe.mock.calls[0]?.[0]).not.toHaveProperty('apiToken');
|
|
fireEvent.click(dialog.getByRole('button', { name: 'Save' }));
|
|
await waitFor(() => expect(save).toHaveBeenCalledTimes(1));
|
|
expect(save.mock.calls[0]?.[0]).toMatchObject({ id: 11 });
|
|
expect(save.mock.calls[0]?.[0]).not.toHaveProperty('apiToken');
|
|
unmount();
|
|
}
|
|
});
|
|
|
|
it('ignores autofill in replacement mode, probes explicit edits, and clears them across opens', async () => {
|
|
const { probe, save, fingerprint, inbounds } = setup();
|
|
const dialog = openForm('Edit primary');
|
|
fireEvent.click(dialog.getByRole('checkbox', { name: 'Replace API Token' }));
|
|
const input = dialog.getByLabelText<HTMLInputElement>('API Token', apiTokenLabel);
|
|
expect(input.value).toBe('');
|
|
fireEvent.input(input, {
|
|
target: { value: unexpectedValue },
|
|
inputType: 'insertReplacementText',
|
|
});
|
|
expect(input.value).toBe('');
|
|
fireEvent.click(dialog.getByRole('button', { name: 'Test Connection' }));
|
|
await waitFor(() => expect(probe).toHaveBeenCalledTimes(1));
|
|
expect(probe.mock.calls[0]?.[0]).not.toHaveProperty('apiToken');
|
|
|
|
fireEvent.input(input, {
|
|
target: { value: 'incorrect-fixture' },
|
|
inputType: 'insertFromPaste',
|
|
});
|
|
fireEvent.click(dialog.getByRole('button', { name: 'Save' }));
|
|
await dialog.findByText('Credential rejected');
|
|
expect(probe.mock.calls[1]?.[0]).toMatchObject({ id: 11, apiToken: 'incorrect-fixture' });
|
|
expect(save).not.toHaveBeenCalled();
|
|
fireEvent.click(dialog.getByRole('button', { name: 'Cancel' }));
|
|
|
|
const reopened = openForm('Edit primary');
|
|
expect(reopened.queryByLabelText('API Token', apiTokenLabel)).toBeNull();
|
|
expect(reopened.queryByText('Credential rejected')).toBeNull();
|
|
fireEvent.click(reopened.getByRole('button', { name: 'Cancel' }));
|
|
|
|
const secondary = openForm('Edit secondary');
|
|
fireEvent.click(secondary.getByRole('button', { name: 'Fetch' }));
|
|
await waitFor(() => expect(fingerprint).toHaveBeenCalledTimes(1));
|
|
expect(fingerprint.mock.calls[0]?.[0]).toMatchObject({ id: 22 });
|
|
expect(fingerprint.mock.calls[0]?.[0]).not.toHaveProperty('apiToken');
|
|
chooseSelectOption(secondary.getByLabelText('Inbound import').id, 'Selected inbounds');
|
|
fireEvent.mouseDown(secondary.getByRole('combobox', { name: 'Inbounds' }));
|
|
fireEvent.click(screen.getByRole('button', { name: 'Load inbounds from node' }));
|
|
await waitFor(() => expect(inbounds).toHaveBeenCalledTimes(1));
|
|
expect(inbounds.mock.calls[0]?.[0]).toMatchObject({ id: 22 });
|
|
expect(inbounds.mock.calls[0]?.[0]).not.toHaveProperty('apiToken');
|
|
});
|
|
|
|
it('updates the probe credential when undoing and redoing a replacement', async () => {
|
|
const { probe } = setup();
|
|
const dialog = openForm('Edit primary');
|
|
fireEvent.click(dialog.getByRole('checkbox', { name: 'Replace API Token' }));
|
|
const input = dialog.getByLabelText<HTMLInputElement>('API Token', apiTokenLabel);
|
|
const replacement = 'replacement-fixture';
|
|
fireEvent.input(input, { target: { value: replacement }, inputType: 'insertFromPaste' });
|
|
fireEvent.input(input, { target: { value: '' }, inputType: 'historyUndo' });
|
|
expect(input.value).toBe('');
|
|
fireEvent.click(dialog.getByRole('button', { name: 'Test Connection' }));
|
|
await waitFor(() => expect(probe).toHaveBeenCalledTimes(1));
|
|
expect(probe.mock.calls[0]?.[0]).toMatchObject({ id: 11 });
|
|
expect(probe.mock.calls[0]?.[0]).not.toHaveProperty('apiToken');
|
|
|
|
fireEvent.input(input, { target: { value: replacement }, inputType: 'historyRedo' });
|
|
expect(input.value).toBe(replacement);
|
|
fireEvent.click(dialog.getByRole('button', { name: 'Test Connection' }));
|
|
await waitFor(() => expect(probe).toHaveBeenCalledTimes(2));
|
|
expect(probe.mock.calls[1]?.[0]).toMatchObject({ id: 11, apiToken: replacement });
|
|
});
|
|
|
|
it('requires an entered token for ordinary additions and allows mTLS additions without one', async () => {
|
|
const { probe, save } = setup();
|
|
const editing = openForm('Edit primary');
|
|
fireEvent.click(editing.getByRole('checkbox', { name: 'Replace API Token' }));
|
|
fireEvent.input(editing.getByLabelText('API Token', apiTokenLabel), {
|
|
target: { value: 'explicit-fixture' },
|
|
inputType: 'insertFromPaste',
|
|
});
|
|
fireEvent.click(editing.getByRole('button', { name: 'Save' }));
|
|
await waitFor(() => expect(save).toHaveBeenCalledTimes(1));
|
|
expect(save.mock.calls[0]?.[0]).toMatchObject({ id: 11, apiToken: 'explicit-fixture' });
|
|
|
|
const adding = openForm('Add node');
|
|
const token = adding.getByLabelText<HTMLInputElement>('API Token', apiTokenLabel);
|
|
expect(token.value).toBe('');
|
|
fireEvent.change(adding.getByLabelText('Name'), { target: { value: 'New node' } });
|
|
fireEvent.change(adding.getByLabelText('Address'), { target: { value: 'new.example.test' } });
|
|
fireEvent.change(token, { target: { value: unexpectedValue } });
|
|
fireEvent.click(adding.getByRole('button', { name: 'Save' }));
|
|
await screen.findByText('Name, address, port and API token are required');
|
|
expect(probe).toHaveBeenCalledTimes(1);
|
|
chooseSelectOption(
|
|
adding.getByLabelText('TLS verification').id,
|
|
'Mutual TLS (client certificate)',
|
|
);
|
|
fireEvent.click(adding.getByRole('button', { name: 'Save' }));
|
|
await waitFor(() => expect(save).toHaveBeenCalledTimes(2));
|
|
expect(save.mock.calls[1]?.[0]).toMatchObject({ id: 0, tlsVerifyMode: 'mtls' });
|
|
expect(save.mock.calls[1]?.[0]).not.toHaveProperty('apiToken');
|
|
});
|