teamBackgroundFile.spec.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { formatFileSize, validateBackgroundFile } from '@/utils/teamBackgroundFile'
  2. import request from '@/utils/request2'
  3. import {
  4. completePublicUpload,
  5. createTeamBackgroundSession,
  6. disableTeamBackgroundFile,
  7. downloadTeamBackgroundFile,
  8. getPublicUploadSession,
  9. listTeamBackgroundFiles,
  10. uploadPublicTeamBackgroundFile,
  11. uploadTeamBackgroundFile,
  12. verifyPublicUploadCode
  13. } from '@/api/teamBackgroundFile'
  14. jest.mock('@/utils/request2', () => jest.fn())
  15. class TestFormData {
  16. constructor() {
  17. this.entries = []
  18. }
  19. append(name, value) {
  20. this.entries.push([name, value])
  21. }
  22. }
  23. global.FormData = TestFormData
  24. beforeEach(() => {
  25. request.mockReset()
  26. })
  27. test('accepts all approved extensions case-insensitively', () => {
  28. for (const ext of ['pdf','doc','docx','xls','xlsx','ppt','pptx','txt','md','csv']) {
  29. expect(validateBackgroundFile({ name: `file.${ext.toUpperCase()}`, size: 1 })).toBe('')
  30. }
  31. })
  32. test('rejects archives, 50MB overflow and names over 255 chars', () => {
  33. expect(validateBackgroundFile({ name: 'file.zip', size: 1 })).toContain('文件类型')
  34. expect(validateBackgroundFile({ name: 'file.pdf', size: 50 * 1024 * 1024 + 1 })).toContain('50MB')
  35. expect(validateBackgroundFile({ name: `${'a'.repeat(252)}.pdf`, size: 1 })).toContain('255')
  36. })
  37. test('formats byte counts for the table', () => {
  38. expect(formatFileSize(0)).toBe('0 B')
  39. expect(formatFileSize(1536)).toBe('1.5 KB')
  40. })
  41. test('uses backend-controlled authenticated file routes', () => {
  42. listTeamBackgroundFiles(7)
  43. createTeamBackgroundSession(7)
  44. disableTeamBackgroundFile(7, 12)
  45. downloadTeamBackgroundFile(7, 12)
  46. expect(request.mock.calls).toEqual([
  47. [{ url: '/core/userteam/7/background-files', method: 'get' }],
  48. [{ url: '/core/userteam/7/background-upload-session', method: 'post' }],
  49. [{ url: '/core/userteam/7/background-files/12', method: 'delete' }],
  50. [{ url: '/core/userteam/7/background-files/12/download', method: 'get', responseType: 'blob' }]
  51. ])
  52. })
  53. test('uploads authenticated files without client-supplied identity fields', () => {
  54. const file = { name: 'brief.pdf' }
  55. uploadTeamBackgroundFile(7, file)
  56. const config = request.mock.calls[0][0]
  57. expect(config).toEqual({
  58. url: '/core/userteam/7/background-files',
  59. method: 'post',
  60. data: expect.any(TestFormData)
  61. })
  62. expect(config.data.entries).toEqual([['file', file]])
  63. })
  64. test('uses fixed public routes and sends only server-issued credentials', () => {
  65. const file = { name: 'brief.pdf' }
  66. const onProgress = jest.fn()
  67. getPublicUploadSession('session-token')
  68. verifyPublicUploadCode('123456')
  69. completePublicUpload('upload-token')
  70. uploadPublicTeamBackgroundFile('upload-token', file, onProgress)
  71. const uploadData = request.mock.calls[3][0].data
  72. expect(request.mock.calls).toEqual([
  73. [{ url: '/public/team-background-upload/session', method: 'get', params: { token: 'session-token' } }],
  74. [{ url: '/public/team-background-upload/verify', method: 'post', data: { code: '123456' } }],
  75. [{ url: '/public/team-background-upload/complete', method: 'post', headers: { 'X-Upload-Token': 'upload-token' } }],
  76. [{
  77. url: '/public/team-background-upload/files',
  78. method: 'post',
  79. data: uploadData,
  80. headers: { 'X-Upload-Token': 'upload-token' },
  81. onUploadProgress: onProgress
  82. }]
  83. ])
  84. expect(uploadData).toEqual(expect.any(TestFormData))
  85. expect(uploadData.entries).toEqual([['file', file]])
  86. })