systemUpdate.test.ts 1.8 KB

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