2019-12-23 08:36:12 -05:00
|
|
|
const chai = require('chai')
|
|
|
|
const { expect } = chai
|
|
|
|
const fs = require('fs')
|
2024-11-08 03:31:26 -05:00
|
|
|
const Stream = require('stream')
|
2021-07-12 12:47:19 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2019-12-23 08:36:12 -05:00
|
|
|
const Path = require('path')
|
|
|
|
const FilestoreApp = require('./FilestoreApp')
|
2020-03-13 12:14:06 -04:00
|
|
|
const TestHelper = require('./TestHelper')
|
2022-05-31 10:30:21 -04:00
|
|
|
const fetch = require('node-fetch')
|
2020-01-07 10:05:51 -05:00
|
|
|
const S3 = require('aws-sdk/clients/s3')
|
2019-12-23 08:36:12 -05:00
|
|
|
const { promisify } = require('util')
|
2020-02-12 06:01:51 -05:00
|
|
|
const { Storage } = require('@google-cloud/storage')
|
2020-02-17 09:04:42 -05:00
|
|
|
const streamifier = require('streamifier')
|
2019-12-23 08:36:12 -05:00
|
|
|
chai.use(require('chai-as-promised'))
|
2020-03-10 13:54:09 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2023-04-20 09:14:00 -04:00
|
|
|
const ChildProcess = require('child_process')
|
2019-12-23 08:36:12 -05:00
|
|
|
|
|
|
|
const fsWriteFile = promisify(fs.writeFile)
|
|
|
|
const fsStat = promisify(fs.stat)
|
2023-04-20 09:14:00 -04:00
|
|
|
const exec = promisify(ChildProcess.exec)
|
2020-04-02 06:58:43 -04:00
|
|
|
const msleep = promisify(setTimeout)
|
2019-12-23 08:36:12 -05:00
|
|
|
|
2020-02-17 09:04:42 -05:00
|
|
|
if (!process.env.AWS_ACCESS_KEY_ID) {
|
|
|
|
throw new Error('please provide credentials for the AWS S3 test server')
|
|
|
|
}
|
|
|
|
|
2021-07-13 07:04:46 -04:00
|
|
|
process.on('unhandledRejection', e => {
|
2020-03-26 18:07:37 -04:00
|
|
|
// eslint-disable-next-line no-console
|
2020-03-26 11:11:22 -04:00
|
|
|
console.log('** Unhandled Promise Rejection **\n', e)
|
|
|
|
throw e
|
|
|
|
})
|
|
|
|
|
2019-12-23 08:36:12 -05:00
|
|
|
// store settings for multiple backends, so that we can test each one.
|
|
|
|
// fs will always be available - add others if they are configured
|
2024-10-31 08:08:26 -04:00
|
|
|
const { BackendSettings, s3Config } = require('./TestConfig')
|
2024-10-31 08:08:41 -04:00
|
|
|
const {
|
|
|
|
AlreadyWrittenError,
|
2024-11-08 03:31:26 -05:00
|
|
|
NotFoundError,
|
2024-10-31 08:08:41 -04:00
|
|
|
NotImplementedError,
|
|
|
|
} = require('@overleaf/object-persistor/src/Errors')
|
2019-12-23 08:36:12 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('Filestore', function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
this.timeout(1000 * 10)
|
2024-04-25 08:56:00 -04:00
|
|
|
const filestoreUrl = `http://127.0.0.1:${Settings.internal.filestore.port}`
|
2019-12-23 08:36:12 -05:00
|
|
|
|
2020-04-02 06:58:43 -04:00
|
|
|
const seenSockets = []
|
|
|
|
async function expectNoSockets() {
|
|
|
|
try {
|
|
|
|
await msleep(1000)
|
2023-04-28 08:54:51 -04:00
|
|
|
const { stdout } = await exec('ss -tn')
|
|
|
|
const lines = stdout.split('\n')
|
|
|
|
const header = lines.shift()
|
2023-04-20 09:14:00 -04:00
|
|
|
|
2020-04-02 06:58:43 -04:00
|
|
|
const badSockets = []
|
2023-04-28 08:54:51 -04:00
|
|
|
for (const socket of lines) {
|
2023-04-20 09:14:00 -04:00
|
|
|
const fields = socket.split(' ').filter(part => part !== '')
|
|
|
|
if (
|
|
|
|
fields.length > 2 &&
|
|
|
|
parseInt(fields[1]) &&
|
|
|
|
!seenSockets.includes(socket)
|
|
|
|
) {
|
|
|
|
badSockets.push(socket)
|
|
|
|
seenSockets.push(socket)
|
2020-04-02 06:58:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (badSockets.length) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(
|
|
|
|
'ERR: Sockets still have receive buffer after connection closed'
|
|
|
|
)
|
2023-04-28 08:54:51 -04:00
|
|
|
console.error(header)
|
2020-04-02 06:58:43 -04:00
|
|
|
for (const socket of badSockets) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(socket)
|
|
|
|
}
|
|
|
|
throw new Error('Sockets still open after connection closed')
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
expect(err).not.to.exist
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-23 08:36:12 -05:00
|
|
|
// redefine the test suite for every available backend
|
2024-10-24 07:42:20 -04:00
|
|
|
for (const [backendVariantWithShardNumber, backendSettings] of Object.entries(
|
|
|
|
BackendSettings
|
|
|
|
)) {
|
|
|
|
describe(backendVariantWithShardNumber, function () {
|
2021-08-26 05:51:22 -04:00
|
|
|
let app,
|
|
|
|
previousEgress,
|
|
|
|
previousIngress,
|
|
|
|
metricPrefix,
|
|
|
|
projectId,
|
|
|
|
otherProjectId
|
2019-12-23 08:36:12 -05:00
|
|
|
|
2024-11-08 03:31:26 -05:00
|
|
|
const dataEncryptionKeySize =
|
|
|
|
backendSettings.backend === 's3SSEC' ? 32 : 0
|
|
|
|
|
2023-07-21 07:04:10 -04:00
|
|
|
const BUCKET_NAMES = [
|
|
|
|
process.env.GCS_USER_FILES_BUCKET_NAME,
|
|
|
|
process.env.GCS_TEMPLATE_FILES_BUCKET_NAME,
|
|
|
|
`${process.env.GCS_USER_FILES_BUCKET_NAME}-deleted`,
|
|
|
|
`${process.env.GCS_TEMPLATE_FILES_BUCKET_NAME}-deleted`,
|
|
|
|
]
|
|
|
|
|
2024-11-08 03:31:26 -05:00
|
|
|
before('start filestore with new settings', async function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
// create the app with the relevant filestore settings
|
2024-10-24 07:42:20 -04:00
|
|
|
Settings.filestore = backendSettings
|
2019-12-23 08:36:12 -05:00
|
|
|
app = new FilestoreApp()
|
|
|
|
await app.runServer()
|
|
|
|
})
|
|
|
|
|
2024-10-24 07:42:20 -04:00
|
|
|
if (backendSettings.gcs) {
|
2024-11-08 03:31:26 -05:00
|
|
|
before('create gcs buckets', async function () {
|
2023-07-21 07:04:10 -04:00
|
|
|
// create test buckets for gcs
|
2020-03-13 12:14:06 -04:00
|
|
|
const storage = new Storage(Settings.filestore.gcs.endpoint)
|
2023-07-21 07:04:10 -04:00
|
|
|
for (const bucketName of BUCKET_NAMES) {
|
|
|
|
await storage.createBucket(bucketName)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-11-08 03:31:26 -05:00
|
|
|
after('delete gcs buckets', async function () {
|
2023-07-21 07:04:10 -04:00
|
|
|
// tear down all the gcs buckets
|
|
|
|
const storage = new Storage(Settings.filestore.gcs.endpoint)
|
|
|
|
for (const bucketName of BUCKET_NAMES) {
|
|
|
|
const bucket = storage.bucket(bucketName)
|
|
|
|
await bucket.deleteFiles()
|
|
|
|
await bucket.delete()
|
|
|
|
}
|
2020-02-12 06:01:51 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-11-08 03:31:26 -05:00
|
|
|
after('stop filestore app', async function () {
|
2020-04-02 06:58:43 -04:00
|
|
|
await app.stop()
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach('fetch previous egress metric', async function () {
|
2020-02-12 06:01:51 -05:00
|
|
|
// retrieve previous metrics from the app
|
2024-11-08 03:31:26 -05:00
|
|
|
if (['s3', 's3SSEC', 'gcs'].includes(Settings.filestore.backend)) {
|
|
|
|
metricPrefix = Settings.filestore.backend.replace('SSEC', '')
|
2020-03-13 12:14:06 -04:00
|
|
|
previousEgress = await TestHelper.getMetric(
|
2020-02-12 06:01:51 -05:00
|
|
|
filestoreUrl,
|
|
|
|
`${metricPrefix}_egress`
|
|
|
|
)
|
2019-12-23 08:36:12 -05:00
|
|
|
}
|
2023-10-12 10:51:43 -04:00
|
|
|
projectId = new ObjectId().toString()
|
|
|
|
otherProjectId = new ObjectId().toString()
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should send a 200 for the status endpoint', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(`${filestoreUrl}/status`)
|
|
|
|
expect(response.status).to.equal(200)
|
|
|
|
const body = await response.text()
|
|
|
|
expect(body).to.contain('filestore')
|
|
|
|
expect(body).to.contain('up')
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('with a file on the server', function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
let fileId, fileUrl, constantFileContent
|
2019-12-23 08:36:12 -05:00
|
|
|
|
|
|
|
const localFileReadPath =
|
|
|
|
'/tmp/filestore_acceptance_tests_file_read.txt'
|
|
|
|
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach('upload file', async function () {
|
2023-10-12 10:51:43 -04:00
|
|
|
fileId = new ObjectId().toString()
|
2020-03-10 13:54:09 -04:00
|
|
|
fileUrl = `${filestoreUrl}/project/${projectId}/file/${fileId}`
|
2020-02-17 09:04:42 -05:00
|
|
|
constantFileContent = [
|
|
|
|
'hello world',
|
|
|
|
`line 2 goes here ${Math.random()}`,
|
2021-07-13 07:04:46 -04:00
|
|
|
'there are 3 lines in all',
|
2020-02-17 09:04:42 -05:00
|
|
|
].join('\n')
|
|
|
|
|
|
|
|
await fsWriteFile(localFileReadPath, constantFileContent)
|
2019-12-23 08:36:12 -05:00
|
|
|
|
|
|
|
const readStream = fs.createReadStream(localFileReadPath)
|
2024-10-24 07:42:20 -04:00
|
|
|
const res = await fetch(fileUrl, { method: 'POST', body: readStream })
|
|
|
|
if (!res.ok) throw new Error(res.statusText)
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach('retrieve previous ingress metric', async function () {
|
2020-02-28 06:27:06 -05:00
|
|
|
// The upload request can bump the ingress metric.
|
|
|
|
// The content hash validation might require a full download
|
|
|
|
// in case the ETag field of the upload response is not a md5 sum.
|
2024-11-08 03:31:26 -05:00
|
|
|
if (['s3', 's3SSEC', 'gcs'].includes(Settings.filestore.backend)) {
|
2020-03-13 12:14:06 -04:00
|
|
|
previousIngress = await TestHelper.getMetric(
|
2020-02-12 06:01:51 -05:00
|
|
|
filestoreUrl,
|
|
|
|
`${metricPrefix}_ingress`
|
|
|
|
)
|
2020-02-28 06:27:06 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should return 404 for a non-existant id', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const url = fileUrl + '___this_is_clearly_wrong___'
|
|
|
|
const response = await fetch(url)
|
|
|
|
expect(response.status).to.equal(404)
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should return the file size on a HEAD request', async function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
const expectedLength = Buffer.byteLength(constantFileContent)
|
2022-05-31 10:30:21 -04:00
|
|
|
const res = await fetch(fileUrl, { method: 'HEAD' })
|
|
|
|
expect(res.status).to.equal(200)
|
|
|
|
expect(res.headers.get('Content-Length')).to.equal(
|
2019-12-23 08:36:12 -05:00
|
|
|
expectedLength.toString()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should be able get the file back', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const res = await fetch(fileUrl)
|
|
|
|
const body = await res.text()
|
|
|
|
expect(body).to.equal(constantFileContent)
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
|
2023-06-01 07:38:45 -04:00
|
|
|
it('should send a 200 for the health-check endpoint using the file', async function () {
|
|
|
|
Settings.health_check = {
|
|
|
|
project_id: projectId,
|
|
|
|
file_id: fileId,
|
|
|
|
}
|
|
|
|
const response = await fetch(`${filestoreUrl}/health_check`)
|
|
|
|
expect(response.status).to.equal(200)
|
|
|
|
const body = await response.text()
|
|
|
|
expect(body).to.equal('OK')
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not leak a socket', async function () {
|
2024-10-24 07:42:20 -04:00
|
|
|
const res = await fetch(fileUrl)
|
|
|
|
if (!res.ok) throw new Error(res.statusText)
|
|
|
|
await res.text()
|
2020-04-02 06:58:43 -04:00
|
|
|
await expectNoSockets()
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should be able to get back the first 9 bytes of the file', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const res = await fetch(fileUrl, { headers: { Range: 'bytes=0-8' } })
|
|
|
|
const body = await res.text()
|
|
|
|
expect(body).to.equal('hello wor')
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should be able to get back bytes 4 through 10 of the file', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const res = await fetch(fileUrl, { headers: { Range: 'bytes=4-10' } })
|
|
|
|
const body = await res.text()
|
|
|
|
expect(body).to.equal('o world')
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should be able to delete the file', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(fileUrl, { method: 'DELETE' })
|
|
|
|
expect(response.status).to.equal(204)
|
|
|
|
const response2 = await fetch(fileUrl)
|
|
|
|
expect(response2.status).to.equal(404)
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should be able to copy files', async function () {
|
2023-10-12 10:51:43 -04:00
|
|
|
const newProjectID = new ObjectId().toString()
|
|
|
|
const newFileId = new ObjectId().toString()
|
2020-03-10 13:54:09 -04:00
|
|
|
const newFileUrl = `${filestoreUrl}/project/${newProjectID}/file/${newFileId}`
|
2022-05-31 10:30:21 -04:00
|
|
|
let response = await fetch(newFileUrl, {
|
|
|
|
method: 'PUT',
|
|
|
|
body: JSON.stringify({
|
2019-12-23 08:36:12 -05:00
|
|
|
source: {
|
2020-02-17 09:04:42 -05:00
|
|
|
project_id: projectId,
|
2021-07-13 07:04:46 -04:00
|
|
|
file_id: fileId,
|
|
|
|
},
|
2022-05-31 10:30:21 -04:00
|
|
|
}),
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
2021-07-13 07:04:46 -04:00
|
|
|
},
|
2022-05-31 10:30:21 -04:00
|
|
|
})
|
|
|
|
expect(response.status).to.equal(200)
|
|
|
|
response = await fetch(fileUrl, { method: 'DELETE' })
|
|
|
|
expect(response.status).to.equal(204)
|
|
|
|
response = await fetch(newFileUrl)
|
|
|
|
const body = await response.text()
|
|
|
|
expect(body).to.equal(constantFileContent)
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should be able to overwrite the file', async function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
const newContent = `here is some different content, ${Math.random()}`
|
|
|
|
const readStream = streamifier.createReadStream(newContent)
|
2022-05-31 10:30:21 -04:00
|
|
|
await fetch(fileUrl, { method: 'POST', body: readStream })
|
2020-02-17 09:04:42 -05:00
|
|
|
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(fileUrl)
|
|
|
|
const body = await response.text()
|
|
|
|
expect(body).to.equal(newContent)
|
2020-02-17 09:04:42 -05:00
|
|
|
})
|
|
|
|
|
2024-10-31 08:08:41 -04:00
|
|
|
describe('IfNoneMatch', function () {
|
|
|
|
if (backendSettings.backend === 'fs') {
|
|
|
|
it('should refuse to handle IfNoneMatch', async function () {
|
|
|
|
await expect(
|
|
|
|
app.persistor.sendStream(
|
|
|
|
Settings.filestore.stores.user_files,
|
|
|
|
`${projectId}/${fileId}`,
|
|
|
|
fs.createReadStream(localFileReadPath),
|
|
|
|
{ ifNoneMatch: '*' }
|
|
|
|
)
|
|
|
|
).to.be.rejectedWith(NotImplementedError)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
it('should reject sendStream on the same key with IfNoneMatch', async function () {
|
|
|
|
await expect(
|
|
|
|
app.persistor.sendStream(
|
|
|
|
Settings.filestore.stores.user_files,
|
|
|
|
`${projectId}/${fileId}`,
|
|
|
|
fs.createReadStream(localFileReadPath),
|
|
|
|
{ ifNoneMatch: '*' }
|
|
|
|
)
|
|
|
|
).to.be.rejectedWith(AlreadyWrittenError)
|
|
|
|
})
|
|
|
|
it('should allow sendStream on a different key with IfNoneMatch', async function () {
|
|
|
|
await app.persistor.sendStream(
|
|
|
|
Settings.filestore.stores.user_files,
|
|
|
|
`${projectId}/${fileId}-other`,
|
|
|
|
fs.createReadStream(localFileReadPath),
|
|
|
|
{ ifNoneMatch: '*' }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-10-24 07:42:20 -04:00
|
|
|
if (backendSettings.backend !== 'fs') {
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should record an egress metric for the upload', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
const metric = await TestHelper.getMetric(
|
2020-02-12 06:01:51 -05:00
|
|
|
filestoreUrl,
|
|
|
|
`${metricPrefix}_egress`
|
|
|
|
)
|
2024-11-08 03:31:26 -05:00
|
|
|
expect(metric - previousEgress).to.equal(
|
|
|
|
constantFileContent.length + dataEncryptionKeySize
|
|
|
|
)
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should record an ingress metric when downloading the file', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(fileUrl)
|
|
|
|
expect(response.ok).to.be.true
|
2024-10-24 07:42:20 -04:00
|
|
|
await response.text()
|
2020-03-13 12:14:06 -04:00
|
|
|
const metric = await TestHelper.getMetric(
|
2020-02-12 06:01:51 -05:00
|
|
|
filestoreUrl,
|
|
|
|
`${metricPrefix}_ingress`
|
|
|
|
)
|
2019-12-23 08:36:12 -05:00
|
|
|
expect(metric - previousIngress).to.equal(
|
2024-11-08 03:31:26 -05:00
|
|
|
constantFileContent.length + dataEncryptionKeySize
|
2019-12-23 08:36:12 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should record an ingress metric for a partial download', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(fileUrl, {
|
|
|
|
headers: { Range: 'bytes=0-8' },
|
|
|
|
})
|
|
|
|
expect(response.ok).to.be.true
|
2024-10-24 07:42:20 -04:00
|
|
|
await response.text()
|
2020-03-13 12:14:06 -04:00
|
|
|
const metric = await TestHelper.getMetric(
|
2020-02-12 06:01:51 -05:00
|
|
|
filestoreUrl,
|
|
|
|
`${metricPrefix}_ingress`
|
|
|
|
)
|
2024-11-08 03:31:26 -05:00
|
|
|
expect(metric - previousIngress).to.equal(9 + dataEncryptionKeySize)
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('with multiple files', function () {
|
2021-08-26 05:51:22 -04:00
|
|
|
let fileIds, fileUrls, otherFileUrls, projectUrl, otherProjectUrl
|
2020-01-06 09:14:22 -05:00
|
|
|
const localFileReadPaths = [
|
|
|
|
'/tmp/filestore_acceptance_tests_file_read_1.txt',
|
2021-07-13 07:04:46 -04:00
|
|
|
'/tmp/filestore_acceptance_tests_file_read_2.txt',
|
2021-08-26 05:51:22 -04:00
|
|
|
'/tmp/filestore_acceptance_tests_file_read_3.txt',
|
2020-01-06 09:14:22 -05:00
|
|
|
]
|
|
|
|
const constantFileContents = [
|
|
|
|
[
|
|
|
|
'hello world',
|
|
|
|
`line 2 goes here ${Math.random()}`,
|
2021-07-13 07:04:46 -04:00
|
|
|
'there are 3 lines in all',
|
2020-01-06 09:14:22 -05:00
|
|
|
].join('\n'),
|
|
|
|
[
|
|
|
|
`for reference: ${Math.random()}`,
|
|
|
|
'cats are the best animals',
|
2021-07-13 07:04:46 -04:00
|
|
|
'wombats are a close second',
|
|
|
|
].join('\n'),
|
2021-08-26 05:51:22 -04:00
|
|
|
[
|
|
|
|
`another file: ${Math.random()}`,
|
|
|
|
'with multiple lines',
|
|
|
|
'the end',
|
|
|
|
].join('\n'),
|
2020-01-06 09:14:22 -05:00
|
|
|
]
|
|
|
|
|
2024-11-08 03:31:26 -05:00
|
|
|
before('create local files', async function () {
|
2024-05-22 05:37:08 -04:00
|
|
|
return await Promise.all([
|
2020-01-06 09:14:22 -05:00
|
|
|
fsWriteFile(localFileReadPaths[0], constantFileContents[0]),
|
2021-07-13 07:04:46 -04:00
|
|
|
fsWriteFile(localFileReadPaths[1], constantFileContents[1]),
|
2021-08-26 05:51:22 -04:00
|
|
|
fsWriteFile(localFileReadPaths[2], constantFileContents[2]),
|
2020-01-06 09:14:22 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach('upload two files', async function () {
|
2020-03-16 07:34:45 -04:00
|
|
|
projectUrl = `${filestoreUrl}/project/${projectId}`
|
2021-08-26 05:51:22 -04:00
|
|
|
otherProjectUrl = `${filestoreUrl}/project/${otherProjectId}`
|
|
|
|
fileIds = [
|
2023-10-12 10:51:43 -04:00
|
|
|
new ObjectId().toString(),
|
|
|
|
new ObjectId().toString(),
|
|
|
|
new ObjectId().toString(),
|
2021-08-26 05:51:22 -04:00
|
|
|
]
|
2020-01-06 09:14:22 -05:00
|
|
|
fileUrls = [
|
2020-03-16 07:34:45 -04:00
|
|
|
`${projectUrl}/file/${fileIds[0]}`,
|
2021-07-13 07:04:46 -04:00
|
|
|
`${projectUrl}/file/${fileIds[1]}`,
|
2020-01-06 09:14:22 -05:00
|
|
|
]
|
2021-08-26 05:51:22 -04:00
|
|
|
otherFileUrls = [`${otherProjectUrl}/file/${fileIds[2]}`]
|
2020-01-06 09:14:22 -05:00
|
|
|
|
2022-05-31 10:30:21 -04:00
|
|
|
await Promise.all([
|
|
|
|
fetch(fileUrls[0], {
|
|
|
|
method: 'POST',
|
|
|
|
body: fs.createReadStream(localFileReadPaths[0]),
|
|
|
|
}),
|
|
|
|
fetch(fileUrls[1], {
|
|
|
|
method: 'POST',
|
|
|
|
body: fs.createReadStream(localFileReadPaths[1]),
|
|
|
|
}),
|
|
|
|
fetch(otherFileUrls[0], {
|
|
|
|
method: 'POST',
|
|
|
|
body: fs.createReadStream(localFileReadPaths[2]),
|
|
|
|
}),
|
2020-01-06 09:14:22 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should get the directory size', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(
|
2020-02-17 09:04:42 -05:00
|
|
|
`${filestoreUrl}/project/${projectId}/size`
|
2020-01-06 09:14:22 -05:00
|
|
|
)
|
2022-05-31 10:30:21 -04:00
|
|
|
const body = await response.text()
|
|
|
|
expect(parseInt(JSON.parse(body)['total bytes'])).to.equal(
|
2020-01-06 09:14:22 -05:00
|
|
|
constantFileContents[0].length + constantFileContents[1].length
|
|
|
|
)
|
|
|
|
})
|
2020-03-16 07:34:45 -04:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should store the files', async function () {
|
2020-03-16 07:34:45 -04:00
|
|
|
for (const index in fileUrls) {
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(fileUrls[index])
|
|
|
|
const body = await response.text()
|
|
|
|
expect(body).to.equal(constantFileContents[index])
|
2020-03-16 07:34:45 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should be able to delete the project', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
let response = await fetch(projectUrl, { method: 'DELETE' })
|
|
|
|
expect(response.status).to.equal(204)
|
2020-03-16 07:34:45 -04:00
|
|
|
|
|
|
|
for (const index in fileUrls) {
|
2022-05-31 10:30:21 -04:00
|
|
|
response = await fetch(fileUrls[index])
|
|
|
|
expect(response.status).to.equal(404)
|
2020-03-16 07:34:45 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-08-26 05:51:22 -04:00
|
|
|
it('should not delete files in other projects', async function () {
|
|
|
|
for (const index in otherFileUrls) {
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(otherFileUrls[index])
|
|
|
|
expect(response.status).to.equal(200)
|
2021-08-26 05:51:22 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not delete a partial project id', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(`${filestoreUrl}/project/5`, {
|
|
|
|
method: 'DELETE',
|
|
|
|
})
|
|
|
|
expect(response.status).to.equal(400)
|
2020-03-16 07:34:45 -04:00
|
|
|
})
|
2020-01-06 09:14:22 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('with a large file', function () {
|
2020-02-17 07:13:31 -05:00
|
|
|
let fileId, fileUrl, largeFileContent, error
|
|
|
|
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach('upload large file', async function () {
|
2023-10-12 10:51:43 -04:00
|
|
|
fileId = new ObjectId().toString()
|
2020-03-10 13:54:09 -04:00
|
|
|
fileUrl = `${filestoreUrl}/project/${projectId}/file/${fileId}`
|
2020-02-17 07:13:31 -05:00
|
|
|
|
|
|
|
largeFileContent = '_wombat_'.repeat(1024 * 1024) // 8 megabytes
|
|
|
|
largeFileContent += Math.random()
|
|
|
|
|
|
|
|
const readStream = streamifier.createReadStream(largeFileContent)
|
2024-10-24 07:42:20 -04:00
|
|
|
const res = await fetch(fileUrl, { method: 'POST', body: readStream })
|
|
|
|
if (!res.ok) throw new Error(res.statusText)
|
2020-02-17 07:13:31 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should be able to get the file back', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(fileUrl)
|
|
|
|
const body = await response.text()
|
|
|
|
expect(body).to.equal(largeFileContent)
|
2020-02-17 07:13:31 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not throw an error', function () {
|
2020-02-17 07:13:31 -05:00
|
|
|
expect(error).not.to.exist
|
|
|
|
})
|
2020-04-02 06:58:43 -04:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not leak a socket', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(fileUrl)
|
|
|
|
await response.text()
|
2020-04-02 06:58:43 -04:00
|
|
|
await expectNoSockets()
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not leak a socket if the connection is aborted', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const controller = new AbortController()
|
|
|
|
const response = await fetch(fileUrl, { signal: controller.signal })
|
|
|
|
expect(response.ok).to.be.true
|
|
|
|
controller.abort()
|
|
|
|
await expectNoSockets()
|
2020-04-02 06:58:43 -04:00
|
|
|
})
|
2020-02-17 07:13:31 -05:00
|
|
|
})
|
|
|
|
|
2024-10-24 07:42:20 -04:00
|
|
|
if (
|
|
|
|
(backendSettings.backend === 's3' && !backendSettings.fallback) ||
|
|
|
|
(backendSettings.backend === 'gcs' &&
|
|
|
|
backendSettings.fallback?.backend === 's3')
|
|
|
|
) {
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('with a file in a specific bucket', function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
let constantFileContent, fileId, fileUrl, bucketName
|
2020-01-07 10:05:51 -05:00
|
|
|
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach('upload file into random bucket', async function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
constantFileContent = `This is a file in a different S3 bucket ${Math.random()}`
|
2023-10-12 10:51:43 -04:00
|
|
|
fileId = new ObjectId().toString()
|
2024-10-31 08:08:26 -04:00
|
|
|
bucketName = `random-bucket-${new ObjectId().toString()}`
|
2020-01-07 10:05:51 -05:00
|
|
|
fileUrl = `${filestoreUrl}/bucket/${bucketName}/key/${fileId}`
|
|
|
|
|
2024-10-31 08:08:26 -04:00
|
|
|
const cfg = s3Config()
|
2020-01-07 10:05:51 -05:00
|
|
|
const s3ClientSettings = {
|
|
|
|
credentials: {
|
2024-10-31 08:08:26 -04:00
|
|
|
accessKeyId: process.env.MINIO_ROOT_USER,
|
|
|
|
secretAccessKey: process.env.MINIO_ROOT_PASSWORD,
|
2020-01-07 10:05:51 -05:00
|
|
|
},
|
2024-10-31 08:08:26 -04:00
|
|
|
endpoint: cfg.endpoint,
|
|
|
|
httpOptions: cfg.httpOptions,
|
|
|
|
s3ForcePathStyle: cfg.pathStyle,
|
2020-01-07 10:05:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const s3 = new S3(s3ClientSettings)
|
|
|
|
await s3
|
|
|
|
.createBucket({
|
2021-07-13 07:04:46 -04:00
|
|
|
Bucket: bucketName,
|
2020-01-07 10:05:51 -05:00
|
|
|
})
|
|
|
|
.promise()
|
|
|
|
await s3
|
|
|
|
.upload({
|
|
|
|
Bucket: bucketName,
|
|
|
|
Key: fileId,
|
2021-07-13 07:04:46 -04:00
|
|
|
Body: constantFileContent,
|
2020-01-07 10:05:51 -05:00
|
|
|
})
|
|
|
|
.promise()
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should get the file from the specified bucket', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(fileUrl)
|
|
|
|
const body = await response.text()
|
|
|
|
expect(body).to.equal(constantFileContent)
|
2020-02-17 09:04:42 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-10-24 07:42:20 -04:00
|
|
|
if (backendSettings.backend === 'gcs') {
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when deleting a file in GCS', function () {
|
2024-10-24 07:43:10 -04:00
|
|
|
let fileId, fileUrl, content, error, dateBefore, dateAfter
|
2020-02-17 09:04:42 -05:00
|
|
|
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach('upload and delete file', async function () {
|
2023-10-12 10:51:43 -04:00
|
|
|
fileId = new ObjectId()
|
2020-03-13 12:14:06 -04:00
|
|
|
fileUrl = `${filestoreUrl}/project/${projectId}/file/${fileId}`
|
2020-02-17 09:04:42 -05:00
|
|
|
|
2020-03-13 12:14:06 -04:00
|
|
|
content = '_wombat_' + Math.random()
|
|
|
|
|
|
|
|
const readStream = streamifier.createReadStream(content)
|
2024-10-24 07:42:20 -04:00
|
|
|
let res = await fetch(fileUrl, { method: 'POST', body: readStream })
|
|
|
|
if (!res.ok) throw new Error(res.statusText)
|
2024-10-24 07:43:10 -04:00
|
|
|
dateBefore = new Date()
|
2024-10-24 07:42:20 -04:00
|
|
|
res = await fetch(fileUrl, { method: 'DELETE' })
|
2024-10-24 07:43:10 -04:00
|
|
|
dateAfter = new Date()
|
2024-10-24 07:42:20 -04:00
|
|
|
if (!res.ok) throw new Error(res.statusText)
|
2020-03-13 12:14:06 -04:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not throw an error', function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
expect(error).not.to.exist
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should copy the file to the deleted-files bucket', async function () {
|
2024-10-24 07:43:10 -04:00
|
|
|
let date = dateBefore
|
|
|
|
const keys = []
|
|
|
|
while (date <= dateAfter) {
|
|
|
|
keys.push(`${projectId}/${fileId}-${date.toISOString()}`)
|
|
|
|
date = new Date(date.getTime() + 1)
|
|
|
|
}
|
|
|
|
await TestHelper.expectPersistorToHaveSomeFile(
|
2020-03-13 12:14:06 -04:00
|
|
|
app.persistor,
|
|
|
|
`${Settings.filestore.stores.user_files}-deleted`,
|
2024-10-24 07:43:10 -04:00
|
|
|
keys,
|
2020-03-13 12:14:06 -04:00
|
|
|
content
|
2020-02-17 09:04:42 -05:00
|
|
|
)
|
2020-03-13 12:14:06 -04:00
|
|
|
})
|
2020-02-17 09:04:42 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should remove the file from the original bucket', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorNotToHaveFile(
|
|
|
|
app.persistor,
|
|
|
|
Settings.filestore.stores.user_files,
|
|
|
|
`${projectId}/${fileId}`
|
2020-02-17 09:04:42 -05:00
|
|
|
)
|
2020-03-13 12:14:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2020-02-17 09:04:42 -05:00
|
|
|
|
2024-10-24 07:42:20 -04:00
|
|
|
if (backendSettings.fallback) {
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('with a fallback', function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
let constantFileContent,
|
|
|
|
fileId,
|
|
|
|
fileKey,
|
|
|
|
fileUrl,
|
|
|
|
bucket,
|
|
|
|
fallbackBucket
|
|
|
|
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach('prepare fallback', function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
constantFileContent = `This is yet more file content ${Math.random()}`
|
2023-10-12 10:51:43 -04:00
|
|
|
fileId = new ObjectId().toString()
|
2020-03-10 13:54:09 -04:00
|
|
|
fileKey = `${projectId}/${fileId}`
|
|
|
|
fileUrl = `${filestoreUrl}/project/${projectId}/file/${fileId}`
|
2020-02-17 09:04:42 -05:00
|
|
|
|
|
|
|
bucket = Settings.filestore.stores.user_files
|
|
|
|
fallbackBucket = Settings.filestore.fallback.buckets[bucket]
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('with a file in the fallback bucket', function () {
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach('upload into fallback', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.uploadStringToPersistor(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.fallbackPersistor,
|
|
|
|
fallbackBucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not find file in the primary', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorNotToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.primaryPersistor,
|
|
|
|
bucket,
|
|
|
|
fileKey
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should find the file in the fallback', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.fallbackPersistor,
|
|
|
|
fallbackBucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when copyOnMiss is disabled', function () {
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach('swap copyOnMiss=false', function () {
|
2020-07-07 08:49:54 -04:00
|
|
|
app.persistor.settings.copyOnMiss = false
|
2020-02-17 09:04:42 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should fetch the file', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const res = await fetch(fileUrl)
|
|
|
|
const body = await res.text()
|
|
|
|
expect(body).to.equal(constantFileContent)
|
2020-02-17 09:04:42 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not copy the file to the primary', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(fileUrl)
|
|
|
|
expect(response.ok).to.be.true
|
2024-10-24 07:42:20 -04:00
|
|
|
await response.text()
|
2020-02-17 09:04:42 -05:00
|
|
|
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorNotToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.primaryPersistor,
|
|
|
|
bucket,
|
|
|
|
fileKey
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when copyOnMiss is enabled', function () {
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach('swap copyOnMiss=true', function () {
|
2020-07-07 08:49:54 -04:00
|
|
|
app.persistor.settings.copyOnMiss = true
|
2020-02-17 09:04:42 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should fetch the file', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const res = await fetch(fileUrl)
|
|
|
|
const body = await res.text()
|
|
|
|
expect(body).to.equal(constantFileContent)
|
2020-02-17 09:04:42 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('copies the file to the primary', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(fileUrl)
|
|
|
|
expect(response.ok).to.be.true
|
2024-10-24 07:42:20 -04:00
|
|
|
await response.text()
|
2020-02-17 09:04:42 -05:00
|
|
|
// wait for the file to copy in the background
|
2020-04-02 06:58:43 -04:00
|
|
|
await msleep(1000)
|
2020-02-17 09:04:42 -05:00
|
|
|
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.primaryPersistor,
|
|
|
|
bucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when copying a file', function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
let newFileId, newFileUrl, newFileKey, opts
|
|
|
|
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach('prepare to copy file', function () {
|
2023-10-12 10:51:43 -04:00
|
|
|
const newProjectID = new ObjectId().toString()
|
|
|
|
newFileId = new ObjectId().toString()
|
2020-03-10 13:54:09 -04:00
|
|
|
newFileUrl = `${filestoreUrl}/project/${newProjectID}/file/${newFileId}`
|
|
|
|
newFileKey = `${newProjectID}/${newFileId}`
|
2020-02-17 09:04:42 -05:00
|
|
|
|
|
|
|
opts = {
|
|
|
|
method: 'put',
|
2022-05-31 10:30:21 -04:00
|
|
|
body: JSON.stringify({
|
2020-02-17 09:04:42 -05:00
|
|
|
source: {
|
|
|
|
project_id: projectId,
|
2021-07-13 07:04:46 -04:00
|
|
|
file_id: fileId,
|
|
|
|
},
|
2022-05-31 10:30:21 -04:00
|
|
|
}),
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
2021-07-13 07:04:46 -04:00
|
|
|
},
|
2020-02-17 09:04:42 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when copyOnMiss is false', function () {
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach('copy with copyOnMiss=false', async function () {
|
2020-07-07 08:49:54 -04:00
|
|
|
app.persistor.settings.copyOnMiss = false
|
2020-02-17 09:04:42 -05:00
|
|
|
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(newFileUrl, opts)
|
|
|
|
expect(response.status).to.equal(200)
|
2020-02-17 09:04:42 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should leave the old file in the old bucket', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.fallbackPersistor,
|
|
|
|
fallbackBucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not create a new file in the old bucket', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorNotToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.fallbackPersistor,
|
|
|
|
fallbackBucket,
|
|
|
|
newFileKey
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should create a new file in the new bucket', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.primaryPersistor,
|
|
|
|
bucket,
|
|
|
|
newFileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not copy the old file to the primary with the old key', async function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
// wait for the file to copy in the background
|
2020-04-02 06:58:43 -04:00
|
|
|
await msleep(1000)
|
2020-02-17 09:04:42 -05:00
|
|
|
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorNotToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.primaryPersistor,
|
|
|
|
bucket,
|
|
|
|
fileKey
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when copyOnMiss is true', function () {
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach('copy with copyOnMiss=false', async function () {
|
2020-07-07 08:49:54 -04:00
|
|
|
app.persistor.settings.copyOnMiss = true
|
2020-02-17 09:04:42 -05:00
|
|
|
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(newFileUrl, opts)
|
|
|
|
expect(response.status).to.equal(200)
|
2020-02-17 09:04:42 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should leave the old file in the old bucket', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.fallbackPersistor,
|
|
|
|
fallbackBucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not create a new file in the old bucket', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorNotToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.fallbackPersistor,
|
|
|
|
fallbackBucket,
|
|
|
|
newFileKey
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should create a new file in the new bucket', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.primaryPersistor,
|
|
|
|
bucket,
|
|
|
|
newFileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should copy the old file to the primary with the old key', async function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
// wait for the file to copy in the background
|
2020-04-02 06:58:43 -04:00
|
|
|
await msleep(1000)
|
2020-02-17 09:04:42 -05:00
|
|
|
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.primaryPersistor,
|
|
|
|
bucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when sending a file', function () {
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach('upload file', async function () {
|
2021-07-13 07:04:46 -04:00
|
|
|
const readStream =
|
|
|
|
streamifier.createReadStream(constantFileContent)
|
2024-10-24 07:42:20 -04:00
|
|
|
const res = await fetch(fileUrl, {
|
|
|
|
method: 'POST',
|
|
|
|
body: readStream,
|
|
|
|
})
|
|
|
|
if (!res.ok) throw new Error(res.statusText)
|
2020-02-17 09:04:42 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should store the file on the primary', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.primaryPersistor,
|
|
|
|
bucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not store the file on the fallback', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorNotToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.fallbackPersistor,
|
|
|
|
fallbackBucket,
|
2020-03-10 13:54:09 -04:00
|
|
|
`${projectId}/${fileId}`
|
2020-02-17 09:04:42 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when deleting a file', function () {
|
|
|
|
describe('when the file exists on the primary', function () {
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach('upload into primary', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.uploadStringToPersistor(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.primaryPersistor,
|
|
|
|
bucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should delete the file', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const response1 = await fetch(fileUrl, { method: 'DELETE' })
|
|
|
|
expect(response1.status).to.equal(204)
|
|
|
|
const response2 = await fetch(fileUrl)
|
|
|
|
expect(response2.status).to.equal(404)
|
2020-02-17 09:04:42 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when the file exists on the fallback', function () {
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach('upload into fallback', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.uploadStringToPersistor(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.fallbackPersistor,
|
|
|
|
fallbackBucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should delete the file', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const response1 = await fetch(fileUrl, { method: 'DELETE' })
|
|
|
|
expect(response1.status).to.equal(204)
|
|
|
|
const response2 = await fetch(fileUrl)
|
|
|
|
expect(response2.status).to.equal(404)
|
2020-02-17 09:04:42 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when the file exists on both the primary and the fallback', function () {
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach(
|
|
|
|
'upload into both primary and fallback',
|
|
|
|
async function () {
|
|
|
|
await TestHelper.uploadStringToPersistor(
|
|
|
|
app.persistor.primaryPersistor,
|
|
|
|
bucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
await TestHelper.uploadStringToPersistor(
|
|
|
|
app.persistor.fallbackPersistor,
|
|
|
|
fallbackBucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2020-02-17 09:04:42 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should delete the files', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const response1 = await fetch(fileUrl, { method: 'DELETE' })
|
|
|
|
expect(response1.status).to.equal(204)
|
|
|
|
const response2 = await fetch(fileUrl)
|
|
|
|
expect(response2.status).to.equal(404)
|
2020-02-17 09:04:42 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when the file does not exist', function () {
|
|
|
|
it('should return return 204', async function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
// S3 doesn't give us a 404 when the object doesn't exist, so to stay
|
|
|
|
// consistent we merrily return 204 ourselves here as well
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(fileUrl, { method: 'DELETE' })
|
|
|
|
expect(response.status).to.equal(204)
|
2020-02-17 09:04:42 -05:00
|
|
|
})
|
|
|
|
})
|
2020-01-07 10:05:51 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('with a pdf file', function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
let fileId, fileUrl, localFileSize
|
|
|
|
const localFileReadPath = Path.resolve(
|
|
|
|
__dirname,
|
|
|
|
'../../fixtures/test.pdf'
|
|
|
|
)
|
|
|
|
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach('upload test.pdf', async function () {
|
2023-10-12 10:51:43 -04:00
|
|
|
fileId = new ObjectId().toString()
|
2020-03-10 13:54:09 -04:00
|
|
|
fileUrl = `${filestoreUrl}/project/${projectId}/file/${fileId}`
|
2019-12-23 08:36:12 -05:00
|
|
|
const stat = await fsStat(localFileReadPath)
|
|
|
|
localFileSize = stat.size
|
|
|
|
const readStream = fs.createReadStream(localFileReadPath)
|
2024-10-24 07:42:20 -04:00
|
|
|
const res = await fetch(fileUrl, { method: 'POST', body: readStream })
|
|
|
|
if (!res.ok) throw new Error(res.statusText)
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should be able get the file back', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(fileUrl)
|
|
|
|
const body = await response.text()
|
|
|
|
expect(body.substring(0, 8)).to.equal('%PDF-1.5')
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
|
2024-10-24 07:42:20 -04:00
|
|
|
if (backendSettings.backend !== 'fs') {
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should record an egress metric for the upload', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
const metric = await TestHelper.getMetric(
|
2020-02-12 06:01:51 -05:00
|
|
|
filestoreUrl,
|
|
|
|
`${metricPrefix}_egress`
|
|
|
|
)
|
2024-11-08 03:31:26 -05:00
|
|
|
expect(metric - previousEgress).to.equal(
|
|
|
|
localFileSize + dataEncryptionKeySize
|
|
|
|
)
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('getting the preview image', function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
this.timeout(1000 * 20)
|
|
|
|
let previewFileUrl
|
|
|
|
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach('prepare previewFileUrl for preview', function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
previewFileUrl = `${fileUrl}?style=preview`
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not time out', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(previewFileUrl)
|
|
|
|
expect(response.status).to.equal(200)
|
2024-10-24 07:42:20 -04:00
|
|
|
await response.arrayBuffer()
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should respond with image data', async function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
// note: this test relies of the imagemagick conversion working
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(previewFileUrl)
|
2024-11-08 03:31:26 -05:00
|
|
|
expect(response.status).to.equal(200)
|
2022-05-31 10:30:21 -04:00
|
|
|
const body = await response.text()
|
|
|
|
expect(body.length).to.be.greaterThan(400)
|
|
|
|
expect(body.substr(1, 3)).to.equal('PNG')
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('warming the cache', function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
this.timeout(1000 * 20)
|
|
|
|
let previewFileUrl
|
|
|
|
|
2024-11-08 03:31:26 -05:00
|
|
|
beforeEach('prepare previewFileUrl for cacheWarn', function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
previewFileUrl = `${fileUrl}?style=preview&cacheWarm=true`
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not time out', async function () {
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(previewFileUrl)
|
|
|
|
expect(response.status).to.equal(200)
|
2024-10-24 07:42:20 -04:00
|
|
|
await response.arrayBuffer()
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
|
2023-05-08 07:03:50 -04:00
|
|
|
it('should not leak sockets', async function () {
|
|
|
|
const response1 = await fetch(previewFileUrl)
|
|
|
|
expect(response1.status).to.equal(200)
|
2024-10-24 07:42:20 -04:00
|
|
|
// do not read the response body, should be destroyed immediately
|
2023-05-08 07:03:50 -04:00
|
|
|
const response2 = await fetch(previewFileUrl)
|
|
|
|
expect(response2.status).to.equal(200)
|
2024-10-24 07:42:20 -04:00
|
|
|
// do not read the response body, should be destroyed immediately
|
2023-05-08 07:03:50 -04:00
|
|
|
await expectNoSockets()
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it("should respond with only an 'OK'", async function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
// note: this test relies of the imagemagick conversion working
|
2022-05-31 10:30:21 -04:00
|
|
|
const response = await fetch(previewFileUrl)
|
|
|
|
const body = await response.text()
|
|
|
|
expect(body).to.equal('OK')
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2024-11-08 03:31:26 -05:00
|
|
|
|
|
|
|
describe('with server side encryption', function () {
|
|
|
|
if (backendSettings.backend !== 's3SSEC') return
|
|
|
|
|
|
|
|
before('sanity check top-level variable', function () {
|
|
|
|
expect(dataEncryptionKeySize).to.equal(32)
|
|
|
|
})
|
|
|
|
|
|
|
|
let fileId1, fileId2, fileKey1, fileKey2, fileUrl1, fileUrl2
|
|
|
|
beforeEach('prepare ids', function () {
|
|
|
|
fileId1 = new ObjectId().toString()
|
|
|
|
fileId2 = new ObjectId().toString()
|
|
|
|
fileKey1 = `${projectId}/${fileId1}`
|
|
|
|
fileKey2 = `${projectId}/${fileId2}`
|
|
|
|
fileUrl1 = `${filestoreUrl}/project/${projectId}/file/${fileId1}`
|
|
|
|
fileUrl2 = `${filestoreUrl}/project/${projectId}/file/${fileId2}`
|
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach('ensure DEK is missing', async function () {
|
|
|
|
// Cannot use test helper expectPersistorNotToHaveFile here, we need to use the KEK.
|
|
|
|
await expect(
|
|
|
|
app.persistor.getDataEncryptionKeySize(
|
|
|
|
backendSettings.stores.user_files,
|
|
|
|
fileKey1
|
|
|
|
)
|
|
|
|
).to.rejectedWith(NotFoundError)
|
|
|
|
})
|
|
|
|
|
|
|
|
async function createRandomContent(url, suffix = '') {
|
|
|
|
const content = Math.random().toString() + suffix
|
|
|
|
const res = await fetch(url, {
|
|
|
|
method: 'POST',
|
|
|
|
body: Stream.Readable.from([content]),
|
|
|
|
})
|
|
|
|
if (!res.ok) throw new Error(res.statusText)
|
|
|
|
return async () => {
|
|
|
|
const res = await fetch(url, { method: 'GET' })
|
|
|
|
if (!res.ok) throw new Error(res.statusText)
|
|
|
|
expect(await res.text()).to.equal(content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should create a DEK when asked explicitly', async function () {
|
|
|
|
await app.persistor.generateDataEncryptionKey(
|
|
|
|
backendSettings.stores.user_files,
|
|
|
|
fileKey1
|
|
|
|
)
|
|
|
|
expect(
|
|
|
|
await app.persistor.getDataEncryptionKeySize(
|
|
|
|
backendSettings.stores.user_files,
|
|
|
|
fileKey1
|
|
|
|
)
|
|
|
|
).to.equal(32)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should create a DEK from writes', async function () {
|
|
|
|
await createRandomContent(fileUrl1)
|
|
|
|
expect(
|
|
|
|
await app.persistor.getDataEncryptionKeySize(
|
|
|
|
backendSettings.stores.user_files,
|
|
|
|
fileKey1
|
|
|
|
)
|
|
|
|
).to.equal(32)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not create a DEK from reads', async function () {
|
|
|
|
const res = await fetch(fileUrl1, {
|
|
|
|
method: 'GET',
|
|
|
|
})
|
|
|
|
if (res.status !== 404) throw new Error(`${res.status} should be 404`)
|
|
|
|
|
|
|
|
// Cannot use test helper expectPersistorNotToHaveFile here, we need to use the KEK.
|
|
|
|
await expect(
|
|
|
|
app.persistor.getDataEncryptionKeySize(
|
|
|
|
backendSettings.stores.user_files,
|
|
|
|
fileKey1
|
|
|
|
)
|
|
|
|
).to.rejectedWith(NotFoundError)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should never overwrite a data encryption key', async function () {
|
|
|
|
const checkGET = await createRandomContent(fileUrl1)
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
app.persistor.generateDataEncryptionKey(
|
|
|
|
backendSettings.stores.user_files,
|
|
|
|
fileKey1
|
|
|
|
)
|
|
|
|
).to.rejectedWith(AlreadyWrittenError)
|
|
|
|
|
|
|
|
await checkGET()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should re-use the data encryption key after a write', async function () {
|
|
|
|
const checkGET1 = await createRandomContent(fileUrl1, '1')
|
|
|
|
const checkGET2 = await createRandomContent(fileUrl2, '2')
|
|
|
|
await checkGET1()
|
|
|
|
await checkGET2()
|
|
|
|
})
|
|
|
|
|
|
|
|
let s3Client
|
|
|
|
before('create s3Client', function () {
|
|
|
|
const cfg = s3Config()
|
|
|
|
const s3ClientSettings = {
|
|
|
|
accessKeyId: cfg.key,
|
|
|
|
secretAccessKey: cfg.secret,
|
|
|
|
endpoint: cfg.endpoint,
|
|
|
|
httpOptions: cfg.httpOptions,
|
|
|
|
s3ForcePathStyle: cfg.pathStyle,
|
|
|
|
}
|
|
|
|
s3Client = new S3(s3ClientSettings)
|
|
|
|
})
|
|
|
|
|
|
|
|
async function checkDEKStorage({
|
|
|
|
dekBucketKeys = [],
|
|
|
|
userFilesBucketKeys = [],
|
|
|
|
}) {
|
|
|
|
await createRandomContent(fileUrl1)
|
|
|
|
|
|
|
|
const { Contents: dekEntries } = await s3Client
|
|
|
|
.listObjectsV2({
|
|
|
|
Bucket: process.env.AWS_S3_USER_FILES_DEK_BUCKET_NAME,
|
|
|
|
Prefix: `${projectId}/`,
|
|
|
|
})
|
|
|
|
.promise()
|
|
|
|
expect(dekEntries).to.have.length(dekBucketKeys.length)
|
|
|
|
// Order is not predictable, use members
|
|
|
|
expect(dekEntries.map(o => o.Key)).to.have.members(dekBucketKeys)
|
|
|
|
|
|
|
|
const { Contents: userFilesEntries } = await s3Client
|
|
|
|
.listObjectsV2({
|
|
|
|
Bucket: backendSettings.stores.user_files,
|
|
|
|
Prefix: `${projectId}/`,
|
|
|
|
})
|
|
|
|
.promise()
|
|
|
|
expect(userFilesEntries).to.have.length(userFilesBucketKeys.length)
|
|
|
|
// Order is not predictable, use members
|
|
|
|
expect(userFilesEntries.map(o => o.Key)).to.have.members(
|
|
|
|
userFilesBucketKeys
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should use a custom bucket for DEKs', async function () {
|
|
|
|
await checkDEKStorage({
|
|
|
|
dekBucketKeys: [`${projectId}/dek`],
|
|
|
|
userFilesBucketKeys: [fileKey1],
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('deleteDirectory', function () {
|
|
|
|
let checkGET2
|
|
|
|
beforeEach('create files', async function () {
|
|
|
|
await createRandomContent(fileUrl1, '1')
|
|
|
|
checkGET2 = await createRandomContent(fileUrl2, '2')
|
|
|
|
})
|
|
|
|
it('should delete sub-folder and keep DEK', async function () {
|
|
|
|
await app.persistor.deleteDirectory(
|
|
|
|
Settings.filestore.stores.user_files,
|
|
|
|
fileKey1 // not really a sub-folder, but it will do for this test.
|
|
|
|
)
|
|
|
|
expect(
|
|
|
|
await app.persistor.checkIfObjectExists(
|
|
|
|
Settings.filestore.stores.user_files,
|
|
|
|
fileKey1
|
|
|
|
)
|
|
|
|
).to.equal(false)
|
|
|
|
expect(
|
|
|
|
await app.persistor.checkIfObjectExists(
|
|
|
|
Settings.filestore.stores.user_files,
|
|
|
|
fileKey2
|
|
|
|
)
|
|
|
|
).to.equal(true)
|
|
|
|
expect(
|
|
|
|
await app.persistor.getDataEncryptionKeySize(
|
|
|
|
Settings.filestore.stores.user_files,
|
|
|
|
fileKey2
|
|
|
|
)
|
|
|
|
).to.equal(32)
|
|
|
|
await checkGET2()
|
|
|
|
})
|
|
|
|
it('should delete project folder and DEK', async function () {
|
|
|
|
await app.persistor.deleteDirectory(
|
|
|
|
Settings.filestore.stores.user_files,
|
|
|
|
`${projectId}/`
|
|
|
|
)
|
|
|
|
expect(
|
|
|
|
await app.persistor.checkIfObjectExists(
|
|
|
|
Settings.filestore.stores.user_files,
|
|
|
|
fileKey1
|
|
|
|
)
|
|
|
|
).to.equal(false)
|
|
|
|
expect(
|
|
|
|
await app.persistor.checkIfObjectExists(
|
|
|
|
Settings.filestore.stores.user_files,
|
|
|
|
fileKey2
|
|
|
|
)
|
|
|
|
).to.equal(false)
|
|
|
|
await expect(
|
|
|
|
app.persistor.getDataEncryptionKeySize(
|
|
|
|
Settings.filestore.stores.user_files,
|
|
|
|
fileKey2
|
|
|
|
)
|
|
|
|
).to.rejectedWith(NotFoundError)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('checkIfObjectExists', function () {
|
|
|
|
it('should return false when the object does not exist', async function () {
|
|
|
|
expect(
|
|
|
|
await app.persistor.checkIfObjectExists(
|
|
|
|
Settings.filestore.stores.user_files,
|
|
|
|
`${projectId}/${new ObjectId().toString()}`
|
|
|
|
)
|
|
|
|
).to.equal(false)
|
|
|
|
})
|
|
|
|
it('should return true when the object exists', async function () {
|
|
|
|
const fileId = new ObjectId().toString()
|
|
|
|
const fileUrl = `${filestoreUrl}/project/${projectId}/file/${fileId}`
|
|
|
|
const res = await fetch(fileUrl, {
|
|
|
|
method: 'POST',
|
|
|
|
body: Stream.Readable.from(['hello']),
|
|
|
|
})
|
|
|
|
if (!res.ok) throw new Error(res.statusText)
|
|
|
|
expect(
|
|
|
|
await app.persistor.checkIfObjectExists(
|
|
|
|
Settings.filestore.stores.user_files,
|
|
|
|
`${projectId}/${fileId}`
|
|
|
|
)
|
|
|
|
).to.equal(true)
|
|
|
|
})
|
|
|
|
})
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
2024-10-24 07:42:20 -04:00
|
|
|
}
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|