54 lines
3.3 KiB
TypeScript
54 lines
3.3 KiB
TypeScript
import { afterEach } from '@jest/globals';
|
|
import { HermesSessionBroker } from './hermes-session-broker';
|
|
const credentials = async () => ({username:'backend',password:'test-only'});
|
|
const response = () => new Response('{"ok":true}', {headers:{'set-cookie':'hermes_session_at=verified-test-token; Max-Age=60; Path=/; HttpOnly'}});
|
|
describe('server-only Hermes session broker',()=>{
|
|
afterEach(()=>{jest.useRealTimers();});
|
|
it('renews expired cached access without retaining the refresh cookie',async()=>{
|
|
jest.useFakeTimers();
|
|
const fetcher=jest.fn().mockImplementation(async()=>response());
|
|
const broker=new HermesSessionBroker(credentials,fetcher);
|
|
await broker.token();
|
|
await jest.advanceTimersByTimeAsync(51000);
|
|
await broker.token();
|
|
expect(fetcher).toHaveBeenCalledTimes(2);
|
|
});
|
|
it('bounds credential waits and releases the pending login after timeout',async()=>{
|
|
jest.useFakeTimers();
|
|
const load=jest.fn().mockImplementationOnce(()=>new Promise(()=>{})).mockImplementation(credentials);
|
|
const broker=new HermesSessionBroker(load,jest.fn().mockImplementation(async()=>response()));
|
|
const result=expect(broker.token()).rejects.toMatchObject({message:'Hermes unavailable'});
|
|
await jest.advanceTimersByTimeAsync(5001);await result;
|
|
expect(await broker.token()).toBe('verified-test-token');
|
|
});
|
|
it('rejects oversized response bodies and short cookie lifetimes',async()=>{
|
|
for(const res of [new Response('x'.repeat(4097)),new Response('{"ok":true}',{headers:{'set-cookie':'hermes_session_at=token; Max-Age=1; Path=/'}})]){
|
|
await expect(new HermesSessionBroker(credentials,jest.fn().mockResolvedValue(res)).token()).rejects.toMatchObject({message:'Hermes auth_required'});
|
|
}
|
|
});
|
|
|
|
it('coalesces login, caches only access token and uses installed password-login contract',async()=>{
|
|
const fetcher=jest.fn().mockImplementation(async()=>response());
|
|
const broker=new HermesSessionBroker(credentials,fetcher);
|
|
expect(await Promise.all([broker.token(),broker.token()])).toEqual(['verified-test-token','verified-test-token']);
|
|
expect(await broker.token()).toBe('verified-test-token');
|
|
expect(fetcher).toHaveBeenCalledTimes(1);
|
|
const [url,options]=fetcher.mock.calls[0];
|
|
expect(url).toBe('http://canina-hermes-inventory:9119/auth/password-login');
|
|
expect(JSON.parse(options.body)).toEqual({provider:'basic',username:'backend',password:'test-only'});
|
|
expect(options.redirect).toBe('error');
|
|
});
|
|
it('fails closed without echoing credentials, response bodies or provider errors',async()=>{
|
|
for(const result of [new Response('private secret',{status:401}),new Response('{"ok":true}'),new Response('{"ok":false}',{headers:{'set-cookie':'hermes_session_at=bad; Max-Age=60'}})]){
|
|
const broker=new HermesSessionBroker(credentials,jest.fn().mockResolvedValue(result));
|
|
await expect(broker.token()).rejects.toMatchObject({message:'Hermes auth_required'});
|
|
}
|
|
});
|
|
it('does not cache failures and permits a later successful login',async()=>{
|
|
const fetcher=jest.fn().mockRejectedValueOnce(new Error('sensitive upstream')).mockImplementationOnce(async()=>response());
|
|
const broker=new HermesSessionBroker(credentials,fetcher);
|
|
await expect(broker.token()).rejects.toMatchObject({message:'Hermes unavailable'});
|
|
expect(await broker.token()).toBe('verified-test-token');
|
|
});
|
|
});
|