Skip to content

Commit

Permalink
Unit test api.js
Browse files Browse the repository at this point in the history
- Not all code is tested, but relevant parts for quota

Issue: CLDSRV-592
  • Loading branch information
williamlardier committed Dec 20, 2024
1 parent bcef399 commit f1717f5
Showing 1 changed file with 115 additions and 0 deletions.
115 changes: 115 additions & 0 deletions tests/unit/api/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
const sinon = require('sinon');
const { errors, auth } = require('arsenal');
const api = require('../../../lib/api/api');
const DummyRequest = require('../DummyRequest');
const { default: AuthInfo } = require('arsenal/build/lib/auth/AuthInfo');
const assert = require('assert');

describe('api.callApiMethod', () => {
let sandbox;
let request;
let response;
let log;
let authServer;

beforeEach(() => {
sandbox = sinon.createSandbox();

request = new DummyRequest('my-obj');
request.query = {};
request.socket = {
remoteAddress: '127.0.0.1',
};

response = {
write: sandbox.stub(),
end: sandbox.stub()
};

log = {
addDefaultFields: sandbox.stub(),
trace: sandbox.stub(),
error: sandbox.stub(),
debug: sandbox.stub()
};

authServer = {
doAuth: sandbox.stub().callsArgWith(2, null, new AuthInfo({}), [{
isAllowed: true,
isImplicit: false,
}], null, {
accountQuota: 5000,
}),
};

callback = sandbox.stub();

Check failure on line 45 in tests/unit/api/api.js

View workflow job for this annotation

GitHub Actions / lint

'callback' is not defined

sandbox.stub(auth, 'server').value(authServer);
});

afterEach(() => {
sandbox.restore();
});

it('should attach apiMethod to request', done => {
const testMethod = 'bucketGet';
api.callApiMethod(testMethod, request, response, log, () => {
assert.strictEqual(request.apiMethod, testMethod);
done();
});
});

it('should initialize finalizerHooks array', done => {
api.callApiMethod('bucketGet', request, response, log, () => {
assert.strictEqual(Array.isArray(request.finalizerHooks), true);
assert.strictEqual(request.finalizerHooks.length, 0);
done();
});
});

it('should handle auth server errors', done => {
authServer.doAuth.callsArgWith(2, errors.AccessDenied);

api.callApiMethod('bucketGet', request, response, log, err => {
assert(err.is.AccessDenied);
done();
});
});

it('should execute finalizer hooks after api method completion', done => {
let called = false;

sandbox.stub(api, 'objectPut').callsFake((userInfo, _request, streamingV4Params, log, cb) => {
request.finalizerHooks.push((err, _done) => {
called = true;
_done();
});
cb();
});
request.objectKey = 'testobject';
api.callApiMethod('objectPut', request, response, log, () => {
assert.strictEqual(called, true);
done();
});
});

it('should set _needQuota to true for completeMultipartUpload', done => {
authServer.doAuth.callsFake((req, log, cb, awsService, requestContexts) => {
assert.strictEqual(requestContexts[0]._needQuota, true);
done();
});
sandbox.stub(api, 'completeMultipartUpload').callsFake(
(userInfo, _request, streamingV4Params, log, cb) => cb);
api.callApiMethod('completeMultipartUpload', request, response, log);
});

it('should set _needQuota to true for multipartDelete', done => {
authServer.doAuth.callsFake((req, log, cb, awsService, requestContexts) => {
assert.strictEqual(requestContexts[0]._needQuota, true);
done();
});
sandbox.stub(api, 'multipartDelete').callsFake(
(userInfo, _request, streamingV4Params, log, cb) => cb);
api.callApiMethod('multipartDelete', request, response, log);
});
});

0 comments on commit f1717f5

Please sign in to comment.