Files
immich/server/src/services/session.service.spec.ts

71 lines
2.2 KiB
TypeScript
Raw Normal View History

2025-02-11 17:15:56 -05:00
import { JobStatus } from 'src/enum';
import { SessionService } from 'src/services/session.service';
import { authStub } from 'test/fixtures/auth.stub';
2025-04-09 10:24:38 -04:00
import { factory } from 'test/small.factory';
2025-02-10 18:47:42 -05:00
import { newTestService, ServiceMocks } from 'test/utils';
describe('SessionService', () => {
let sut: SessionService;
2025-02-10 18:47:42 -05:00
let mocks: ServiceMocks;
beforeEach(() => {
2025-02-10 18:47:42 -05:00
({ sut, mocks } = newTestService(SessionService));
});
it('should be defined', () => {
expect(sut).toBeDefined();
});
describe('handleCleanup', () => {
it('should clean sessions', async () => {
mocks.session.cleanup.mockResolvedValue([]);
2025-07-15 14:50:13 -04:00
await expect(sut.handleCleanup()).resolves.toEqual(JobStatus.Success);
});
});
describe('getAll', () => {
it('should get the devices', async () => {
2025-04-09 10:24:38 -04:00
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 }),
]);
2025-04-09 10:24:38 -04:00
expect(mocks.session.getByUserId).toHaveBeenCalledWith(auth.user.id);
});
});
describe('logoutDevices', () => {
it('should logout all devices', async () => {
2025-04-09 10:24:38 -04:00
const currentSession = factory.session();
const auth = factory.auth({ session: currentSession });
mocks.session.invalidate.mockResolvedValue();
2025-04-09 10:24:38 -04:00
await sut.deleteAll(auth);
expect(mocks.session.invalidate).toHaveBeenCalledWith({ userId: auth.user.id, excludeId: currentSession.id });
});
});
describe('logoutDevice', () => {
it('should logout the device', async () => {
2025-02-10 18:47:42 -05:00
mocks.access.authDevice.checkOwnerAccess.mockResolvedValue(new Set(['token-1']));
2025-03-10 16:52:44 -04:00
mocks.session.delete.mockResolvedValue();
await sut.delete(authStub.user1, 'token-1');
2025-02-10 18:47:42 -05:00
expect(mocks.access.authDevice.checkOwnerAccess).toHaveBeenCalledWith(
authStub.user1.user.id,
new Set(['token-1']),
);
expect(mocks.session.delete).toHaveBeenCalledWith('token-1');
});
});
});