|
|
@@ -1,4 +1,5 @@
|
|
|
-import type { Response, NextFunction } from 'express';
|
|
|
+import { faker } from '@faker-js/faker';
|
|
|
+import type { Response } from 'express';
|
|
|
import { mock } from 'vitest-mock-extended';
|
|
|
|
|
|
import type Crowi from '~/server/crowi';
|
|
|
@@ -9,19 +10,6 @@ import type { AccessTokenParserReq } from './interfaces';
|
|
|
import { accessTokenParser } from '.';
|
|
|
|
|
|
|
|
|
-// jest.mock('mongoose', () => ({
|
|
|
-// model: jest.fn().mockReturnValue({
|
|
|
-// findUserByApiToken: jest.fn(),
|
|
|
-// }),
|
|
|
-// }));
|
|
|
-
|
|
|
-// const mockUser = {
|
|
|
-// _id: 'userId',
|
|
|
-// username: 'testuser',
|
|
|
-// email: 'testuser@example.com',
|
|
|
-// };
|
|
|
-
|
|
|
-
|
|
|
describe('access-token-parser middleware', () => {
|
|
|
|
|
|
let User;
|
|
|
@@ -77,18 +65,62 @@ describe('access-token-parser middleware', () => {
|
|
|
expect(nextMock).toHaveBeenCalled();
|
|
|
});
|
|
|
|
|
|
- // it('should call next if access token is invalid', async() => {
|
|
|
- // (mongoose.model().findUserByApiToken as jest.Mock).mockResolvedValue(null);
|
|
|
- // req.query.access_token = 'invalidToken';
|
|
|
- // await accessTokenParser(req as Request, res as Response, next);
|
|
|
- // expect(next).toHaveBeenCalled();
|
|
|
- // });
|
|
|
-
|
|
|
- // it('should set req.user if access token is valid', async() => {
|
|
|
- // (mongoose.model().findUserByApiToken as jest.Mock).mockResolvedValue(mockUser);
|
|
|
- // req.query.access_token = 'validToken';
|
|
|
- // await accessTokenParser(req as Request, res as Response, next);
|
|
|
- // expect(req.user).toEqual(mockUser);
|
|
|
- // expect(next).toHaveBeenCalled();
|
|
|
- // });
|
|
|
+ it('should set req.user with a valid access token in query', async() => {
|
|
|
+ // arrange
|
|
|
+ const reqMock = mock<AccessTokenParserReq>({
|
|
|
+ user: undefined,
|
|
|
+ });
|
|
|
+ const resMock = mock<Response>();
|
|
|
+ const nextMock = vi.fn();
|
|
|
+
|
|
|
+ expect(reqMock.user).toBeUndefined();
|
|
|
+
|
|
|
+ // prepare a user with an access token
|
|
|
+ const targetUser = await User.create({
|
|
|
+ name: faker.person.fullName(),
|
|
|
+ username: faker.string.uuid(),
|
|
|
+ password: faker.internet.password(),
|
|
|
+ lang: 'en_US',
|
|
|
+ apiToken: faker.internet.password(),
|
|
|
+ });
|
|
|
+
|
|
|
+ // act
|
|
|
+ reqMock.query.access_token = targetUser.apiToken;
|
|
|
+ await accessTokenParser(reqMock, resMock, nextMock);
|
|
|
+
|
|
|
+ // assert
|
|
|
+ expect(reqMock.user).toBeDefined();
|
|
|
+ expect(reqMock.user?._id).toStrictEqual(targetUser._id);
|
|
|
+ expect(nextMock).toHaveBeenCalled();
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should set req.user with a valid access token in body', async() => {
|
|
|
+ // arrange
|
|
|
+ const reqMock = mock<AccessTokenParserReq>({
|
|
|
+ user: undefined,
|
|
|
+ });
|
|
|
+ const resMock = mock<Response>();
|
|
|
+ const nextMock = vi.fn();
|
|
|
+
|
|
|
+ expect(reqMock.user).toBeUndefined();
|
|
|
+
|
|
|
+ // prepare a user with an access token
|
|
|
+ const targetUser = await User.create({
|
|
|
+ name: faker.person.fullName(),
|
|
|
+ username: faker.string.uuid(),
|
|
|
+ password: faker.internet.password(),
|
|
|
+ lang: 'en_US',
|
|
|
+ apiToken: faker.internet.password(),
|
|
|
+ });
|
|
|
+
|
|
|
+ // act
|
|
|
+ reqMock.body.access_token = targetUser.apiToken;
|
|
|
+ await accessTokenParser(reqMock, resMock, nextMock);
|
|
|
+
|
|
|
+ // assert
|
|
|
+ expect(reqMock.user).toBeDefined();
|
|
|
+ expect(reqMock.user?._id).toStrictEqual(targetUser._id);
|
|
|
+ expect(nextMock).toHaveBeenCalled();
|
|
|
+ });
|
|
|
+
|
|
|
});
|