allowedHostsPanel.test.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { flushPromises, mount } from '@vue/test-utils'
  2. import { afterEach, describe, expect, it, vi } from 'vitest'
  3. import { clearApiSecurityContext, setApiCsrfToken } from '@/api/client'
  4. import SystemManagementPanel from '@/components/app/SystemManagementPanel.vue'
  5. afterEach(() => {
  6. clearApiSecurityContext()
  7. vi.unstubAllGlobals()
  8. vi.restoreAllMocks()
  9. })
  10. describe('allowed hosts settings panel', () => {
  11. it('shows environment entries separately and saves Web-managed domains and IPs', async () => {
  12. const calls: Array<{ path: string; init?: RequestInit }> = []
  13. vi.stubGlobal('fetch', vi.fn(async (path: string, init?: RequestInit) => {
  14. calls.push({ path, init })
  15. const editableHosts = init?.method === 'PUT'
  16. ? (JSON.parse(String(init.body)) as { hosts: string[] }).hosts
  17. : ['old.example.com']
  18. return new Response(JSON.stringify({
  19. source: 'file',
  20. hosts: ['127.0.0.1', ...editableHosts],
  21. editableHosts,
  22. environmentHosts: ['127.0.0.1', 'localhost'],
  23. }), { status: 200, headers: { 'content-type': 'application/json' } })
  24. }))
  25. setApiCsrfToken('csrf-panel')
  26. const wrapper = mount(SystemManagementPanel, {
  27. props: { section: 'connection', active: true },
  28. global: { stubs: { AppIcon: true } },
  29. })
  30. await flushPromises()
  31. expect(wrapper.text()).toContain('外网访问地址')
  32. expect(wrapper.text()).toContain('环境变量保留地址(Web 中不能移除)')
  33. expect(wrapper.get<HTMLTextAreaElement>('textarea[name="allowed-hosts"]').element.value).toBe('old.example.com')
  34. await wrapper.get('textarea[name="allowed-hosts"]').setValue('yaoyao.example.com\n203.0.113.10')
  35. const save = wrapper.findAll('button').find(button => button.text() === '保存访问地址')
  36. expect(save).toBeDefined()
  37. await save!.trigger('submit')
  38. await flushPromises()
  39. expect(calls.map(call => call.path)).toEqual([
  40. '/api/app/system/allowed-hosts',
  41. '/api/app/admin/upstream-connection',
  42. '/api/app/system/allowed-hosts',
  43. ])
  44. expect(calls[2]!.init?.body).toBe(JSON.stringify({
  45. hosts: ['yaoyao.example.com', '203.0.113.10'],
  46. }))
  47. expect(wrapper.text()).toContain('允许的访问地址已保存并立即生效')
  48. })
  49. })