-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
18 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: '[email protected]' } as User; | ||
const mockToken = { | ||
userId: '1', | ||
|
@@ -120,26 +116,26 @@ 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('[email protected]'), | ||
).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: '[email protected]' } as User; | ||
|
||
jest.spyOn(userRepository, 'findOneBy').mockResolvedValue(mockUser); | ||
jest.spyOn(environmentService, 'get').mockReturnValue(''); // No environment variable set | ||
|
||
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); | ||
}); | ||
}); | ||
}); |