Skip to content

Commit

Permalink
Mark the callback as optional
Browse files Browse the repository at this point in the history
Without callback, it is impossible to unit-test the backbeat
routes, as we run async code and do not yet support promises.

Issue: CLDSRV-591
  • Loading branch information
williamlardier committed Dec 10, 2024
1 parent a0f2c3f commit c3b007f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
39 changes: 16 additions & 23 deletions lib/routes/routeBackbeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -1310,38 +1310,37 @@ const indexingSchema = joi.array().items(indexEntrySchema).min(1);

function respondToRequest(err, response, log, callback) {
responseJSONBody(err, null, response, log);
return callback(err);
// The callback is optional, as it is only used for testing purposes
// but value may be set to non-undefined or null due to the arsenal
// routes implementation
if (callback && typeof callback === 'function') {
return callback(err);
}
return undefined;
}

function routeIndexingAPIs(request, response, userInfo, log, callback) {
const route = backbeatRoutes[request.method][request.resourceType];

if (!['GET', 'POST'].includes(request.method)) {
responseJSONBody(errors.MethodNotAllowed, null, response, log);
return callback(errors.MethodNotAllowed);
return respondToRequest(errors.MethodNotAllowed, response, log, callback);
}

if (request.method === 'GET') {
return route(request, response, userInfo, log, err => {
if (err) {
responseJSONBody(err, null, response, log);
}
return callback(err);
});
return route(request, response, userInfo, log, err =>
respondToRequest(err, response, log, callback));
}

const op = request.query.operation;

if (!op || typeof route[op] !== 'function') {
log.error('Invalid operataion parameter', { operation: op });
responseJSONBody(errors.BadRequest, null, response, log);
return callback(errors.BadRequest);
return respondToRequest(errors.BadRequest, response, log, callback);
}

return _getRequestPayload(request, (err, payload) => {
if (err) {
responseJSONBody(err, null, response, log);
return callback(err);
return respondToRequest(err, response, log, callback);
}

let parsedIndex;
Expand All @@ -1350,16 +1349,11 @@ function routeIndexingAPIs(request, response, userInfo, log, callback) {
parsedIndex = joi.attempt(JSON.parse(payload), indexingSchema, 'invalid payload');
} catch (err) {
log.error('Unable to parse index request body', { error: err });
responseJSONBody(errors.BadRequest, null, response, log);
return callback(errors.BadRequest);
return respondToRequest(errors.BadRequest, response, log, callback);
}

return route[op](parsedIndex, request, response, userInfo, log, err => {
if (err) {
responseJSONBody(err, null, response, log);
}
return callback(err);
});
return route[op](parsedIndex, request, response, userInfo, log, err =>
respondToRequest(err, response, log, callback));
});
}

Expand Down Expand Up @@ -1477,8 +1471,7 @@ function routeBackbeat(clientIP, request, response, log, callback) {
resourceType: request.resourceType,
query: request.query,
});
responseJSONBody(errors.MethodNotAllowed, null, response, log);
return callback(errors.MethodNotAllowed);
return respondToRequest(errors.MethodNotAllowed, response, log, callback);
}

return async.waterfall([
Expand Down
5 changes: 0 additions & 5 deletions tests/unit/routeBackbeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ const testBucket = {
namespace,
headers: {
'host': `${bucketName}.s3.amazonaws.com`,
// set versioning

},
url: `/${bucketName}`,
actionImplicitDenies: false,
Expand All @@ -47,7 +45,6 @@ describe('routeBackbeat', () => {
let response;

beforeEach(() => {
// Mock backbeatRoutes
sinon.stub(backbeatRoutes, 'PUT').returns({
data: sinon.stub(),
metadata: sinon.stub(),
Expand Down Expand Up @@ -86,7 +83,6 @@ describe('routeBackbeat', () => {
index: sinon.stub(),
});

// Mock request and response
request = new DummyRequest(
{
method: 'GET',
Expand Down Expand Up @@ -131,7 +127,6 @@ describe('routeBackbeat', () => {
target: `${bucketName}/${objectName}`,
operation: null,
versionId: false,
// not sending any body here, so expect error
expect: errors.MalformedPOSTRequest,
},
{
Expand Down

0 comments on commit c3b007f

Please sign in to comment.