Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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