mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 08:00:06 +00:00
test(demo): the OAuth demo lock cases take their controller from OauthModule
This commit is contained in:
@@ -3,16 +3,21 @@
|
||||
* cannot register, rotate or delete an OAuth client, end a connected app, or
|
||||
* approve an app's request for access. Turning a request down stays possible.
|
||||
* Every refusal comes before the service is asked, so nothing is written.
|
||||
*
|
||||
* The controller comes out of OauthModule with OauthService replaced by the
|
||||
* stand-in below, so it is wired the way the app wires it.
|
||||
*/
|
||||
import { DEMO_ACCOUNT_REFUSAL } from '../../../src/nest/common/demo-write';
|
||||
import { RateLimitService } from '../../../src/nest/common/rate-limit.service';
|
||||
import { OauthApiController } from '../../../src/nest/oauth/oauth-api.controller';
|
||||
import type { OauthService } from '../../../src/nest/oauth/oauth.service';
|
||||
import { OauthModule } from '../../../src/nest/oauth/oauth.module';
|
||||
import { OauthService } from '../../../src/nest/oauth/oauth.service';
|
||||
import type { User } from '../../../src/types';
|
||||
import { createSnapshotTestDb } from '../../helpers/db-mock';
|
||||
import { createTestModule, type TestModule } from '../../helpers/test-module';
|
||||
import { HttpException } from '@nestjs/common';
|
||||
|
||||
import type { Request } from 'express';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
vi.mock('../../../src/nest/audit/client-ip', () => ({ getClientIp: vi.fn(() => '1.2.3.4') }));
|
||||
|
||||
@@ -33,8 +38,22 @@ function service() {
|
||||
};
|
||||
}
|
||||
|
||||
let svc: ReturnType<typeof service>;
|
||||
const controller = () => new OauthApiController(svc as unknown as OauthService, new RateLimitService());
|
||||
const db = createSnapshotTestDb();
|
||||
// The module hands the controller this very object, so each test re-arms its
|
||||
// mocks in place instead of building a new container.
|
||||
const svc = service();
|
||||
let t: TestModule;
|
||||
let api: OauthApiController;
|
||||
|
||||
beforeAll(async () => {
|
||||
t = await createTestModule({ db, imports: [OauthModule], overrides: [{ provide: OauthService, useValue: svc }] });
|
||||
api = t.get(OauthApiController);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await t.close();
|
||||
db.close();
|
||||
});
|
||||
|
||||
async function refusal(fn: () => Promise<unknown>): Promise<{ status: number; body: unknown }> {
|
||||
try {
|
||||
@@ -61,7 +80,7 @@ const MCP_REFUSAL = { status: 403, body: { error: DEMO_ACCOUNT_REFUSAL.mcp } };
|
||||
|
||||
beforeEach(() => {
|
||||
vi.stubEnv('DEMO_MODE', 'true');
|
||||
svc = service();
|
||||
Object.assign(svc, service());
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -70,41 +89,41 @@ afterEach(() => {
|
||||
|
||||
describe('OAuth routes for the demo account', () => {
|
||||
it('OAUTH-DEMO-001: registering, rotating and deleting a client are refused before the service runs', async () => {
|
||||
expect(await refusal(() => controller().createClient(demo, { name: 'CLI', allowed_scopes: ['a'] }, req))).toEqual(
|
||||
expect(await refusal(() => api.createClient(demo, { name: 'CLI', allowed_scopes: ['a'] }, req))).toEqual(
|
||||
MCP_REFUSAL,
|
||||
);
|
||||
expect(await refusal(() => controller().rotateClient(demo, 'c1', req))).toEqual(MCP_REFUSAL);
|
||||
expect(await refusal(() => controller().deleteClient(demo, 'c1', req))).toEqual(MCP_REFUSAL);
|
||||
expect(await refusal(() => api.rotateClient(demo, 'c1', req))).toEqual(MCP_REFUSAL);
|
||||
expect(await refusal(() => api.deleteClient(demo, 'c1', req))).toEqual(MCP_REFUSAL);
|
||||
expect(svc.createOAuthClient).not.toHaveBeenCalled();
|
||||
expect(svc.rotateOAuthClientSecret).not.toHaveBeenCalled();
|
||||
expect(svc.deleteOAuthClient).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('OAUTH-DEMO-002: a connected app cannot be ended from the demo account', async () => {
|
||||
expect(await refusal(() => controller().revokeSession(demo, '7', req))).toEqual(MCP_REFUSAL);
|
||||
expect(await refusal(() => api.revokeSession(demo, '7', req))).toEqual(MCP_REFUSAL);
|
||||
expect(svc.revokeSession).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('OAUTH-DEMO-003: approving an app is refused, turning it down still redirects', async () => {
|
||||
expect(await refusal(() => controller().authorize(demo, consent(true), req))).toEqual(MCP_REFUSAL);
|
||||
expect(await refusal(() => api.authorize(demo, consent(true), req))).toEqual(MCP_REFUSAL);
|
||||
expect(svc.saveConsent).not.toHaveBeenCalled();
|
||||
expect(svc.createAuthCode).not.toHaveBeenCalled();
|
||||
|
||||
const denied = await controller().authorize(demo, consent(false), req);
|
||||
const denied = await api.authorize(demo, consent(false), req);
|
||||
expect((denied as { redirect: string }).redirect).toContain('error=access_denied');
|
||||
});
|
||||
|
||||
it('OAUTH-DEMO-004: every other account of the demo instance keeps the routes', async () => {
|
||||
expect(await controller().createClient(other, { name: 'CLI', allowed_scopes: ['a'] }, req)).toEqual({
|
||||
expect(await api.createClient(other, { name: 'CLI', allowed_scopes: ['a'] }, req)).toEqual({
|
||||
client_id: 'c1',
|
||||
client_secret: 's',
|
||||
});
|
||||
const approved = await controller().authorize(other, consent(true), req);
|
||||
const approved = await api.authorize(other, consent(true), req);
|
||||
expect((approved as { redirect: string }).redirect).toContain('code=the_code');
|
||||
});
|
||||
|
||||
it('OAUTH-DEMO-005: off a demo instance the demo address keeps them too', async () => {
|
||||
vi.stubEnv('DEMO_MODE', 'false');
|
||||
expect(await controller().deleteClient(demo, 'c1', req)).toEqual({ success: true });
|
||||
expect(await api.deleteClient(demo, 'c1', req)).toEqual({ success: true });
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user