mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 09:22:15 +00:00
e606ebd9b9
* chore(nest): register LlmParseModule in AppModule
The module was only in the container because BookingImportModule and
PluginsModule happen to import it, so /api/admin/llm/local depended on those two
keeping that import. Register it where every other module is registered.
* refactor(nest): give the rate limiter one home and one instance
RateLimitService keeps its counters in a Map on the instance, but it was listed
in the providers of auth, oauth, transit and trip-invite, so each of them got its
own. Nothing is broken today — the bucket names in use (login/mfa/forgot/reset,
oauth_*, transit_*, trip_invite) don't overlap — but two modules picking the same
name would have quietly allowed twice the configured attempts.
Move it to nest/common, where it belongs now that four domains use it, and hand
it out through a small module they import. Not @Global: the e2e suites build a
container around one domain module, so a global AppModule never pulled in isn't
there at all.
transit.mcp.ts keeps its own instance on purpose — it keys by userId under
mcp_transit_* while the REST path keys by IP.
* test(ws): pin the wire envelope before touching the transport
Every frame on the socket is flat: {type, tripId, ...payload} out, {type, tripId}
in. Nest's WsAdapter dispatches on message.event and answers {event, data}, so
moving this onto a gateway without a custom serialiser would leave join silently
unhandled — no welcome.socketId to send back as X-Socket-Id, and every client
would then receive its own writes.
Nothing catches that today: the 100+ tests that touch realtime mock src/websocket
and never look at the bytes. WS-032 sends the exact shape the stock adapter would
and asserts the server ignores it.
* docs(nest): drop the parts of the README that no longer exist
strangler.ts, DEFAULT_NEST_PREFIXES/NEST_PREFIXES and tests/parity/ are all gone —
every request goes through Nest now — but the README still walks a new contributor
through adding a prefix and writing a parity test against the Express route.
Also spell out that the 80% gate is an average over src/nest/**, not a floor per
file, so a large untested module can sit behind the well-covered small ones.
178 lines
7.6 KiB
TypeScript
178 lines
7.6 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
|
// The Nest service is a thin wrapper that forwards to the legacy oauthService
|
|
// plus the addon/notification helpers. Mock those and assert the delegation.
|
|
const { oauth } = vi.hoisted(() => ({
|
|
oauth: {
|
|
consumeAuthCode: vi.fn(),
|
|
authenticateClient: vi.fn(),
|
|
verifyPKCE: vi.fn(),
|
|
issueTokens: vi.fn(),
|
|
issueClientCredentialsToken: vi.fn(),
|
|
refreshTokens: vi.fn(),
|
|
revokeToken: vi.fn(),
|
|
getUserByAccessToken: vi.fn(),
|
|
validateAuthorizeRequest: vi.fn(),
|
|
saveConsent: vi.fn(),
|
|
createAuthCode: vi.fn(),
|
|
listOAuthClients: vi.fn(),
|
|
createOAuthClient: vi.fn(),
|
|
rotateOAuthClientSecret: vi.fn(),
|
|
deleteOAuthClient: vi.fn(),
|
|
listOAuthSessions: vi.fn(),
|
|
revokeSession: vi.fn(),
|
|
},
|
|
}));
|
|
vi.mock('../../../src/services/oauthService', () => oauth);
|
|
|
|
const { isAddonEnabled } = vi.hoisted(() => ({ isAddonEnabled: vi.fn() }));
|
|
|
|
const { getMcpSafeUrl } = vi.hoisted(() => ({ getMcpSafeUrl: vi.fn() }));
|
|
vi.mock('../../../src/app-config', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('../../../src/app-config')>();
|
|
return { ...actual, getMcpSafeUrl };
|
|
});
|
|
|
|
import { OauthService } from '../../../src/nest/oauth/oauth.service';
|
|
import type { AddonsService } from '../../../src/nest/addons/addons.service';
|
|
import { ADDON_IDS } from '../../../src/addons';
|
|
|
|
const addonsStub = { isAddonEnabled } as unknown as AddonsService;
|
|
function svc() { return new OauthService(addonsStub); }
|
|
|
|
beforeEach(() => vi.clearAllMocks());
|
|
|
|
describe('OauthService', () => {
|
|
it('mcpEnabled checks the MCP addon flag', () => {
|
|
isAddonEnabled.mockReturnValue(true);
|
|
expect(svc().mcpEnabled()).toBe(true);
|
|
expect(isAddonEnabled).toHaveBeenCalledWith(ADDON_IDS.MCP);
|
|
isAddonEnabled.mockReturnValue(false);
|
|
expect(svc().mcpEnabled()).toBe(false);
|
|
});
|
|
|
|
it('mcpSafeUrl forwards to the app-config helper', () => {
|
|
getMcpSafeUrl.mockReturnValue('https://safe');
|
|
expect(svc().mcpSafeUrl()).toBe('https://safe');
|
|
expect(getMcpSafeUrl).toHaveBeenCalled();
|
|
});
|
|
|
|
it('consumeAuthCode delegates', () => {
|
|
oauth.consumeAuthCode.mockReturnValue({ clientId: 'c' });
|
|
expect(svc().consumeAuthCode('code')).toEqual({ clientId: 'c' });
|
|
expect(oauth.consumeAuthCode).toHaveBeenCalledWith('code');
|
|
});
|
|
|
|
it('authenticateClient delegates with both args', () => {
|
|
oauth.authenticateClient.mockReturnValue({ id: 'c' });
|
|
expect(svc().authenticateClient('c', 'secret')).toEqual({ id: 'c' });
|
|
expect(oauth.authenticateClient).toHaveBeenCalledWith('c', 'secret');
|
|
});
|
|
|
|
it('verifyPKCE delegates', () => {
|
|
oauth.verifyPKCE.mockReturnValue(true);
|
|
expect(svc().verifyPKCE('v', 'ch')).toBe(true);
|
|
expect(oauth.verifyPKCE).toHaveBeenCalledWith('v', 'ch');
|
|
});
|
|
|
|
it('issueTokens forwards the full argument list', () => {
|
|
oauth.issueTokens.mockReturnValue({ access_token: 'at' });
|
|
expect(svc().issueTokens('c', 1, ['s'], null, 'aud')).toEqual({ access_token: 'at' });
|
|
expect(oauth.issueTokens).toHaveBeenCalledWith('c', 1, ['s'], null, 'aud');
|
|
});
|
|
|
|
it('issueClientCredentialsToken forwards the full argument list', () => {
|
|
oauth.issueClientCredentialsToken.mockReturnValue({ access_token: 'cc' });
|
|
expect(svc().issueClientCredentialsToken('c', 1, ['s'], 'aud')).toEqual({ access_token: 'cc' });
|
|
expect(oauth.issueClientCredentialsToken).toHaveBeenCalledWith('c', 1, ['s'], 'aud');
|
|
});
|
|
|
|
it('refreshTokens forwards the full argument list', () => {
|
|
oauth.refreshTokens.mockReturnValue({ tokens: { access_token: 'new' } });
|
|
expect(svc().refreshTokens('rt', 'c', 's', '1.2.3.4')).toEqual({ tokens: { access_token: 'new' } });
|
|
expect(oauth.refreshTokens).toHaveBeenCalledWith('rt', 'c', 's', '1.2.3.4');
|
|
});
|
|
|
|
it('revokeToken forwards the full argument list', () => {
|
|
svc().revokeToken('t', 'c', undefined, '1.2.3.4');
|
|
expect(oauth.revokeToken).toHaveBeenCalledWith('t', 'c', undefined, '1.2.3.4');
|
|
});
|
|
|
|
it('getUserByAccessToken delegates', () => {
|
|
oauth.getUserByAccessToken.mockReturnValue({ user: { id: 1 } });
|
|
expect(svc().getUserByAccessToken('tok')).toEqual({ user: { id: 1 } });
|
|
expect(oauth.getUserByAccessToken).toHaveBeenCalledWith('tok');
|
|
});
|
|
|
|
it('validateAuthorizeRequest delegates with the user id', () => {
|
|
oauth.validateAuthorizeRequest.mockReturnValue({ valid: true });
|
|
const params = { response_type: 'code' } as never;
|
|
expect(svc().validateAuthorizeRequest(params, 5)).toEqual({ valid: true });
|
|
expect(oauth.validateAuthorizeRequest).toHaveBeenCalledWith(params, 5);
|
|
});
|
|
|
|
it('saveConsent forwards the full argument list', () => {
|
|
svc().saveConsent('c', 1, ['s'], '1.2.3.4');
|
|
expect(oauth.saveConsent).toHaveBeenCalledWith('c', 1, ['s'], '1.2.3.4');
|
|
});
|
|
|
|
it('createAuthCode forwards the params object', () => {
|
|
oauth.createAuthCode.mockReturnValue('the_code');
|
|
const p = { clientId: 'c', userId: 1, redirectUri: 'u', scopes: ['s'], resource: null, codeChallenge: 'cc', codeChallengeMethod: 'S256' } as const;
|
|
expect(svc().createAuthCode(p)).toBe('the_code');
|
|
expect(oauth.createAuthCode).toHaveBeenCalledWith(p);
|
|
});
|
|
|
|
it('listOAuthClients delegates', () => {
|
|
oauth.listOAuthClients.mockReturnValue([{ id: 'c1' }]);
|
|
expect(svc().listOAuthClients(1)).toEqual([{ id: 'c1' }]);
|
|
expect(oauth.listOAuthClients).toHaveBeenCalledWith(1);
|
|
});
|
|
|
|
it('createOAuthClient forwards the full argument list', () => {
|
|
oauth.createOAuthClient.mockReturnValue({ client_id: 'c1' });
|
|
expect(svc().createOAuthClient(1, 'CLI', ['https://cb'], ['a'], '1.2.3.4', { allowsClientCredentials: true })).toEqual({ client_id: 'c1' });
|
|
expect(oauth.createOAuthClient).toHaveBeenCalledWith(1, 'CLI', ['https://cb'], ['a'], '1.2.3.4', { allowsClientCredentials: true });
|
|
});
|
|
|
|
it('rotateOAuthClientSecret delegates', () => {
|
|
oauth.rotateOAuthClientSecret.mockReturnValue({ client_secret: 'new' });
|
|
expect(svc().rotateOAuthClientSecret(1, 'c1', '1.2.3.4')).toEqual({ client_secret: 'new' });
|
|
expect(oauth.rotateOAuthClientSecret).toHaveBeenCalledWith(1, 'c1', '1.2.3.4');
|
|
});
|
|
|
|
it('deleteOAuthClient delegates', () => {
|
|
oauth.deleteOAuthClient.mockReturnValue({});
|
|
expect(svc().deleteOAuthClient(1, 'c1', '1.2.3.4')).toEqual({});
|
|
expect(oauth.deleteOAuthClient).toHaveBeenCalledWith(1, 'c1', '1.2.3.4');
|
|
});
|
|
|
|
it('listOAuthSessions delegates', () => {
|
|
oauth.listOAuthSessions.mockReturnValue([{ id: 1 }]);
|
|
expect(svc().listOAuthSessions(1)).toEqual([{ id: 1 }]);
|
|
expect(oauth.listOAuthSessions).toHaveBeenCalledWith(1);
|
|
});
|
|
|
|
it('revokeSession delegates', () => {
|
|
oauth.revokeSession.mockReturnValue({});
|
|
expect(svc().revokeSession(1, 7, '1.2.3.4')).toEqual({});
|
|
expect(oauth.revokeSession).toHaveBeenCalledWith(1, 7, '1.2.3.4');
|
|
});
|
|
});
|
|
|
|
describe('OauthModule', () => {
|
|
it('wires the public + api controllers and the providers', async () => {
|
|
const { OauthModule } = await import('../../../src/nest/oauth/oauth.module');
|
|
const { OauthPublicController } = await import('../../../src/nest/oauth/oauth-public.controller');
|
|
const { OauthApiController } = await import('../../../src/nest/oauth/oauth-api.controller');
|
|
const { OauthService: Svc } = await import('../../../src/nest/oauth/oauth.service');
|
|
|
|
const controllers = Reflect.getMetadata('controllers', OauthModule);
|
|
const providers = Reflect.getMetadata('providers', OauthModule);
|
|
expect(controllers).toEqual([OauthPublicController, OauthApiController]);
|
|
// RateLimitService is deliberately absent: it comes from the global
|
|
// RateLimitModule so all consumers share one set of counters.
|
|
expect(providers).toEqual([Svc]);
|
|
});
|
|
});
|