Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Add includeData parameter to GET /credentials #12220

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
despairblue marked this conversation as resolved.
Show resolved Hide resolved

// 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');
});
});
Loading