credentialVaultPanel.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
  2. import { afterEach, beforeEach, expect, it, vi } from 'vitest'
  3. import CredentialVaultPanel from '@/components/app/CredentialVaultPanel.vue'
  4. const api=vi.hoisted(()=>vi.fn())
  5. vi.mock('@/api/client',()=>({apiRequest:api}))
  6. let wrapper:VueWrapper|undefined
  7. const initial={protocol:1,online:true,initialized:true,unlocked:false,execution:'disabled',entries:[],leases:[],tasks:[],requests:[]}
  8. let state:any
  9. function deferred(){let resolve!:(value:any)=>void;const promise=new Promise<any>(r=>{resolve=r});return {resolve,promise}}
  10. beforeEach(()=>{state=structuredClone(initial);api.mockImplementation(async(_path,options)=>options?.method==='POST'?{}:structuredClone(state))})
  11. afterEach(()=>{wrapper?.unmount();wrapper=undefined;vi.useRealTimers();vi.restoreAllMocks();api.mockReset()})
  12. async function setup(){wrapper=mount(CredentialVaultPanel);await flushPromises();return wrapper}
  13. it('shows an offline state without offering secret entry and explicitly keeps real login disabled',async()=>{
  14. state.online=false;const w=await setup()
  15. expect(w.find('input[type=password]').exists()).toBe(false);expect(w.text()).toContain('尚未启用');expect(w.text()).toContain('不会自动安装或启动服务')
  16. })
  17. it('offers local credential management and persistent Bot approval without requiring an independent service',async()=>{
  18. state.initialized=false;state.storageMode='local';state.execution='local-adapters';const w=await setup()
  19. expect(w.text()).toContain('授权一次即可使用');expect(w.text()).toContain('创建密码库')
  20. state.initialized=true;state.unlocked=true
  21. await w.findAll('button').find(b=>b.text()==='刷新状态')!.trigger('click');await flushPromises()
  22. await w.findAll('button').find(b=>b.text()==='添加凭据')!.trigger('click')
  23. const editor=w.findAll('form')[0]!,label=(text:string)=>editor.findAll('label').find(l=>l.text().startsWith(text))!
  24. await label('名称').get('input').setValue('Dummy site');await label('用户名').get('input').setValue('dummy')
  25. await label('准确网站').get('input').setValue('https://example.test');await label('密码').get('input').setValue('DUMMY-new-secret')
  26. await editor.trigger('submit');await flushPromises()
  27. expect(api).toHaveBeenCalledWith('/api/app/vault/entries',expect.objectContaining({method:'POST',body:{name:'Dummy site',username:'dummy',target:{kind:'website',origin:'https://example.test'},secret:'DUMMY-new-secret'}}))
  28. expect(w.findAll('button').find(b=>b.text()==='授权此 Bot 使用')!.attributes('disabled')).toBeDefined()
  29. expect(w.text()).not.toContain('DUMMY-new-secret');expect(w.find('input[type=password]').exists()).toBe(true)
  30. })
  31. it('reports a wrong vault master password without expiring the application login or echoing errors',async()=>{
  32. const w=await setup();await w.get('input[type=password]').setValue('DUMMY-wrong-master')
  33. api.mockImplementation(async(path)=>{if(path.endsWith('/unlock'))throw Object.assign(new Error('DUMMY-private-error-DUMMY-wrong-master'),{code:'vault_unlock_failed'});return structuredClone(state)})
  34. await w.get('form').trigger('submit');await flushPromises()
  35. expect(api).toHaveBeenCalledWith('/api/app/vault/unlock',expect.objectContaining({notifyUnauthorized:false}))
  36. expect(w.get('[role=alert]').text()).toContain('主密码错误');expect(w.text()).not.toContain('DUMMY-private-error');expect(w.text()).not.toContain('DUMMY-wrong-master')
  37. expect(w.get<HTMLInputElement>('input[type=password]').element.value).toBe('')
  38. })
  39. it('preserves a master password draft while locked status is polled, then clears it after manual unlock',async()=>{
  40. vi.useFakeTimers();const storage=vi.spyOn(Storage.prototype,'setItem'),w=await setup(),input=w.get<HTMLInputElement>('input[type=password]')
  41. await input.setValue('DUMMY-ONLY-master');await vi.advanceTimersByTimeAsync(3000);await flushPromises();expect(input.element.value).toBe('DUMMY-ONLY-master')
  42. await w.get('form').trigger('submit');await flushPromises()
  43. expect(api).toHaveBeenCalledWith('/api/app/vault/unlock',expect.objectContaining({method:'POST',body:{password:'DUMMY-ONLY-master',seconds:300}}))
  44. expect(input.element.value).toBe('');expect(storage).not.toHaveBeenCalled()
  45. })
  46. it('checks confirmation before initialization and leaves no draft in unmounted UI',async()=>{
  47. state.initialized=false;const w=await setup(),inputs=w.findAll('input[type=password]')
  48. await inputs[0]!.setValue('dummy-master-A');await inputs[1]!.setValue('dummy-master-B');await w.get('form').trigger('submit')
  49. expect(w.get('[role=alert]').text()).toContain('不一致');expect(api.mock.calls.filter(([,o])=>o?.method==='POST')).toHaveLength(0)
  50. w.unmount();wrapper=undefined
  51. })
  52. it('editing exposes no saved secret and omits a blank replacement in the request',async()=>{
  53. state.unlocked=true;state.entries=[{id:'dummy-id',name:'dummy',username:'fixture',target:{kind:'website',origin:'https://example.test'},revision:3,updatedAt:1}]
  54. const w=await setup();await w.findAll('button').find(b=>b.text()==='编辑')!.trigger('click')
  55. const password=w.get<HTMLInputElement>('input[type=password]');expect(password.element.value).toBe('')
  56. await w.findAll('form')[0]!.trigger('submit');await flushPromises()
  57. const call=api.mock.calls.find(([p,o])=>p==='/api/app/vault/entries/dummy-id'&&o.method==='PUT')!
  58. expect(call[1].body).not.toHaveProperty('secret');expect(call[1].body.revision).toBe(3)
  59. expect(api.mock.calls.some(([p])=>p.includes('read-secret'))).toBe(false)
  60. })
  61. it('unmount aborts an in-flight unlock and sends an explicit lock without a password',async()=>{
  62. const w=await setup();await w.get('input[type=password]').setValue('dummy-master-only')
  63. const pending=deferred();api.mockImplementation((p)=>p.endsWith('/unlock')?pending.promise:Promise.resolve({}))
  64. await w.get('form').trigger('submit');const call=api.mock.calls.find(([p])=>p.endsWith('/unlock'))!
  65. w.unmount();wrapper=undefined;expect(call[1].signal.aborted).toBe(true)
  66. expect(api).toHaveBeenCalledWith('/api/app/vault/lock',expect.objectContaining({body:{}}));pending.resolve({});await flushPromises()
  67. })
  68. it('hiding the page locks even while unlock is still pending',async()=>{
  69. const w=await setup();await w.get('input[type=password]').setValue('dummy-master-only')
  70. const pending=deferred();api.mockImplementation(p=>p.endsWith('/unlock')?pending.promise:Promise.resolve({}))
  71. await w.get('form').trigger('submit');const call=api.mock.calls.find(([p])=>p.endsWith('/unlock'))!
  72. vi.spyOn(document,'hidden','get').mockReturnValue(true);document.dispatchEvent(new Event('visibilitychange'))
  73. await w.vm.$nextTick()
  74. expect(call[1].signal.aborted).toBe(true);expect(api).toHaveBeenCalledWith('/api/app/vault/lock',expect.objectContaining({body:{}}))
  75. expect(w.get<HTMLInputElement>('input[type=password]').element.value).toBe('');pending.resolve({});await flushPromises()
  76. })
  77. it('configures an encrypted fixed SFTP body in the dedicated UI, clears it after save and never reads it back',async()=>{
  78. state.unlocked=true;state.execution='protected-adapters';const w=await setup()
  79. await w.findAll('button').find(b=>b.text()==='添加凭据')!.trigger('click')
  80. const editor=w.findAll('form')[0]!
  81. await editor.get('select').setValue('ssh');await editor.get('input[type=checkbox]').setValue(true)
  82. await editor.findAll('label').find(l=>l.text().startsWith('SSH 认证方式'))!.get('select').setValue('privateKey')
  83. await editor.findAll('label').find(l=>l.text().startsWith('允许操作'))!.get('select').setValue('sftp.write')
  84. const label=(text:string)=>editor.findAll('label').find(l=>l.text().startsWith(text))!
  85. await label('名称').get('input').setValue('dummy transfer');await label('用户名').get('input').setValue('dummy')
  86. await label('SSH 主机').get('input').setValue('127.0.0.1');await label('服务器指纹').get('input').setValue('SHA256:'+'A'.repeat(43))
  87. await label('私钥').get('textarea').setValue('DUMMY-private-key');await label('批准的远端').get('input').setValue('/approved/new.txt')
  88. await label('固定写入正文').get('textarea').setValue('DUMMY-private-write-body')
  89. await editor.trigger('submit');await flushPromises()
  90. expect(api).toHaveBeenCalledWith('/api/app/vault/entries',expect.objectContaining({method:'POST',body:expect.objectContaining({usage:{kind:'sftp.write',remotePath:'/approved/new.txt',contents:'DUMMY-private-write-body'}})}))
  91. expect(w.find('textarea').exists()).toBe(false);expect(w.text()).not.toContain('DUMMY-private-write-body')
  92. })
  93. it('defaults to a masked SSH password and saves it without a host fingerprint or opening advanced settings',async()=>{
  94. state.unlocked=true;state.storageMode='local';const w=await setup()
  95. await w.findAll('button').find(b=>b.text()==='添加凭据')!.trigger('click')
  96. const editor=w.get('form'),label=(text:string)=>editor.findAll('label').find(l=>l.text().startsWith(text))!
  97. await label('用途').get('select').setValue('ssh');expect(label('SSH 认证方式').get('select').element.value).toBe('password')
  98. expect(editor.get('details').attributes('open')).toBeUndefined();expect(label('服务器指纹').get('input').attributes('required')).toBeUndefined()
  99. await label('名称').get('input').setValue('Dummy password SSH');await label('用户名').get('input').setValue('dummy')
  100. await label('SSH 主机').get('input').setValue('ssh.example.test')
  101. const field=label('SSH 登录密码').get<HTMLInputElement>('input');expect(field.attributes('type')).toBe('password')
  102. await field.setValue('DUMMY-ssh-login-password')
  103. expect(editor.text()).not.toContain('私钥口令');expect(editor.find('textarea').exists()).toBe(false)
  104. await editor.trigger('submit');await flushPromises()
  105. expect(api).toHaveBeenCalledWith('/api/app/vault/entries',expect.objectContaining({method:'POST',body:{name:'Dummy password SSH',username:'dummy',target:{kind:'ssh',host:'ssh.example.test',port:22,auth:'password'},secret:'DUMMY-ssh-login-password'}}))
  106. expect(w.text()).not.toContain('DUMMY-ssh-login-password');expect(w.text()).toContain('授权 Bot 使用')
  107. })
  108. it('clears SSH secret drafts on authentication changes and requires a new secret when editing a legacy key entry',async()=>{
  109. state.unlocked=true;state.storageMode='local';state.entries=[{id:'legacy-id',name:'legacy SSH',username:'dummy',target:{kind:'ssh',host:'ssh.example.test',port:22,hostKey:'SHA256:'+'A'.repeat(43)},revision:2,updatedAt:1}]
  110. const w=await setup();await w.findAll('button').find(b=>b.text()==='编辑')!.trigger('click')
  111. const editor=w.get('form'),label=(text:string)=>editor.findAll('label').find(l=>l.text().startsWith(text))!
  112. expect(label('SSH 认证方式').get('select').element.value).toBe('privateKey')
  113. await label('私钥').get('textarea').setValue('DUMMY-private-key-draft');await label('私钥口令').get('input').setValue('DUMMY-passphrase-draft')
  114. await label('SSH 认证方式').get('select').setValue('password')
  115. const field=label('SSH 登录密码').get<HTMLInputElement>('input')
  116. expect(field.element.value).toBe('');expect(field.attributes('required')).toBeDefined()
  117. await editor.trigger('submit');await flushPromises()
  118. expect(w.get('[role=alert]').text()).toContain('重新填写');expect(api.mock.calls.some(([,o])=>o?.method==='PUT')).toBe(false)
  119. await field.setValue('DUMMY-replacement-password');await editor.trigger('submit');await flushPromises()
  120. const body=api.mock.calls.find(([p,o])=>p.endsWith('/legacy-id')&&o.method==='PUT')![1].body
  121. expect(body).toMatchObject({target:{auth:'password'},secret:'DUMMY-replacement-password',revision:2});expect(body).not.toHaveProperty('passphrase')
  122. expect(JSON.stringify(body)).not.toContain('DUMMY-private-key-draft')
  123. })
  124. it('keeps an existing SSH password when its masked replacement field is left blank',async()=>{
  125. state.unlocked=true;state.storageMode='local';state.entries=[{id:'password-id',name:'password SSH',username:'dummy',target:{kind:'ssh',host:'ssh.example.test',port:22,hostKey:'SHA256:'+'A'.repeat(43),auth:'password'},revision:3,updatedAt:1}]
  126. const w=await setup();await w.findAll('button').find(b=>b.text()==='编辑')!.trigger('click')
  127. const field=w.get<HTMLInputElement>('input[type=password]');expect(field.element.value).toBe('');expect(field.attributes('required')).toBeUndefined()
  128. await w.get('form').trigger('submit');await flushPromises()
  129. const body=api.mock.calls.find(([p,o])=>p.endsWith('/password-id')&&o.method==='PUT')![1].body
  130. expect(body.target.auth).toBe('password');expect(body).not.toHaveProperty('secret');expect(body).not.toHaveProperty('passphrase')
  131. })
  132. it.each(['pinned','first-use'])('grants a %s local credential to a specified Bot without a task or secret in the approval request',async trust=>{
  133. state.unlocked=true;state.storageMode='local';state.execution='local-adapters'
  134. state.entries=[{id:'credential-id',name:'SSH test',username:'dummy',target:{kind:'ssh',host:'ssh.example.test',port:22,...(trust==='pinned'?{hostKey:'SHA256:'+'A'.repeat(43)}:{}),auth:'password'},usage:{kind:'ssh.exec',command:'/usr/bin/uptime'},revision:1,updatedAt:1}]
  135. state.tasks=[{workId:'work-id',agentId:'bot-id',name:'Current Bot task'}]
  136. state.bots=[{id:'bot-id',name:'Selected Bot'}]
  137. state.requests=[{workId:'work-id',agentId:'bot-id',credentialRef:'credential-id',operation:'ssh.exec'}]
  138. const w=await setup(),button=w.findAll('button').find(b=>b.text()==='授权此 Bot 使用')!
  139. expect(button.attributes('disabled')).toBeDefined()
  140. const label=(text:string)=>w.findAll('label').find(l=>l.text().startsWith(text))!
  141. await label('凭据').get('select').setValue('credential-id')
  142. if(trust==='first-use')expect(w.text()).toContain('首次使用会自动记住服务器指纹')
  143. await label('Bot').get('select').setValue('bot-id')
  144. expect(button.attributes('disabled')).toBeUndefined()
  145. await w.findAll('form')[0]!.trigger('submit');await flushPromises()
  146. expect(api).toHaveBeenCalledWith('/api/app/vault/bot-grant',expect.objectContaining({method:'POST',body:{credentialRef:'credential-id',agentId:'bot-id'}}))
  147. expect(w.text()).toContain('此 Bot 已获持续授权');expect(w.text()).not.toContain('本次任务授权')
  148. })
  149. it('shows persistent approval while locked and saves explicit Bot approval together with a configured entry',async()=>{
  150. state.storageMode='local';state.bots=[{id:'selected-bot',name:'竹儿'}];state.botGrants=[{credentialRef:'entry-id',agentId:'selected-bot',name:'SSH test',operation:'ssh.exec'}]
  151. const w=await setup();expect(w.text()).toContain('SSH test → 竹儿 · 已持续授权')
  152. expect(w.findAll('button').find(b=>b.text()==='撤销 Bot 授权')!.attributes('disabled')).toBeDefined()
  153. state.unlocked=true;await w.findAll('button').find(b=>b.text()==='刷新状态')!.trigger('click');await flushPromises()
  154. await w.findAll('button').find(b=>b.text()==='添加凭据')!.trigger('click')
  155. const editor=w.get('form'),label=(text:string)=>editor.findAll('label').find(l=>l.text().startsWith(text))!
  156. await label('名称').get('input').setValue('SSH new');await label('用户名').get('input').setValue('dummy')
  157. await label('用途').get('select').setValue('ssh');await label('SSH 主机').get('input').setValue('127.0.0.1')
  158. await label('SSH 登录密码').get('input').setValue('DUMMY-new-password')
  159. await label('授权给 Bot').get('select').setValue('selected-bot')
  160. api.mockImplementation(async(p,o)=>p.endsWith('/entries')?{id:'new-entry'}:o?.method==='POST'?{}:structuredClone(state))
  161. await editor.trigger('submit');await flushPromises()
  162. expect(api).toHaveBeenCalledWith('/api/app/vault/bot-grant',expect.objectContaining({body:{credentialRef:'new-entry',agentId:'selected-bot'}}))
  163. expect(w.text()).toContain('凭据已加密保存并授权所选 Bot');expect(w.text()).not.toContain('DUMMY-new-password')
  164. })