fix(core): Fix user telemetry bugs (#10293)

This commit is contained in:
Iván Ovejero 2024-08-05 13:24:26 +02:00 committed by GitHub
parent 7fb3f62a36
commit 42a0b594d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 64 additions and 3 deletions

View file

@ -46,6 +46,7 @@ describe('MeController', () => {
it('should update the user in the DB, and issue a new cookie', async () => { it('should update the user in the DB, and issue a new cookie', async () => {
const user = mock<User>({ const user = mock<User>({
id: '123', id: '123',
email: 'valid@email.com',
password: 'password', password: 'password',
authIdentities: [], authIdentities: [],
role: 'global:owner', role: 'global:owner',
@ -53,6 +54,7 @@ describe('MeController', () => {
const reqBody = { email: 'valid@email.com', firstName: 'John', lastName: 'Potato' }; const reqBody = { email: 'valid@email.com', firstName: 'John', lastName: 'Potato' };
const req = mock<MeRequest.UserUpdate>({ user, body: reqBody, browserId }); const req = mock<MeRequest.UserUpdate>({ user, body: reqBody, browserId });
const res = mock<Response>(); const res = mock<Response>();
userRepository.findOneByOrFail.mockResolvedValue(user);
userRepository.findOneOrFail.mockResolvedValue(user); userRepository.findOneOrFail.mockResolvedValue(user);
jest.spyOn(jwt, 'sign').mockImplementation(() => 'signed-token'); jest.spyOn(jwt, 'sign').mockImplementation(() => 'signed-token');
userService.toPublic.mockResolvedValue({} as unknown as PublicUser); userService.toPublic.mockResolvedValue({} as unknown as PublicUser);
@ -66,7 +68,10 @@ describe('MeController', () => {
]); ]);
expect(userService.update).toHaveBeenCalled(); expect(userService.update).toHaveBeenCalled();
expect(eventService.emit).toHaveBeenCalledWith('user-updated', {
user,
fieldsChanged: ['firstName', 'lastName'], // email did not change
});
expect(res.cookie).toHaveBeenCalledWith( expect(res.cookie).toHaveBeenCalledWith(
AUTH_COOKIE_NAME, AUTH_COOKIE_NAME,
'signed-token', 'signed-token',

View file

@ -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,
});
});
});
});

View file

@ -89,6 +89,7 @@ export class MeController {
await this.externalHooks.run('user.profile.beforeUpdate', [userId, currentEmail, payload]); await this.externalHooks.run('user.profile.beforeUpdate', [userId, currentEmail, payload]);
const preUpdateUser = await this.userRepository.findOneByOrFail({ id: userId });
await this.userService.update(userId, payload); await this.userService.update(userId, payload);
const user = await this.userRepository.findOneOrFail({ const user = await this.userRepository.findOneOrFail({
where: { id: userId }, where: { id: userId },
@ -98,7 +99,10 @@ export class MeController {
this.authService.issueCookie(res, user, req.browserId); 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 }); this.eventService.emit('user-updated', { user, fieldsChanged });
const publicUser = await this.userService.toPublic(user); const publicUser = await this.userService.toPublic(user);

View file

@ -292,7 +292,7 @@ export class UsersController {
this.eventService.emit('user-changed-role', { this.eventService.emit('user-changed-role', {
userId: req.user.id, userId: req.user.id,
targetUserId: targetUser.id, targetUserId: targetUser.id,
targetUserNewRole: ['global', payload.newRoleName].join(' '), targetUserNewRole: payload.newRoleName,
publicApi: false, publicApi: false,
}); });