Skip to content

Commit

Permalink
feat: 登录校验优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Codeniu committed May 30, 2024
1 parent b448440 commit 465f0ad
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 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

0 comments on commit 465f0ad

Please sign in to comment.