-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): Fix user telemetry bugs (#10293)
- Loading branch information
Showing
4 changed files
with
64 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,13 +46,15 @@ describe('MeController', () => { | |
it('should update the user in the DB, and issue a new cookie', async () => { | ||
const user = mock<User>({ | ||
id: '123', | ||
email: '[email protected]', | ||
password: 'password', | ||
authIdentities: [], | ||
role: 'global:owner', | ||
}); | ||
const reqBody = { email: '[email protected]', 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', | ||
|
52 changes: 52 additions & 0 deletions
52
packages/cli/src/controllers/__tests__/users.controller.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters