Skip to content

Commit 216370a

Browse files
committed
fix: keep SSH tunnel details behind explicit editing
1 parent 1e733f1 commit 216370a

5 files changed

Lines changed: 421 additions & 191 deletions

File tree

‎src/__tests__/SshTunnelConfigPanel.test.ts‎

Lines changed: 140 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
* SshTunnelConfigPanel — configure-as-intent journey tests.
77
*
88
* The tunnel has no explicit enable toggle: adding a hop / attaching the
9-
* system proxy / typing inline fields IS the intent (enabled is derived in
9+
* system proxy / saving inline settings IS the intent (enabled is derived in
1010
* patch()), mirroring the sibling NoSQL client. These tests pin that semantic.
1111
*/
1212
import { flushPromises, mount } from '@vue/test-utils'
1313
import { createPinia, setActivePinia } from 'pinia'
14+
import Sortable from 'sortablejs'
15+
import SshProfileDialog from '@/components/connections/SshProfileDialog.vue'
1416
import SshTunnelConfigPanel from '@/components/connections/SshTunnelConfigPanel.vue'
1517

1618
jest.mock('@tauri-apps/api/core', () => ({
@@ -41,6 +43,8 @@ jest.mock('sortablejs', () => ({
4143
default: { create: jest.fn(() => ({ destroy: jest.fn() })) },
4244
}))
4345

46+
const mockSaveProfile = jest.fn(async (profile: { id: string, name: string }) => ({ ...profile, id: profile.id || 'new-1' }))
47+
4448
jest.mock('@/store/sshProfileStore', () => ({
4549
emptySshProfile: () => ({
4650
id: '',
@@ -65,7 +69,7 @@ jest.mock('@/store/sshProfileStore', () => ({
6569
byId: (id: string) => HOP_PROFILES.find(p => p.id === id),
6670
// eslint-disable-next-line ts/no-use-before-define
6771
profiles: HOP_PROFILES,
68-
save: jest.fn(async (profile: { id: string, name: string }) => ({ ...profile, id: profile.id || 'new-1' })),
72+
save: mockSaveProfile,
6973
}),
7074
profileToTransportLayer: (profile: { host: string, port: number, username: string }) => ({
7175
type: 'ssh',
@@ -90,7 +94,8 @@ jest.mock('@/store/entitlementStore', () => ({
9094
}))
9195

9296
jest.mock('@/components/connections/SshProfileDialog.vue', () => ({
93-
default: { template: '<span />', methods: { show: jest.fn() } },
97+
__esModule: true,
98+
default: { name: 'SshProfileDialog', template: '<span />', methods: { show: jest.fn() } },
9499
}))
95100

96101
jest.mock('@/components/ui/button', () => ({ Button: { template: '<button><slot /></button>' } }))
@@ -102,6 +107,14 @@ jest.mock('@/components/ui/input', () => ({
102107
template: '<input :value="modelValue" @input="$emit(\'update:modelValue\', $event.target.value)" />',
103108
},
104109
}))
110+
jest.mock('@/components/ui/dialog', () => ({
111+
Dialog: { props: ['open'], template: '<div v-if="open" role="dialog"><slot /></div>' },
112+
DialogContent: { template: '<div><slot /></div>' },
113+
DialogHeader: { template: '<header><slot /></header>' },
114+
DialogTitle: { template: '<h2><slot /></h2>' },
115+
DialogDescription: { template: '<p><slot /></p>' },
116+
DialogFooter: { template: '<footer><slot /></footer>' },
117+
}))
105118
jest.mock('@/components/ui/select', () => ({
106119
Select: { template: '<select><slot /></select>' },
107120
SelectContent: { template: '<span><slot /></span>' },
@@ -112,9 +125,10 @@ jest.mock('@/components/ui/select', () => ({
112125
}))
113126
jest.mock('@/components/ui/combobox', () => ({
114127
SearchableSelect: {
115-
props: ['modelValue', 'options', 'placeholder', 'searchThreshold'],
128+
name: 'SearchableSelect',
129+
props: ['modelValue', 'options', 'placeholder', 'searchThreshold', 'disabled'],
116130
emits: ['update:modelValue'],
117-
template: '<select class="stub-combobox" :value="modelValue" @change="$emit(\'update:modelValue\', $event.target.value)"><option v-for="o in options" :key="o.value" :value="o.value">{{ o.label }}</option></select>',
131+
template: '<select class="stub-combobox" :disabled="disabled" :value="modelValue" @change="$emit(\'update:modelValue\', $event.target.value)"><option v-for="o in options" :key="o.value" :value="o.value">{{ o.label }}</option></select>',
118132
},
119133
}))
120134
jest.mock('@/components/upgrade', () => ({ ProBadge: { template: '<span />' } }))
@@ -154,6 +168,7 @@ async function syncProps(wrapper: ReturnType<typeof mountPanel>) {
154168

155169
describe('sshTunnelConfigPanel — configure-as-intent', () => {
156170
beforeEach(() => {
171+
jest.clearAllMocks()
157172
setActivePinia(createPinia())
158173
})
159174

@@ -228,22 +243,131 @@ describe('sshTunnelConfigPanel — configure-as-intent', () => {
228243
expect(payload.enabled).toBe(true)
229244
})
230245

231-
it('legacy inline connections keep editing fields and stay enabled', async () => {
232-
const wrapper = mountPanel({ enabled: true, host: 'jump.example.com', port: 22, username: 'ops' })
233-
expect(wrapper.text()).toContain('components.serverForm.ssh.legacyManual')
246+
const legacyConfig = {
247+
enabled: true,
248+
host: 'jump.example.com',
249+
port: 2222,
250+
username: 'ops',
251+
authMethod: 'password',
252+
password: 'synthetic-password',
253+
useSystemProxy: true,
254+
}
234255

235-
const hostInput = wrapper.find('#ssh-host')
236-
expect(hostInput.exists()).toBe(true)
237-
await hostInput.setValue('new-jump.example.com')
256+
it('shows a compact inline tunnel without converting or revealing credentials', async () => {
257+
const wrapper = mountPanel(legacyConfig)
258+
await flushPromises()
259+
expect(wrapper.find('#ssh-host').exists()).toBe(false)
260+
expect(wrapper.text()).toContain('[email protected]:2222')
261+
expect(wrapper.text()).not.toContain('synthetic-password')
262+
expect(mockSaveProfile).not.toHaveBeenCalled()
263+
expect(wrapper.emitted('update:modelValue')).toBeUndefined()
264+
expect(wrapper.find('.stub-combobox').attributes('disabled')).toBeDefined()
265+
wrapper.findComponent({ name: 'SearchableSelect' }).vm.$emit('update:modelValue', 'p1')
266+
await wrapper.vm.$nextTick()
267+
expect(wrapper.emitted('update:modelValue')).toBeUndefined()
268+
})
238269

239-
const payload = lastPayload(wrapper)
240-
expect(payload.host).toBe('new-jump.example.com')
241-
expect(payload.enabled).toBe(true)
270+
it('edits tunnel details in a draft and keeps Cancel separate from Save', async () => {
271+
const wrapper = mountPanel(legacyConfig)
272+
const edit = () => wrapper.get('button[title="components.serverForm.ssh.editTunnel"]')
273+
await edit().trigger('click')
274+
expect(wrapper.find('[role="dialog"]').exists()).toBe(true)
275+
await wrapper.get('#ssh-host').setValue('cancelled.example.com')
276+
expect(wrapper.emitted('update:modelValue')).toBeUndefined()
277+
await wrapper.get('footer button').trigger('click')
278+
expect(wrapper.find('#ssh-host').exists()).toBe(false)
279+
await edit().trigger('click')
280+
expect((wrapper.get('#ssh-host').element as HTMLInputElement).value).toBe('jump.example.com')
281+
await wrapper.get('#ssh-host').setValue('new-jump.example.com')
282+
await wrapper.findAll('footer button')[1]!.trigger('click')
283+
expect(lastPayload(wrapper)).toMatchObject({ ...legacyConfig, host: 'new-jump.example.com' })
284+
expect(mockSaveProfile).not.toHaveBeenCalled()
285+
expect(wrapper.find('#ssh-host').exists()).toBe(false)
286+
})
287+
288+
it.each(['password', 'privateKey', 'agent'])('converts %s only on click and preserves authentication and proxy', async (authMethod) => {
289+
const config = { ...legacyConfig, authMethod, privateKey: '/synthetic/key', privateKeyPassphrase: 'synthetic-passphrase' }
290+
const wrapper = mountPanel(config)
291+
await flushPromises()
292+
const convert = wrapper.findAll('button').find(b => b.text() === 'components.serverForm.ssh.convertToProfile')!
293+
await convert.trigger('click')
294+
await flushPromises()
295+
expect(mockSaveProfile).toHaveBeenCalledTimes(1)
296+
expect(mockSaveProfile).toHaveBeenCalledWith(expect.objectContaining({
297+
host: config.host,
298+
port: config.port,
299+
username: config.username,
300+
authMethod: authMethod === 'privateKey' ? 'key' : authMethod,
301+
password: config.password,
302+
keyPath: config.privateKey,
303+
keyPassphrase: config.privateKeyPassphrase,
304+
useSshAgent: authMethod === 'agent',
305+
}))
306+
expect(lastPayload(wrapper)).toMatchObject({
307+
enabled: true,
308+
profileIds: ['new-1'],
309+
useSystemProxy: true,
310+
host: '',
311+
username: '',
312+
password: '',
313+
privateKey: '',
314+
privateKeyPassphrase: '',
315+
})
316+
})
317+
318+
it('keeps inline settings on conversion failure and allows a retry', async () => {
319+
mockSaveProfile.mockRejectedValueOnce(new Error('Profile storage unavailable'))
320+
const wrapper = mountPanel(legacyConfig)
321+
await flushPromises()
322+
const convert = wrapper.findAll('button').find(b => b.text() === 'components.serverForm.ssh.convertToProfile')!
323+
await convert.trigger('click')
324+
await flushPromises()
325+
expect(wrapper.get('[role="alert"]').text()).toContain('Profile storage unavailable')
326+
expect(wrapper.emitted('update:modelValue')).toBeUndefined()
327+
await convert.trigger('click')
328+
await flushPromises()
329+
expect(lastPayload(wrapper).profileIds).toEqual(['new-1'])
330+
})
331+
332+
it('detaches the inline tunnel while retaining its attached proxy', async () => {
333+
const wrapper = mountPanel(legacyConfig)
334+
await wrapper.get('button[title="components.serverForm.ssh.detachAction"]').trigger('click')
335+
expect(lastPayload(wrapper)).toMatchObject({ enabled: true, useSystemProxy: true, host: '', password: '' })
336+
})
337+
338+
it('reorders saved hops with keyboard-accessible buttons', async () => {
339+
const wrapper = mountPanel({ enabled: true, profileIds: ['p1', 'p2'] })
340+
await wrapper.findAll('button[title="components.serverForm.ssh.moveDown"]')[0]!.trigger('click')
341+
await syncProps(wrapper)
342+
expect(lastPayload(wrapper).profileIds).toEqual(['p2', 'p1'])
343+
expect(wrapper.findAll('.ssh-hop-handle').map(el => el.element.parentElement?.textContent)).toEqual([
344+
expect.stringContaining('Bastion B'),
345+
expect.stringContaining('Bastion A'),
346+
])
347+
})
348+
349+
it('restores the sortable DOM before applying a drag reorder and reinitializes after detach', async () => {
350+
const wrapper = mountPanel({ enabled: true, profileIds: ['p1', 'p2'] })
351+
await flushPromises()
352+
const create = Sortable.create as jest.Mock
353+
const options = create.mock.calls[create.mock.calls.length - 1]![1]
354+
const rows = wrapper.findAll('.ssh-hop-handle').map(el => el.element.parentElement!)
355+
rows[0]!.parentElement!.appendChild(rows[0]!)
356+
options.onEnd({ item: rows[0], oldIndex: 0, newIndex: 1 })
357+
await syncProps(wrapper)
358+
expect(lastPayload(wrapper).profileIds).toEqual(['p2', 'p1'])
359+
expect(wrapper.findAll('.ssh-hop-handle')[0]!.element.parentElement?.textContent).toContain('Bastion B')
360+
const instance = create.mock.results[create.mock.results.length - 1]!.value
361+
await wrapper.setProps({ modelValue: { enabled: false, profileIds: [] } as never })
362+
expect(instance.destroy).toHaveBeenCalled()
363+
const previous = create.mock.calls.length
364+
await wrapper.setProps({ modelValue: { enabled: true, profileIds: ['p1'] } as never })
365+
expect(create.mock.calls.length).toBe(previous + 1)
242366
})
243367

244368
it('saving a NEW profile appends it to the chain', async () => {
245369
const wrapper = mountPanel({ enabled: true, profileIds: ['p1'] })
246-
await wrapper.findComponent({ name: 'SshProfileDialog' })?.vm?.$emit('save', { id: 'p2', name: 'Bastion B' })
370+
await wrapper.getComponent(SshProfileDialog).vm.$emit('save', { id: 'p2', name: 'Bastion B' })
247371
await wrapper.vm.$nextTick()
248372

249373
expect(lastPayload(wrapper).profileIds).toEqual(['p1', 'p2'])
@@ -252,7 +376,7 @@ describe('sshTunnelConfigPanel — configure-as-intent', () => {
252376
it('saving an EXISTING hop does not duplicate it', async () => {
253377
const wrapper = mountPanel({ enabled: true, profileIds: ['p1'] })
254378
const before = wrapper.emitted('update:modelValue')?.length ?? 0
255-
await wrapper.findComponent({ name: 'SshProfileDialog' })?.vm?.$emit('save', { id: 'p1', name: 'Bastion A' })
379+
await wrapper.getComponent(SshProfileDialog).vm.$emit('save', { id: 'p1', name: 'Bastion A' })
256380
await wrapper.vm.$nextTick()
257381

258382
// No state change → no new emit; the chain keeps exactly one hop.

0 commit comments

Comments
 (0)