-
-
Notifications
You must be signed in to change notification settings - Fork 720
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show deleted user sessions (#8749)
- Loading branch information
Showing
9 changed files
with
126 additions
and
19 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
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 |
---|---|---|
|
@@ -18,6 +18,7 @@ import PasswordMismatch from '../../../lib/error/password-mismatch'; | |
import type { EventService } from '../../../lib/services'; | ||
import { | ||
CREATE_ADDON, | ||
type IFlagResolver, | ||
type IUnleashStores, | ||
type IUserStore, | ||
SYSTEM_USER_AUDIT, | ||
|
@@ -45,6 +46,8 @@ let eventService: EventService; | |
let accessService: AccessService; | ||
let eventBus: EventEmitter; | ||
|
||
const allowedSessions = 2; | ||
|
||
beforeAll(async () => { | ||
db = await dbInit('user_service_serial', getLogger); | ||
stores = db.stores; | ||
|
@@ -63,14 +66,31 @@ beforeAll(async () => { | |
sessionService = new SessionService(stores, config); | ||
settingService = new SettingService(stores, config, eventService); | ||
|
||
userService = new UserService(stores, config, { | ||
accessService, | ||
resetTokenService, | ||
emailService, | ||
eventService, | ||
sessionService, | ||
settingService, | ||
}); | ||
const flagResolver = { | ||
isEnabled() { | ||
return true; | ||
}, | ||
getVariant() { | ||
return { | ||
feature_enabled: true, | ||
payload: { | ||
value: String(allowedSessions), | ||
}, | ||
}; | ||
}, | ||
} as unknown as IFlagResolver; | ||
userService = new UserService( | ||
stores, | ||
{ ...config, flagResolver }, | ||
{ | ||
accessService, | ||
resetTokenService, | ||
emailService, | ||
eventService, | ||
sessionService, | ||
settingService, | ||
}, | ||
); | ||
userStore = stores.userStore; | ||
const rootRoles = await accessService.getRootRoles(); | ||
adminRole = rootRoles.find((r) => r.name === RoleName.ADMIN)!; | ||
|
@@ -95,8 +115,9 @@ afterAll(async () => { | |
await db.destroy(); | ||
}); | ||
|
||
afterEach(async () => { | ||
beforeEach(async () => { | ||
await userStore.deleteAll(); | ||
await settingService.deleteAll(); | ||
}); | ||
|
||
test('should create initial admin user', async () => { | ||
|
@@ -361,6 +382,43 @@ test("deleting a user should delete the user's sessions", async () => { | |
expect(noSessions.length).toBe(0); | ||
}); | ||
|
||
test('user login should remove stale sessions', async () => { | ||
const email = '[email protected]'; | ||
const user = await userService.createUser( | ||
{ | ||
email, | ||
password: 'A very strange P4ssw0rd_', | ||
rootRole: adminRole.id, | ||
}, | ||
TEST_AUDIT_USER, | ||
); | ||
const userSession = (index: number) => ({ | ||
sid: `sid${index}`, | ||
sess: { | ||
cookie: { | ||
originalMaxAge: minutesToMilliseconds(48), | ||
expires: addDays(Date.now(), 1).toDateString(), | ||
secure: false, | ||
httpOnly: true, | ||
path: '/', | ||
}, | ||
user, | ||
}, | ||
}); | ||
|
||
for (let i = 0; i < allowedSessions; i++) { | ||
await sessionService.insertSession(userSession(i)); | ||
} | ||
|
||
const loggedInUser = await userService.loginUser( | ||
email, | ||
'A very strange P4ssw0rd_', | ||
); | ||
|
||
expect(loggedInUser.deletedSessions).toBe(1); | ||
expect(loggedInUser.activeSessions).toBe(allowedSessions); | ||
}); | ||
|
||
test('updating a user without an email should not strip the email', async () => { | ||
const email = '[email protected]'; | ||
const user = await userService.createUser( | ||
|