0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-27 21:39:16 -05:00
logto/packages/core/src/middleware/koa-user-info.test.ts

39 lines
1.1 KiB
TypeScript
Raw Normal View History

import { mockUser, mockUserResponse } from '@/__mocks__';
import RequestError from '@/errors/RequestError';
import * as userQueries from '@/queries/user';
import { createContextWithRouteParameters } from '@/utils/test-utils';
import koaUserInfo from './koa-user-info';
const findUserByIdSpy = jest.spyOn(userQueries, 'findUserById');
describe('koaUserInfo middleware', () => {
const next = jest.fn();
it('should set userInfo to the context', async () => {
findUserByIdSpy.mockImplementationOnce(async () => Promise.resolve(mockUser));
const ctx = {
...createContextWithRouteParameters(),
auth: 'foo',
userInfo: { id: '' }, // Bypass the middleware Context type
};
await koaUserInfo()(ctx, next);
expect(ctx.userInfo).toEqual(mockUserResponse);
});
it('should throw if is not authenticated', async () => {
const ctx = {
...createContextWithRouteParameters(),
auth: 'foo',
userInfo: { id: '' }, // Bypass the middleware Context type
};
await expect(koaUserInfo()(ctx, next)).rejects.toMatchError(
new RequestError({ code: 'auth.unauthorized', status: 401 })
);
});
});