Skip to content

Commit

Permalink
Correção parra passagem dos testes em CI
Browse files Browse the repository at this point in the history
  • Loading branch information
leafrse committed Feb 12, 2025
1 parent 0277746 commit 24189b9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { JwtService } from '@nestjs/jwt';
import { SignUpDto } from '../auth/dtos/signUp.dto';
import * as bcrypt from 'bcryptjs';
import { repositoryMockFactory } from '../../test/database/utils';
import { BadRequestException, UnauthorizedException } from '@nestjs/common';
import { BadRequestException} from '@nestjs/common';
import * as nodemailer from 'nodemailer';
import { ConfigService } from '@nestjs/config';
import { SignInDto } from './dtos/signIn.dto';
Expand Down
31 changes: 15 additions & 16 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
Injectable,
UnauthorizedException,
BadRequestException,
NotFoundException,
} from '@nestjs/common';
Expand Down Expand Up @@ -36,9 +35,9 @@ export class AuthService {
const hasUpperCase = /[A-Z]/.test(password);
const hasNumber = /[0-9]/.test(password);
const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(password);

const errors = [];

if (password.length < minLength) {
errors.push(`A senha deve ter pelo menos ${minLength} caracteres.`);
}
Expand All @@ -51,9 +50,9 @@ export class AuthService {
if (!hasSpecialChar) {
errors.push('A senha deve conter pelo menos um caractere especial (!@#$%^&*(),.?":{}|<>).');
}

if (errors.length > 0) {
throw new BadRequestException(errors.join(' '));
throw new BadRequestException(errors.join(' '));
}
}

Expand All @@ -75,12 +74,12 @@ export class AuthService {
accessToken: await this.jwtService.signAsync(payload, {
expiresIn: accessTokenExpiresIn,
}),
refreshToken: await this.jwtService.signAsync(payload),
refreshToken: await this.jwtService.signAsync(payload),
};
}

async signUp(dto: SignUpDto): Promise<SignInResponseDto> {

this.validatePassword(dto.password);

const userExists = await this.usersRepository.findOneBy({
Expand Down Expand Up @@ -141,30 +140,30 @@ export class AuthService {
userId: string,
changePasswordDto: ChangePasswordDto,
): Promise<void> {
const user = await this.usersRepository.findOneBy( { id: userId } );
const user = await this.usersRepository.findOneBy({ id: userId });

if (!user) {
throw new NotFoundException('Usuário não encontrado.');
}


const passwordMatches = await bcrypt.compare(
changePasswordDto.currentPassword,
user.password,
);

if (!passwordMatches) {
throw new BadRequestException('Senha atual incorreta.');

}

this.validatePassword(changePasswordDto.newPassword);

const hashedPassword = await bcrypt.hash(
changePasswordDto.newPassword,
10,
);

user.password = hashedPassword;
await this.usersRepository.save(user);
}
Expand Down
14 changes: 6 additions & 8 deletions src/auth/dtos/changePassword.dto.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { IsNotEmpty, IsString, Length, Matches } from 'class-validator';

export class ChangePasswordDto {
static currentPassword(currentPassword: any, password: string): any {
throw new Error('Method not implemented.');
}
@IsNotEmpty()
@IsString()
currentPassword: string;
public currentPassword: string;

@IsNotEmpty()
@IsString()
@Length(8, 128, { message: 'A nova senha deve ter entre 8 e 128 caracteres.' })
@Matches(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*(),.?":{}|<>])[A-Za-z\d!@#$%^&*(),.?":{}|<>]+$/, {
message:
'A nova senha deve conter pelo menos uma letra minúscula, uma letra maiúscula, um número e um caractere especial.',
})
public newPassword: string;

@IsNotEmpty()
@IsString()
newPassword: string;
constructor(currentPassword: string, newPassword: string) {
this.currentPassword = currentPassword;
this.newPassword = newPassword;
}
}

0 comments on commit 24189b9

Please sign in to comment.