mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-31 16:26:30 +00:00
fix(frontend): harden account data batch import
This commit is contained in:
@@ -4,11 +4,13 @@ import ImportDataModal from '@/components/admin/account/ImportDataModal.vue'
|
||||
|
||||
const showError = vi.fn()
|
||||
const showSuccess = vi.fn()
|
||||
const showWarning = vi.fn()
|
||||
|
||||
vi.mock('@/stores/app', () => ({
|
||||
useAppStore: () => ({
|
||||
showError,
|
||||
showSuccess
|
||||
showSuccess,
|
||||
showWarning
|
||||
})
|
||||
}))
|
||||
|
||||
@@ -26,50 +28,110 @@ vi.mock('vue-i18n', () => ({
|
||||
})
|
||||
}))
|
||||
|
||||
const mountModal = () =>
|
||||
mount(ImportDataModal, {
|
||||
props: { show: true },
|
||||
global: {
|
||||
stubs: {
|
||||
BaseDialog: { template: '<div><slot /><slot name="footer" /></div>' }
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const makeJsonFile = (name: string, content: string, type = 'application/json') => {
|
||||
const file = new File([content], name, { type })
|
||||
Object.defineProperty(file, 'text', {
|
||||
value: () => Promise.resolve(content)
|
||||
})
|
||||
return file
|
||||
}
|
||||
|
||||
const setInputFiles = (element: Element, files: File[]) => {
|
||||
Object.defineProperty(element, 'files', {
|
||||
value: files,
|
||||
configurable: true
|
||||
})
|
||||
}
|
||||
|
||||
describe('ImportDataModal', () => {
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
showError.mockReset()
|
||||
showSuccess.mockReset()
|
||||
showWarning.mockReset()
|
||||
const { adminAPI } = await import('@/api/admin')
|
||||
vi.mocked(adminAPI.accounts.importData).mockReset()
|
||||
})
|
||||
|
||||
it('未选择文件时提示错误', async () => {
|
||||
const wrapper = mount(ImportDataModal, {
|
||||
props: { show: true },
|
||||
global: {
|
||||
stubs: {
|
||||
BaseDialog: { template: '<div><slot /><slot name="footer" /></div>' }
|
||||
}
|
||||
}
|
||||
})
|
||||
const wrapper = mountModal()
|
||||
|
||||
await wrapper.find('form').trigger('submit')
|
||||
expect(showError).toHaveBeenCalledWith('admin.accounts.dataImportSelectFile')
|
||||
})
|
||||
|
||||
it('无效 JSON 时提示解析失败', async () => {
|
||||
const wrapper = mount(ImportDataModal, {
|
||||
props: { show: true },
|
||||
global: {
|
||||
stubs: {
|
||||
BaseDialog: { template: '<div><slot /><slot name="footer" /></div>' }
|
||||
}
|
||||
}
|
||||
})
|
||||
it('无效 JSON 时按文件名提示解析失败', async () => {
|
||||
const { adminAPI } = await import('@/api/admin')
|
||||
const wrapper = mountModal()
|
||||
|
||||
const input = wrapper.find('input[type="file"]')
|
||||
const file = new File(['invalid json'], 'data.json', { type: 'application/json' })
|
||||
Object.defineProperty(file, 'text', {
|
||||
value: () => Promise.resolve('invalid json')
|
||||
})
|
||||
Object.defineProperty(input.element, 'files', {
|
||||
value: [file]
|
||||
})
|
||||
setInputFiles(input.element, [makeJsonFile('data.json', 'invalid json')])
|
||||
|
||||
await input.trigger('change')
|
||||
await wrapper.find('form').trigger('submit')
|
||||
await Promise.resolve()
|
||||
await flushPromises()
|
||||
|
||||
expect(showError).toHaveBeenCalledWith('admin.accounts.dataImportParseFailed')
|
||||
expect(showError).toHaveBeenCalledWith('admin.accounts.dataImportParseFailedFile')
|
||||
expect(adminAPI.accounts.importData).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('不是导出数据的 JSON 按文件名拒绝', async () => {
|
||||
const { adminAPI } = await import('@/api/admin')
|
||||
const wrapper = mountModal()
|
||||
|
||||
const input = wrapper.find('input[type="file"]')
|
||||
setInputFiles(input.element, [makeJsonFile('random.json', JSON.stringify({ name: 'test' }))])
|
||||
|
||||
await input.trigger('change')
|
||||
await wrapper.find('form').trigger('submit')
|
||||
await flushPromises()
|
||||
|
||||
expect(showError).toHaveBeenCalledWith('admin.accounts.dataImportInvalidFile')
|
||||
expect(adminAPI.accounts.importData).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('无有效 JSON 的选择不清空已有选择', async () => {
|
||||
const { adminAPI } = await import('@/api/admin')
|
||||
vi.mocked(adminAPI.accounts.importData).mockResolvedValue({
|
||||
proxy_created: 0,
|
||||
proxy_reused: 0,
|
||||
proxy_failed: 0,
|
||||
account_created: 1,
|
||||
account_failed: 0
|
||||
})
|
||||
|
||||
const wrapper = mountModal()
|
||||
const input = wrapper.find('input[type="file"]')
|
||||
|
||||
const valid = makeJsonFile(
|
||||
'valid.json',
|
||||
JSON.stringify({ exported_at: '2026-07-05T00:00:00Z', proxies: [], accounts: [{ name: 'a' }] })
|
||||
)
|
||||
setInputFiles(input.element, [valid])
|
||||
await input.trigger('change')
|
||||
|
||||
setInputFiles(input.element, [new File(['hello'], 'notes.txt', { type: 'text/plain' })])
|
||||
await input.trigger('change')
|
||||
expect(showError).toHaveBeenCalledWith('admin.accounts.dataImportSelectFile')
|
||||
|
||||
await wrapper.find('form').trigger('submit')
|
||||
await flushPromises()
|
||||
|
||||
expect(adminAPI.accounts.importData).toHaveBeenCalledWith({
|
||||
data: expect.objectContaining({
|
||||
accounts: [{ name: 'a' }]
|
||||
}),
|
||||
skip_default_group_bind: true
|
||||
})
|
||||
})
|
||||
|
||||
it('merges multiple selected JSON files before importing', async () => {
|
||||
@@ -82,32 +144,22 @@ describe('ImportDataModal', () => {
|
||||
account_failed: 0
|
||||
})
|
||||
|
||||
const wrapper = mount(ImportDataModal, {
|
||||
props: { show: true },
|
||||
global: {
|
||||
stubs: {
|
||||
BaseDialog: { template: '<div><slot /><slot name="footer" /></div>' }
|
||||
}
|
||||
}
|
||||
})
|
||||
const wrapper = mountModal()
|
||||
|
||||
const input = wrapper.find('input[type="file"]')
|
||||
const first = new File([
|
||||
const first = makeJsonFile(
|
||||
'first.json',
|
||||
JSON.stringify({ exported_at: '2026-07-05T00:00:00Z', proxies: [], accounts: [{ name: 'a' }] })
|
||||
], 'first.json', { type: 'application/json' })
|
||||
const second = new File([
|
||||
JSON.stringify({ exported_at: '2026-07-05T00:00:01Z', proxies: [{ proxy_key: 'p' }], accounts: [{ name: 'b' }] })
|
||||
], 'second.json', { type: 'application/json' })
|
||||
Object.defineProperty(first, 'text', {
|
||||
value: () => Promise.resolve(JSON.stringify({ exported_at: '2026-07-05T00:00:00Z', proxies: [], accounts: [{ name: 'a' }] }))
|
||||
})
|
||||
Object.defineProperty(second, 'text', {
|
||||
value: () => Promise.resolve(JSON.stringify({ exported_at: '2026-07-05T00:00:01Z', proxies: [{ proxy_key: 'p' }], accounts: [{ name: 'b' }] }))
|
||||
})
|
||||
|
||||
Object.defineProperty(input.element, 'files', {
|
||||
value: [first, second]
|
||||
})
|
||||
)
|
||||
const second = makeJsonFile(
|
||||
'second.json',
|
||||
JSON.stringify({
|
||||
exported_at: '2026-07-05T00:00:01Z',
|
||||
proxies: [{ proxy_key: 'p' }],
|
||||
accounts: [{ name: 'b' }]
|
||||
})
|
||||
)
|
||||
setInputFiles(input.element, [first, second])
|
||||
|
||||
await input.trigger('change')
|
||||
await wrapper.find('form').trigger('submit')
|
||||
@@ -122,4 +174,41 @@ describe('ImportDataModal', () => {
|
||||
})
|
||||
expect(showSuccess).toHaveBeenCalledWith('admin.accounts.dataImportSuccess')
|
||||
})
|
||||
|
||||
it('部分成功时关闭弹窗仍通知父组件刷新', async () => {
|
||||
const { adminAPI } = await import('@/api/admin')
|
||||
vi.mocked(adminAPI.accounts.importData).mockResolvedValue({
|
||||
proxy_created: 0,
|
||||
proxy_reused: 0,
|
||||
proxy_failed: 0,
|
||||
account_created: 1,
|
||||
account_failed: 1
|
||||
})
|
||||
|
||||
const wrapper = mountModal()
|
||||
const input = wrapper.find('input[type="file"]')
|
||||
setInputFiles(input.element, [
|
||||
makeJsonFile(
|
||||
'mixed.json',
|
||||
JSON.stringify({
|
||||
exported_at: '2026-07-05T00:00:00Z',
|
||||
proxies: [],
|
||||
accounts: [{ name: 'a' }, { name: 'b' }]
|
||||
})
|
||||
)
|
||||
])
|
||||
|
||||
await input.trigger('change')
|
||||
await wrapper.find('form').trigger('submit')
|
||||
await flushPromises()
|
||||
|
||||
expect(showError).toHaveBeenCalledWith('admin.accounts.dataImportCompletedWithErrors')
|
||||
expect(wrapper.emitted('imported')).toBeUndefined()
|
||||
|
||||
// 第二个 btn-secondary 是 footer 的取消按钮(第一个是选择文件)
|
||||
await wrapper.findAll('button.btn-secondary')[1]!.trigger('click')
|
||||
|
||||
expect(wrapper.emitted('imported')).toHaveLength(1)
|
||||
expect(wrapper.emitted('close')).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user