Files
speckle-server/packages/server/modules/fileuploads/tests/fileuploads.spec.ts
T
Daniel Gak Anagrov b15d5b836c feat(fileupload): create new endpoint for file upload (#4693)
* fix(server): remove redundant code from bad merge

* wrong router

* WIP: http api for receiving file import results

* Lints

* fix

* WIP: tests

* test fix

* auth middleware has implicit requirement for param named streamId

* complete tests and fixes

* can run the old way alongside the existing, no conflicts

* tidying

* correct error thrown

* feat(fileUploads): new upload endpoint skeleton

* feat(fileUploads): added coverage for new file importer endpoint

* fix(workspaces): added tests, fix bugged on handling after stream

* refactor: removed useless file

* fix: comments, added tests

* feat: added modelId to file upload table

* fix: ensureError log

---------

Co-authored-by: Iain Sproat <68657+iainsproat@users.noreply.github.com>
2025-05-09 00:16:29 +02:00

93 lines
3.3 KiB
TypeScript

import cryptoRandomString from 'crypto-random-string'
import { db } from '@/db/knex'
import { getStreamBranchByNameFactory } from '@/modules/core/repositories/branches'
import {
getFileInfoFactory,
saveUploadFileFactory
} from '@/modules/fileuploads/repositories/fileUploads'
import { insertNewUploadAndNotifyFactory } from '@/modules/fileuploads/services/management'
import { publish } from '@/modules/shared/utils/subscriptions'
import { testLogger as logger } from '@/observability/logging'
import { sleep } from '@/test/helpers'
import { expect } from 'chai'
import { FileUploadConvertedStatus } from '@/modules/fileuploads/helpers/types'
import { TIME } from '@speckle/shared'
import { initUploadTestEnvironment } from '@/modules/fileuploads/tests/helpers/init'
const { createStream, createUser, garbageCollector } = initUploadTestEnvironment()
describe('FileUploads @fileuploads', () => {
const userOne = {
name: cryptoRandomString({ length: 10 }),
email: `${cryptoRandomString({ length: 10 })}@example.org`,
password: cryptoRandomString({ length: 10 })
}
let userOneId: string
let createdStreamId: string
before(async () => {
userOneId = await createUser(userOne)
})
beforeEach(async () => {
createdStreamId = await createStream({ ownerId: userOneId })
})
afterEach(async () => {
createdStreamId = ''
})
describe('Convert files', () => {
it('Should garbage collect expired files', async () => {
const insertNewUploadAndNotify = insertNewUploadAndNotifyFactory({
getStreamBranchByName: getStreamBranchByNameFactory({ db }),
saveUploadFile: saveUploadFileFactory({ db }),
publish
})
const fileId = cryptoRandomString({ length: 10 })
await insertNewUploadAndNotify({
streamId: createdStreamId,
branchName: 'main',
userId: userOneId,
fileId,
fileName: 'testfile.txt',
fileSize: 100,
fileType: 'text/plain'
})
await sleep(2000)
await garbageCollector({ logger, timeoutThresholdSeconds: 1 })
const results = await getFileInfoFactory({ db })({ fileId })
if (!results) {
expect(results).to.not.be.undefined
return //HACK to appease typescript
}
expect(results.convertedStatus).to.be.equal(FileUploadConvertedStatus.Error)
})
it('Should not garbage collect files that are not expired', async () => {
const insertNewUploadAndNotify = insertNewUploadAndNotifyFactory({
getStreamBranchByName: getStreamBranchByNameFactory({ db }),
saveUploadFile: saveUploadFileFactory({ db }),
publish
})
const fileId = cryptoRandomString({ length: 10 })
await insertNewUploadAndNotify({
streamId: createdStreamId,
branchName: 'main',
userId: userOneId,
fileId,
fileName: 'testfile.txt',
fileSize: 100,
fileType: 'text/plain'
})
// timeout far in the future, so it won't be garbage collected
await garbageCollector({ logger, timeoutThresholdSeconds: 1 * TIME.hour })
const results = await getFileInfoFactory({ db })({ fileId })
if (!results) {
expect(results).to.not.be.undefined
return //HACK to appease typescript
}
expect(results.convertedStatus).to.be.equal(FileUploadConvertedStatus.Queued)
})
})
})