Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
thomtrp committed Aug 1, 2024
1 parent e89bdb3 commit a7f1dc3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 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,25 +1,21 @@
import { Test, TestingModule } from '@nestjs/testing';
import { JwtService } from '@nestjs/jwt';
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import {
BadRequestException,
InternalServerErrorException,
NotFoundException,
} from '@nestjs/common';

import crypto from 'crypto';

import { IsNull, MoreThan, Repository } from 'typeorm';

import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
import {
AppToken,
AppTokenType,
} from 'src/engine/core-modules/app-token/app-token.entity';
import { User } from 'src/engine/core-modules/user/user.entity';
import { AuthException } from 'src/engine/core-modules/auth/auth.exception';
import { JwtAuthStrategy } from 'src/engine/core-modules/auth/strategies/jwt.auth.strategy';
import { EmailService } from 'src/engine/integrations/email/email.service';
import { User } from 'src/engine/core-modules/user/user.entity';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { EmailService } from 'src/engine/integrations/email/email.service';
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';

import { TokenService } from './token.service';

Expand Down Expand Up @@ -108,7 +104,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 @@ -122,26 +118,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 @@ -183,17 +179,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 @@ -214,10 +210,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 @@ -236,7 +232,7 @@ describe('TokenService', () => {
.mockResolvedValue(mockToken as AppToken);
await expect(
service.validatePasswordResetToken(resetToken),
).rejects.toThrow(NotFoundException);
).rejects.toThrow(AuthException);
});
});
});

0 comments on commit a7f1dc3

Please sign in to comment.