feat: Let InitialInteractionHandler redirect requests

This commit is contained in:
Joachim Van Herwegen
2021-07-09 10:41:52 +02:00
parent 0e67004ef4
commit 60ebf5454a
5 changed files with 65 additions and 65 deletions

View File

@@ -87,7 +87,12 @@ export class IdentityTestState {
expect(nextUrl.startsWith(this.oidcIssuer)).toBeTruthy();
// Need to catch the redirect so we can copy the cookies
const res = await this.fetchIdp(nextUrl);
let res = await this.fetchIdp(nextUrl);
expect(res.status).toBe(302);
nextUrl = res.headers.get('location')!;
// Redirect from main page to specific page (login or confirmation)
res = await this.fetchIdp(nextUrl);
expect(res.status).toBe(302);
nextUrl = res.headers.get('location')!;

View File

@@ -1,45 +1,43 @@
import type { MockResponse } from 'node-mocks-http';
import { createResponse } from 'node-mocks-http';
import type { Provider } from 'oidc-provider';
import type { RenderHandlerMap } from '../../../../../src/identity/interaction/util/InitialInteractionHandler';
import type { RedirectMap } from '../../../../../src/identity/interaction/util/InitialInteractionHandler';
import { InitialInteractionHandler } from '../../../../../src/identity/interaction/util/InitialInteractionHandler';
import type { HttpRequest } from '../../../../../src/server/HttpRequest';
import type { HttpResponse } from '../../../../../src/server/HttpResponse';
describe('An InitialInteractionHandler', (): void => {
const baseUrl = 'http://test.com/';
const request: HttpRequest = {} as any;
const response: HttpResponse = {} as any;
let provider: Provider;
let response: MockResponse<any>;
let provider: jest.Mocked<Provider>;
// `Interaction` type is not exposed
let details: any;
let map: RenderHandlerMap;
let map: RedirectMap;
let handler: InitialInteractionHandler;
beforeEach(async(): Promise<void> => {
response = createResponse();
map = {
default: { handleSafe: jest.fn() },
test: { handleSafe: jest.fn() },
} as any;
default: '/idp/login',
test: '/idp/test',
};
details = { prompt: { name: 'test' }};
provider = {
interactionDetails: jest.fn().mockResolvedValue(details),
} as any;
handler = new InitialInteractionHandler(map);
handler = new InitialInteractionHandler(baseUrl, map);
});
it('uses the named handler if it is found.', async(): Promise<void> => {
await expect(handler.handle({ request, response, provider })).resolves.toBeUndefined();
expect(provider.interactionDetails).toHaveBeenCalledTimes(1);
expect(provider.interactionDetails).toHaveBeenLastCalledWith(request, response);
expect(map.default.handleSafe).toHaveBeenCalledTimes(0);
expect(map.test.handleSafe).toHaveBeenCalledTimes(1);
expect(map.test.handleSafe).toHaveBeenLastCalledWith({
response,
contents: {
errorMessage: '',
prefilled: {},
},
});
expect(response._isEndCalled()).toBe(true);
expect(response.getHeader('location')).toBe('http://test.com/idp/test');
expect(response.statusCode).toBe(302);
});
it('uses the default handler if there is no match.', async(): Promise<void> => {
@@ -47,14 +45,8 @@ describe('An InitialInteractionHandler', (): void => {
await expect(handler.handle({ request, response, provider })).resolves.toBeUndefined();
expect(provider.interactionDetails).toHaveBeenCalledTimes(1);
expect(provider.interactionDetails).toHaveBeenLastCalledWith(request, response);
expect(map.default.handleSafe).toHaveBeenCalledTimes(1);
expect(map.test.handleSafe).toHaveBeenCalledTimes(0);
expect(map.default.handleSafe).toHaveBeenLastCalledWith({
response,
contents: {
errorMessage: '',
prefilled: {},
},
});
expect(response._isEndCalled()).toBe(true);
expect(response.getHeader('location')).toBe('http://test.com/idp/login');
expect(response.statusCode).toBe(302);
});
});