From 737391c721dea6d88a176f1e7624255f08c53693 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Tue, 1 Oct 2024 15:23:52 +0000 Subject: [PATCH] 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(); + }); +});