|
|
@@ -0,0 +1,102 @@
|
|
|
+import { formatFileSize, validateBackgroundFile } from '@/utils/teamBackgroundFile'
|
|
|
+import request from '@/utils/request2'
|
|
|
+import {
|
|
|
+ completePublicUpload,
|
|
|
+ createTeamBackgroundSession,
|
|
|
+ disableTeamBackgroundFile,
|
|
|
+ downloadTeamBackgroundFile,
|
|
|
+ getPublicUploadSession,
|
|
|
+ listTeamBackgroundFiles,
|
|
|
+ uploadPublicTeamBackgroundFile,
|
|
|
+ uploadTeamBackgroundFile,
|
|
|
+ verifyPublicUploadCode
|
|
|
+} from '@/api/teamBackgroundFile'
|
|
|
+
|
|
|
+jest.mock('@/utils/request2', () => jest.fn())
|
|
|
+
|
|
|
+class TestFormData {
|
|
|
+ constructor() {
|
|
|
+ this.entries = []
|
|
|
+ }
|
|
|
+
|
|
|
+ append(name, value) {
|
|
|
+ this.entries.push([name, value])
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+global.FormData = TestFormData
|
|
|
+
|
|
|
+beforeEach(() => {
|
|
|
+ request.mockReset()
|
|
|
+})
|
|
|
+
|
|
|
+test('accepts all approved extensions case-insensitively', () => {
|
|
|
+ for (const ext of ['pdf','doc','docx','xls','xlsx','ppt','pptx','txt','md','csv']) {
|
|
|
+ expect(validateBackgroundFile({ name: `file.${ext.toUpperCase()}`, size: 1 })).toBe('')
|
|
|
+ }
|
|
|
+})
|
|
|
+
|
|
|
+test('rejects archives, 50MB overflow and names over 255 chars', () => {
|
|
|
+ expect(validateBackgroundFile({ name: 'file.zip', size: 1 })).toContain('文件类型')
|
|
|
+ expect(validateBackgroundFile({ name: 'file.pdf', size: 50 * 1024 * 1024 + 1 })).toContain('50MB')
|
|
|
+ expect(validateBackgroundFile({ name: `${'a'.repeat(252)}.pdf`, size: 1 })).toContain('255')
|
|
|
+})
|
|
|
+
|
|
|
+test('formats byte counts for the table', () => {
|
|
|
+ expect(formatFileSize(0)).toBe('0 B')
|
|
|
+ expect(formatFileSize(1536)).toBe('1.5 KB')
|
|
|
+})
|
|
|
+
|
|
|
+test('uses backend-controlled authenticated file routes', () => {
|
|
|
+ listTeamBackgroundFiles(7)
|
|
|
+ createTeamBackgroundSession(7)
|
|
|
+ disableTeamBackgroundFile(7, 12)
|
|
|
+ downloadTeamBackgroundFile(7, 12)
|
|
|
+
|
|
|
+ expect(request.mock.calls).toEqual([
|
|
|
+ [{ url: '/core/userteam/7/background-files', method: 'get' }],
|
|
|
+ [{ url: '/core/userteam/7/background-upload-session', method: 'post' }],
|
|
|
+ [{ url: '/core/userteam/7/background-files/12', method: 'delete' }],
|
|
|
+ [{ url: '/core/userteam/7/background-files/12/download', method: 'get', responseType: 'blob' }]
|
|
|
+ ])
|
|
|
+})
|
|
|
+
|
|
|
+test('uploads authenticated files without client-supplied identity fields', () => {
|
|
|
+ const file = { name: 'brief.pdf' }
|
|
|
+
|
|
|
+ uploadTeamBackgroundFile(7, file)
|
|
|
+
|
|
|
+ const config = request.mock.calls[0][0]
|
|
|
+ expect(config).toEqual({
|
|
|
+ url: '/core/userteam/7/background-files',
|
|
|
+ method: 'post',
|
|
|
+ data: expect.any(TestFormData)
|
|
|
+ })
|
|
|
+ expect(config.data.entries).toEqual([['file', file]])
|
|
|
+})
|
|
|
+
|
|
|
+test('uses fixed public routes and sends only server-issued credentials', () => {
|
|
|
+ const file = { name: 'brief.pdf' }
|
|
|
+ const onProgress = jest.fn()
|
|
|
+
|
|
|
+ getPublicUploadSession('session-token')
|
|
|
+ verifyPublicUploadCode('123456')
|
|
|
+ completePublicUpload('upload-token')
|
|
|
+ uploadPublicTeamBackgroundFile('upload-token', file, onProgress)
|
|
|
+
|
|
|
+ const uploadData = request.mock.calls[3][0].data
|
|
|
+ expect(request.mock.calls).toEqual([
|
|
|
+ [{ url: '/public/team-background-upload/session', method: 'get', params: { token: 'session-token' } }],
|
|
|
+ [{ url: '/public/team-background-upload/verify', method: 'post', data: { code: '123456' } }],
|
|
|
+ [{ url: '/public/team-background-upload/complete', method: 'post', headers: { 'X-Upload-Token': 'upload-token' } }],
|
|
|
+ [{
|
|
|
+ url: '/public/team-background-upload/files',
|
|
|
+ method: 'post',
|
|
|
+ data: uploadData,
|
|
|
+ headers: { 'X-Upload-Token': 'upload-token' },
|
|
|
+ onUploadProgress: onProgress
|
|
|
+ }]
|
|
|
+ ])
|
|
|
+ expect(uploadData).toEqual(expect.any(TestFormData))
|
|
|
+ expect(uploadData.entries).toEqual([['file', file]])
|
|
|
+})
|