feat(server): remove inactive sessions (#9121)

* feat(server): remove inactive sessions

* add rudimentary unit test

---------

Co-authored-by: Daniel Dietzler <mail@ddietzler.dev>
This commit is contained in:
Jason Rasmussen 2024-04-27 16:45:16 -04:00 committed by GitHub
parent 953896a35a
commit 034c928d9e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 82 additions and 3 deletions

View file

@ -1,3 +1,5 @@
import { UserEntity } from 'src/entities/user.entity';
import { JobStatus } from 'src/interfaces/job.interface';
import { ILoggerRepository } from 'src/interfaces/logger.interface';
import { ISessionRepository } from 'src/interfaces/session.interface';
import { SessionService } from 'src/services/session.service';
@ -26,6 +28,32 @@ describe('SessionService', () => {
expect(sut).toBeDefined();
});
describe('handleCleanup', () => {
it('should return skipped if nothing is to be deleted', async () => {
sessionMock.search.mockResolvedValue([]);
await expect(sut.handleCleanup()).resolves.toEqual(JobStatus.SKIPPED);
expect(sessionMock.search).toHaveBeenCalled();
});
it('should delete sessions', async () => {
sessionMock.search.mockResolvedValue([
{
createdAt: new Date('1970-01-01T00:00:00.00Z'),
updatedAt: new Date('1970-01-02T00:00:00.00Z'),
deviceOS: '',
deviceType: '',
id: '123',
token: '420',
user: {} as UserEntity,
userId: '42',
},
]);
await expect(sut.handleCleanup()).resolves.toEqual(JobStatus.SUCCESS);
expect(sessionMock.delete).toHaveBeenCalledWith('123');
});
});
describe('getAll', () => {
it('should get the devices', async () => {
sessionMock.getByUserId.mockResolvedValue([sessionStub.valid, sessionStub.inactive]);