diff --git a/server/src/enums/exceptionCode.ts b/server/src/enums/exceptionCode.ts index cb15614b..7a9eafcc 100644 --- a/server/src/enums/exceptionCode.ts +++ b/server/src/enums/exceptionCode.ts @@ -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, // 问卷类型错误 diff --git a/server/src/modules/auth/__test/auth.controller.spec.ts b/server/src/modules/auth/__test/auth.controller.spec.ts index b3806c08..9c705944 100644 --- a/server/src/modules/auth/__test/auth.controller.spec.ts +++ b/server/src/modules/auth/__test/auth.controller.spec.ts @@ -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); @@ -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, + ), ); }); }); diff --git a/server/src/modules/auth/controllers/auth.controller.ts b/server/src/modules/auth/controllers/auth.controller.ts index 5fcdc5af..9d5d6d08 100644 --- a/server/src/modules/auth/controllers/auth.controller.ts +++ b/server/src/modules/auth/controllers/auth.controller.ts @@ -85,6 +85,16 @@ 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, @@ -92,7 +102,7 @@ export class AuthController { if (user === null) { throw new HttpException( '用户名或密码错误', - EXCEPTION_CODE.USER_NOT_EXISTS, + EXCEPTION_CODE.USER_PASSWORD_WRONG, ); } let token; diff --git a/web/src/management/pages/list/components/BaseList.vue b/web/src/management/pages/list/components/BaseList.vue index 7b082ac3..fe83d284 100644 --- a/web/src/management/pages/list/components/BaseList.vue +++ b/web/src/management/pages/list/components/BaseList.vue @@ -61,7 +61,7 @@ - +