Skip to content

Commit 59147d6

Browse files
committed
chore: implementando verificação de ultimo login no changePass
1 parent c19bfa5 commit 59147d6

2 files changed

Lines changed: 46 additions & 5 deletions

File tree

api/src/service/auth/auth.service.spec.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,48 @@ describe('AuthService', () => {
9797
await expect(service.login(args)).rejects.toThrow(expectedResponse);
9898
});
9999
});
100+
100101
describe('changePassword', () => {
101102
const args = { cpf: '0000000000', newPass: 'nova-senha' };
102103
it('Resolve: Deve resolver retornando payload', async () => {
103104
const expectedResponse = { message: 'success' } as any;
104-
prismaMock.usuario.findUnique.mockResolvedValue({ id: 'um-id' } as any);
105+
106+
prismaMock.usuario.findUnique.mockResolvedValue({
107+
id: 'um-id',
108+
} as any);
109+
110+
prismaMock.acesso.findUnique.mockResolvedValue({
111+
ultimoLogin: null,
112+
} as any);
113+
105114
prismaMock.acesso.update.mockResolvedValue({ message: 'success' } as any);
106115

107-
await expect(service.changePassword(args)).resolves.toEqual(
108-
expectedResponse,
109-
);
116+
const args = { cpf: '12345678900', newPass: 'new-password' };
117+
118+
// --- executa só uma vez ---
119+
const result = await service.changePassword(args);
120+
121+
// valida retorno
122+
expect(result).toEqual(expectedResponse);
123+
124+
// valida chamadas do prisma
125+
// eslint-disable-next-line @typescript-eslint/unbound-method
126+
expect(prismaMock.usuario.findUnique).toHaveBeenCalledWith({
127+
where: { cpf: args.cpf },
128+
});
129+
130+
// eslint-disable-next-line @typescript-eslint/unbound-method
131+
expect(prismaMock.acesso.findUnique).toHaveBeenCalledWith({
132+
where: { usuarioId: 'um-id' },
133+
});
134+
135+
// eslint-disable-next-line @typescript-eslint/unbound-method
136+
expect(prismaMock.acesso.update).toHaveBeenCalledWith({
137+
where: { usuarioId: 'um-id' },
138+
data: { senha: args.newPass },
139+
});
110140
});
141+
111142
it('Reject: Deve rejeitar caso não encontre usuario com jogando mensagem de erro', async () => {
112143
const expectedResponse = 'Usuario não encontrado';
113144
prismaMock.usuario.findUnique.mockResolvedValue(null);

api/src/service/auth/auth.service.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export class AuthService {
4848
console.log('senha incorreta');
4949
throw new UnauthorizedError('Usuário ou senha incorretos.');
5050
}
51+
5152
const { ultimoLogin } = access;
5253
// eslint-disable-next-line @typescript-eslint/no-unused-vars
5354
const { perfilFuncionalId, ...formatUser } = user;
@@ -64,11 +65,20 @@ export class AuthService {
6465
const user = await this.#database.usuario.findUnique({
6566
where: { cpf },
6667
});
68+
6769
if (!user) {
6870
throw new RecordNotFoundError('Usuario não encontrado');
6971
}
72+
const access = await this.#database.acesso.findUnique({
73+
where: { usuarioId: user.id },
74+
});
75+
76+
if (access?.ultimoLogin) {
77+
throw new UnauthorizedError('Não Autoriazdo');
78+
}
79+
7080
// eslint-disable-next-line @typescript-eslint/no-unused-vars
71-
const access = await this.#database.acesso.update({
81+
await this.#database.acesso.update({
7282
where: { usuarioId: user?.id },
7383
data: { senha: newPass },
7484
});

0 commit comments

Comments
 (0)