File size: 3,630 Bytes
aec3094 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | import type { PullWorkFolderRequestDto, PushWorkFolderRequestDto } from '@n8n/api-types';
import type { Response } from 'express';
import { mock } from 'jest-mock-extended';
import type { EventService } from '@/events/event.service';
import type { AuthenticatedRequest } from '@/requests';
import type { SourceControlPreferencesService } from '../source-control-preferences.service.ee';
import { SourceControlController } from '../source-control.controller.ee';
import type { SourceControlService } from '../source-control.service.ee';
import type { SourceControlRequest } from '../types/requests';
import type { SourceControlGetStatus } from '../types/source-control-get-status';
describe('SourceControlController', () => {
let controller: SourceControlController;
let sourceControlService: SourceControlService;
let sourceControlPreferencesService: SourceControlPreferencesService;
let eventService: EventService;
beforeEach(() => {
sourceControlService = {
pushWorkfolder: jest.fn().mockResolvedValue({ statusCode: 200 }),
pullWorkfolder: jest.fn().mockResolvedValue({ statusCode: 200 }),
getStatus: jest.fn().mockResolvedValue([]),
setGitUserDetails: jest.fn(),
} as unknown as SourceControlService;
sourceControlPreferencesService = mock<SourceControlPreferencesService>();
eventService = mock<EventService>();
controller = new SourceControlController(
sourceControlService,
sourceControlPreferencesService,
mock(),
eventService,
);
});
describe('pushWorkfolder', () => {
it('should push workfolder with expected parameters', async () => {
const req = mock<AuthenticatedRequest>({
user: { firstName: 'John', lastName: 'Doe', email: 'john.doe@example.com' },
});
const res = mock<Response>();
const payload = { force: true } as PushWorkFolderRequestDto;
await controller.pushWorkfolder(req, res, payload);
expect(sourceControlService.setGitUserDetails).toHaveBeenCalledWith(
'John Doe',
'john.doe@example.com',
);
expect(sourceControlService.pushWorkfolder).toHaveBeenCalledWith(req.user, payload);
});
});
describe('pullWorkfolder', () => {
it('should pull workfolder with expected parameters', async () => {
const req = mock<AuthenticatedRequest>({
user: { firstName: 'John', lastName: 'Doe', email: 'john.doe@example.com' },
});
const res = mock<Response>();
const payload = { force: true } as PullWorkFolderRequestDto;
await controller.pullWorkfolder(req, res, payload);
expect(sourceControlService.pullWorkfolder).toHaveBeenCalledWith(req.user, payload);
});
});
describe('getStatus', () => {
it('should call getStatus with expected parameters', async () => {
const user = { firstName: 'John', lastName: 'Doe', email: 'john.doe@example.com' };
const query = {
direction: 'pull',
preferLocalVersion: true,
verbose: false,
} as SourceControlGetStatus;
const req = mock<SourceControlRequest.GetStatus>({
query,
user,
});
await controller.getStatus(req);
expect(sourceControlService.getStatus).toHaveBeenCalledWith(user, query);
});
});
describe('status', () => {
it('should call getStatus with expected parameters', async () => {
const user = { firstName: 'John', lastName: 'Doe', email: 'john.doe@example.com' };
const query = {
direction: 'pull',
preferLocalVersion: true,
verbose: false,
} as SourceControlGetStatus;
const req = mock<SourceControlRequest.GetStatus>({
query,
user,
});
await controller.status(req);
expect(sourceControlService.getStatus).toHaveBeenCalledWith(user, query);
});
});
});
|