1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-12 13:48:35 +02:00
unleash.unleash/src/lib/middleware/pat-middleware.test.ts
Christopher Kolstad b681702b77
task: migrate tests to vitest
Vitest Pros:
* Automated failing test comments on github PRs
* A nice local UI with incremental testing when changing files (`yarn
test:ui`)
* Also nicely supported in all major IDEs, click to run test works (so
we won't miss what we had with jest).
* Works well with ESM

Vitest Cons:
* The ESBuild transformer vitest uses takes a little longer to transform
than our current SWC/jest setup, however, it is possible to setup SWC as
the transformer for vitest as well (though it only does one transform,
so we're paying ~7-10 seconds instead of ~ 2-3 seconds in transform
phase).
* Exposes how slow our tests are (tongue in cheek here)
2025-05-16 11:19:10 +02:00

154 lines
3.9 KiB
TypeScript

import getLogger from '../../test/fixtures/no-logger.js';
import patMiddleware from './pat-middleware.js';
import User from '../types/user.js';
import NotFoundError from '../error/notfound-error.js';
import type { AccountService } from '../services/account-service.js';
import { vi } from 'vitest';
let config: any;
beforeEach(() => {
config = {
getLogger,
flagResolver: {
isEnabled: vi.fn().mockReturnValue(true),
},
};
});
test('should not set user if unknown token', async () => {
// @ts-expect-error wrong type
const accountService = {
getAccountByPersonalAccessToken: vi.fn(),
addPATSeen: vi.fn(),
} as AccountService;
const func = patMiddleware(config, { accountService });
const cb = vi.fn();
const req = {
header: vi.fn().mockReturnValue('user:some-token'),
user: undefined,
};
await func(req, undefined, cb);
expect(cb).toHaveBeenCalled();
expect(req.header).toHaveBeenCalled();
expect(req.user).toBeFalsy();
});
test('should not set user if token wrong format', async () => {
// @ts-expect-error wrong type
const accountService = {
getAccountByPersonalAccessToken: vi.fn(),
} as AccountService;
const func = patMiddleware(config, { accountService });
const cb = vi.fn();
const req = {
header: vi.fn().mockReturnValue('token-not-starting-with-user'),
user: undefined,
};
await func(req, undefined, cb);
expect(
accountService.getAccountByPersonalAccessToken,
).not.toHaveBeenCalled();
expect(cb).toHaveBeenCalled();
expect(req.header).toHaveBeenCalled();
expect(req.user).toBeFalsy();
});
test('should add user if known token', async () => {
const apiUser = new User({
id: 44,
username: 'my-user',
});
// @ts-expect-error wrong type
const accountService = {
getAccountByPersonalAccessToken: vi.fn().mockReturnValue(apiUser),
addPATSeen: vi.fn(),
} as AccountService;
const func = patMiddleware(config, { accountService });
const cb = vi.fn();
const req = {
header: vi.fn().mockReturnValue('user:some-known-token'),
user: undefined,
path: '/api/client',
};
await func(req, undefined, cb);
expect(cb).toHaveBeenCalled();
expect(req.header).toHaveBeenCalled();
expect(req.user).toBe(apiUser);
});
test('should call next if accountService throws exception', async () => {
getLogger.setMuteError(true);
// @ts-expect-error wrong types
const accountService = {
getAccountByPersonalAccessToken: () => {
throw new Error('Error occurred');
},
} as AccountService;
const func = patMiddleware(config, { accountService });
const cb = vi.fn();
const req = {
header: vi.fn().mockReturnValue('user:some-token'),
user: undefined,
};
await func(req, undefined, cb);
expect(cb).toHaveBeenCalled();
getLogger.setMuteError(false);
});
test('Should not log at error level if user not found', async () => {
const fakeLogger = {
debug: () => {},
info: () => {},
warn: vi.fn(),
error: vi.fn(),
fatal: console.error,
};
const conf = {
getLogger: () => {
return fakeLogger;
},
flagResolver: {
isEnabled: vi.fn().mockReturnValue(true),
},
};
// @ts-expect-error wrong type
const accountService = {
getAccountByPersonalAccessToken: vi.fn().mockImplementation(() => {
throw new NotFoundError('Could not find pat');
}),
} as AccountService;
const mw = patMiddleware(conf, { accountService });
const cb = vi.fn();
const req = {
header: vi.fn().mockReturnValue('user:some-token'),
user: undefined,
};
await mw(req, undefined, cb);
expect(fakeLogger.error).not.toHaveBeenCalled();
expect(fakeLogger.warn).toHaveBeenCalled();
});