Skip to content

Commit

Permalink
Aprimorando o teste
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielVilar committed Feb 12, 2025
1 parent 24189b9 commit 1ef5264
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Repository } from 'typeorm';
import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from './auth.service';
import { AuthService, InvalidPasswordException } from './auth.service';
import { getRepositoryToken } from '@nestjs/typeorm';
import { User, UserRoles } from '../database/entities/user.entity';
import { JwtService } from '@nestjs/jwt';
Expand Down Expand Up @@ -414,5 +414,38 @@ describe('AuthService', () => {
expect(bcrypt.hash).not.toHaveBeenCalled();
expect(userRepository.save).not.toHaveBeenCalled();
});
});
});

describe('InvalidPasswordException', () => {
it('should throw InvalidPasswordException with the correct message', () => {
const message = 'Invalid password';
const exception = new InvalidPasswordException(message);
expect(exception.message).toBe(message);
});
});

describe('validatePassword', () => {
it('should throw BadRequestException if password does not meet criteria', async () => {
const invalidPasswords = [
{ password: 'short', expectedError: 'A senha deve ter pelo menos 8 caracteres.' },
{ password: 'nouppercase', expectedError: 'A senha deve conter pelo menos uma letra maiúscula.' },
{ password: 'NoNumber', expectedError: 'A senha deve conter pelo menos um número.' },
{ password: 'NoSpecial1', expectedError: 'A senha deve conter pelo menos um caractere especial (!@#$%^&*(),.?":{}|<>).' },
];

const validSignUpDto: SignUpDto = {
firstName: 'Test',
lastName: 'User',
email: '[email protected]',
phone: '123456789',
password: 'ValidPassword1!',
};

for (const { password, expectedError } of invalidPasswords) {
await expect(service.signUp({ ...validSignUpDto, password })).rejects.toThrow(BadRequestException);
await expect(service.signUp({ ...validSignUpDto, password })).rejects.toThrow(expectedError);
}
});
});

});

0 comments on commit 1ef5264

Please sign in to comment.