feat: Implement update user rest API endpoint

This commit is contained in:
Faruk AYDIN
2024-08-28 17:07:13 +03:00
parent 4eeda10f3f
commit 4054f551d4
4 changed files with 93 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
import { describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import app from '../../../../app.js';
import createAuthTokenByUserId from '../../../../helpers/create-auth-token-by-user-id.js';
import { createUser } from '../../../../../test/factories/user.js';
import updateCurrentUserMock from '../../../../../test/mocks/rest/api/v1/users/update-current-user.js';
describe('PATCH /api/v1/users/:userId', () => {
let currentUser, token;
beforeEach(async () => {
currentUser = await createUser();
token = await createAuthTokenByUserId(currentUser.id);
});
it('should return updated user with valid data', async () => {
const userData = {
email: 'updated@sample.com',
fullName: 'Updated Full Name',
};
const response = await request(app)
.patch(`/api/v1/users/${currentUser.id}`)
.set('Authorization', token)
.send(userData)
.expect(200);
const refetchedCurrentUser = await currentUser.$query();
const expectedPayload = updateCurrentUserMock({
...refetchedCurrentUser,
...userData,
});
expect(response.body).toMatchObject(expectedPayload);
});
it('should return HTTP 422 with invalid user data', async () => {
const userData = {
email: null,
fullName: null,
};
const response = await request(app)
.patch(`/api/v1/users/${currentUser.id}`)
.set('Authorization', token)
.send(userData)
.expect(422);
expect(response.body.meta.type).toEqual('ModelValidation');
expect(response.body.errors).toMatchObject({
email: ['must be string'],
fullName: ['must be string'],
});
});
});