refactor: test mocks (#16008)
This commit is contained in:
@@ -1,20 +1,15 @@
|
||||
import { JobStatus } from 'src/interfaces/job.interface';
|
||||
import { SessionService } from 'src/services/session.service';
|
||||
import { ISessionRepository } from 'src/types';
|
||||
import { authStub } from 'test/fixtures/auth.stub';
|
||||
import { sessionStub } from 'test/fixtures/session.stub';
|
||||
import { IAccessRepositoryMock } from 'test/repositories/access.repository.mock';
|
||||
import { newTestService } from 'test/utils';
|
||||
import { Mocked } from 'vitest';
|
||||
import { newTestService, ServiceMocks } from 'test/utils';
|
||||
|
||||
describe('SessionService', () => {
|
||||
let sut: SessionService;
|
||||
|
||||
let accessMock: Mocked<IAccessRepositoryMock>;
|
||||
let sessionMock: Mocked<ISessionRepository>;
|
||||
let mocks: ServiceMocks;
|
||||
|
||||
beforeEach(() => {
|
||||
({ sut, accessMock, sessionMock } = newTestService(SessionService));
|
||||
({ sut, mocks } = newTestService(SessionService));
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
@@ -23,13 +18,13 @@ describe('SessionService', () => {
|
||||
|
||||
describe('handleCleanup', () => {
|
||||
it('should return skipped if nothing is to be deleted', async () => {
|
||||
sessionMock.search.mockResolvedValue([]);
|
||||
mocks.session.search.mockResolvedValue([]);
|
||||
await expect(sut.handleCleanup()).resolves.toEqual(JobStatus.SKIPPED);
|
||||
expect(sessionMock.search).toHaveBeenCalled();
|
||||
expect(mocks.session.search).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should delete sessions', async () => {
|
||||
sessionMock.search.mockResolvedValue([
|
||||
mocks.session.search.mockResolvedValue([
|
||||
{
|
||||
createdAt: new Date('1970-01-01T00:00:00.00Z'),
|
||||
updatedAt: new Date('1970-01-02T00:00:00.00Z'),
|
||||
@@ -42,13 +37,13 @@ describe('SessionService', () => {
|
||||
]);
|
||||
|
||||
await expect(sut.handleCleanup()).resolves.toEqual(JobStatus.SUCCESS);
|
||||
expect(sessionMock.delete).toHaveBeenCalledWith('123');
|
||||
expect(mocks.session.delete).toHaveBeenCalledWith('123');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getAll', () => {
|
||||
it('should get the devices', async () => {
|
||||
sessionMock.getByUserId.mockResolvedValue([sessionStub.valid as any, sessionStub.inactive]);
|
||||
mocks.session.getByUserId.mockResolvedValue([sessionStub.valid as any, sessionStub.inactive]);
|
||||
await expect(sut.getAll(authStub.user1)).resolves.toEqual([
|
||||
{
|
||||
createdAt: '2021-01-01T00:00:00.000Z',
|
||||
@@ -68,30 +63,33 @@ describe('SessionService', () => {
|
||||
},
|
||||
]);
|
||||
|
||||
expect(sessionMock.getByUserId).toHaveBeenCalledWith(authStub.user1.user.id);
|
||||
expect(mocks.session.getByUserId).toHaveBeenCalledWith(authStub.user1.user.id);
|
||||
});
|
||||
});
|
||||
|
||||
describe('logoutDevices', () => {
|
||||
it('should logout all devices', async () => {
|
||||
sessionMock.getByUserId.mockResolvedValue([sessionStub.inactive, sessionStub.valid] as any[]);
|
||||
mocks.session.getByUserId.mockResolvedValue([sessionStub.inactive, sessionStub.valid] as any[]);
|
||||
|
||||
await sut.deleteAll(authStub.user1);
|
||||
|
||||
expect(sessionMock.getByUserId).toHaveBeenCalledWith(authStub.user1.user.id);
|
||||
expect(sessionMock.delete).toHaveBeenCalledWith('not_active');
|
||||
expect(sessionMock.delete).not.toHaveBeenCalledWith('token-id');
|
||||
expect(mocks.session.getByUserId).toHaveBeenCalledWith(authStub.user1.user.id);
|
||||
expect(mocks.session.delete).toHaveBeenCalledWith('not_active');
|
||||
expect(mocks.session.delete).not.toHaveBeenCalledWith('token-id');
|
||||
});
|
||||
});
|
||||
|
||||
describe('logoutDevice', () => {
|
||||
it('should logout the device', async () => {
|
||||
accessMock.authDevice.checkOwnerAccess.mockResolvedValue(new Set(['token-1']));
|
||||
mocks.access.authDevice.checkOwnerAccess.mockResolvedValue(new Set(['token-1']));
|
||||
|
||||
await sut.delete(authStub.user1, 'token-1');
|
||||
|
||||
expect(accessMock.authDevice.checkOwnerAccess).toHaveBeenCalledWith(authStub.user1.user.id, new Set(['token-1']));
|
||||
expect(sessionMock.delete).toHaveBeenCalledWith('token-1');
|
||||
expect(mocks.access.authDevice.checkOwnerAccess).toHaveBeenCalledWith(
|
||||
authStub.user1.user.id,
|
||||
new Set(['token-1']),
|
||||
);
|
||||
expect(mocks.session.delete).toHaveBeenCalledWith('token-1');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user