@unito/integration-sdk
Version:
Integration SDK
53 lines (42 loc) • 1.48 kB
text/typescript
import express from 'express';
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: string) => credentials } as express.Request;
const response = { locals: {} } as express.Response;
extractCredentials(request, response, () => {});
assert.deepEqual(response.locals, {
credentials: {
accessToken: 'abc',
},
});
});
it('malformed header', async () => {
const request = { header: (_key: string) => 'nope' } as express.Request;
const response = { locals: {} } as express.Response;
assert.throws(() => extractCredentials(request, response, () => {}), BadRequestError);
});
it('variables', async () => {
const credentials = Buffer.from(
JSON.stringify({
apiKey: 'abc',
}),
).toString('base64');
const request = { header: (_key: string) => credentials } as express.Request;
const response = { locals: {} } as express.Response;
extractCredentials(request, response, () => {});
assert.deepEqual(response.locals, {
credentials: {
apiKey: 'abc',
},
});
});
});