From 737391c721dea6d88a176f1e7624255f08c53693 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Tue, 1 Oct 2024 15:23:52 +0000 Subject: [PATCH 1/3] test(app-auth-client): write model tests --- .../app-auth-client.test.js.snap | 39 ++++++ .../backend/src/models/app-auth-client.js | 1 + .../src/models/app-auth-client.test.js | 121 ++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 packages/backend/src/models/__snapshots__/app-auth-client.test.js.snap create mode 100644 packages/backend/src/models/app-auth-client.test.js diff --git a/packages/backend/src/models/__snapshots__/app-auth-client.test.js.snap b/packages/backend/src/models/__snapshots__/app-auth-client.test.js.snap new file mode 100644 index 00000000..87b5cc8c --- /dev/null +++ b/packages/backend/src/models/__snapshots__/app-auth-client.test.js.snap @@ -0,0 +1,39 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`AppAuthClient model > jsonSchema should have correct validations 1`] = ` +{ + "properties": { + "active": { + "type": "boolean", + }, + "appKey": { + "type": "string", + }, + "authDefaults": { + "type": [ + "string", + "null", + ], + }, + "createdAt": { + "type": "string", + }, + "formattedAuthDefaults": { + "type": "object", + }, + "id": { + "format": "uuid", + "type": "string", + }, + "updatedAt": { + "type": "string", + }, + }, + "required": [ + "name", + "appKey", + "formattedAuthDefaults", + ], + "type": "object", +} +`; diff --git a/packages/backend/src/models/app-auth-client.js b/packages/backend/src/models/app-auth-client.js index 0121a727..e4e9753e 100644 --- a/packages/backend/src/models/app-auth-client.js +++ b/packages/backend/src/models/app-auth-client.js @@ -31,6 +31,7 @@ class AppAuthClient extends Base { delete this.formattedAuthDefaults; } + decryptData() { if (!this.eligibleForDecryption()) return; diff --git a/packages/backend/src/models/app-auth-client.test.js b/packages/backend/src/models/app-auth-client.test.js new file mode 100644 index 00000000..13849197 --- /dev/null +++ b/packages/backend/src/models/app-auth-client.test.js @@ -0,0 +1,121 @@ +import { describe, it, expect, vi } from 'vitest'; +import AES from 'crypto-js/aes.js'; +import enc from 'crypto-js/enc-utf8.js'; + +import AppAuthClient from './app-auth-client.js'; +import appConfig from '../config/app.js'; +import { createAppAuthClient } from '../../test/factories/app-auth-client.js'; + +describe('AppAuthClient model', () => { + it('tableName should return correct name', () => { + expect(AppAuthClient.tableName).toBe('app_auth_clients'); + }); + + it('jsonSchema should have correct validations', () => { + expect(AppAuthClient.jsonSchema).toMatchSnapshot(); + }); + + it('encryptData should encrypt formattedAuthDefaults and set it to authDefaults', async () => { + const formattedAuthDefaults = { + key: 'value', + }; + + const appAuthClient = await createAppAuthClient({ + formattedAuthDefaults, + }); + + expect( + JSON.parse( + AES.decrypt( + appAuthClient.authDefaults, + appConfig.encryptionKey + ).toString(enc) + ) + ).toStrictEqual(formattedAuthDefaults); + }); + + it('decryptData should decrypt authDefaults and set it to formattedAuthDefaults', async () => { + const formattedAuthDefaults = { + key: 'value', + }; + + const appAuthClient = await createAppAuthClient({ + formattedAuthDefaults, + }); + + const refetchedAppAuthClient = await appAuthClient.$query(); + + expect(refetchedAppAuthClient.formattedAuthDefaults).toStrictEqual( + formattedAuthDefaults + ); + }); + + describe('eligibleForEncryption', () => { + it('should return true when formattedAuthDefaults property exists', async () => { + const appAuthClient = await createAppAuthClient(); + + expect(appAuthClient.eligibleForEncryption()).toBe(true); + }); + + it("should return false when formattedAuthDefaults property doesn't exist", async () => { + const appAuthClient = await createAppAuthClient(); + + delete appAuthClient.formattedAuthDefaults; + + expect(appAuthClient.eligibleForEncryption()).toBe(false); + }); + }); + + describe('eligibleForDecryption', () => { + it('should return true when authDefaults property exists', async () => { + const appAuthClient = await createAppAuthClient(); + + expect(appAuthClient.eligibleForDecryption()).toBe(true); + }); + + it("should return false when authDefaults property doesn't exist", async () => { + const appAuthClient = await createAppAuthClient(); + + delete appAuthClient.authDefaults; + + expect(appAuthClient.eligibleForDecryption()).toBe(false); + }); + }); + + it('$beforeInsert should call AppAuthClient.encryptData', async () => { + const appAuthClientBeforeInsertSpy = vi.spyOn( + AppAuthClient.prototype, + 'encryptData' + ); + + await createAppAuthClient(); + + expect(appAuthClientBeforeInsertSpy).toHaveBeenCalledOnce(); + }); + + it('$beforeUpdate should call AppAuthClient.encryptData', async () => { + const appAuthClient = await createAppAuthClient(); + + const appAuthClientBeforeUpdateSpy = vi.spyOn( + AppAuthClient.prototype, + 'encryptData' + ); + + await appAuthClient.$query().patchAndFetch({ name: 'sample' }); + + expect(appAuthClientBeforeUpdateSpy).toHaveBeenCalledOnce(); + }); + + it('$afterFind should call AppAuthClient.decryptData', async () => { + const appAuthClient = await createAppAuthClient(); + + const appAuthClientAfterFindSpy = vi.spyOn( + AppAuthClient.prototype, + 'decryptData' + ); + + await appAuthClient.$query(); + + expect(appAuthClientAfterFindSpy).toHaveBeenCalledOnce(); + }); +}); From 26be72f76d726bf407f8a4c7834b38d3544e576d Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Mon, 7 Oct 2024 14:21:14 +0200 Subject: [PATCH 2/3] 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 () => { From 0b956a71b97f009676a5357e25799d422627e608 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Mon, 7 Oct 2024 13:25:04 +0000 Subject: [PATCH 3/3] test(models/app-auth-client): improve decrypt data test cases --- .../src/models/app-auth-client.test.js | 51 +++++++++++++++---- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/packages/backend/src/models/app-auth-client.test.js b/packages/backend/src/models/app-auth-client.test.js index 043117cd..d95ade3d 100644 --- a/packages/backend/src/models/app-auth-client.test.js +++ b/packages/backend/src/models/app-auth-client.test.js @@ -41,7 +41,14 @@ describe('AppAuthClient model', () => { appAuthClient.formattedAuthDefaults = formattedAuthDefaults; appAuthClient.encryptData(); - expect(appAuthClient.authDefaults).toBeDefined(); + const expectedDecryptedValue = JSON.parse( + AES.decrypt( + appAuthClient.authDefaults, + appConfig.encryptionKey + ).toString(enc) + ); + + expect(formattedAuthDefaults).toStrictEqual(expectedDecryptedValue); expect(appAuthClient.authDefaults).not.toEqual(formattedAuthDefaults); }); @@ -63,20 +70,42 @@ describe('AppAuthClient model', () => { }); }); - it('decryptData should decrypt authDefaults and set it to formattedAuthDefaults', async () => { - const formattedAuthDefaults = { - key: 'value', - }; + describe('decryptData', () => { + it('should return undefined if eligibleForDecryption is not true', () => { + vi.spyOn( + AppAuthClient.prototype, + 'eligibleForDecryption' + ).mockReturnValue(false); - const appAuthClient = await createAppAuthClient({ - formattedAuthDefaults, + const appAuthClient = new AppAuthClient(); + + expect(appAuthClient.decryptData()).toBeUndefined(); }); - const refetchedAppAuthClient = await appAuthClient.$query(); + it('should decrypt authDefaults and set it to formattedAuthDefaults', async () => { + vi.spyOn( + AppAuthClient.prototype, + 'eligibleForDecryption' + ).mockReturnValue(true); - expect(refetchedAppAuthClient.formattedAuthDefaults).toStrictEqual( - formattedAuthDefaults - ); + const formattedAuthDefaults = { + key: 'value', + }; + + const authDefaults = AES.encrypt( + JSON.stringify(formattedAuthDefaults), + appConfig.encryptionKey + ).toString(); + + const appAuthClient = new AppAuthClient(); + appAuthClient.authDefaults = authDefaults; + appAuthClient.decryptData(); + + expect(appAuthClient.formattedAuthDefaults).toStrictEqual( + formattedAuthDefaults + ); + expect(appAuthClient.authDefaults).not.toEqual(formattedAuthDefaults); + }); }); describe('eligibleForEncryption', () => {