Skip to content

Commit

Permalink
Add tests to createCredential method in CredentialService
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoE105 committed Dec 26, 2024
1 parent 041fb91 commit 0325106
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
69 changes: 69 additions & 0 deletions packages/cli/src/credentials/__tests__/credentials.service.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { mock } from 'jest-mock-extended';
import { nanoId, date } from 'minifaker';
import { CREDENTIAL_EMPTY_VALUE, type ICredentialType } from 'n8n-workflow';

import { CREDENTIAL_BLANKING_VALUE } from '@/constants';
import type { CredentialTypes } from '@/credential-types';
import { CredentialsService } from '@/credentials/credentials.service';
import type { CredentialsEntity } from '@/databases/entities/credentials-entity';
import type { AuthenticatedRequest } from '@/requests';

import { createNewCredentialsPayload, credentialScopes } from './credentials.test-data';

let req = { user: { id: '123' } } as AuthenticatedRequest;

describe('CredentialsService', () => {
const credType = mock<ICredentialType>({
Expand Down Expand Up @@ -68,4 +74,67 @@ describe('CredentialsService', () => {
});
});
});

describe('createCredential', () => {
it('it should create new credentials and return with scopes', async () => {
// Arrange

const encryptedData = 'encryptedData';

const newCredentialPayloadData = createNewCredentialsPayload();

const newCredential = mock<CredentialsEntity>({
name: newCredentialPayloadData.name,
data: JSON.stringify(newCredentialPayloadData.data),
type: newCredentialPayloadData.type,
});

const encryptedDataResponse = {
name: newCredentialPayloadData.name,
type: newCredentialPayloadData.type,
updatedAt: date(),
data: encryptedData,
};

const saveCredentialsResponse = {
id: nanoId.nanoid(),
name: newCredentialPayloadData.name,
type: newCredentialPayloadData.type,
updatedAt: encryptedDataResponse.updatedAt,
createdAt: date(),
data: encryptedDataResponse.data,
isManaged: false,
shared: undefined,
};

service.prepareCreateData = jest.fn().mockReturnValue(newCredential);
service.createEncryptedData = jest.fn().mockImplementation(() => encryptedDataResponse);
service.save = jest.fn().mockResolvedValue(saveCredentialsResponse);
service.getCredentialScopes = jest.fn().mockReturnValue(credentialScopes);

// Act

const createdCredential = await service.createCredential(newCredentialPayloadData, req.user);

// Assert

expect(service.prepareCreateData).toHaveBeenCalledWith(newCredentialPayloadData);
expect(service.createEncryptedData).toHaveBeenCalledWith(null, newCredential);
expect(service.save).toHaveBeenCalledWith(
newCredential,
encryptedDataResponse,
req.user,
newCredentialPayloadData.projectId,
);
expect(service.getCredentialScopes).toHaveBeenCalledWith(
req.user,
saveCredentialsResponse.id,
);

expect(createdCredential).toEqual({
...saveCredentialsResponse,
scopes: credentialScopes,
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const data = {
};
const projectId = nanoId.nanoid();

const credentialScopes: Scope[] = [
export const credentialScopes: Scope[] = [
'credential:create',
'credential:delete',
'credential:list',
Expand Down

0 comments on commit 0325106

Please sign in to comment.