Merge pull request #2044 from automatisch/rest-update-user
feat: Implement update user rest API endpoint
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
import { renderObject } from '../../../../helpers/renderer.js';
|
||||
|
||||
export default async (request, response) => {
|
||||
const user = await request.currentUser
|
||||
.$query()
|
||||
.patchAndFetch(userParams(request));
|
||||
|
||||
renderObject(response, user);
|
||||
};
|
||||
|
||||
const userParams = (request) => {
|
||||
const { email, fullName } = request.body;
|
||||
return { email, fullName };
|
||||
};
|
@@ -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'],
|
||||
});
|
||||
});
|
||||
});
|
@@ -3,6 +3,7 @@ import { authenticateUser } from '../../../helpers/authentication.js';
|
||||
import { authorizeUser } from '../../../helpers/authorization.js';
|
||||
import checkIsCloud from '../../../helpers/check-is-cloud.js';
|
||||
import getCurrentUserAction from '../../../controllers/api/v1/users/get-current-user.js';
|
||||
import updateCurrentUserAction from '../../../controllers/api/v1/users/update-current-user.js';
|
||||
import getUserTrialAction from '../../../controllers/api/v1/users/get-user-trial.ee.js';
|
||||
import getAppsAction from '../../../controllers/api/v1/users/get-apps.js';
|
||||
import getInvoicesAction from '../../../controllers/api/v1/users/get-invoices.ee.js';
|
||||
@@ -15,6 +16,7 @@ import resetPasswordAction from '../../../controllers/api/v1/users/reset-passwor
|
||||
const router = Router();
|
||||
|
||||
router.get('/me', authenticateUser, getCurrentUserAction);
|
||||
router.patch('/:userId', authenticateUser, updateCurrentUserAction);
|
||||
router.get('/:userId/apps', authenticateUser, authorizeUser, getAppsAction);
|
||||
router.get('/invoices', authenticateUser, checkIsCloud, getInvoicesAction);
|
||||
|
||||
|
@@ -0,0 +1,21 @@
|
||||
const updateCurrentUserMock = (currentUser) => {
|
||||
return {
|
||||
data: {
|
||||
createdAt: currentUser.createdAt.getTime(),
|
||||
email: currentUser.email,
|
||||
fullName: currentUser.fullName,
|
||||
id: currentUser.id,
|
||||
status: currentUser.status,
|
||||
updatedAt: currentUser.updatedAt.getTime(),
|
||||
},
|
||||
meta: {
|
||||
count: 1,
|
||||
currentPage: null,
|
||||
isArray: false,
|
||||
totalPages: null,
|
||||
type: 'User',
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export default updateCurrentUserMock;
|
Reference in New Issue
Block a user