@unito/integration-sdk
Version:
Integration SDK
67 lines (66 loc) • 2.73 kB
JavaScript
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import extractCredentials from '../../src/middlewares/credentials.js';
import { BadRequestError } from '../../src/httpErrors.js';
describe('credentials middleware', () => {
it('generates', async () => {
const credentials = Buffer.from(JSON.stringify({
accessToken: 'abc',
})).toString('base64');
const request = { header: (_key) => credentials };
const response = { locals: {} };
extractCredentials(request, response, () => { });
assert.deepEqual(response.locals, {
credentials: {
accessToken: 'abc',
},
});
});
it('malformed header', async () => {
const request = { header: (_key) => 'nope' };
const response = { locals: {} };
assert.throws(() => extractCredentials(request, response, () => { }), BadRequestError);
});
it('variables', async () => {
const credentials = Buffer.from(JSON.stringify({
apiKey: 'abc',
})).toString('base64');
const request = { header: (_key) => credentials };
const response = { locals: {} };
extractCredentials(request, response, () => { });
assert.deepEqual(response.locals, {
credentials: {
apiKey: 'abc',
},
});
});
it('extracts a credential carrying a rateLimitOverride', async () => {
const credentials = Buffer.from(JSON.stringify({
accessToken: 'abc',
rateLimitOverride: { standard: { reqPerPeriod: 5000, periodMs: 60_000 } },
})).toString('base64');
const request = { header: (_key) => credentials };
const response = { locals: {} };
extractCredentials(request, response, () => { });
assert.deepEqual(response.locals, {
credentials: {
accessToken: 'abc',
rateLimitOverride: { standard: { reqPerPeriod: 5000, periodMs: 60_000 } },
},
});
});
it('decorates the logger with the credential id and user id', async () => {
const credentials = Buffer.from(JSON.stringify({
accessToken: 'abc',
unitoCredentialId: 'cred-123',
unitoUserId: 'user-456',
})).toString('base64');
const decorated = [];
const request = { header: (_key) => credentials };
const response = {
locals: { logger: { decorate: (metadata) => decorated.push(metadata) } },
};
extractCredentials(request, response, () => { });
assert.deepEqual(decorated, [{ credentialId: 'cred-123' }, { userId: 'user-456' }]);
});
});