From ea83099192f94dd35f7958fa7b6fd0b355d73e64 Mon Sep 17 00:00:00 2001 From: Thomas Trompette Date: Thu, 1 Aug 2024 18:52:40 +0200 Subject: [PATCH] Fix test --- .../engine/core-modules/auth/auth.resolver.ts | 10 +++---- .../auth/services/token.service.spec.ts | 30 ++++++++----------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/packages/twenty-server/src/engine/core-modules/auth/auth.resolver.ts b/packages/twenty-server/src/engine/core-modules/auth/auth.resolver.ts index fd767793ba03..abbe55077dec 100644 --- a/packages/twenty-server/src/engine/core-modules/auth/auth.resolver.ts +++ b/packages/twenty-server/src/engine/core-modules/auth/auth.resolver.ts @@ -277,14 +277,14 @@ export class AuthResolver { @Mutation(() => InvalidatePassword) async updatePasswordViaResetToken( - @Args() args: UpdatePasswordViaResetTokenInput, + @Args() + { passwordResetToken, newPassword }: UpdatePasswordViaResetTokenInput, ): Promise { try { - const { id } = await this.tokenService.validatePasswordResetToken( - args.passwordResetToken, - ); + const { id } = + await this.tokenService.validatePasswordResetToken(passwordResetToken); - await this.authService.updatePassword(id, args.newPassword); + await this.authService.updatePassword(id, newPassword); return await this.tokenService.invalidatePasswordResetToken(id); } catch (error) { diff --git a/packages/twenty-server/src/engine/core-modules/auth/services/token.service.spec.ts b/packages/twenty-server/src/engine/core-modules/auth/services/token.service.spec.ts index 34fa1a954ca4..0d807db41bff 100644 --- a/packages/twenty-server/src/engine/core-modules/auth/services/token.service.spec.ts +++ b/packages/twenty-server/src/engine/core-modules/auth/services/token.service.spec.ts @@ -1,8 +1,3 @@ -import { - BadRequestException, - InternalServerErrorException, - NotFoundException, -} from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { getRepositoryToken } from '@nestjs/typeorm'; @@ -14,6 +9,7 @@ import { AppToken, AppTokenType, } from 'src/engine/core-modules/app-token/app-token.entity'; +import { AuthException } from 'src/engine/core-modules/auth/auth.exception'; import { JwtAuthStrategy } from 'src/engine/core-modules/auth/strategies/jwt.auth.strategy'; import { JwtWrapperService } from 'src/engine/core-modules/jwt/services/jwt-wrapper.service'; import { User } from 'src/engine/core-modules/user/user.entity'; @@ -106,7 +102,7 @@ describe('TokenService', () => { expect(result.passwordResetTokenExpiresAt).toBeDefined(); }); - it('should throw BadRequestException if an existing valid token is found', async () => { + it('should throw AuthException if an existing valid token is found', async () => { const mockUser = { id: '1', email: 'test@example.com' } as User; const mockToken = { userId: '1', @@ -120,18 +116,18 @@ describe('TokenService', () => { await expect( service.generatePasswordResetToken(mockUser.email), - ).rejects.toThrow(BadRequestException); + ).rejects.toThrow(AuthException); }); - it('should throw NotFoundException if no user is found', async () => { + it('should throw AuthException if no user is found', async () => { jest.spyOn(userRepository, 'findOneBy').mockResolvedValue(null); await expect( service.generatePasswordResetToken('nonexistent@example.com'), - ).rejects.toThrow(NotFoundException); + ).rejects.toThrow(AuthException); }); - it('should throw InternalServerErrorException if environment variable is not found', async () => { + it('should throw AuthException if environment variable is not found', async () => { const mockUser = { id: '1', email: 'test@example.com' } as User; jest.spyOn(userRepository, 'findOneBy').mockResolvedValue(mockUser); @@ -139,7 +135,7 @@ describe('TokenService', () => { await expect( service.generatePasswordResetToken(mockUser.email), - ).rejects.toThrow(InternalServerErrorException); + ).rejects.toThrow(AuthException); }); }); @@ -181,17 +177,17 @@ describe('TokenService', () => { expect(result).toEqual({ id: mockUser.id, email: mockUser.email }); }); - it('should throw NotFoundException if token is invalid or expired', async () => { + it('should throw AuthException if token is invalid or expired', async () => { const resetToken = 'invalid-reset-token'; jest.spyOn(appTokenRepository, 'findOne').mockResolvedValue(null); await expect( service.validatePasswordResetToken(resetToken), - ).rejects.toThrow(NotFoundException); + ).rejects.toThrow(AuthException); }); - it('should throw NotFoundException if user does not exist for a valid token', async () => { + it('should throw AuthException if user does not exist for a valid token', async () => { const resetToken = 'orphan-token'; const hashedToken = crypto .createHash('sha256') @@ -212,10 +208,10 @@ describe('TokenService', () => { await expect( service.validatePasswordResetToken(resetToken), - ).rejects.toThrow(NotFoundException); + ).rejects.toThrow(AuthException); }); - it('should throw NotFoundException if token is revoked', async () => { + it('should throw AuthException if token is revoked', async () => { const resetToken = 'revoked-token'; const hashedToken = crypto .createHash('sha256') @@ -234,7 +230,7 @@ describe('TokenService', () => { .mockResolvedValue(mockToken as AppToken); await expect( service.validatePasswordResetToken(resetToken), - ).rejects.toThrow(NotFoundException); + ).rejects.toThrow(AuthException); }); }); });