-
-
Notifications
You must be signed in to change notification settings - Fork 728
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: user loging event emitting with login order (#8021)
- Loading branch information
Showing
7 changed files
with
79 additions
and
14 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,8 @@ import { | |
import { CUSTOM_ROOT_ROLE_TYPE } from '../../../lib/util'; | ||
import { PasswordPreviouslyUsedError } from '../../../lib/error/password-previously-used'; | ||
import { createEventsService } from '../../../lib/features'; | ||
import type EventEmitter from 'events'; | ||
import { USER_LOGIN } from '../../../lib/metric-events'; | ||
|
||
let db: ITestDb; | ||
let stores: IUnleashStores; | ||
|
@@ -41,11 +43,13 @@ let sessionService: SessionService; | |
let settingService: SettingService; | ||
let eventService: EventService; | ||
let accessService: AccessService; | ||
let eventBus: EventEmitter; | ||
|
||
beforeAll(async () => { | ||
db = await dbInit('user_service_serial', getLogger); | ||
stores = db.stores; | ||
const config = createTestConfig(); | ||
eventBus = config.eventBus; | ||
eventService = createEventsService(db.rawDatabase, config); | ||
const groupService = new GroupService(stores, config, eventService); | ||
accessService = new AccessService( | ||
|
@@ -138,6 +142,10 @@ test('should not be allowed to create existing user', async () => { | |
}); | ||
|
||
test('should create user with password', async () => { | ||
const recordedEvents: Array<{ loginOrder: number }> = []; | ||
eventBus.on(USER_LOGIN, (data) => { | ||
recordedEvents.push(data); | ||
}); | ||
await userService.createUser( | ||
{ | ||
username: 'test', | ||
|
@@ -151,6 +159,7 @@ test('should create user with password', async () => { | |
'A very strange P4ssw0rd_', | ||
); | ||
expect(user.username).toBe('test'); | ||
expect(recordedEvents).toEqual([{ loginOrder: 0 }]); | ||
}); | ||
|
||
test('should create user with rootRole in audit-log', async () => { | ||
|
@@ -377,6 +386,10 @@ test('updating a user without an email should not strip the email', async () => | |
}); | ||
|
||
test('should login and create user via SSO', async () => { | ||
const recordedEvents: Array<{ loginOrder: number }> = []; | ||
eventBus.on(USER_LOGIN, (data) => { | ||
recordedEvents.push(data); | ||
}); | ||
const email = '[email protected]'; | ||
const user = await userService.loginUserSSO({ | ||
email, | ||
|
@@ -390,6 +403,7 @@ test('should login and create user via SSO', async () => { | |
expect(user.name).toBe('some'); | ||
expect(userWithRole.name).toBe('some'); | ||
expect(userWithRole.rootRole).toBe(viewerRole.id); | ||
expect(recordedEvents).toEqual([{ loginOrder: 0 }]); | ||
}); | ||
|
||
test('should throw if rootRole is wrong via SSO', async () => { | ||
|
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 |
---|---|---|
|
@@ -7,14 +7,20 @@ let stores: IUnleashStores; | |
let db: ITestDb; | ||
|
||
beforeAll(async () => { | ||
db = await dbInit('user_store_serial', getLogger); | ||
db = await dbInit('user_store_serial', getLogger, { | ||
experimental: { flags: { onboardingMetrics: true } }, | ||
}); | ||
stores = db.stores; | ||
}); | ||
|
||
afterAll(async () => { | ||
await db.destroy(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await stores.userStore.deleteAll(); | ||
}); | ||
|
||
test('should have no users', async () => { | ||
const users = await stores.userStore.getAll(); | ||
expect(users).toEqual([]); | ||
|
@@ -108,6 +114,19 @@ test('should reset user after successful login', async () => { | |
expect(storedUser.seenAt! >= user.seenAt!).toBe(true); | ||
}); | ||
|
||
test('should return first login order for every new user', async () => { | ||
const store = stores.userStore; | ||
const user1 = await store.insert({ email: '[email protected]' }); | ||
const user2 = await store.insert({ email: '[email protected]' }); | ||
const user3 = await store.insert({ email: '[email protected]' }); | ||
|
||
expect(await store.successfullyLogin(user1)).toBe(0); | ||
expect(await store.successfullyLogin(user1)).toBe(0); | ||
expect(await store.successfullyLogin(user2)).toBe(1); | ||
expect(await store.successfullyLogin(user1)).toBe(0); | ||
expect(await store.successfullyLogin(user3)).toBe(2); | ||
}); | ||
|
||
test('should only update specified fields on user', async () => { | ||
const store = stores.userStore; | ||
const email = '[email protected]'; | ||
|
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