From be916b274ca42c557dca596de0982b70a1c7fcda Mon Sep 17 00:00:00 2001 From: Wang Sijie Date: Wed, 23 Feb 2022 17:23:21 +0800 Subject: [PATCH] test(session): test setup for session route (#276) --- packages/core/src/routes/session.test.ts | 42 ++++++++++++++++++++++++ packages/core/src/routes/session.ts | 2 ++ packages/core/src/utils/test-utils.ts | 41 ++++++++++++++++++----- packages/phrases/src/locales/en.ts | 1 + packages/phrases/src/locales/zh-cn.ts | 1 + 5 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 packages/core/src/routes/session.test.ts diff --git a/packages/core/src/routes/session.test.ts b/packages/core/src/routes/session.test.ts new file mode 100644 index 000000000..dbe66822a --- /dev/null +++ b/packages/core/src/routes/session.test.ts @@ -0,0 +1,42 @@ +import { Provider } from 'oidc-provider'; + +import { createRequester } from '@/utils/test-utils'; + +import sessionRoutes from './session'; + +jest.mock('oidc-provider'); +const MockedProvider = Provider as jest.MockedClass; + +describe('sessionRoutes', () => { + const sessionRequest = createRequester({ + anonymousRoutes: sessionRoutes, + provider: new Provider(''), + }); + + afterAll(() => jest.clearAllMocks()); + + it('POST /session with consent prompt name', async () => { + ( + MockedProvider.mock.instances[0]?.interactionDetails as unknown as jest.MockedFunction< + () => Promise<{ prompt: { name: string } }> + > + ).mockResolvedValue({ + prompt: { name: 'consent' }, + }); + const response = await sessionRequest.post('/session'); + + expect(response.status).toEqual(200); + expect(response.body).toHaveProperty('redirectTo', expect.stringContaining('/session/consent')); + }); + + it('POST /session with invalid prompt name', async () => { + ( + MockedProvider.mock.instances[0]?.interactionDetails as unknown as jest.MockedFunction< + () => Promise<{ prompt: { name: string } }> + > + ).mockResolvedValue({ + prompt: { name: 'invalid' }, + }); + await expect(sessionRequest.post('/session').send({})).resolves.toHaveProperty('status', 400); + }); +}); diff --git a/packages/core/src/routes/session.ts b/packages/core/src/routes/session.ts index a44c680f1..cd967a3b8 100644 --- a/packages/core/src/routes/session.ts +++ b/packages/core/src/routes/session.ts @@ -47,6 +47,8 @@ export default function sessionRoutes(router: T, prov return next(); } + + throw new RequestError('session.unsupported_prompt_name'); }); router.post( diff --git a/packages/core/src/utils/test-utils.ts b/packages/core/src/utils/test-utils.ts index 501847a19..d6517155f 100644 --- a/packages/core/src/utils/test-utils.ts +++ b/packages/core/src/utils/test-utils.ts @@ -51,27 +51,52 @@ export const createContextWithRouteParameters = ( }; }; -type RouteLauncher = ( +type RouteLauncher = (router: T) => void; + +type ProviderRouteLauncher = ( router: T, - provider?: Provider + provider: Provider ) => void; -export const createRequester = ({ +export function createRequester( + payload: + | { + anonymousRoutes?: RouteLauncher | Array>; + authedRoutes?: RouteLauncher | Array>; + } + | { + anonymousRoutes?: + | ProviderRouteLauncher + | Array>; + authedRoutes?: RouteLauncher | Array>; + provider: Provider; + } +): request.SuperTest; + +export function createRequester({ anonymousRoutes, authedRoutes, provider, }: { - anonymousRoutes?: RouteLauncher | Array>; + anonymousRoutes?: + | RouteLauncher + | Array> + | ProviderRouteLauncher + | Array>; authedRoutes?: RouteLauncher | Array>; provider?: Provider; -}): request.SuperTest => { +}): request.SuperTest { const app = new Koa(); if (anonymousRoutes) { const anonymousRouter: AnonymousRouter = new Router(); for (const route of Array.isArray(anonymousRoutes) ? anonymousRoutes : [anonymousRoutes]) { - route(anonymousRouter, provider); + if (provider) { + route(anonymousRouter, provider); + } else { + (route as RouteLauncher)(anonymousRouter); + } } app.use(anonymousRouter.routes()).use(anonymousRouter.allowedMethods()); @@ -81,11 +106,11 @@ export const createRequester = ({ const authRouter: AuthedRouter = new Router(); for (const route of Array.isArray(authedRoutes) ? authedRoutes : [authedRoutes]) { - route(authRouter, provider); + route(authRouter); } app.use(authRouter.routes()).use(authRouter.allowedMethods()); } return request(app.callback()); -}; +} diff --git a/packages/phrases/src/locales/en.ts b/packages/phrases/src/locales/en.ts index bbd1c2884..fffe0b516 100644 --- a/packages/phrases/src/locales/en.ts +++ b/packages/phrases/src/locales/en.ts @@ -67,6 +67,7 @@ const errors = { connector_id_mismatch: 'The connectorId is mismatched with session record.', connector_session_not_found: 'Connector session not found. Please go back and sign in again.', unauthorized: 'Please sign in first.', + unsupported_prompt_name: 'Unsupported prompt name', }, connector: { general: 'An unexpected error occured in connector.', diff --git a/packages/phrases/src/locales/zh-cn.ts b/packages/phrases/src/locales/zh-cn.ts index fd5ac277a..dea116510 100644 --- a/packages/phrases/src/locales/zh-cn.ts +++ b/packages/phrases/src/locales/zh-cn.ts @@ -68,6 +68,7 @@ const errors = { connector_id_mismatch: '传入的 connectorId 与 session 中保存的记录不一致。', connector_session_not_found: '无法找到 connector 登录信息,请尝试重新登录。', unauthorized: '请先登录。', + unsupported_prompt_name: '不支持的 prompt name。', }, connector: { general: 'Connector 发生未知错误。',