Browse Source

test: jest test - Integration encryption

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

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

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

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

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

Loading…
Cancel
Save