Fix incorrect key when deleting projects

This commit is contained in:
Simon Detheridge 2020-03-16 11:35:01 +00:00
parent 9f74aac1a0
commit 06c4c0f74f
5 changed files with 18 additions and 14 deletions

View file

@ -25,6 +25,7 @@ class ConversionsDisabledError extends BackwardCompatibleError {}
class ConversionError extends BackwardCompatibleError {} class ConversionError extends BackwardCompatibleError {}
class SettingsError extends BackwardCompatibleError {} class SettingsError extends BackwardCompatibleError {}
class TimeoutError extends BackwardCompatibleError {} class TimeoutError extends BackwardCompatibleError {}
class InvalidParametersError extends BackwardCompatibleError {}
class FailedCommandError extends OError { class FailedCommandError extends OError {
constructor(command, code, stdout, stderr) { constructor(command, code, stdout, stderr) {
@ -50,5 +51,6 @@ module.exports = {
ConversionError, ConversionError,
HealthCheckError, HealthCheckError,
SettingsError, SettingsError,
TimeoutError TimeoutError,
InvalidParametersError
} }

View file

@ -161,13 +161,16 @@ function deleteFile(req, res, next) {
function deleteProject(req, res, next) { function deleteProject(req, res, next) {
metrics.inc('deleteProject') metrics.inc('deleteProject')
const { project_id: projectId, bucket } = req const { key, bucket } = req
req.requestLogger.setMessage('getting project size') req.requestLogger.setMessage('deleting project')
req.requestLogger.addFields({ projectId, bucket }) req.requestLogger.addFields({ key, bucket })
FileHandler.deleteProject(bucket, projectId, function(err) { FileHandler.deleteProject(bucket, key, function(err) {
if (err) { if (err) {
if (err instanceof Errors.InvalidParametersError) {
return res.sendStatus(400)
}
next(err) next(err)
} else { } else {
res.sendStatus(204) res.sendStatus(204)

View file

@ -5,7 +5,7 @@ const LocalFileWriter = require('./LocalFileWriter')
const FileConverter = require('./FileConverter') const FileConverter = require('./FileConverter')
const KeyBuilder = require('./KeyBuilder') const KeyBuilder = require('./KeyBuilder')
const ImageOptimiser = require('./ImageOptimiser') const ImageOptimiser = require('./ImageOptimiser')
const { ConversionError, WriteError } = require('./Errors') const { ConversionError, InvalidParametersError } = require('./Errors')
module.exports = { module.exports = {
insertFile: callbackify(insertFile), insertFile: callbackify(insertFile),
@ -27,7 +27,7 @@ module.exports = {
async function insertFile(bucket, key, stream) { async function insertFile(bucket, key, stream) {
const convertedKey = KeyBuilder.getConvertedFolderKey(key) const convertedKey = KeyBuilder.getConvertedFolderKey(key)
if (!convertedKey.match(/^[0-9a-f]{24}\/[0-9a-f]{24}/i)) { if (!convertedKey.match(/^[0-9a-f]{24}\/[0-9a-f]{24}/i)) {
throw new WriteError({ throw new InvalidParametersError({
message: 'key does not match validation regex', message: 'key does not match validation regex',
info: { bucket, key, convertedKey } info: { bucket, key, convertedKey }
}) })
@ -39,7 +39,7 @@ async function insertFile(bucket, key, stream) {
async function deleteFile(bucket, key) { async function deleteFile(bucket, key) {
const convertedKey = KeyBuilder.getConvertedFolderKey(key) const convertedKey = KeyBuilder.getConvertedFolderKey(key)
if (!convertedKey.match(/^[0-9a-f]{24}\/[0-9a-f]{24}/i)) { if (!convertedKey.match(/^[0-9a-f]{24}\/[0-9a-f]{24}/i)) {
throw new WriteError({ throw new InvalidParametersError({
message: 'key does not match validation regex', message: 'key does not match validation regex',
info: { bucket, key, convertedKey } info: { bucket, key, convertedKey }
}) })
@ -52,7 +52,7 @@ async function deleteFile(bucket, key) {
async function deleteProject(bucket, key) { async function deleteProject(bucket, key) {
if (!key.match(/^[0-9a-f]{24}\//i)) { if (!key.match(/^[0-9a-f]{24}\//i)) {
throw new WriteError({ throw new InvalidParametersError({
message: 'key does not match validation regex', message: 'key does not match validation regex',
info: { bucket, key } info: { bucket, key }
}) })

View file

@ -263,7 +263,7 @@ describe('FileController', function() {
it('should tell the file handler', function(done) { it('should tell the file handler', function(done) {
res.sendStatus = code => { res.sendStatus = code => {
code.should.equal(204) code.should.equal(204)
expect(FileHandler.deleteProject).to.have.been.calledWith(bucket, projectId) expect(FileHandler.deleteProject).to.have.been.calledWith(bucket, key)
done() done()
} }
FileController.deleteProject(req, res, next) FileController.deleteProject(req, res, next)

View file

@ -159,10 +159,9 @@ describe('FileHandler', function() {
it('should tell the filestore manager to delete the folder', function(done) { it('should tell the filestore manager to delete the folder', function(done) {
FileHandler.deleteProject(bucket, projectKey, err => { FileHandler.deleteProject(bucket, projectKey, err => {
expect(err).not.to.exist expect(err).not.to.exist
expect(PersistorManager.promises.deleteDirectory).to.have.been.calledWith( expect(
bucket, PersistorManager.promises.deleteDirectory
projectKey ).to.have.been.calledWith(bucket, projectKey)
)
done() done()
}) })
}) })