0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-02-03 21:48:55 -05:00
logto/packages/core/src/middleware/koa-oidc-error-handler.test.ts
simeng-li 9c3f017704
test(core): add more middleware tests [2 of 2] (#247)
* test(core): add more middleware tests

add more middleware tests

* fix(ut): fix koaUserInfo ut

fix koaUserInfo ut

* fix(ut): fix ut

fix ut

* fix(ut): revert chagnes

revert changes

* fix(ut): fix ut fail after schema update bug

fix ut fail after schema update bug
2022-02-18 16:25:32 +08:00

116 lines
2.9 KiB
TypeScript

import { errors } from 'oidc-provider';
import RequestError from '@/errors/RequestError';
import { createContextWithRouteParameters } from '@/utils/test-utils';
import koaOIDCErrorHandler from './koa-oidc-error-handler';
describe('koaOIDCErrorHandler middleware', () => {
const next = jest.fn();
const ctx = createContextWithRouteParameters();
it('should throw no errors if no errors are catched', async () => {
await expect(koaOIDCErrorHandler()(ctx, next)).resolves.not.toThrow();
});
it('should throw original error if error type is no OIDCProviderError', async () => {
const error = new Error('err');
next.mockImplementationOnce(() => {
throw error;
});
await expect(koaOIDCErrorHandler()(ctx, next)).rejects.toMatchError(error);
});
it('Invalid Scope', async () => {
const error_description = 'Mock scope is invalid';
const mockScope = 'read:foo';
const error = new errors.InvalidScope(error_description, mockScope);
next.mockImplementationOnce(() => {
throw error;
});
await expect(koaOIDCErrorHandler()(ctx, next)).rejects.toMatchError(
new RequestError(
{
code: 'oidc.invalid_scope',
status: error.status,
expose: true,
scope: mockScope,
},
{ error_description }
)
);
});
it('Session Not Found', async () => {
const error_description = 'session not found';
const error = new errors.SessionNotFound('session not found');
next.mockImplementationOnce(() => {
throw error;
});
await expect(koaOIDCErrorHandler()(ctx, next)).rejects.toMatchError(
new RequestError(
{
code: 'session.not_found',
status: error.status,
expose: true,
},
{
error_description,
}
)
);
});
it('Insufficient Scope', async () => {
const error_description = 'Insufficient scope for access_token';
const scope = 'read:foo';
const error = new errors.InsufficientScope(error_description, scope);
next.mockImplementationOnce(() => {
throw error;
});
await expect(koaOIDCErrorHandler()(ctx, next)).rejects.toMatchError(
new RequestError(
{
code: 'oidc.insufficient_scope',
status: error.status,
expose: true,
scopes: scope,
},
{
error_description,
}
)
);
});
it('Unhandled OIDCProvider Error', async () => {
const error = new errors.AuthorizationPending();
next.mockImplementationOnce(() => {
throw error;
});
await expect(koaOIDCErrorHandler()(ctx, next)).rejects.toMatchError(
new RequestError(
{
code: 'oidc.provider_error',
status: error.status,
expose: true,
message: error.message,
},
{
error_description: error.error_description,
error_detail: error.error_detail,
}
)
);
});
});