84 lines
2.8 KiB
TypeScript
84 lines
2.8 KiB
TypeScript
import { Test } from '@nestjs/testing';
|
|
import { INestApplication } from '@nestjs/common';
|
|
import { PassportModule } from '@nestjs/passport';
|
|
import { Strategy, ExtractJwt } from 'passport-jwt';
|
|
import passport from 'passport';
|
|
import { sign } from 'jsonwebtoken';
|
|
import request from 'supertest';
|
|
import { RuntimeReadModule } from './runtime-read.module';
|
|
import { HermesIntegrationService } from './hermes-integration.service';
|
|
describe('runtime HTTP authentication and read contract', () => {
|
|
let app: INestApplication;
|
|
const health = jest.fn().mockResolvedValue({ ok: true, auth_required: true });
|
|
beforeAll(async () => {
|
|
passport.use(
|
|
'jwt',
|
|
new Strategy(
|
|
{
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
secretOrKey: 'test-only-http-key',
|
|
},
|
|
(payload, done) => done(null, { id: payload.sub, role: payload.role }),
|
|
),
|
|
);
|
|
const module = await Test.createTestingModule({
|
|
imports: [
|
|
PassportModule,
|
|
RuntimeReadModule.register({ 'admin-1': ['default'] }),
|
|
],
|
|
})
|
|
.overrideProvider(HermesIntegrationService)
|
|
.useValue({ health })
|
|
.compile();
|
|
app = module.createNestApplication();
|
|
await app.init();
|
|
});
|
|
afterAll(async () => {
|
|
await app.close();
|
|
passport.unuse('jwt');
|
|
});
|
|
it('rejects missing and forged credentials before upstream', async () => {
|
|
health.mockClear();
|
|
await request(app.getHttpServer())
|
|
.get('/admin/orchestration/runtime/health')
|
|
.expect(401);
|
|
await request(app.getHttpServer())
|
|
.get('/admin/orchestration/runtime/health')
|
|
.set(
|
|
'Authorization',
|
|
'Bearer ' + sign({ sub: 'admin-1', role: 'Admin' }, 'forged'),
|
|
)
|
|
.expect(401);
|
|
expect(health).not.toHaveBeenCalled();
|
|
});
|
|
it('accepts verified allowlisted admin but rejects signed ungranted subject', async () => {
|
|
await request(app.getHttpServer())
|
|
.get('/admin/orchestration/runtime/health')
|
|
.set(
|
|
'Authorization',
|
|
'Bearer ' + sign({ sub: 'other', role: 'Admin' }, 'test-only-http-key'),
|
|
)
|
|
.expect(403);
|
|
const r = await request(app.getHttpServer())
|
|
.get('/admin/orchestration/runtime/health')
|
|
.set(
|
|
'Authorization',
|
|
'Bearer ' +
|
|
sign({ sub: 'admin-1', role: 'Admin' }, 'test-only-http-key'),
|
|
)
|
|
.expect(200);
|
|
expect(r.body).toEqual({ ok: true, auth_required: true });
|
|
expect(r.headers['set-cookie']).toBeUndefined();
|
|
});
|
|
it('does not register mutation handlers', async () => {
|
|
await request(app.getHttpServer())
|
|
.post('/admin/orchestration/runtime/schedules')
|
|
.set(
|
|
'Authorization',
|
|
'Bearer ' +
|
|
sign({ sub: 'admin-1', role: 'Admin' }, 'test-only-http-key'),
|
|
)
|
|
.expect(404);
|
|
});
|
|
});
|