|
| 1 | +/********************************************************************* |
| 2 | + * Copyright (c) Intel Corporation 2025 |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + **********************************************************************/ |
| 5 | + |
| 6 | +import PostgresDb from '../index.js' |
| 7 | +import { API_UNEXPECTED_EXCEPTION } from '../../../utils/constants.js' |
| 8 | +import { type ProfileProxyConfigs } from '../../../models/RCS.Config.js' |
| 9 | +import { ProfileProxyConfigsTable } from './profileProxyConfigs.js' |
| 10 | +import { jest } from '@jest/globals' |
| 11 | +import { type SpyInstance, spyOn } from 'jest-mock' |
| 12 | + |
| 13 | +describe('profileproxyconfig tests', () => { |
| 14 | + let db: PostgresDb |
| 15 | + let profilesProxyConfigsTable: ProfileProxyConfigsTable |
| 16 | + let querySpy: SpyInstance<any> |
| 17 | + const proxyConfigs: ProfileProxyConfigs[] = [ |
| 18 | + { configName: 'proxyConfig', priority: 1 } as any |
| 19 | + ] |
| 20 | + const configName = 'profileName' |
| 21 | + const tenantId = 'tenantId' |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + db = new PostgresDb('') |
| 25 | + profilesProxyConfigsTable = new ProfileProxyConfigsTable(db) |
| 26 | + querySpy = spyOn(profilesProxyConfigsTable.db, 'query') |
| 27 | + }) |
| 28 | + afterEach(() => { |
| 29 | + jest.clearAllMocks() |
| 30 | + }) |
| 31 | + describe('Get', () => { |
| 32 | + test('should Get', async () => { |
| 33 | + querySpy.mockResolvedValueOnce({ rows: [{}], rowCount: 1 }) |
| 34 | + const result = await profilesProxyConfigsTable.getProfileProxyConfigs(configName) |
| 35 | + expect(result).toStrictEqual([{}]) |
| 36 | + expect(querySpy).toBeCalledTimes(1) |
| 37 | + expect(querySpy).toBeCalledWith( |
| 38 | + ` |
| 39 | + SELECT |
| 40 | + priority as "priority", |
| 41 | + access_info as "configName" |
| 42 | + FROM profiles_proxyconfigs |
| 43 | + WHERE profile_name = $1 and tenant_id = $2 |
| 44 | + ORDER BY priority`, |
| 45 | + [configName, ''] |
| 46 | + ) |
| 47 | + }) |
| 48 | + }) |
| 49 | + describe('Delete', () => { |
| 50 | + test('should Delete', async () => { |
| 51 | + querySpy.mockResolvedValueOnce({ rows: [{}], rowCount: 1 }) |
| 52 | + const result = await profilesProxyConfigsTable.deleteProfileProxyConfigs(configName) |
| 53 | + expect(result).toBe(true) |
| 54 | + expect(querySpy).toBeCalledTimes(1) |
| 55 | + expect(querySpy).toBeCalledWith( |
| 56 | + ` |
| 57 | + DELETE |
| 58 | + FROM profiles_proxyconfigs |
| 59 | + WHERE profile_name = $1 and tenant_id = $2`, |
| 60 | + [configName, ''] |
| 61 | + ) |
| 62 | + }) |
| 63 | + test('should Delete with tenandId', async () => { |
| 64 | + querySpy.mockResolvedValueOnce({ rows: [{}], rowCount: 1 }) |
| 65 | + const result = await profilesProxyConfigsTable.deleteProfileProxyConfigs(configName, tenantId) |
| 66 | + expect(result).toBe(true) |
| 67 | + expect(querySpy).toBeCalledTimes(1) |
| 68 | + expect(querySpy).toBeCalledWith( |
| 69 | + ` |
| 70 | + DELETE |
| 71 | + FROM profiles_proxyconfigs |
| 72 | + WHERE profile_name = $1 and tenant_id = $2`, |
| 73 | + [configName, tenantId] |
| 74 | + ) |
| 75 | + }) |
| 76 | + }) |
| 77 | + describe('Insert', () => { |
| 78 | + test('should Insert', async () => { |
| 79 | + querySpy.mockResolvedValueOnce({ rows: [{}], rowCount: 1 }) |
| 80 | + const result = await profilesProxyConfigsTable.createProfileProxyConfigs(proxyConfigs, configName) |
| 81 | + expect(result).toBe(true) |
| 82 | + expect(querySpy).toBeCalledTimes(1) |
| 83 | + expect(querySpy).toBeCalledWith(` |
| 84 | + INSERT INTO |
| 85 | + profiles_proxyconfigs (access_info, profile_name, priority, tenant_id) |
| 86 | + VALUES ('proxyConfig', 'profileName', '1', '')`) |
| 87 | + }) |
| 88 | + test('should NOT insert when no proxyconfigs', async () => { |
| 89 | + await expect(profilesProxyConfigsTable.createProfileProxyConfigs([], configName)).rejects.toThrow( |
| 90 | + 'Operation failed: profileName' |
| 91 | + ) |
| 92 | + }) |
| 93 | + test('should NOT insert when constraint violation', async () => { |
| 94 | + querySpy.mockRejectedValueOnce({ code: '23503', detail: 'error' }) |
| 95 | + await expect(profilesProxyConfigsTable.createProfileProxyConfigs(proxyConfigs, configName)).rejects.toThrow( |
| 96 | + 'error' |
| 97 | + ) |
| 98 | + }) |
| 99 | + test('should NOT insert when unknown error', async () => { |
| 100 | + querySpy.mockRejectedValueOnce('unknown') |
| 101 | + await expect(profilesProxyConfigsTable.createProfileProxyConfigs(proxyConfigs, configName)).rejects.toThrow( |
| 102 | + API_UNEXPECTED_EXCEPTION(configName) |
| 103 | + ) |
| 104 | + }) |
| 105 | + }) |
| 106 | +}) |
0 commit comments