Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
thomtrp committed Aug 6, 2024
1 parent 34a76fb commit ea83099
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,14 @@ export class AuthResolver {

@Mutation(() => InvalidatePassword)
async updatePasswordViaResetToken(
@Args() args: UpdatePasswordViaResetTokenInput,
@Args()
{ passwordResetToken, newPassword }: UpdatePasswordViaResetTokenInput,
): Promise<InvalidatePassword | void> {
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) {
Expand Down
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';

Expand All @@ -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';
Expand Down Expand Up @@ -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',
Expand All @@ -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);
});
});

Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -234,7 +230,7 @@ describe('TokenService', () => {
.mockResolvedValue(mockToken as AppToken);
await expect(
service.validatePasswordResetToken(resetToken),
).rejects.toThrow(NotFoundException);
).rejects.toThrow(AuthException);
});
});
});

0 comments on commit ea83099

Please sign in to comment.