Skip to content

Commit

Permalink
add tests to increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
despairblue committed Dec 30, 2024
1 parent dccbd33 commit 8a1b523
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { mock } from 'jest-mock-extended';
import { nanoId, date } from 'minifaker';
import { Cipher } from 'n8n-core';
import { CREDENTIAL_EMPTY_VALUE, type ICredentialType } from 'n8n-workflow';
import Container from 'typedi';

import { CREDENTIAL_BLANKING_VALUE } from '@/constants';
import type { CredentialTypes } from '@/credential-types';
Expand Down Expand Up @@ -30,6 +32,8 @@ describe('CredentialsService', () => {
},
],
});

const cipher = Container.get(Cipher);
const credentialTypes = mock<CredentialTypes>();
const service = new CredentialsService(
mock(),
Expand Down Expand Up @@ -61,7 +65,7 @@ describe('CredentialsService', () => {
csrfSecret: 'super-secret',
};

credentialTypes.getByName.calledWith(credential.type).mockReturnValue(credType);
credentialTypes.getByName.calledWith(credential.type).mockReturnValueOnce(credType);

const redactedData = service.redact(decryptedData, credential);

Expand Down Expand Up @@ -137,4 +141,60 @@ describe('CredentialsService', () => {
});
});
});

describe('decrypt', () => {
it('should redact sensitive values by default', () => {
// ARRANGE
const data = {
clientId: 'abc123',
clientSecret: 'sensitiveSecret',
accessToken: '',
oauthTokenData: 'super-secret',
csrfSecret: 'super-secret',
};
const credential = mock<CredentialsEntity>({
id: '123',
name: 'Test Credential',
type: 'oauth2',
data: cipher.encrypt(data),
});
credentialTypes.getByName.calledWith(credential.type).mockReturnValueOnce(credType);

// ACT
const redactedData = service.decrypt(credential);

// ASSERT
expect(redactedData).toEqual({
clientId: 'abc123',
clientSecret: CREDENTIAL_BLANKING_VALUE,
accessToken: CREDENTIAL_EMPTY_VALUE,
oauthTokenData: CREDENTIAL_BLANKING_VALUE,
csrfSecret: CREDENTIAL_BLANKING_VALUE,
});
});

it('should return sensitive values if `includeRawData` is true', () => {
// ARRANGE
const data = {
clientId: 'abc123',
clientSecret: 'sensitiveSecret',
accessToken: '',
oauthTokenData: 'super-secret',
csrfSecret: 'super-secret',
};
const credential = mock<CredentialsEntity>({
id: '123',
name: 'Test Credential',
type: 'oauth2',
data: cipher.encrypt(data),
});
credentialTypes.getByName.calledWith(credential.type).mockReturnValueOnce(credType);

// ACT
const redactedData = service.decrypt(credential, true);

// ASSERT
expect(redactedData).toEqual(data);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { mock } from 'jest-mock-extended';
import { Container } from 'typedi';

import { CredentialsEntity } from '@/databases/entities/credentials-entity';
import { mockEntityManager } from '@test/mocking';

import { CredentialsRepository } from '../credentials.repository';

const entityManager = mockEntityManager(CredentialsEntity);
const repository = Container.get(CredentialsRepository);

describe('findMany', () => {
const credentialsId = 'cred_123';
const credential = mock<CredentialsEntity>({ id: credentialsId });

beforeEach(() => {
jest.resetAllMocks();
});

test('return `data` property if `includeData:true` and select is using the record syntax', async () => {
// ARRANGE
entityManager.find.mockResolvedValueOnce([credential]);

// ACT
const credentials = await repository.findMany({ includeData: true, select: { id: true } });

// ASSERT
expect(credentials).toHaveLength(1);
expect(credentials[0]).toHaveProperty('data');
});

test('return `data` property if `includeData:true` and select is using the record syntax', async () => {
// ARRANGE
entityManager.find.mockResolvedValueOnce([credential]);

// ACT
const credentials = await repository.findMany({
includeData: true,
//TODO: fix this
// The function's type does not support this but this is what it
// actually gets from the service because the middlewares are typed
// loosely.
select: ['id'] as never,
});

// ASSERT
expect(credentials).toHaveLength(1);
expect(credentials[0]).toHaveProperty('data');
});
});

0 comments on commit 8a1b523

Please sign in to comment.