-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6dd34ff
commit 6ddfe74
Showing
5 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { afterAll, beforeAll, describe, expect, it } from 'vitest' | ||
import request from 'supertest' | ||
import { app } from '../../app' | ||
import { createAndAuthenticateUser } from '../../utils/create-and-authenticate-user' | ||
|
||
let userAuth: { | ||
token: string | ||
userId: string | ||
} | ||
|
||
describe('edit User Pass E2E', () => { | ||
beforeAll(async () => { | ||
await app.ready() | ||
userAuth = await createAndAuthenticateUser(app) | ||
}) | ||
|
||
afterAll(async () => { | ||
await app.close() | ||
}) | ||
|
||
it('should be able to edit a user pass', async () => { | ||
const editUserPassResponse = await request(app.server) | ||
.put(`/user/edit/pass`) | ||
.send({ | ||
newPassword: 'newAwesomePassword', | ||
oldPassword: '12345678', | ||
}) | ||
.set('Authorization', `Bearer ${userAuth.token}`) | ||
|
||
expect(editUserPassResponse.statusCode).toEqual(200) | ||
expect(editUserPassResponse.body.user).toEqual( | ||
expect.objectContaining({ | ||
name: 'John', | ||
surname: 'Doe', | ||
email: '[email protected]', | ||
id: userAuth.userId, | ||
}), | ||
) | ||
|
||
const userData = await request(app.server) | ||
.post('/login') | ||
.send({ email: '[email protected]', password: 'newAwesomePassword' }) | ||
|
||
expect(userData.statusCode).toEqual(200) | ||
expect(userData.body).toEqual({ | ||
user: expect.any(Object), | ||
token: expect.any(String), | ||
}) | ||
}) | ||
|
||
it('should not be able to edit a user pass with the old pass wrong', async () => { | ||
const editUserPassResponse = await request(app.server) | ||
.put(`/user/edit/pass`) | ||
.send({ | ||
newPassword: 'newAwesomePassword', | ||
oldPassword: 'WRONGPASS', | ||
}) | ||
.set('Authorization', `Bearer ${userAuth.token}`) | ||
|
||
expect(editUserPassResponse.statusCode).toEqual(401) | ||
expect(editUserPassResponse.body).toEqual( | ||
expect.objectContaining({ error: 'Invalid old Password!' }), | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { FastifyRequest, FastifyReply } from 'fastify' | ||
import { z } from 'zod' | ||
import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository' | ||
import { EditUserPasswordUseCase } from '../../use-cases/user/editUserPasswordUseCase' | ||
import { ResourceNotFoundError } from '../../use-cases/errors/ResourceNotFoundError' | ||
import { InvalidCredentialsError } from '../../use-cases/errors/InvalidCredentialsError' | ||
|
||
export async function editUserPassword( | ||
request: FastifyRequest, | ||
response: FastifyReply, | ||
) { | ||
const registerBodySchema = z.object({ | ||
oldPassword: z.string(), | ||
newPassword: z.string().min(6), | ||
}) | ||
|
||
const { oldPassword, newPassword } = registerBodySchema.parse(request.body) | ||
|
||
const usersRepository = new PrismaUsersRepository() | ||
const editUserPasswordUseCase = new EditUserPasswordUseCase(usersRepository) | ||
try { | ||
const { user } = await editUserPasswordUseCase.execute({ | ||
oldPassword, | ||
newPassword, | ||
userId: request.user.sub, | ||
}) | ||
return response | ||
.status(200) | ||
.send({ user: { ...user, password_hash: undefined } }) | ||
} catch (error) { | ||
if (error instanceof ResourceNotFoundError) { | ||
return response.status(404).send({ error: 'User was not Found !' }) | ||
} else if (error instanceof InvalidCredentialsError) { | ||
return response.status(401).send({ error: 'Invalid old Password!' }) | ||
} | ||
|
||
throw error | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters