From b5323e8194e82f7b7c8c765f2b57da9669707998 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Sun, 6 Oct 2024 07:21:34 +0000 Subject: [PATCH] test: jest test - Integration encryption --- .../nocodb/src/models/Integration.spec.ts | 19 ++++++++++--------- packages/nocodb/src/utils/encryptDecrypt.ts | 9 +++++---- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/nocodb/src/models/Integration.spec.ts b/packages/nocodb/src/models/Integration.spec.ts index d2c13bafe9..45f58545ec 100644 --- a/packages/nocodb/src/models/Integration.spec.ts +++ b/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 }); diff --git a/packages/nocodb/src/utils/encryptDecrypt.ts b/packages/nocodb/src/utils/encryptDecrypt.ts index d36b9d543b..d95fe83ad9 100644 --- a/packages/nocodb/src/utils/encryptDecrypt.ts +++ b/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; prop?: string; @@ -28,7 +29,7 @@ export const encryptPropIfRequired = ({ export const decryptPropIfRequired = ({ data, prop = 'config', - secret = credentialEncryptSecret, + secret = getCredentialEncryptSecret(), }: { data: Record; prop?: string;