diff --git a/packages/cli/src/controllers/__tests__/me.controller.test.ts b/packages/cli/src/controllers/__tests__/me.controller.test.ts index 6e187f9cd5..4a313552af 100644 --- a/packages/cli/src/controllers/__tests__/me.controller.test.ts +++ b/packages/cli/src/controllers/__tests__/me.controller.test.ts @@ -46,6 +46,7 @@ describe('MeController', () => { it('should update the user in the DB, and issue a new cookie', async () => { const user = mock({ id: '123', + email: 'valid@email.com', password: 'password', authIdentities: [], role: 'global:owner', @@ -53,6 +54,7 @@ describe('MeController', () => { const reqBody = { email: 'valid@email.com', firstName: 'John', lastName: 'Potato' }; const req = mock({ user, body: reqBody, browserId }); const res = mock(); + userRepository.findOneByOrFail.mockResolvedValue(user); userRepository.findOneOrFail.mockResolvedValue(user); jest.spyOn(jwt, 'sign').mockImplementation(() => 'signed-token'); userService.toPublic.mockResolvedValue({} as unknown as PublicUser); @@ -66,7 +68,10 @@ describe('MeController', () => { ]); expect(userService.update).toHaveBeenCalled(); - + expect(eventService.emit).toHaveBeenCalledWith('user-updated', { + user, + fieldsChanged: ['firstName', 'lastName'], // email did not change + }); expect(res.cookie).toHaveBeenCalledWith( AUTH_COOKIE_NAME, 'signed-token', diff --git a/packages/cli/src/controllers/__tests__/users.controller.test.ts b/packages/cli/src/controllers/__tests__/users.controller.test.ts new file mode 100644 index 0000000000..38a4399cab --- /dev/null +++ b/packages/cli/src/controllers/__tests__/users.controller.test.ts @@ -0,0 +1,52 @@ +import { mock } from 'jest-mock-extended'; +import { UsersController } from '../users.controller'; +import type { UserRequest } from '@/requests'; +import type { EventService } from '@/events/event.service'; +import type { User } from '@/databases/entities/User'; +import type { UserRepository } from '@/databases/repositories/user.repository'; +import type { ProjectService } from '@/services/project.service'; + +describe('UsersController', () => { + const eventService = mock(); + const userRepository = mock(); + const projectService = mock(); + const controller = new UsersController( + mock(), + mock(), + mock(), + mock(), + userRepository, + mock(), + mock(), + mock(), + mock(), + mock(), + projectService, + eventService, + ); + + beforeEach(() => { + jest.restoreAllMocks(); + }); + + describe('changeGlobalRole', () => { + it('should emit event user-changed-role', async () => { + const request = mock({ + user: { id: '123' }, + params: { id: '456' }, + body: { newRoleName: 'global:member' }, + }); + userRepository.findOne.mockResolvedValue(mock({ id: '456' })); + projectService.getUserOwnedOrAdminProjects.mockResolvedValue([]); + + await controller.changeGlobalRole(request); + + expect(eventService.emit).toHaveBeenCalledWith('user-changed-role', { + userId: '123', + targetUserId: '456', + targetUserNewRole: 'global:member', + publicApi: false, + }); + }); + }); +}); diff --git a/packages/cli/src/controllers/me.controller.ts b/packages/cli/src/controllers/me.controller.ts index 2073f737c4..2a0526b3cc 100644 --- a/packages/cli/src/controllers/me.controller.ts +++ b/packages/cli/src/controllers/me.controller.ts @@ -89,6 +89,7 @@ export class MeController { await this.externalHooks.run('user.profile.beforeUpdate', [userId, currentEmail, payload]); + const preUpdateUser = await this.userRepository.findOneByOrFail({ id: userId }); await this.userService.update(userId, payload); const user = await this.userRepository.findOneOrFail({ where: { id: userId }, @@ -98,7 +99,10 @@ export class MeController { this.authService.issueCookie(res, user, req.browserId); - const fieldsChanged = Object.keys(payload); + const fieldsChanged = (Object.keys(payload) as Array).filter( + (key) => payload[key] !== preUpdateUser[key], + ); + this.eventService.emit('user-updated', { user, fieldsChanged }); const publicUser = await this.userService.toPublic(user); diff --git a/packages/cli/src/controllers/users.controller.ts b/packages/cli/src/controllers/users.controller.ts index c834d2cc23..f3055ee4dc 100644 --- a/packages/cli/src/controllers/users.controller.ts +++ b/packages/cli/src/controllers/users.controller.ts @@ -292,7 +292,7 @@ export class UsersController { this.eventService.emit('user-changed-role', { userId: req.user.id, targetUserId: targetUser.id, - targetUserNewRole: ['global', payload.newRoleName].join(' '), + targetUserNewRole: payload.newRoleName, publicApi: false, });