0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

test(session): test setup for session route (#276)

This commit is contained in:
Wang Sijie 2022-02-23 17:23:21 +08:00 committed by GitHub
parent c033c5e4ec
commit be916b274c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 79 additions and 8 deletions

View file

@ -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<typeof Provider>;
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);
});
});

View file

@ -47,6 +47,8 @@ export default function sessionRoutes<T extends AnonymousRouter>(router: T, prov
return next();
}
throw new RequestError('session.unsupported_prompt_name');
});
router.post(

View file

@ -51,27 +51,52 @@ export const createContextWithRouteParameters = (
};
};
type RouteLauncher<T extends AuthedRouter | AnonymousRouter> = (
type RouteLauncher<T extends AuthedRouter | AnonymousRouter> = (router: T) => void;
type ProviderRouteLauncher<T extends AuthedRouter | AnonymousRouter> = (
router: T,
provider?: Provider
provider: Provider
) => void;
export const createRequester = ({
export function createRequester(
payload:
| {
anonymousRoutes?: RouteLauncher<AnonymousRouter> | Array<RouteLauncher<AnonymousRouter>>;
authedRoutes?: RouteLauncher<AuthedRouter> | Array<RouteLauncher<AuthedRouter>>;
}
| {
anonymousRoutes?:
| ProviderRouteLauncher<AnonymousRouter>
| Array<ProviderRouteLauncher<AnonymousRouter>>;
authedRoutes?: RouteLauncher<AuthedRouter> | Array<RouteLauncher<AuthedRouter>>;
provider: Provider;
}
): request.SuperTest<request.Test>;
export function createRequester({
anonymousRoutes,
authedRoutes,
provider,
}: {
anonymousRoutes?: RouteLauncher<AnonymousRouter> | Array<RouteLauncher<AnonymousRouter>>;
anonymousRoutes?:
| RouteLauncher<AnonymousRouter>
| Array<RouteLauncher<AnonymousRouter>>
| ProviderRouteLauncher<AnonymousRouter>
| Array<ProviderRouteLauncher<AnonymousRouter>>;
authedRoutes?: RouteLauncher<AuthedRouter> | Array<RouteLauncher<AuthedRouter>>;
provider?: Provider;
}): request.SuperTest<request.Test> => {
}): request.SuperTest<request.Test> {
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>)(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());
};
}

View file

@ -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.',

View file

@ -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 发生未知错误。',