desktopHosts.test.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // @vitest-environment node
  2. import {afterEach,beforeEach,expect,it} from 'vitest'
  3. import {mkdtempSync,rmSync} from 'node:fs'
  4. import {tmpdir} from 'node:os'
  5. import {join} from 'node:path'
  6. import Koa from 'koa'
  7. import {bodyParser} from '@koa/bodyparser'
  8. import request from 'supertest'
  9. import {WorkspaceStore} from '../../src/server/workspaceStore'
  10. import {DesktopHostHub} from '../../src/server/desktopHosts'
  11. import type {DesktopHostExchange} from '../../src/shared/desktopHost'
  12. let home:string,store:WorkspaceStore,hub:DesktopHostHub,token:string,hostId:string,exchanged:DesktopHostExchange[]
  13. const auth={requireAdmin:()=>({id:'admin',role:'admin'}),require:()=>({id:'owner'})} as any
  14. const body=():DesktopHostExchange=>({host:{id:'11111111-1111-4111-8111-111111111111',name:'书房 iMac',platform:'darwin',screen:true,accessibility:true,approved:[]},results:[]})
  15. beforeEach(()=>{home=mkdtempSync(join(tmpdir(),'desktop-hosts-'));store=new WorkspaceStore(home);hub=new DesktopHostHub(store,auth);exchanged=[];hub.exchange=(id,value)=>{exchanged.push(value);hostId=id;return {commands:[]}}})
  16. afterEach(()=>{hub.close();store.close();rmSync(home,{recursive:true,force:true})})
  17. const app=()=>{const a=new Koa();a.use(async(ctx,next)=>{try{await next()}catch(e:any){ctx.status=e.status??500;ctx.body={error:e.message,code:e.code}}});a.use((ctx,next)=>ctx.path.startsWith('/api/app/')?bodyParser()(ctx,next):next());a.use(hub.middleware());a.use(hub.adminRouter().routes());return a.callback()}
  18. async function enroll(name='书房 iMac'){const response=await request(app()).post('/api/app/admin/desktop-hosts').send({name}).expect(201);token=response.body.token;hostId=response.body.host.id;return response.body}
  19. const post=(url:string)=>request(app()).post(url).set('Authorization','Bearer '+token).set('x-desktop-host-protocol','1')
  20. it('issues the token once and keeps it out of listings',async()=>{
  21. const enrolled=await enroll()
  22. expect(token).toMatch(/^[\w-]{40,}$/)
  23. expect(enrolled.host).not.toHaveProperty('tokenHash')
  24. const list=(await request(app()).get('/api/app/admin/desktop-hosts').expect(200)).body
  25. expect(list.protocol).toBe(1)
  26. expect(JSON.stringify(list)).not.toContain(token)
  27. expect(list.hosts).toHaveLength(1)
  28. expect(list.hosts[0]).toMatchObject({name:'书房 iMac',enabled:true,online:false})
  29. })
  30. it('rejects unauthorized transports before parsing the body',async()=>{
  31. await enroll()
  32. await request(app()).post(`/api/desktop-host/v1/${hostId}/exchange`).send(body()).expect(403)
  33. await post(`/api/desktop-host/v1/${hostId}/exchange`).set('Origin','http://evil.test').send(body()).expect(403)
  34. await request(app()).post(`/api/desktop-host/v1/${hostId}/exchange`).set('Authorization','Bearer wrong-token-wrong-token-wrong').set('x-desktop-host-protocol','1').send(body()).expect(403)
  35. await request(app()).post(`/api/desktop-host/v1/11111111-2222-4333-8444-555555555555/exchange`).set('Authorization','Bearer '+token).set('x-desktop-host-protocol','1').send(body()).expect(403)
  36. })
  37. it('requires the machine protocol header and POST method',async()=>{
  38. await enroll()
  39. await request(app()).post(`/api/desktop-host/v1/${hostId}/exchange`).set('Authorization','Bearer '+token).send(body()).expect(409)
  40. await request(app()).get(`/api/desktop-host/v1/${hostId}/exchange`).set('Authorization','Bearer '+token).set('x-desktop-host-protocol','1').send(body()).expect(405)
  41. })
  42. it('forwards a valid exchange to the desktop environment and reports it online',async()=>{
  43. await enroll()
  44. const response=await post(`/api/desktop-host/v1/${hostId}/exchange`).send(body()).expect(200)
  45. expect(response.body).toEqual({commands:[]})
  46. expect(exchanged).toHaveLength(1)
  47. expect(exchanged[0].host.name).toBe('书房 iMac')
  48. const list=(await request(app()).get('/api/app/admin/desktop-hosts').expect(200)).body
  49. expect(list.hosts[0]).toMatchObject({online:true,hostName:'书房 iMac',platform:'darwin'})
  50. })
  51. it('revocation fences the transport and notifies the live session',async()=>{
  52. const dropped:string[]=[]
  53. hub.drop=id=>dropped.push(id)
  54. await enroll()
  55. await post(`/api/desktop-host/v1/${hostId}/exchange`).send(body()).expect(200)
  56. await request(app()).delete(`/api/app/admin/desktop-hosts/${hostId}`).expect(200)
  57. expect(dropped).toEqual([hostId])
  58. await post(`/api/desktop-host/v1/${hostId}/exchange`).send(body()).expect(403)
  59. })
  60. it('renames the local computer and a paired host without touching the token', async () => {
  61. await enroll()
  62. const local = await request(app()).put('/api/app/admin/desktop-hosts/local/name').send({ name: '工作室' }).expect(200)
  63. expect(local.body).toEqual({ id: 'local', name: '工作室' })
  64. const renamed = await request(app()).put(`/api/app/admin/desktop-hosts/${hostId}/name`).send({ name: '前台' }).expect(200)
  65. expect(renamed.body.name).toBe('前台')
  66. const list = (await request(app()).get('/api/app/admin/desktop-hosts').expect(200)).body
  67. expect(list.localName).toBe('工作室')
  68. expect(list.hosts[0].displayName).toBe('前台')
  69. expect(JSON.stringify(list)).not.toContain(token)
  70. })
  71. it('validates the exchange payload shape',async()=>{
  72. await enroll()
  73. await post(`/api/desktop-host/v1/${hostId}/exchange`).send({host:{id:'not-a-uuid',name:'x',platform:'darwin',screen:true,accessibility:true,approved:[]},results:[]}).expect(400)
  74. await post(`/api/desktop-host/v1/${hostId}/exchange`).send('string').expect(400)
  75. })
  76. it('reuses the same computer when the installed Mac logs in again',async()=>{
  77. const installId = '22222222-2222-4222-8222-222222222222'
  78. const first = await request(app()).post('/api/app/admin/desktop-hosts').send({ name: '书房 iMac', installId }).expect(201)
  79. const again = await request(app()).post('/api/app/admin/desktop-hosts').send({ name: '书房 iMac', installId }).expect(201)
  80. expect(again.body.host.id).toBe(first.body.host.id)
  81. expect(again.body.token).not.toBe(first.body.token)
  82. const list = (await request(app()).get('/api/app/admin/desktop-hosts').expect(200)).body
  83. expect(list.hosts.filter((host: { enabled: boolean }) => host.enabled)).toHaveLength(1)
  84. })
  85. it('re-enables the exact disabled legacy computer after an explicit administrator login',async()=>{
  86. const first=await request(app()).post('/api/app/admin/desktop-hosts').send({name:'旧版 Mac'}).expect(201)
  87. await request(app()).delete(`/api/app/admin/desktop-hosts/${first.body.host.id}`).expect(200)
  88. const installId='33333333-3333-4333-8333-333333333333'
  89. const restored=await request(app()).post('/api/app/admin/desktop-hosts').send({name:'旧版 Mac',installId,previousHostId:first.body.host.id}).expect(201)
  90. expect(restored.body.host).toMatchObject({id:first.body.host.id,installId,enabled:true})
  91. expect(restored.body.token).not.toBe(first.body.token)
  92. const list=(await request(app()).get('/api/app/admin/desktop-hosts').expect(200)).body
  93. expect(list.hosts).toHaveLength(1)
  94. expect(list.hosts[0]).toMatchObject({id:first.body.host.id,installId,enabled:true})
  95. await request(app()).post(`/api/desktop-host/v1/${first.body.host.id}/exchange`)
  96. .set('Authorization','Bearer '+first.body.token)
  97. .set('x-desktop-host-protocol','1')
  98. .send(body())
  99. .expect(403)
  100. })
  101. it('admits optional negotiated environment facts through the same remote transport schema',async()=>{
  102. await enroll()
  103. const input=body()
  104. input.host.fileTransferVersion=1
  105. input.host.environment={version:1,osRelease:'25.0.0',arch:'arm64',shell:'/bin/zsh',homeDirectory:'/Users/fixture',defaultCwd:'/Users/fixture',fileRoots:['/Users/fixture'],shellScope:'user',timezone:'Asia/Shanghai'}
  106. await post(`/api/desktop-host/v1/${hostId}/exchange`).send(input).expect(200)
  107. expect(exchanged[0]?.host.environment).toEqual(input.host.environment)
  108. await post(`/api/desktop-host/v1/${hostId}/exchange`).send({...input,host:{...input.host,environment:{...input.host.environment,token:'must-not-be-metadata'}}}).expect(400)
  109. expect(exchanged).toHaveLength(1)
  110. })