Skip to content

Commit

Permalink
add unit tests to the controller
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoE105 committed Dec 26, 2024
1 parent c45f47a commit 041fb91
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/cli/src/__tests__/project.test-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { nanoId, date, firstName, lastName, email } from 'minifaker';
import 'minifaker/locales/en';

import type { Project, ProjectType } from '@/databases/entities/project';

type RawProjectData = Pick<Project, 'name' | 'type' | 'createdAt' | 'updatedAt' | 'id'>;

const projectName = `${firstName()} ${lastName()} <${email}>`;

export const createRawProjectData = (payload: Partial<RawProjectData>): Project => {
return {
createdAt: date(),
updatedAt: date(),
id: nanoId.nanoid(),
name: projectName,
type: 'personal' as ProjectType,
...payload,
} as Project;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { mock } from 'jest-mock-extended';

import { createRawProjectData } from '@/__tests__/project.test-data';
import type { SharedCredentialsRepository } from '@/databases/repositories/shared-credentials.repository';
import type { EventService } from '@/events/event.service';
import type { AuthenticatedRequest } from '@/requests';

import { createdCredentialsWithScopes, createNewCredentialsPayload } from './credentials.test-data';
import { CredentialsController } from '../credentials.controller';
import type { CredentialsService } from '../credentials.service';

describe('CredentialsController', () => {
const eventService = mock<EventService>();
const credentialsService = mock<CredentialsService>();
const sharedCredentialsRepository = mock<SharedCredentialsRepository>();

const credentialsController = new CredentialsController(
mock(),
credentialsService,
mock(),
mock(),
mock(),
mock(),
mock(),
sharedCredentialsRepository,
mock(),
eventService,
);

let req: AuthenticatedRequest;
beforeAll(() => {
req = { user: { id: '123' } } as AuthenticatedRequest;
});

describe('createCredentials', () => {
it('it should create new credentials and emit "credentials-created"', async () => {
// Arrange

const newCredentialsPayload = createNewCredentialsPayload();

req.body = newCredentialsPayload;

const { data, ...payloadWithoutData } = newCredentialsPayload;

const createdCredentials = createdCredentialsWithScopes(payloadWithoutData);

const projectOwningCredentialData = createRawProjectData({
id: newCredentialsPayload.projectId,
});

credentialsService.createCredential.mockResolvedValue(createdCredentials);

sharedCredentialsRepository.findCredentialOwningProject.mockResolvedValue(
projectOwningCredentialData,
);

// Act

const newApiKey = await credentialsController.createCredentials(req);

// Assert

expect(credentialsService.createCredential).toHaveBeenCalledWith(
newCredentialsPayload,
req.user,
);
expect(sharedCredentialsRepository.findCredentialOwningProject).toHaveBeenCalledWith(
createdCredentials.id,
);
expect(eventService.emit).toHaveBeenCalledWith('credentials-created', {
user: expect.objectContaining({ id: req.user.id }),
credentialId: createdCredentials.id,
credentialType: createdCredentials.type,
projectId: projectOwningCredentialData.id,
projectType: projectOwningCredentialData.type,
publicApi: false,
});

expect(newApiKey).toEqual(createdCredentials);
});
});
});
62 changes: 62 additions & 0 deletions packages/cli/src/credentials/__tests__/credentials.test-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type { Scope } from '@n8n/permissions';
import { nanoId, date } from 'minifaker';
import { randomString } from 'n8n-workflow';

import type { CredentialRequest } from '@/requests';

type NewCredentialWithSCopes = {
scopes: Scope[];
name: string;
data: string;
type: string;
isManaged: boolean;
id: string;
createdAt: Date;
updatedAt: Date;
};

const name = 'new Credential';
const type = 'openAiApi';
const data = {
apiKey: 'apiKey',
url: 'url',
};
const projectId = nanoId.nanoid();

const credentialScopes: Scope[] = [
'credential:create',
'credential:delete',
'credential:list',
'credential:move',
'credential:read',
'credential:share',
'credential:update',
];

export const createNewCredentialsPayload = (
payload?: Partial<CredentialRequest.CredentialProperties>,
): CredentialRequest.CredentialProperties => {
return {
name,
type,
data,
projectId,
...payload,
};
};

export const createdCredentialsWithScopes = (
payload?: Partial<NewCredentialWithSCopes>,
): NewCredentialWithSCopes => {
return {
name,
type,
data: randomString(20),
id: nanoId.nanoid(),
createdAt: date(),
updatedAt: date(),
isManaged: false,
scopes: credentialScopes,
...payload,
};
};

0 comments on commit 041fb91

Please sign in to comment.