Browse Source

test: jest test - Integration encryption

pull/9499/head
Pranav C 2 months ago
parent
commit
b5323e8194
  1. 17
      packages/nocodb/src/models/Integration.spec.ts
  2. 9
      packages/nocodb/src/utils/encryptDecrypt.ts

17
packages/nocodb/src/models/Integration.spec.ts

@ -1,7 +1,6 @@
import { IntegrationsType } from 'nocodb-sdk';
import { Integration } from '~/models';
import { MetaTable } from '~/utils/globals';
import Noco from '~/Noco';
import { decryptPropIfRequired } from '~/utils';
// Mock dependencies
@ -152,16 +151,18 @@ describe('Integration Model', () => {
},
);
});
});
it('should create a new integration with encrypted config', async () => {
// overwrite env variable
const config = Noco.getConfig();
describe('create with encryption', () => {
beforeAll(() => {
process.env.NC_KEY_CREDENTIAL_ENCRYPT = 'test-secret';
});
Noco.getConfig = jest.fn().mockReturnValue({
...config,
credentialSecret: 'test-secret',
afterAll(() => {
process.env.NC_KEY_CREDENTIAL_ENCRYPT = undefined;
});
it('should create a new integration with encrypted config', async () => {
const newIntegration = {
id: 'new-id',
title: 'New Integration',
@ -195,7 +196,7 @@ describe('Integration Model', () => {
const calledWithArgs = mockNcMeta.metaInsert2.mock.calls[0][3];
// veify the 'config' field is encrypted
expect(calledWithArgs.config).not.toEqual(newIntegration.config);
expect(calledWithArgs.config).not.toEqual(JSON.stringify(newIntegration.config));
// Decrypt the 'config' field
const decryptedConfig = decryptPropIfRequired({ data: calledWithArgs });

9
packages/nocodb/src/utils/encryptDecrypt.ts

@ -1,14 +1,15 @@
import CryptoJS from 'crypto-js';
export const credentialEncryptSecret = process.env.NC_KEY_CREDENTIAL_ENCRYPT;
export const getCredentialEncryptSecret = () =>
process.env.NC_KEY_CREDENTIAL_ENCRYPT;
export const isEncryptionRequired = (secret = credentialEncryptSecret) => {
export const isEncryptionRequired = (secret = getCredentialEncryptSecret()) => {
return !!secret;
};
export const encryptPropIfRequired = ({
data,
prop = 'config',
secret = credentialEncryptSecret,
secret = getCredentialEncryptSecret(),
}: {
data: Record<string, any>;
prop?: string;
@ -28,7 +29,7 @@ export const encryptPropIfRequired = ({
export const decryptPropIfRequired = ({
data,
prop = 'config',
secret = credentialEncryptSecret,
secret = getCredentialEncryptSecret(),
}: {
data: Record<string, any>;
prop?: string;

Loading…
Cancel
Save