fix: Make IDP routes independent of handlers

This commit is contained in:
Joachim Van Herwegen
2022-02-14 12:02:03 +01:00
parent 1ed45c8903
commit 1769b799df
17 changed files with 156 additions and 123 deletions

View File

@@ -0,0 +1,12 @@
import {
AbsolutePathInteractionRoute,
} from '../../../../../src/identity/interaction/routing/AbsolutePathInteractionRoute';
describe('An AbsolutePathInteractionRoute', (): void => {
const path = 'http://example.com/idp/path/';
const route = new AbsolutePathInteractionRoute(path);
it('returns the given path.', async(): Promise<void> => {
expect(route.getPath()).toBe('http://example.com/idp/path/');
});
});

View File

@@ -1,59 +1,53 @@
import type { Operation } from '../../../../../src/http/Operation';
import { BasicRepresentation } from '../../../../../src/http/representation/BasicRepresentation';
import type { Representation } from '../../../../../src/http/representation/Representation';
import type {
InteractionHandler,
} from '../../../../../src/identity/interaction/InteractionHandler';
import { BasicInteractionRoute } from '../../../../../src/identity/interaction/routing/BasicInteractionRoute';
import type { InteractionHandler } from '../../../../../src/identity/interaction/InteractionHandler';
import type { InteractionRoute } from '../../../../../src/identity/interaction/routing/InteractionRoute';
import { InteractionRouteHandler } from '../../../../../src/identity/interaction/routing/InteractionRouteHandler';
import { APPLICATION_JSON } from '../../../../../src/util/ContentTypes';
import { NotFoundHttpError } from '../../../../../src/util/errors/NotFoundHttpError';
import { createPostJsonOperation } from '../email-password/handler/Util';
describe('A BasicInteractionRoute', (): void => {
describe('An InteractionRouteHandler', (): void => {
const path = 'http://example.com/idp/path/';
let operation: Operation;
let representation: Representation;
let route: InteractionRoute;
let source: jest.Mocked<InteractionHandler>;
let route: BasicInteractionRoute;
let handler: InteractionRouteHandler;
beforeEach(async(): Promise<void> => {
operation = createPostJsonOperation({}, 'http://example.com/idp/path/');
operation = createPostJsonOperation({}, path);
representation = new BasicRepresentation(JSON.stringify({}), APPLICATION_JSON);
route = {
getPath: jest.fn().mockReturnValue(path),
};
source = {
canHandle: jest.fn(),
handle: jest.fn().mockResolvedValue(representation),
} as any;
route = new BasicInteractionRoute(path, source);
});
it('returns the given path.', async(): Promise<void> => {
expect(route.getPath()).toBe('http://example.com/idp/path/');
handler = new InteractionRouteHandler(route, source);
});
it('rejects other paths.', async(): Promise<void> => {
operation = createPostJsonOperation({}, 'http://example.com/idp/otherPath/');
await expect(route.canHandle({ operation })).rejects.toThrow(NotFoundHttpError);
await expect(handler.canHandle({ operation })).rejects.toThrow(NotFoundHttpError);
});
it('rejects input its source cannot handle.', async(): Promise<void> => {
source.canHandle.mockRejectedValueOnce(new Error('bad data'));
await expect(route.canHandle({ operation })).rejects.toThrow('bad data');
await expect(handler.canHandle({ operation })).rejects.toThrow('bad data');
});
it('can handle requests its source can handle.', async(): Promise<void> => {
await expect(route.canHandle({ operation })).resolves.toBeUndefined();
await expect(handler.canHandle({ operation })).resolves.toBeUndefined();
});
it('lets its source handle requests.', async(): Promise<void> => {
await expect(route.handle({ operation })).resolves.toBe(representation);
});
it('defaults to an UnsupportedAsyncHandler if no source is provided.', async(): Promise<void> => {
route = new BasicInteractionRoute(path);
await expect(route.canHandle({ operation })).rejects.toThrow('This route has no associated handler.');
await expect(route.handle({ operation })).rejects.toThrow('This route has no associated handler.');
await expect(handler.handle({ operation })).resolves.toBe(representation);
});
});

View File

@@ -1,30 +0,0 @@
import type {
InteractionHandler,
} from '../../../../../src/identity/interaction/InteractionHandler';
import type { InteractionRoute } from '../../../../../src/identity/interaction/routing/InteractionRoute';
import { RelativeInteractionRoute } from '../../../../../src/identity/interaction/routing/RelativeInteractionRoute';
describe('A RelativeInteractionRoute', (): void => {
const relativePath = '/relative/';
let route: jest.Mocked<InteractionRoute>;
let source: jest.Mocked<InteractionHandler>;
let relativeRoute: RelativeInteractionRoute;
beforeEach(async(): Promise<void> => {
route = {
getPath: jest.fn().mockReturnValue('http://example.com/'),
} as any;
source = {
canHandle: jest.fn(),
} as any;
});
it('returns the joined path.', async(): Promise<void> => {
relativeRoute = new RelativeInteractionRoute(route, relativePath, source);
expect(relativeRoute.getPath()).toBe('http://example.com/relative/');
relativeRoute = new RelativeInteractionRoute('http://example.com/', relativePath, source);
expect(relativeRoute.getPath()).toBe('http://example.com/relative/');
});
});

View File

@@ -0,0 +1,24 @@
import type { InteractionRoute } from '../../../../../src/identity/interaction/routing/InteractionRoute';
import {
RelativePathInteractionRoute,
} from '../../../../../src/identity/interaction/routing/RelativePathInteractionRoute';
describe('A RelativePathInteractionRoute', (): void => {
const relativePath = '/relative/';
let route: jest.Mocked<InteractionRoute>;
let relativeRoute: RelativePathInteractionRoute;
beforeEach(async(): Promise<void> => {
route = {
getPath: jest.fn().mockReturnValue('http://example.com/'),
};
});
it('returns the joined path.', async(): Promise<void> => {
relativeRoute = new RelativePathInteractionRoute(route, relativePath);
expect(relativeRoute.getPath()).toBe('http://example.com/relative/');
relativeRoute = new RelativePathInteractionRoute('http://example.com/test/', relativePath);
expect(relativeRoute.getPath()).toBe('http://example.com/test/relative/');
});
});