Skip to content

Commit

Permalink
feat: 问卷列表操作栏固定 & 登录校验优化(jtest测试文件更新) (#241)
Browse files Browse the repository at this point in the history
* refactor: 使用vue3组合式API重构登录页代码

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

* feat: 登录校验优化
  • Loading branch information
Codeniu authored May 30, 2024
1 parent 8db8f9a commit 17b84ef
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions server/src/enums/exceptionCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export enum EXCEPTION_CODE {
PARAMETER_ERROR = 1002, // 参数有误
USER_EXISTS = 2001, // 用户已存在
USER_NOT_EXISTS = 2002, // 用户不存在
USER_PASSWORD_WRONG = 2003, // 用户名或密码错误
NO_SURVEY_PERMISSION = 3001, // 没有问卷权限
SURVEY_STATUS_TRANSFORM_ERROR = 3002, // 问卷状态转换报错
SURVEY_TYPE_ERROR = 3003, // 问卷类型错误
Expand Down
41 changes: 40 additions & 1 deletion server/src/modules/auth/__test/auth.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,21 @@ describe('AuthController', () => {
jest
.spyOn(captchaService, 'checkCaptchaIsCorrect')
.mockResolvedValue(true);

jest.spyOn(userService, 'getUser').mockResolvedValue(
Promise.resolve({
username: 'testUser',
_id: new ObjectId(),
} as User),
);

jest.spyOn(userService, 'getUserByUsername').mockResolvedValue(
Promise.resolve({
username: 'testUser',
_id: new ObjectId(),
} as User),
);

jest.spyOn(authService, 'generateToken').mockResolvedValue('testToken');

const result = await controller.login(mockUserInfo);
Expand Down Expand Up @@ -143,10 +152,40 @@ describe('AuthController', () => {
jest
.spyOn(captchaService, 'checkCaptchaIsCorrect')
.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);

await expect(controller.login(mockUserInfo)).rejects.toThrow(
new HttpException('用户名或密码错误', EXCEPTION_CODE.USER_NOT_EXISTS),
new HttpException(
'用户名或密码错误',
EXCEPTION_CODE.USER_PASSWORD_WRONG,
),
);
});
});
Expand Down
12 changes: 11 additions & 1 deletion server/src/modules/auth/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,24 @@ export class AuthController {
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({
username: userInfo.username,
password: userInfo.password,
});
if (user === null) {
throw new HttpException(
'用户名或密码错误',
EXCEPTION_CODE.USER_NOT_EXISTS,
EXCEPTION_CODE.USER_PASSWORD_WRONG,
);
}
let token;
Expand Down
2 changes: 1 addition & 1 deletion web/src/management/pages/list/components/BaseList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
</template>
</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">
<ToolBar
:data="scope.row"
Expand Down

0 comments on commit 17b84ef

Please sign in to comment.