Files
immich/server/src/services/session.service.spec.ts
Jorge Montejo 382481735a feat: logout sessions on password change (#23188)
* log out ohter sessions on password change

* translations

* update and add tests

* rename event to UserLogoutOtherSessions

* fix typo

* requested changes

* fix tests

* fix medium:test

* use ValidateBoolean

* fix format

* dont delete current session id

* Update server/src/dtos/auth.dto.ts

Co-authored-by: Daniel Dietzler <36593685+danieldietzler@users.noreply.github.com>

* rename event and invalidateOtherSessions

* chore: cleanup

---------

Co-authored-by: Daniel Dietzler <36593685+danieldietzler@users.noreply.github.com>
Co-authored-by: Jason Rasmussen <jason@rasm.me>
2025-10-27 13:16:10 +00:00

71 lines
2.2 KiB
TypeScript

import { JobStatus } from 'src/enum';
import { SessionService } from 'src/services/session.service';
import { authStub } from 'test/fixtures/auth.stub';
import { factory } from 'test/small.factory';
import { newTestService, ServiceMocks } from 'test/utils';
describe('SessionService', () => {
let sut: SessionService;
let mocks: ServiceMocks;
beforeEach(() => {
({ sut, mocks } = newTestService(SessionService));
});
it('should be defined', () => {
expect(sut).toBeDefined();
});
describe('handleCleanup', () => {
it('should clean sessions', async () => {
mocks.session.cleanup.mockResolvedValue([]);
await expect(sut.handleCleanup()).resolves.toEqual(JobStatus.Success);
});
});
describe('getAll', () => {
it('should get the devices', async () => {
const currentSession = factory.session();
const otherSession = factory.session();
const auth = factory.auth({ session: currentSession });
mocks.session.getByUserId.mockResolvedValue([currentSession, otherSession]);
await expect(sut.getAll(auth)).resolves.toEqual([
expect.objectContaining({ current: true, id: currentSession.id }),
expect.objectContaining({ current: false, id: otherSession.id }),
]);
expect(mocks.session.getByUserId).toHaveBeenCalledWith(auth.user.id);
});
});
describe('logoutDevices', () => {
it('should logout all devices', async () => {
const currentSession = factory.session();
const auth = factory.auth({ session: currentSession });
mocks.session.invalidate.mockResolvedValue();
await sut.deleteAll(auth);
expect(mocks.session.invalidate).toHaveBeenCalledWith({ userId: auth.user.id, excludeId: currentSession.id });
});
});
describe('logoutDevice', () => {
it('should logout the device', async () => {
mocks.access.authDevice.checkOwnerAccess.mockResolvedValue(new Set(['token-1']));
mocks.session.delete.mockResolvedValue();
await sut.delete(authStub.user1, 'token-1');
expect(mocks.access.authDevice.checkOwnerAccess).toHaveBeenCalledWith(
authStub.user1.user.id,
new Set(['token-1']),
);
expect(mocks.session.delete).toHaveBeenCalledWith('token-1');
});
});
});