feat: Pass optional Interaction to InteractionHandlers

This commit is contained in:
Joachim Van Herwegen
2021-08-03 15:57:11 +02:00
parent 66accacde8
commit d3de5f3114
14 changed files with 113 additions and 119 deletions

View File

@@ -1,45 +1,31 @@
import type { Provider } from 'oidc-provider';
import type { ProviderFactory } from '../../../../src/identity/configuration/ProviderFactory';
import type { Interaction } from '../../../../src/identity/interaction/email-password/handler/InteractionHandler';
import { SessionHttpHandler } from '../../../../src/identity/interaction/SessionHttpHandler';
import type { HttpRequest } from '../../../../src/server/HttpRequest';
import type { HttpResponse } from '../../../../src/server/HttpResponse';
import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError';
describe('A SessionHttpHandler', (): void => {
const request: HttpRequest = {} as any;
const response: HttpResponse = {} as any;
const webId = 'http://test.com/id#me';
let details: any = {};
let provider: Provider;
let oidcInteraction: Interaction;
let handler: SessionHttpHandler;
beforeEach(async(): Promise<void> => {
details = { session: { accountId: webId }};
provider = {
interactionDetails: jest.fn().mockResolvedValue(details),
} as any;
oidcInteraction = { session: { accountId: webId }} as any;
const factory: ProviderFactory = {
getProvider: jest.fn().mockResolvedValue(provider),
};
handler = new SessionHttpHandler(factory);
handler = new SessionHttpHandler();
});
it('requires a session and accountId.', async(): Promise<void> => {
details.session = undefined;
await expect(handler.handle({ request, response })).rejects.toThrow(NotImplementedHttpError);
it('requires a defined oidcInteraction with a session.', async(): Promise<void> => {
oidcInteraction!.session = undefined;
await expect(handler.handle({ request, oidcInteraction })).rejects.toThrow(NotImplementedHttpError);
details.session = { accountId: undefined };
await expect(handler.handle({ request, response })).rejects.toThrow(NotImplementedHttpError);
await expect(handler.handle({ request })).rejects.toThrow(NotImplementedHttpError);
});
it('calls the oidc completer with the webId in the session.', async(): Promise<void> => {
await expect(handler.handle({ request, response })).resolves.toEqual({
it('returns an InteractionCompleteResult when done.', async(): Promise<void> => {
await expect(handler.handle({ request, oidcInteraction })).resolves.toEqual({
details: { webId },
type: 'complete',
});
expect(provider.interactionDetails).toHaveBeenCalledTimes(1);
expect(provider.interactionDetails).toHaveBeenLastCalledWith(request, response);
});
});