mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
refactor: Move InteractionRoute behaviour to separate class
This commit is contained in:
@@ -1,14 +1,8 @@
|
||||
import type { Provider } from 'oidc-provider';
|
||||
import type { ProviderFactory } from '../../../src/identity/configuration/ProviderFactory';
|
||||
import type {
|
||||
IdentityProviderHttpHandlerArgs,
|
||||
} from '../../../src/identity/IdentityProviderHttpHandler';
|
||||
import {
|
||||
InteractionRoute,
|
||||
IdentityProviderHttpHandler,
|
||||
} from '../../../src/identity/IdentityProviderHttpHandler';
|
||||
import type { InteractionHandler } from '../../../src/identity/interaction/email-password/handler/InteractionHandler';
|
||||
import { IdpInteractionError } from '../../../src/identity/interaction/util/IdpInteractionError';
|
||||
import type { IdentityProviderHttpHandlerArgs } from '../../../src/identity/IdentityProviderHttpHandler';
|
||||
import { IdentityProviderHttpHandler } from '../../../src/identity/IdentityProviderHttpHandler';
|
||||
import type { InteractionRoute } from '../../../src/identity/interaction/routing/InteractionRoute';
|
||||
import type { InteractionCompleter } from '../../../src/identity/interaction/util/InteractionCompleter';
|
||||
import type { ErrorHandler } from '../../../src/ldp/http/ErrorHandler';
|
||||
import type { RequestParser } from '../../../src/ldp/http/RequestParser';
|
||||
@@ -24,7 +18,6 @@ import type {
|
||||
RepresentationConverter,
|
||||
RepresentationConverterArgs,
|
||||
} from '../../../src/storage/conversion/RepresentationConverter';
|
||||
import { BadRequestHttpError } from '../../../src/util/errors/BadRequestHttpError';
|
||||
import { joinUrl } from '../../../src/util/PathUtil';
|
||||
import { readableToString } from '../../../src/util/StreamUtil';
|
||||
import { CONTENT_TYPE, SOLID_HTTP, SOLID_META } from '../../../src/util/Vocabularies';
|
||||
@@ -37,7 +30,7 @@ describe('An IdentityProviderHttpHandler', (): void => {
|
||||
const response: HttpResponse = {} as any;
|
||||
let requestParser: jest.Mocked<RequestParser>;
|
||||
let providerFactory: jest.Mocked<ProviderFactory>;
|
||||
let routes: { response: InteractionRoute; complete: InteractionRoute };
|
||||
let routes: { response: jest.Mocked<InteractionRoute>; complete: jest.Mocked<InteractionRoute> };
|
||||
let controls: Record<string, string>;
|
||||
let interactionCompleter: jest.Mocked<InteractionCompleter>;
|
||||
let converter: jest.Mocked<RepresentationConverter>;
|
||||
@@ -69,22 +62,25 @@ describe('An IdentityProviderHttpHandler', (): void => {
|
||||
getProvider: jest.fn().mockResolvedValue(provider),
|
||||
};
|
||||
|
||||
const handlers: InteractionHandler[] = [
|
||||
{ handleSafe: jest.fn().mockResolvedValue({ type: 'response', details: { key: 'val' }}) } as any,
|
||||
{ handleSafe: jest.fn().mockResolvedValue({ type: 'complete', details: { webId: 'webId' }}) } as any,
|
||||
];
|
||||
|
||||
routes = {
|
||||
response: new InteractionRoute('^/routeResponse$',
|
||||
{ 'text/html': '/view1' },
|
||||
handlers[0],
|
||||
'login',
|
||||
{ 'text/html': '/response1' },
|
||||
{ response: '/routeResponse' }),
|
||||
complete: new InteractionRoute('^/routeComplete$',
|
||||
{ 'text/html': '/view2' },
|
||||
handlers[1],
|
||||
'other'),
|
||||
response: {
|
||||
getControls: jest.fn().mockReturnValue({ response: '/routeResponse' }),
|
||||
supportsPath: jest.fn((path: string): boolean => /^\/routeResponse$/u.test(path)),
|
||||
handleOperation: jest.fn().mockResolvedValue({
|
||||
type: 'response',
|
||||
details: { key: 'val' },
|
||||
templateFiles: { 'text/html': '/response' },
|
||||
}),
|
||||
},
|
||||
complete: {
|
||||
getControls: jest.fn().mockReturnValue({}),
|
||||
supportsPath: jest.fn((path: string): boolean => /^\/routeComplete$/u.test(path)),
|
||||
handleOperation: jest.fn().mockResolvedValue({
|
||||
type: 'complete',
|
||||
details: { webId: 'webId' },
|
||||
templateFiles: {},
|
||||
}),
|
||||
},
|
||||
};
|
||||
controls = { response: 'http://test.com/idp/routeResponse' };
|
||||
|
||||
@@ -124,27 +120,13 @@ describe('An IdentityProviderHttpHandler', (): void => {
|
||||
expect(provider.callback).toHaveBeenLastCalledWith(request, response);
|
||||
});
|
||||
|
||||
it('creates default Representations for GET requests.', async(): Promise<void> => {
|
||||
request.url = '/idp/routeResponse';
|
||||
await expect(handler.handle({ request, response })).resolves.toBeUndefined();
|
||||
|
||||
expect(responseWriter.handleSafe).toHaveBeenCalledTimes(1);
|
||||
const { response: mockResponse, result } = responseWriter.handleSafe.mock.calls[0][0];
|
||||
expect(mockResponse).toBe(response);
|
||||
expect(JSON.parse(await readableToString(result.data!)))
|
||||
.toEqual({ apiVersion, authenticating: false, controls });
|
||||
expect(result.statusCode).toBe(200);
|
||||
expect(result.metadata?.contentType).toBe('text/html');
|
||||
expect(result.metadata?.get(SOLID_META.template)?.value).toBe(routes.response.viewTemplates['text/html']);
|
||||
});
|
||||
|
||||
it('creates Representations for InteractionResponseResults.', async(): Promise<void> => {
|
||||
request.url = '/idp/routeResponse';
|
||||
request.method = 'POST';
|
||||
await expect(handler.handle({ request, response })).resolves.toBeUndefined();
|
||||
const operation: Operation = await requestParser.handleSafe.mock.results[0].value;
|
||||
expect(routes.response.handler.handleSafe).toHaveBeenCalledTimes(1);
|
||||
expect(routes.response.handler.handleSafe).toHaveBeenLastCalledWith({ operation });
|
||||
expect(routes.response.handleOperation).toHaveBeenCalledTimes(1);
|
||||
expect(routes.response.handleOperation).toHaveBeenLastCalledWith(operation, undefined);
|
||||
expect(operation.body?.metadata.contentType).toBe('application/json');
|
||||
|
||||
expect(responseWriter.handleSafe).toHaveBeenCalledTimes(1);
|
||||
@@ -154,7 +136,7 @@ describe('An IdentityProviderHttpHandler', (): void => {
|
||||
.toEqual({ apiVersion, key: 'val', authenticating: false, controls });
|
||||
expect(result.statusCode).toBe(200);
|
||||
expect(result.metadata?.contentType).toBe('text/html');
|
||||
expect(result.metadata?.get(SOLID_META.template)?.value).toBe(routes.response.responseTemplates['text/html']);
|
||||
expect(result.metadata?.get(SOLID_META.template)?.value).toBe('/response');
|
||||
});
|
||||
|
||||
it('indicates to the templates if the request is part of an auth flow.', async(): Promise<void> => {
|
||||
@@ -162,7 +144,8 @@ describe('An IdentityProviderHttpHandler', (): void => {
|
||||
request.method = 'POST';
|
||||
const oidcInteraction = { session: { accountId: 'account' }, prompt: {}} as any;
|
||||
provider.interactionDetails.mockResolvedValueOnce(oidcInteraction);
|
||||
(routes.response.handler as jest.Mocked<InteractionHandler>).handleSafe.mockResolvedValueOnce({ type: 'response' });
|
||||
routes.response.handleOperation
|
||||
.mockResolvedValueOnce({ type: 'response', templateFiles: { 'text/html': '/response' }});
|
||||
await expect(handler.handle({ request, response })).resolves.toBeUndefined();
|
||||
|
||||
expect(responseWriter.handleSafe).toHaveBeenCalledTimes(1);
|
||||
@@ -176,8 +159,8 @@ describe('An IdentityProviderHttpHandler', (): void => {
|
||||
errorHandler.handleSafe.mockResolvedValueOnce({ statusCode: 400 });
|
||||
await expect(handler.handle({ request, response })).resolves.toBeUndefined();
|
||||
const operation: Operation = await requestParser.handleSafe.mock.results[0].value;
|
||||
expect(routes.complete.handler.handleSafe).toHaveBeenCalledTimes(1);
|
||||
expect(routes.complete.handler.handleSafe).toHaveBeenLastCalledWith({ operation });
|
||||
expect(routes.complete.handleOperation).toHaveBeenCalledTimes(1);
|
||||
expect(routes.complete.handleOperation).toHaveBeenLastCalledWith(operation, undefined);
|
||||
expect(interactionCompleter.handleSafe).toHaveBeenCalledTimes(0);
|
||||
expect(operation.body?.metadata.contentType).toBe('application/json');
|
||||
|
||||
@@ -198,8 +181,8 @@ describe('An IdentityProviderHttpHandler', (): void => {
|
||||
provider.interactionDetails.mockResolvedValueOnce(oidcInteraction);
|
||||
await expect(handler.handle({ request, response })).resolves.toBeUndefined();
|
||||
const operation: Operation = await requestParser.handleSafe.mock.results[0].value;
|
||||
expect(routes.complete.handler.handleSafe).toHaveBeenCalledTimes(1);
|
||||
expect(routes.complete.handler.handleSafe).toHaveBeenLastCalledWith({ operation, oidcInteraction });
|
||||
expect(routes.complete.handleOperation).toHaveBeenCalledTimes(1);
|
||||
expect(routes.complete.handleOperation).toHaveBeenLastCalledWith(operation, oidcInteraction);
|
||||
expect(operation.body?.metadata.contentType).toBe('application/json');
|
||||
|
||||
expect(interactionCompleter.handleSafe).toHaveBeenCalledTimes(1);
|
||||
@@ -212,50 +195,6 @@ describe('An IdentityProviderHttpHandler', (): void => {
|
||||
expect(args.result.metadata?.get(SOLID_HTTP.terms.location)?.value).toBe(location);
|
||||
});
|
||||
|
||||
it('matches paths based on prompt for requests to the root IDP.', async(): Promise<void> => {
|
||||
request.url = '/idp';
|
||||
request.method = 'POST';
|
||||
const oidcInteraction = { prompt: { name: 'other' }};
|
||||
provider.interactionDetails.mockResolvedValueOnce(oidcInteraction as any);
|
||||
await expect(handler.handle({ request, response })).resolves.toBeUndefined();
|
||||
const operation: Operation = await requestParser.handleSafe.mock.results[0].value;
|
||||
expect(routes.response.handler.handleSafe).toHaveBeenCalledTimes(0);
|
||||
expect(routes.complete.handler.handleSafe).toHaveBeenCalledTimes(1);
|
||||
expect(routes.complete.handler.handleSafe).toHaveBeenLastCalledWith({ operation, oidcInteraction });
|
||||
expect(operation.body?.metadata.contentType).toBe('application/json');
|
||||
});
|
||||
|
||||
it('displays a viewTemplate again in case of POST errors.', async(): Promise<void> => {
|
||||
request.url = '/idp/routeResponse';
|
||||
request.method = 'POST';
|
||||
(routes.response.handler.handleSafe as any)
|
||||
.mockRejectedValueOnce(new IdpInteractionError(500, 'handle error', { name: 'name' }));
|
||||
await expect(handler.handle({ request, response })).resolves.toBeUndefined();
|
||||
|
||||
expect(responseWriter.handleSafe).toHaveBeenCalledTimes(1);
|
||||
const { response: mockResponse, result } = responseWriter.handleSafe.mock.calls[0][0];
|
||||
expect(mockResponse).toBe(response);
|
||||
expect(JSON.parse(await readableToString(result.data!))).toEqual(
|
||||
{ apiVersion, errorMessage: 'handle error', prefilled: { name: 'name' }, authenticating: false, controls },
|
||||
);
|
||||
expect(result.statusCode).toBe(200);
|
||||
expect(result.metadata?.contentType).toBe('text/html');
|
||||
expect(result.metadata?.get(SOLID_META.template)?.value).toBe(routes.response.viewTemplates['text/html']);
|
||||
});
|
||||
|
||||
it('defaults to an empty prefilled object in case of POST errors.', async(): Promise<void> => {
|
||||
request.url = '/idp/routeResponse';
|
||||
request.method = 'POST';
|
||||
(routes.response.handler.handleSafe as any).mockRejectedValueOnce(new Error('handle error'));
|
||||
await expect(handler.handle({ request, response })).resolves.toBeUndefined();
|
||||
|
||||
expect(responseWriter.handleSafe).toHaveBeenCalledTimes(1);
|
||||
const { response: mockResponse, result } = responseWriter.handleSafe.mock.calls[0][0];
|
||||
expect(mockResponse).toBe(response);
|
||||
expect(JSON.parse(await readableToString(result.data!)))
|
||||
.toEqual({ apiVersion, errorMessage: 'handle error', authenticating: false, controls });
|
||||
});
|
||||
|
||||
it('calls the errorHandler if there is a problem resolving the request.', async(): Promise<void> => {
|
||||
request.url = '/idp/routeResponse';
|
||||
request.method = 'GET';
|
||||
@@ -268,16 +207,4 @@ describe('An IdentityProviderHttpHandler', (): void => {
|
||||
expect(responseWriter.handleSafe).toHaveBeenCalledTimes(1);
|
||||
expect(responseWriter.handleSafe).toHaveBeenLastCalledWith({ response, result: { statusCode: 500 }});
|
||||
});
|
||||
|
||||
it('can only resolve GET/POST requests.', async(): Promise<void> => {
|
||||
request.url = '/idp/routeResponse';
|
||||
request.method = 'DELETE';
|
||||
const error = new BadRequestHttpError('Unsupported request: DELETE http://test.com/idp/routeResponse');
|
||||
errorHandler.handleSafe.mockResolvedValueOnce({ statusCode: 500 });
|
||||
await expect(handler.handle({ request, response })).resolves.toBeUndefined();
|
||||
expect(errorHandler.handleSafe).toHaveBeenCalledTimes(1);
|
||||
expect(errorHandler.handleSafe).toHaveBeenLastCalledWith({ error, preferences: { type: { 'text/html': 1 }}});
|
||||
expect(responseWriter.handleSafe).toHaveBeenCalledTimes(1);
|
||||
expect(responseWriter.handleSafe).toHaveBeenLastCalledWith({ response, result: { statusCode: 500 }});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import type {
|
||||
InteractionHandler,
|
||||
} from '../../../../../src/identity/interaction/email-password/handler/InteractionHandler';
|
||||
import { BasicInteractionRoute } from '../../../../../src/identity/interaction/routing/BasicInteractionRoute';
|
||||
import { IdpInteractionError } from '../../../../../src/identity/interaction/util/IdpInteractionError';
|
||||
import { BadRequestHttpError } from '../../../../../src/util/errors/BadRequestHttpError';
|
||||
|
||||
describe('A BasicInteractionRoute', (): void => {
|
||||
const path = '^/route$';
|
||||
const viewTemplates = { 'text/html': '/viewTemplate' };
|
||||
let handler: jest.Mocked<InteractionHandler>;
|
||||
const prompt = 'login';
|
||||
const responseTemplates = { 'text/html': '/responseTemplate' };
|
||||
const controls = { login: '/route' };
|
||||
const response = { type: 'response' };
|
||||
let route: BasicInteractionRoute;
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
handler = {
|
||||
handleSafe: jest.fn().mockResolvedValue(response),
|
||||
} as any;
|
||||
|
||||
route = new BasicInteractionRoute(path, viewTemplates, handler, prompt, responseTemplates, controls);
|
||||
});
|
||||
|
||||
it('returns its controls.', async(): Promise<void> => {
|
||||
expect(route.getControls()).toEqual(controls);
|
||||
});
|
||||
|
||||
it('supports a path if it matches the stored route.', async(): Promise<void> => {
|
||||
expect(route.supportsPath('/route')).toBe(true);
|
||||
expect(route.supportsPath('/notRoute')).toBe(false);
|
||||
});
|
||||
|
||||
it('supports prompts when targeting the base path.', async(): Promise<void> => {
|
||||
expect(route.supportsPath('/', prompt)).toBe(true);
|
||||
expect(route.supportsPath('/notRoute', prompt)).toBe(false);
|
||||
expect(route.supportsPath('/', 'notPrompt')).toBe(false);
|
||||
});
|
||||
|
||||
it('returns a response result on a GET request.', async(): Promise<void> => {
|
||||
await expect(route.handleOperation({ method: 'GET' } as any))
|
||||
.resolves.toEqual({ type: 'response', templateFiles: viewTemplates });
|
||||
});
|
||||
|
||||
it('returns the result of the InteractionHandler on POST requests.', async(): Promise<void> => {
|
||||
await expect(route.handleOperation({ method: 'POST' } as any))
|
||||
.resolves.toEqual({ ...response, templateFiles: responseTemplates });
|
||||
expect(handler.handleSafe).toHaveBeenCalledTimes(1);
|
||||
expect(handler.handleSafe).toHaveBeenLastCalledWith({ operation: { method: 'POST' }});
|
||||
});
|
||||
|
||||
it('creates a response result in case the InteractionHandler errors.', async(): Promise<void> => {
|
||||
const error = new Error('bad data');
|
||||
handler.handleSafe.mockRejectedValueOnce(error);
|
||||
await expect(route.handleOperation({ method: 'POST' } as any))
|
||||
.resolves.toEqual({ type: 'response', details: { errorMessage: 'bad data' }, templateFiles: viewTemplates });
|
||||
});
|
||||
|
||||
it('adds prefilled data in case the error is an IdpInteractionError.', async(): Promise<void> => {
|
||||
const error = new IdpInteractionError(400, 'bad data', { name: 'Alice' });
|
||||
handler.handleSafe.mockRejectedValueOnce(error);
|
||||
await expect(route.handleOperation({ method: 'POST' } as any)).resolves.toEqual({
|
||||
type: 'response',
|
||||
details: { errorMessage: 'bad data', prefilled: { name: 'Alice' }},
|
||||
templateFiles: viewTemplates,
|
||||
});
|
||||
});
|
||||
|
||||
it('errors for non-supported operations.', async(): Promise<void> => {
|
||||
const prom = route.handleOperation({ method: 'DELETE', target: { path: '/route' }} as any);
|
||||
await expect(prom).rejects.toThrow(BadRequestHttpError);
|
||||
await expect(prom).rejects.toThrow('Unsupported request: DELETE /route');
|
||||
expect(handler.handleSafe).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
||||
it('defaults to empty controls.', async(): Promise<void> => {
|
||||
route = new BasicInteractionRoute(path, viewTemplates, handler, prompt);
|
||||
expect(route.getControls()).toEqual({});
|
||||
});
|
||||
|
||||
it('defaults to empty response templates.', async(): Promise<void> => {
|
||||
route = new BasicInteractionRoute(path, viewTemplates, handler, prompt);
|
||||
await expect(route.handleOperation({ method: 'POST' } as any)).resolves.toEqual({ ...response, templateFiles: {}});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user