feat: Full rework of account management

Complete rewrite of the account management and related systems.
Makes the architecture more modular,
allowing for easier extensions and configurations.
This commit is contained in:
Joachim Van Herwegen
2022-03-16 10:12:13 +01:00
parent ade977bb4f
commit a47f5236ef
366 changed files with 12345 additions and 5111 deletions

View File

@@ -9,4 +9,9 @@ describe('An AbsolutePathInteractionRoute', (): void => {
it('returns the given path.', async(): Promise<void> => {
expect(route.getPath()).toBe('http://example.com/idp/path/');
});
it('matches a path if it is identical to the stored path.', async(): Promise<void> => {
expect(route.matchPath(path)).toEqual({});
expect(route.matchPath('http://example.com/somewhere/else')).toBeUndefined();
});
});

View File

@@ -0,0 +1,56 @@
import { RepresentationMetadata } from '../../../../../src/http/representation/RepresentationMetadata';
import type { ResourceIdentifier } from '../../../../../src/http/representation/ResourceIdentifier';
import type { AccountIdRoute } from '../../../../../src/identity/interaction/account/AccountIdRoute';
import type { JsonInteractionHandler,
JsonInteractionHandlerInput } from '../../../../../src/identity/interaction/JsonInteractionHandler';
import { AuthorizedRouteHandler } from '../../../../../src/identity/interaction/routing/AuthorizedRouteHandler';
import { ForbiddenHttpError } from '../../../../../src/util/errors/ForbiddenHttpError';
import { UnauthorizedHttpError } from '../../../../../src/util/errors/UnauthorizedHttpError';
describe('An AuthorizedRouteHandler', (): void => {
const accountId = 'accountId';
const target: ResourceIdentifier = { path: 'http://example.com/foo' };
let input: JsonInteractionHandlerInput;
let route: jest.Mocked<AccountIdRoute>;
let source: jest.Mocked<JsonInteractionHandler>;
let handler: AuthorizedRouteHandler;
beforeEach(async(): Promise<void> => {
input = {
target,
json: { data: 'data' },
metadata: new RepresentationMetadata(),
method: 'GET',
accountId,
};
route = {
matchPath: jest.fn().mockReturnValue({ accountId }),
getPath: jest.fn(),
};
source = {
handle: jest.fn().mockResolvedValue('response'),
} as any;
handler = new AuthorizedRouteHandler(route, source);
});
it('calls the source handler with the input.', async(): Promise<void> => {
await expect(handler.handle(input)).resolves.toBe('response');
expect(source.handle).toHaveBeenCalledTimes(1);
expect(source.handle).toHaveBeenLastCalledWith(input);
});
it('errors if there is no account ID in the input.', async(): Promise<void> => {
delete input.accountId;
await expect(handler.handle(input)).rejects.toThrow(UnauthorizedHttpError);
expect(source.handle).toHaveBeenCalledTimes(0);
});
it('errors if the account ID does not match the route result.', async(): Promise<void> => {
route.matchPath.mockReturnValueOnce({ accountId: 'otherId' });
await expect(handler.handle(input)).rejects.toThrow(ForbiddenHttpError);
expect(source.handle).toHaveBeenCalledTimes(0);
});
});

View File

@@ -0,0 +1,48 @@
import { IdInteractionRoute } from '../../../../../src/identity/interaction/routing/IdInteractionRoute';
import type { InteractionRoute } from '../../../../../src/identity/interaction/routing/InteractionRoute';
import { InternalServerError } from '../../../../../src/util/errors/InternalServerError';
describe('An IdInteractionRoute', (): void => {
const idName = 'id';
let base: jest.Mocked<InteractionRoute<'base'>>;
let route: IdInteractionRoute<'base', 'id'>;
beforeEach(async(): Promise<void> => {
base = {
getPath: jest.fn().mockReturnValue('http://example.com/'),
matchPath: jest.fn().mockReturnValue({ base: 'base' }),
};
route = new IdInteractionRoute<'base', 'id'>(base, idName);
});
describe('#getPath', (): void => {
it('appends the identifier value to generate the path.', async(): Promise<void> => {
expect(route.getPath({ base: 'base', id: '12345' })).toBe('http://example.com/12345/');
});
it('errors if there is no input identifier.', async(): Promise<void> => {
expect((): string => route.getPath({ base: 'base' } as any)).toThrow(InternalServerError);
});
it('can be configured not to add a slash at the end.', async(): Promise<void> => {
route = new IdInteractionRoute<'base', 'id'>(base, idName, false);
expect(route.getPath({ base: 'base', id: '12345' })).toBe('http://example.com/12345');
});
});
describe('#matchPath', (): void => {
it('returns the matching values.', async(): Promise<void> => {
expect(route.matchPath('http://example.com/1234/')).toEqual({ base: 'base', id: '1234' });
});
it('returns undefined if there is no match.', async(): Promise<void> => {
expect(route.matchPath('http://example.com/1234')).toBeUndefined();
});
it('returns undefined if there is no base match.', async(): Promise<void> => {
base.matchPath.mockReturnValueOnce(undefined);
expect(route.matchPath('http://example.com/1234/')).toBeUndefined();
});
});
});

