1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-23 00:22:19 +01:00

Add test for api-token-middleware to not make database call when using PAT (#2110)

* Middleware first version

* Middleware tests

* Add tests

* Finish middleware tests

* Add type for request

* Add flagresolver

* Fix snapshot

* Update flags and tests

* Put it back as default

* Update snapshot

* Add middleware test
This commit is contained in:
sjaanus 2022-09-29 07:53:29 +02:00 committed by GitHub
parent d79ace57ec
commit d5f621f3b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -58,6 +58,28 @@ test('should not add user if unknown token', async () => {
expect(req.user).toBeFalsy();
});
test('should not make database query when provided PAT format', async () => {
const apiTokenService = {
getUserForToken: jest.fn(),
};
const func = apiTokenMiddleware(config, { apiTokenService });
const cb = jest.fn();
const req = {
header: jest.fn().mockReturnValue('user:asdkjsdhg3'),
user: undefined,
};
await func(req, undefined, cb);
expect(apiTokenService.getUserForToken).not.toHaveBeenCalled();
expect(req.header).toHaveBeenCalled();
expect(cb).toHaveBeenCalled();
expect(req.user).toBeFalsy();
});
test('should add user if known token', async () => {
const apiUser = new ApiUser({
username: 'default',