feat: 问卷列表操作栏固定 & 登录校验优化(jtest测试文件更新) (#241)

* refactor: 使用vue3组合式API重构登录页代码

* feat: 问卷列表操作栏固定

* feat: 登录校验优化
This commit is contained in:
codeniu 2024-05-30 15:17:03 +08:00 committed by sudoooooo
parent f31cd0f773
commit c904fd3932
4 changed files with 53 additions and 3 deletions

View File

@ -3,6 +3,7 @@ export enum EXCEPTION_CODE {
PARAMETER_ERROR = 1002, // 参数有误 PARAMETER_ERROR = 1002, // 参数有误
USER_EXISTS = 2001, // 用户已存在 USER_EXISTS = 2001, // 用户已存在
USER_NOT_EXISTS = 2002, // 用户不存在 USER_NOT_EXISTS = 2002, // 用户不存在
USER_PASSWORD_WRONG = 2003, // 用户名或密码错误
NO_SURVEY_PERMISSION = 3001, // 没有问卷权限 NO_SURVEY_PERMISSION = 3001, // 没有问卷权限
SURVEY_STATUS_TRANSFORM_ERROR = 3002, // 问卷状态转换报错 SURVEY_STATUS_TRANSFORM_ERROR = 3002, // 问卷状态转换报错
SURVEY_TYPE_ERROR = 3003, // 问卷类型错误 SURVEY_TYPE_ERROR = 3003, // 问卷类型错误

View File

@ -96,12 +96,21 @@ describe('AuthController', () => {
jest jest
.spyOn(captchaService, 'checkCaptchaIsCorrect') .spyOn(captchaService, 'checkCaptchaIsCorrect')
.mockResolvedValue(true); .mockResolvedValue(true);
jest.spyOn(userService, 'getUser').mockResolvedValue( jest.spyOn(userService, 'getUser').mockResolvedValue(
Promise.resolve({ Promise.resolve({
username: 'testUser', username: 'testUser',
_id: new ObjectId(), _id: new ObjectId(),
} as User), } as User),
); );
jest.spyOn(userService, 'getUserByUsername').mockResolvedValue(
Promise.resolve({
username: 'testUser',
_id: new ObjectId(),
} as User),
);
jest.spyOn(authService, 'generateToken').mockResolvedValue('testToken'); jest.spyOn(authService, 'generateToken').mockResolvedValue('testToken');
const result = await controller.login(mockUserInfo); const result = await controller.login(mockUserInfo);
@ -143,10 +152,40 @@ describe('AuthController', () => {
jest jest
.spyOn(captchaService, 'checkCaptchaIsCorrect') .spyOn(captchaService, 'checkCaptchaIsCorrect')
.mockResolvedValue(true); .mockResolvedValue(true);
jest.spyOn(userService, 'getUserByUsername').mockResolvedValue(null);
await expect(controller.login(mockUserInfo)).rejects.toThrow(
new HttpException(
'账号未注册,请进行注册',
EXCEPTION_CODE.USER_NOT_EXISTS,
),
);
});
it('should throw HttpException with USER_NOT_EXISTS code when user is not found', async () => {
const mockUserInfo = {
username: 'testUser',
password: 'testPassword',
captchaId: 'testCaptchaId',
captcha: 'testCaptcha',
};
jest
.spyOn(captchaService, 'checkCaptchaIsCorrect')
.mockResolvedValue(true);
jest.spyOn(userService, 'getUserByUsername').mockResolvedValue(
Promise.resolve({
username: 'testUser',
_id: new ObjectId(),
} as User),
);
jest.spyOn(userService, 'getUser').mockResolvedValue(null); jest.spyOn(userService, 'getUser').mockResolvedValue(null);
await expect(controller.login(mockUserInfo)).rejects.toThrow( await expect(controller.login(mockUserInfo)).rejects.toThrow(
new HttpException('用户名或密码错误', EXCEPTION_CODE.USER_NOT_EXISTS), new HttpException(
'用户名或密码错误',
EXCEPTION_CODE.USER_PASSWORD_WRONG,
),
); );
}); });
}); });

View File

@ -85,6 +85,16 @@ export class AuthController {
throw new HttpException('验证码不正确', EXCEPTION_CODE.CAPTCHA_INCORRECT); throw new HttpException('验证码不正确', EXCEPTION_CODE.CAPTCHA_INCORRECT);
} }
const username = await this.userService.getUserByUsername(
userInfo.username,
);
if (!username) {
throw new HttpException(
'账号未注册,请进行注册',
EXCEPTION_CODE.USER_NOT_EXISTS,
);
}
const user = await this.userService.getUser({ const user = await this.userService.getUser({
username: userInfo.username, username: userInfo.username,
password: userInfo.password, password: userInfo.password,
@ -92,7 +102,7 @@ export class AuthController {
if (user === null) { if (user === null) {
throw new HttpException( throw new HttpException(
'用户名或密码错误', '用户名或密码错误',
EXCEPTION_CODE.USER_NOT_EXISTS, EXCEPTION_CODE.USER_PASSWORD_WRONG,
); );
} }
let token; let token;

View File

@ -61,7 +61,7 @@
</template> </template>
</el-table-column> </el-table-column>
<el-table-column label="操作" :width="300" class-name="table-options"> <el-table-column label="操作" :width="300" class-name="table-options" fixed="right">
<template #default="scope"> <template #default="scope">
<ToolBar <ToolBar
:data="scope.row" :data="scope.row"