From 26be72f76d726bf407f8a4c7834b38d3544e576d Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Mon, 7 Oct 2024 14:21:14 +0200 Subject: [PATCH] test: Improve encrypt data tests for AppAuthClient model --- .../src/models/app-auth-client.test.js | 57 ++++++++++++++----- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/packages/backend/src/models/app-auth-client.test.js b/packages/backend/src/models/app-auth-client.test.js index 13849197..043117cd 100644 --- a/packages/backend/src/models/app-auth-client.test.js +++ b/packages/backend/src/models/app-auth-client.test.js @@ -15,23 +15,52 @@ describe('AppAuthClient model', () => { expect(AppAuthClient.jsonSchema).toMatchSnapshot(); }); - it('encryptData should encrypt formattedAuthDefaults and set it to authDefaults', async () => { - const formattedAuthDefaults = { - key: 'value', - }; + describe('encryptData', () => { + it('should return undefined if eligibleForEncryption is not true', async () => { + vi.spyOn( + AppAuthClient.prototype, + 'eligibleForEncryption' + ).mockReturnValue(false); - const appAuthClient = await createAppAuthClient({ - formattedAuthDefaults, + const appAuthClient = new AppAuthClient(); + + expect(appAuthClient.encryptData()).toBeUndefined(); }); - expect( - JSON.parse( - AES.decrypt( - appAuthClient.authDefaults, - appConfig.encryptionKey - ).toString(enc) - ) - ).toStrictEqual(formattedAuthDefaults); + it('should encrypt formattedAuthDefaults and set it to authDefaults', async () => { + vi.spyOn( + AppAuthClient.prototype, + 'eligibleForEncryption' + ).mockReturnValue(true); + + const formattedAuthDefaults = { + key: 'value', + }; + + const appAuthClient = new AppAuthClient(); + appAuthClient.formattedAuthDefaults = formattedAuthDefaults; + appAuthClient.encryptData(); + + expect(appAuthClient.authDefaults).toBeDefined(); + expect(appAuthClient.authDefaults).not.toEqual(formattedAuthDefaults); + }); + + it('should encrypt formattedAuthDefaults and remove formattedAuthDefaults', async () => { + vi.spyOn( + AppAuthClient.prototype, + 'eligibleForEncryption' + ).mockReturnValue(true); + + const formattedAuthDefaults = { + key: 'value', + }; + + const appAuthClient = new AppAuthClient(); + appAuthClient.formattedAuthDefaults = formattedAuthDefaults; + appAuthClient.encryptData(); + + expect(appAuthClient.formattedAuthDefaults).not.toBeDefined(); + }); }); it('decryptData should decrypt authDefaults and set it to formattedAuthDefaults', async () => {