diff --git a/packages/backend/src/models/__snapshots__/app-config.test.js.snap b/packages/backend/src/models/__snapshots__/app-config.test.js.snap new file mode 100644 index 00000000..ee1e862c --- /dev/null +++ b/packages/backend/src/models/__snapshots__/app-config.test.js.snap @@ -0,0 +1,44 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`AppConfig model > jsonSchema should have correct validations 1`] = ` +{ + "properties": { + "allowCustomConnection": { + "default": false, + "type": "boolean", + }, + "createdAt": { + "type": "string", + }, + "disabled": { + "default": false, + "type": "boolean", + }, + "id": { + "format": "uuid", + "type": "string", + }, + "key": { + "type": "string", + }, + "shared": { + "default": false, + "type": "boolean", + }, + "updatedAt": { + "type": "string", + }, + }, + "required": [ + "key", + ], + "type": "object", +} +`; + +exports[`AppConfig model > virtualAttributes should return correct properties 1`] = ` +[ + "canConnect", + "canCustomConnect", +] +`; diff --git a/packages/backend/src/models/app-config.test.js b/packages/backend/src/models/app-config.test.js new file mode 100644 index 00000000..7c14f19e --- /dev/null +++ b/packages/backend/src/models/app-config.test.js @@ -0,0 +1,115 @@ +import { describe, it, expect } from 'vitest'; + +import Base from './base.js'; +import AppConfig from './app-config.js'; +import AppAuthClient from './app-auth-client.js'; +import { createAppConfig } from '../../test/factories/app-config.js'; +import { createAppAuthClient } from '../../test/factories/app-auth-client.js'; + +describe('AppConfig model', () => { + it('tableName should return correct name', () => { + expect(AppConfig.tableName).toBe('app_configs'); + }); + + it('jsonSchema should have correct validations', () => { + expect(AppConfig.jsonSchema).toMatchSnapshot(); + }); + + it('relationMappings should return correct associations', () => { + const relationMappings = AppConfig.relationMappings(); + + const expectedRelations = { + appAuthClients: { + relation: Base.HasManyRelation, + modelClass: AppAuthClient, + join: { + from: 'app_configs.key', + to: 'app_auth_clients.app_key', + }, + }, + }; + + expect(relationMappings).toStrictEqual(expectedRelations); + }); + + it('virtualAttributes should return correct properties', () => { + expect(AppConfig.virtualAttributes).toMatchSnapshot(); + }); + + it('getApp should return associated application', async () => { + const appConfig = await createAppConfig({ key: 'deepl' }); + + const app = await appConfig.getApp(); + + expect(app.key).toBe('deepl'); + }); + + describe('canCustomConnect', () => { + it('should return true when app is enabled and allows custom connection', async () => { + const appConfig = await createAppConfig({ + disabled: false, + allowCustomConnection: true, + }); + + expect(appConfig.canCustomConnect).toBe(true); + }); + + it('should return false when app is disabled', async () => { + const appConfig = await createAppConfig({ + disabled: true, + allowCustomConnection: true, + }); + + expect(appConfig.canCustomConnect).toBe(false); + }); + + it(`should return false when app doesn't allow custom connection`, async () => { + const appConfig = await createAppConfig({ + disabled: false, + allowCustomConnection: false, + }); + + expect(appConfig.canCustomConnect).toBe(false); + }); + }); + + describe('canConnect', () => { + it('should return true when app is enabled, shared and allows custom connection', async () => { + await createAppAuthClient({ + appKey: 'deepl', + active: true, + }); + + let appConfig = await createAppConfig({ + disabled: false, + allowCustomConnection: true, + shared: true, + key: 'deepl', + }); + + appConfig = await appConfig.$query().withGraphFetched({ + appAuthClients: true, + }); + + expect(appConfig.canConnect).toBe(true); + }); + + it('should return false when app is disabled', async () => { + const appConfig = await createAppConfig({ + disabled: true, + allowCustomConnection: true, + }); + + expect(appConfig.canConnect).toBe(false); + }); + + it(`should return false when app doesn't allow custom connection`, async () => { + const appConfig = await createAppConfig({ + disabled: false, + allowCustomConnection: false, + }); + + expect(appConfig.canConnect).toBe(false); + }); + }); +});