systemUpdate.test.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { afterEach, describe, expect, it, vi } from 'vitest'
  2. import {
  3. applySystemUpdate,
  4. activateSystemUpdate,
  5. checkSystemUpdate,
  6. rollbackSystemUpdate,
  7. systemUpdateJob,
  8. systemUpdateStatus,
  9. } from '@/api/systemUpdate'
  10. import { setApiCsrfToken } from '@/api/client'
  11. afterEach(() => {
  12. vi.unstubAllGlobals()
  13. setApiCsrfToken('')
  14. })
  15. describe('system update client protocol', () => {
  16. it('uses authenticated app routes and protects mutations with CSRF', async () => {
  17. const calls: Array<{ path: string; init?: RequestInit }> = []
  18. vi.stubGlobal('fetch', vi.fn(async (path: string, init?: RequestInit) => {
  19. calls.push({ path, init })
  20. return new Response(JSON.stringify({
  21. current: { schemaVersion: 1, releaseVersion: '0.2.0', webVersion: '0.2.0', pluginVersion: '1.7.1', gitTag: 'v0.2.0' },
  22. versionsMatch: true,
  23. installationMode: 'source',
  24. updateAvailable: false,
  25. }), { status: 200, headers: { 'content-type': 'application/json' } })
  26. }))
  27. setApiCsrfToken('csrf-system-update')
  28. await systemUpdateStatus()
  29. await checkSystemUpdate()
  30. await applySystemUpdate('0.3.0')
  31. await systemUpdateJob('11111111-1111-4111-8111-111111111111')
  32. await rollbackSystemUpdate()
  33. await activateSystemUpdate('11111111-1111-4111-8111-111111111111')
  34. expect(calls.map(call => call.path)).toEqual([
  35. '/api/app/system/update/status',
  36. '/api/app/system/update/check',
  37. '/api/app/system/update/apply',
  38. '/api/app/system/update/jobs/11111111-1111-4111-8111-111111111111',
  39. '/api/app/system/update/rollback',
  40. '/api/app/system/update/activate',
  41. ])
  42. expect(new Headers(calls[1]!.init?.headers).get('X-CSRF-Token')).toBe('csrf-system-update')
  43. expect(calls[2]!.init?.body).toBe(JSON.stringify({ targetVersion: '0.3.0' }))
  44. expect(new Headers(calls[4]!.init?.headers).get('X-CSRF-Token')).toBe('csrf-system-update')
  45. expect(calls[5]!.init?.body).toBe(JSON.stringify({ jobID: '11111111-1111-4111-8111-111111111111' }))
  46. expect(new Headers(calls[5]!.init?.headers).get('X-CSRF-Token')).toBe('csrf-system-update')
  47. })
  48. })