Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions data/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,25 @@ CREATE TABLE IF NOT EXISTS domains(
PRIMARY KEY (name, domain_suffix, tenant_id)
);
CREATE TABLE IF NOT EXISTS proxyconfigs(
access_info varchar(256) NOT NULL,
info_format integer,
port integer,
proxy_config_name citext,
access_info citext NOT NULL,
info_format integer NOT NULL,
port integer NOT NULL,
network_dns_suffix varchar(192),
creation_date timestamp,
tenant_id varchar(36) NOT NULL,
PRIMARY KEY (access_info, tenant_id)
tenant_id varchar(36),
CONSTRAINT access_info_port UNIQUE (access_info, port, tenant_id),
PRIMARY KEY (proxy_config_name, tenant_id)
);
CREATE TABLE IF NOT EXISTS profiles_proxyconfigs(
proxy_config_name citext,
access_info citext,
Comment thread
graikhel-intel marked this conversation as resolved.
profile_name citext,
FOREIGN KEY (access_info,tenant_id) REFERENCES proxyconfigs(access_info,tenant_id),
FOREIGN KEY (proxy_config_name,tenant_id) REFERENCES proxyconfigs(proxy_config_name,tenant_id),
FOREIGN KEY (profile_name,tenant_id) REFERENCES profiles(profile_name,tenant_id),
priority integer,
creation_date timestamp,
created_by varchar(40),
tenant_id varchar(36) NOT NULL,
PRIMARY KEY (access_info, profile_name, priority, tenant_id)
tenant_id varchar(36),
PRIMARY KEY (proxy_config_name, profile_name, priority, tenant_id)
);
30 changes: 15 additions & 15 deletions src/data/postgres/tables/profileProxyConfigs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ describe('profileproxyconfig tests', () => {
let profilesProxyConfigsTable: ProfileProxyConfigsTable
let querySpy: SpyInstance<any>
const proxyConfigs: ProfileProxyConfigs[] = [
{ configName: 'proxyConfig', priority: 1 } as any
{ profileName: 'proxyConfig', priority: 1 } as any
]
const configName = 'profileName'
const profileName = 'profileName'
const tenantId = 'tenantId'

beforeEach(() => {
Expand All @@ -31,75 +31,75 @@ describe('profileproxyconfig tests', () => {
describe('Get', () => {
test('should Get', async () => {
querySpy.mockResolvedValueOnce({ rows: [{}], rowCount: 1 })
const result = await profilesProxyConfigsTable.getProfileProxyConfigs(configName)
const result = await profilesProxyConfigsTable.getProfileProxyConfigs(profileName)
expect(result).toStrictEqual([{}])
expect(querySpy).toBeCalledTimes(1)
expect(querySpy).toBeCalledWith(
`
SELECT
priority as "priority",
access_info as "configName"
proxy_config_name as "profileName"
FROM profiles_proxyconfigs
WHERE profile_name = $1 and tenant_id = $2
ORDER BY priority`,
[configName, '']
[profileName, '']
)
})
})
describe('Delete', () => {
test('should Delete', async () => {
querySpy.mockResolvedValueOnce({ rows: [{}], rowCount: 1 })
const result = await profilesProxyConfigsTable.deleteProfileProxyConfigs(configName)
const result = await profilesProxyConfigsTable.deleteProfileProxyConfigs(profileName)
expect(result).toBe(true)
expect(querySpy).toBeCalledTimes(1)
expect(querySpy).toBeCalledWith(
`
DELETE
FROM profiles_proxyconfigs
WHERE profile_name = $1 and tenant_id = $2`,
[configName, '']
[profileName, '']
)
})
test('should Delete with tenandId', async () => {
querySpy.mockResolvedValueOnce({ rows: [{}], rowCount: 1 })
const result = await profilesProxyConfigsTable.deleteProfileProxyConfigs(configName, tenantId)
const result = await profilesProxyConfigsTable.deleteProfileProxyConfigs(profileName, tenantId)
expect(result).toBe(true)
expect(querySpy).toBeCalledTimes(1)
expect(querySpy).toBeCalledWith(
`
DELETE
FROM profiles_proxyconfigs
WHERE profile_name = $1 and tenant_id = $2`,
[configName, tenantId]
[profileName, tenantId]
)
})
})
describe('Insert', () => {
test('should Insert', async () => {
querySpy.mockResolvedValueOnce({ rows: [{}], rowCount: 1 })
const result = await profilesProxyConfigsTable.createProfileProxyConfigs(proxyConfigs, configName)
const result = await profilesProxyConfigsTable.createProfileProxyConfigs(proxyConfigs, profileName)
expect(result).toBe(true)
expect(querySpy).toBeCalledTimes(1)
expect(querySpy).toBeCalledWith(`
INSERT INTO
profiles_proxyconfigs (access_info, profile_name, priority, tenant_id)
profiles_proxyconfigs (proxy_config_name, profile_name, priority, tenant_id)
VALUES ('proxyConfig', 'profileName', '1', '')`)
})
test('should NOT insert when no proxyconfigs', async () => {
await expect(profilesProxyConfigsTable.createProfileProxyConfigs([], configName)).rejects.toThrow(
await expect(profilesProxyConfigsTable.createProfileProxyConfigs([], profileName)).rejects.toThrow(
'Operation failed: profileName'
)
})
test('should NOT insert when constraint violation', async () => {
querySpy.mockRejectedValueOnce({ code: '23503', detail: 'error' })
await expect(profilesProxyConfigsTable.createProfileProxyConfigs(proxyConfigs, configName)).rejects.toThrow(
await expect(profilesProxyConfigsTable.createProfileProxyConfigs(proxyConfigs, profileName)).rejects.toThrow(
'error'
)
})
test('should NOT insert when unknown error', async () => {
querySpy.mockRejectedValueOnce('unknown')
await expect(profilesProxyConfigsTable.createProfileProxyConfigs(proxyConfigs, configName)).rejects.toThrow(
API_UNEXPECTED_EXCEPTION(configName)
await expect(profilesProxyConfigsTable.createProfileProxyConfigs(proxyConfigs, profileName)).rejects.toThrow(
API_UNEXPECTED_EXCEPTION(profileName)
)
})
})
Expand Down
20 changes: 10 additions & 10 deletions src/data/postgres/tables/profileProxyConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,32 @@ export class ProfileProxyConfigsTable implements IProfileProxyConfigsTable {

/**
* @description Get AMT Profile associated proxy configs
* @param {string} configName
* @param {string} profileName
* @returns {ProfileProxyConfigs[]} Return an array of proxy configs
*/
async getProfileProxyConfigs(configName: string, tenantId = ''): Promise<ProfileProxyConfigs[]> {
async getProfileProxyConfigs(profileName: string, tenantId = ''): Promise<ProfileProxyConfigs[]> {
const results = await this.db.query<ProfileProxyConfigs>(
`
SELECT
priority as "priority",
access_info as "configName"
proxy_config_name as "profileName"
FROM profiles_proxyconfigs
WHERE profile_name = $1 and tenant_id = $2
ORDER BY priority`,
[configName, tenantId]
[profileName, tenantId]
)
return results.rows
}

/**
* @description Insert proxy configs associated with AMT profile
* @param {ProfileProxyConfigs[]} proxyConfigs
* @param {string} configName
* @param {string} profileName
* @returns {ProfileProxyConfigs[]} Return an array of proxy configs
*/
async createProfileProxyConfigs(
proxyConfigs: ProfileProxyConfigs[],
configName: string,
profileName: string,
tenantId = ''
): Promise<boolean> {
try {
Expand All @@ -53,16 +53,16 @@ export class ProfileProxyConfigsTable implements IProfileProxyConfigsTable {
}
// Preparing data for inserting multiple rows
const configs = proxyConfigs.map((config) => [
config.configName,
configName,
config.profileName,
profileName,
config.priority,
tenantId
])
const proxyProfilesQueryResults = await this.db.query(
format(
`
INSERT INTO
profiles_proxyconfigs (access_info, profile_name, priority, tenant_id)
profiles_proxyconfigs (proxy_config_name, profile_name, priority, tenant_id)
VALUES %L`,
configs
)
Expand All @@ -75,7 +75,7 @@ export class ProfileProxyConfigsTable implements IProfileProxyConfigsTable {
if (error.code === PostgresErr.C23_FOREIGN_KEY_VIOLATION) {
throw new RPSError(error.detail, 'Foreign key constraint violation')
}
throw new RPSError(API_UNEXPECTED_EXCEPTION(configName))
throw new RPSError(API_UNEXPECTED_EXCEPTION(profileName))
}
return false
}
Expand Down
4 changes: 2 additions & 2 deletions src/data/postgres/tables/profiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('profiles tests', () => {
COALESCE(json_agg(json_build_object('profileName',wc.wireless_profile_name, 'priority', wc.priority)) FILTER (WHERE wc.wireless_profile_name IS NOT NULL), '[]') AS "wifiConfigs",
ip_sync_enabled as "ipSyncEnabled",
local_wifi_sync_enabled as "localWifiSyncEnabled",
COALESCE(json_agg(json_build_object('configName',pc.access_info, 'priority', pc.priority)) FILTER (WHERE pc.access_info IS NOT NULL), '[]') AS "proxyConfigs"
COALESCE(json_agg(json_build_object('profileName',pc.proxy_config_name, 'priority', pc.priority)) FILTER (WHERE pc.proxy_config_name IS NOT NULL), '[]') AS "proxyConfigs"
FROM profiles p
LEFT JOIN profiles_wirelessconfigs wc ON wc.profile_name = p.profile_name AND wc.tenant_id = p.tenant_id
LEFT JOIN profiles_proxyconfigs pc ON pc.profile_name = p.profile_name AND pc.tenant_id = p.tenant_id
Expand Down Expand Up @@ -191,7 +191,7 @@ describe('profiles tests', () => {
COALESCE(json_agg(json_build_object('profileName',wc.wireless_profile_name, 'priority', wc.priority)) FILTER (WHERE wc.wireless_profile_name IS NOT NULL), '[]') AS "wifiConfigs",
ip_sync_enabled as "ipSyncEnabled",
local_wifi_sync_enabled as "localWifiSyncEnabled",
COALESCE(json_agg(json_build_object('configName',pc.access_info, 'priority', pc.priority)) FILTER (WHERE pc.access_info IS NOT NULL), '[]') AS "proxyConfigs"
COALESCE(json_agg(json_build_object('profileName',pc.proxy_config_name, 'priority', pc.priority)) FILTER (WHERE pc.proxy_config_name IS NOT NULL), '[]') AS "proxyConfigs"
FROM profiles p
LEFT JOIN profiles_wirelessconfigs wc ON wc.profile_name = p.profile_name AND wc.tenant_id = p.tenant_id
LEFT JOIN profiles_proxyconfigs pc ON pc.profile_name = p.profile_name AND pc.tenant_id = p.tenant_id
Expand Down
4 changes: 2 additions & 2 deletions src/data/postgres/tables/profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class ProfilesTable implements IProfilesTable {
COALESCE(json_agg(json_build_object('profileName',wc.wireless_profile_name, 'priority', wc.priority)) FILTER (WHERE wc.wireless_profile_name IS NOT NULL), '[]') AS "wifiConfigs",
ip_sync_enabled as "ipSyncEnabled",
local_wifi_sync_enabled as "localWifiSyncEnabled",
COALESCE(json_agg(json_build_object('configName',pc.access_info, 'priority', pc.priority)) FILTER (WHERE pc.access_info IS NOT NULL), '[]') AS "proxyConfigs"
COALESCE(json_agg(json_build_object('profileName',pc.proxy_config_name, 'priority', pc.priority)) FILTER (WHERE pc.proxy_config_name IS NOT NULL), '[]') AS "proxyConfigs"
FROM profiles p
LEFT JOIN profiles_wirelessconfigs wc ON wc.profile_name = p.profile_name AND wc.tenant_id = p.tenant_id
LEFT JOIN profiles_proxyconfigs pc ON pc.profile_name = p.profile_name AND pc.tenant_id = p.tenant_id
Expand Down Expand Up @@ -143,7 +143,7 @@ export class ProfilesTable implements IProfilesTable {
COALESCE(json_agg(json_build_object('profileName',wc.wireless_profile_name, 'priority', wc.priority)) FILTER (WHERE wc.wireless_profile_name IS NOT NULL), '[]') AS "wifiConfigs",
ip_sync_enabled as "ipSyncEnabled",
local_wifi_sync_enabled as "localWifiSyncEnabled",
COALESCE(json_agg(json_build_object('configName',pc.access_info, 'priority', pc.priority)) FILTER (WHERE pc.access_info IS NOT NULL), '[]') AS "proxyConfigs"
COALESCE(json_agg(json_build_object('profileName',pc.proxy_config_name, 'priority', pc.priority)) FILTER (WHERE pc.proxy_config_name IS NOT NULL), '[]') AS "proxyConfigs"
FROM profiles p
LEFT JOIN profiles_wirelessconfigs wc ON wc.profile_name = p.profile_name AND wc.tenant_id = p.tenant_id
LEFT JOIN profiles_proxyconfigs pc ON pc.profile_name = p.profile_name AND pc.tenant_id = p.tenant_id
Expand Down
Loading