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 7af350b
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions tests/unit/api/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
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.only('api.callApiMethod', () => {

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

View workflow job for this annotation

GitHub Actions / lint

Unexpected exclusive mocha test
let sandbox;
let request;
let response;
let log;
let callback;

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

View workflow job for this annotation

GitHub Actions / lint

'callback' is assigned a value but never used
let authServer;

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

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

View workflow job for this annotation

GitHub Actions / lint

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

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

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
response = {
write: sandbox.stub(),
end: sandbox.stub()
};

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

View workflow job for this annotation

GitHub Actions / lint

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

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

View workflow job for this annotation

GitHub Actions / lint

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

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

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
callback = sandbox.stub();

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

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
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);

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

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
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);
});
});

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

View workflow job for this annotation

GitHub Actions / lint

Newline required at end of file but not found

0 comments on commit 7af350b

Please sign in to comment.