View File

@@ -1,53 +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 { RepresentationMetadata } from '../../../../../src/http/representation/RepresentationMetadata';
import type { JsonInteractionHandler,
JsonInteractionHandlerInput } from '../../../../../src/identity/interaction/JsonInteractionHandler';
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('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 handler: InteractionRouteHandler;
const path = 'http://example.com/foo/';
let input: JsonInteractionHandlerInput;
let route: jest.Mocked<InteractionRoute<'base'>>;
let source: jest.Mocked<JsonInteractionHandler>;
let handler: InteractionRouteHandler<InteractionRoute<'base'>>;
beforeEach(async(): Promise<void> => {
operation = createPostJsonOperation({}, path);
representation = new BasicRepresentation(JSON.stringify({}), APPLICATION_JSON);
input = {
target: { path },
json: { data: 'data' },
metadata: new RepresentationMetadata(),
method: 'GET',
};
route = {
getPath: jest.fn().mockReturnValue(path),
matchPath: jest.fn().mockReturnValue({ base: 'base' }),
};
source = {
canHandle: jest.fn(),
handle: jest.fn().mockResolvedValue(representation),
handle: jest.fn().mockResolvedValue('response'),
} as any;
handler = new InteractionRouteHandler(route, source);
});
it('rejects other paths.', async(): Promise<void> => {
operation = createPostJsonOperation({}, 'http://example.com/idp/otherPath/');
await expect(handler.canHandle({ operation })).rejects.toThrow(NotFoundHttpError);
route.matchPath.mockReturnValueOnce(undefined);
await expect(handler.canHandle(input)).rejects.toThrow(NotFoundHttpError);
});
it('rejects input its source cannot handle.', async(): Promise<void> => {
source.canHandle.mockRejectedValueOnce(new Error('bad data'));
await expect(handler.canHandle({ operation })).rejects.toThrow('bad data');
await expect(handler.canHandle(input)).rejects.toThrow('bad data');
});
it('can handle requests its source can handle.', async(): Promise<void> => {
await expect(handler.canHandle({ operation })).resolves.toBeUndefined();
await expect(handler.canHandle(input)).resolves.toBeUndefined();
});
it('lets its source handle requests.', async(): Promise<void> => {
await expect(handler.handle({ operation })).resolves.toBe(representation);
await expect(handler.handle(input)).resolves.toBe('response');
});
});

View File

@@ -2,23 +2,34 @@ import type { InteractionRoute } from '../../../../../src/identity/interaction/r
import {
RelativePathInteractionRoute,
} from '../../../../../src/identity/interaction/routing/RelativePathInteractionRoute';
import { InternalServerError } from '../../../../../src/util/errors/InternalServerError';
describe('A RelativePathInteractionRoute', (): void => {
const relativePath = '/relative/';
let route: jest.Mocked<InteractionRoute>;
let relativeRoute: RelativePathInteractionRoute;
let route: jest.Mocked<InteractionRoute<'base'>>;
let relativeRoute: RelativePathInteractionRoute<'base'>;
beforeEach(async(): Promise<void> => {
route = {
getPath: jest.fn().mockReturnValue('http://example.com/'),
matchPath: jest.fn().mockReturnValue({ base: 'base' }),
};
relativeRoute = new RelativePathInteractionRoute(route, relativePath);
});
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/');
it('matches paths by checking if the tail matches the relative path.', async(): Promise<void> => {
expect(relativeRoute.matchPath('http://example.com/relative/')).toEqual({ base: 'base' });
expect(relativeRoute.matchPath('http://example.com/relative')).toBeUndefined();
});
it('errors if the base path does not end in a slash.', async(): Promise<void> => {
route.getPath.mockReturnValueOnce('http://example.com/foo');
expect((): string => relativeRoute.getPath()).toThrow(InternalServerError);
});
});