mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-24 04:04:06 -08:00
fix(core): Fix user telemetry bugs (#10293)
This commit is contained in:
parent
7fb3f62a36
commit
42a0b594d6
|
@ -46,6 +46,7 @@ describe('MeController', () => {
|
|||
it('should update the user in the DB, and issue a new cookie', async () => {
|
||||
const user = mock<User>({
|
||||
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<MeRequest.UserUpdate>({ user, body: reqBody, browserId });
|
||||
const res = mock<Response>();
|
||||
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',
|
||||
|
|
|
@ -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<EventService>();
|
||||
const userRepository = mock<UserRepository>();
|
||||
const projectService = mock<ProjectService>();
|
||||
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<UserRequest.ChangeRole>({
|
||||
user: { id: '123' },
|
||||
params: { id: '456' },
|
||||
body: { newRoleName: 'global:member' },
|
||||
});
|
||||
userRepository.findOne.mockResolvedValue(mock<User>({ 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,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
|
@ -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<keyof UserUpdatePayload>).filter(
|
||||
(key) => payload[key] !== preUpdateUser[key],
|
||||
);
|
||||
|
||||
this.eventService.emit('user-updated', { user, fieldsChanged });
|
||||
|
||||
const publicUser = await this.userService.toPublic(user);
|
||||
|
|
|
@ -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,
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue