Skip to content

Commit

Permalink
unit test added
Browse files Browse the repository at this point in the history
  • Loading branch information
benzekrimaha committed Jan 15, 2025
1 parent f047a0e commit e2a6c8e
Showing 1 changed file with 62 additions and 13 deletions.
75 changes: 62 additions & 13 deletions tests/unit/RoleCredentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const assert = require('assert');
const http = require('http');
const { Client } = require('vaultclient');
const { Logger } = require('werelogs');
const { ArsenalError } = require('arsenal/build/lib/errors');
const { errors } = require('arsenal');

Check failure on line 6 in tests/unit/RoleCredentials.js

View workflow job for this annotation

GitHub Actions / tests

'errors' is assigned a value but never used
const { proxyPath } = require('../../extensions/replication/constants');
const RoleCredentials = require('../../lib/credentials/RoleCredentials');
Expand Down Expand Up @@ -144,21 +145,69 @@ describe('Credentials Manager', () => {
});
});

it('should handle non arsenal errors on refresh', function test(done) {
this.timeout(10000);
const retryTimeout = (roleCredentials.expiration - Date.now()) +
1000;
return setTimeout(() => {
simulateServerError = true;
roleCredentials.get(err => {
assert(err);
// check that err is another instance distinct from
// the global arsenal error
assert.notStrictEqual(err, errors.InternalError);
done();
it('should properly handle Arsenal and non-Arsenal errors', function(done) {

Check warning on line 148 in tests/unit/RoleCredentials.js

View workflow job for this annotation

GitHub Actions / tests

Unexpected function expression

Check warning on line 148 in tests/unit/RoleCredentials.js

View workflow job for this annotation

GitHub Actions / tests

Unexpected unnamed function

Check failure on line 148 in tests/unit/RoleCredentials.js

View workflow job for this annotation

GitHub Actions / tests

Missing space before function parentheses
const testCases = [
{
name: 'non-Arsenal error',
error: Object.assign(new Error('custom error'), {
code: 'CustomError',
description: 'test description'
}),
statusCode: 400,
expectedResult: error => {
assert(error instanceof ArsenalError);
assert.strictEqual(error.code, 400);
assert.strictEqual(error.description, 'test description');
}
},
{
name: 'retryable Internal error',
error: Object.assign(new Error('internal error'), {
code: 'InternalError',
InternalError: true
}),
statusCode: 500,
expectedResult: error => {
assert(error.retryable === true);
}
}
];

Check failure on line 175 in tests/unit/RoleCredentials.js

View workflow job for this annotation

GitHub Actions / tests

Trailing spaces not allowed
let completedTests = 0;

Check failure on line 177 in tests/unit/RoleCredentials.js

View workflow job for this annotation

GitHub Actions / tests

Trailing spaces not allowed
testCases.forEach(testCase => {
const mockVaultClient = {
assumeRoleBackbeat: (roleArn, roleSessionName, options, callback) => {
// Simulate error returned by the vault client
const errorWithCode = new Error(testCase.error.message);
Object.assign(errorWithCode, testCase.error);
callback(errorWithCode, null, testCase.statusCode);
}
};

Check failure on line 187 in tests/unit/RoleCredentials.js

View workflow job for this annotation

GitHub Actions / tests

Trailing spaces not allowed
const credentials = new RoleCredentials(
mockVaultClient,
role,
extension,
new Logger('test:RoleCredentials').newRequestLogger('request-uid')
);

Check failure on line 194 in tests/unit/RoleCredentials.js

View workflow job for this annotation

GitHub Actions / tests

Trailing spaces not allowed
credentials.refresh(err => {
try {
testCase.expectedResult(err);
completedTests += 1;

Check failure on line 199 in tests/unit/RoleCredentials.js

View workflow job for this annotation

GitHub Actions / tests

Trailing spaces not allowed
if (completedTests === testCases.length) {
done();
}
} catch (assertError) {
done(assertError);
}
});
}, retryTimeout);
});
});

Check failure on line 209 in tests/unit/RoleCredentials.js

View workflow job for this annotation

GitHub Actions / tests

Trailing spaces not allowed

Check failure on line 210 in tests/unit/RoleCredentials.js

View workflow job for this annotation

GitHub Actions / tests

Trailing spaces not allowed

Check failure on line 211 in tests/unit/RoleCredentials.js

View workflow job for this annotation

GitHub Actions / tests

More than 2 blank lines not allowed
it('RoleCredentials should use a default renewal anticipation delay if not explicit', () => {
const vaultclient = new Client(
Expand Down

0 comments on commit e2a6c8e

Please sign in to comment.