Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Script Type Imports and Exports #471

Merged
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
205 changes: 205 additions & 0 deletions src/api/ScriptTypeApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
import util from 'util';

import { State } from '../shared/State';
import {
AmConfigEntityInterface,
IdObjectSkeletonInterface,
PagedResult,
} from './ApiTypes';
import { generateAmApi } from './BaseApi';

const scriptTypeURLTemplate =
'%s/json/global-config/services/scripting/contexts/%s';
const scriptTypesURLTemplate =
'%s/json/global-config/services/scripting/contexts?_queryFilter=true';
const scriptTypeContextURLTemplate = '%s/json/contexts/%s';

const scriptingEngineConfigurationURLTemplate =
scriptTypeURLTemplate + '/engineConfiguration';

const apiVersion = 'protocol=2.0,resource=1.0';

function getApiConfig() {
return {
apiVersion,
};
}

export type ScriptTypeSkeleton = AmConfigEntityInterface & {
defaultScript: string;
languages: string[];
};

export type EngineConfigurationSkeleton = AmConfigEntityInterface & {
blackList: string[];
coreThreads: number;
idleTimeout: number;
maxThreads: number;
propertyNamePrefix: string;
queueSize: number;
serverTimeout: number;
useSecurityManager: boolean;
whiteList: string[];
};

export type ScriptingContextSkeleton = IdObjectSkeletonInterface & {
allowLists: Record<string, string[]>;
evaluatorVersions: Record<string, string[]>;
};

/**
* Get scriptType
* @param {string} scriptTypeId scriptType id
* @returns {Promise<ScriptTypeSkeleton>} a promise that resolves to a scriptType object
*/
export async function getScriptType({
scriptTypeId,
state,
}: {
scriptTypeId: string;
state: State;
}): Promise<ScriptTypeSkeleton> {
const urlString = util.format(
scriptTypeURLTemplate,
state.getHost(),
scriptTypeId
);
const { data } = await generateAmApi({ resource: getApiConfig(), state }).get(
urlString,
{
withCredentials: true,
}
);
return data;
}

/**
* Get all scriptTypes
* @returns {Promise<PagedResult<ScriptTypeSkeleton[]>>} a promise that resolves to an array of scriptType objects
*/
export async function getScriptTypes({
state,
}: {
state: State;
}): Promise<PagedResult<ScriptTypeSkeleton>> {
const urlString = util.format(scriptTypesURLTemplate, state.getHost());
const { data } = await generateAmApi({
resource: getApiConfig(),
state,
}).get(urlString, {
withCredentials: true,
});
return data;
}

/**
* Get scripting engine configuration
* @param {string} scriptTypeId Script type id
* @returns {Promise<EngineConfigurationSkeleton>} a promise that resolves to an EngineConfigurationSkeleton object
*/
export async function getScriptingEngineConfiguration({
scriptTypeId,
state,
}: {
scriptTypeId: string;
state: State;
}): Promise<EngineConfigurationSkeleton> {
const urlString = util.format(
scriptingEngineConfigurationURLTemplate,
state.getHost(),
scriptTypeId
);
const { data } = await generateAmApi({
resource: getApiConfig(),
state,
}).get(urlString, {
withCredentials: true,
});
return data;
}

/**
* Get scripting contexts
* @param {string} scriptTypeId Script type id
* @returns {Promise<ScriptingContextSkeleton>} a promise that resolves to an ScriptingContextSkeleton object
*/
export async function getScriptingContext({
scriptTypeId,
state,
}: {
scriptTypeId: string;
state: State;
}): Promise<ScriptingContextSkeleton> {
const urlString = util.format(
scriptTypeContextURLTemplate,
state.getHost(),
scriptTypeId
);
const { data } = await generateAmApi({
resource: getApiConfig(),
state,
}).get(urlString, {
withCredentials: true,
});
return data;
}

/**
* Put script type
* @param {string} scriptTypeId script type id
* @param {ScriptTypeSkeleton} scriptTypeData script type config object
* @returns {Promise<ScriptTypeSkeleton>} a promise that resolves to a script type object
*/
export async function putScriptType({
scriptTypeId,
scriptTypeData,
state,
}: {
scriptTypeId: string;
scriptTypeData: ScriptTypeSkeleton;
state: State;
}): Promise<ScriptTypeSkeleton> {
const urlString = util.format(
scriptTypeURLTemplate,
state.getHost(),
scriptTypeId
);
const { data } = await generateAmApi({
resource: getApiConfig(),
state,
}).put(urlString, scriptTypeData, {
withCredentials: true,
headers: { 'If-Match': '*' },
});
return data;
}

/**
* Put scripting engine configuration
* @param {string} scriptTypeId script type id
* @param {EngineConfigurationSkeleton} engineConfigurationData engine config object
* @returns {Promise<EngineConfigurationSkeleton>} a promise that resolves to a script type object
*/
export async function putScriptingEngineConfiguration({
scriptTypeId,
engineConfigurationData,
state,
}: {
scriptTypeId: string;
engineConfigurationData: EngineConfigurationSkeleton;
state: State;
}): Promise<EngineConfigurationSkeleton> {
const urlString = util.format(
scriptingEngineConfigurationURLTemplate,
state.getHost(),
scriptTypeId
);
const { data } = await generateAmApi({ resource: getApiConfig(), state }).put(
urlString,
engineConfigurationData,
{
withCredentials: true,
}
);
return data;
}
3 changes: 3 additions & 0 deletions src/lib/FrodoLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import ReconOps, { Recon } from '../ops/ReconOps';
import ResourceTypeOps, { ResourceType } from '../ops/ResourceTypeOps';
import Saml2Ops, { Saml2 } from '../ops/Saml2Ops';
import ScriptOps, { Script } from '../ops/ScriptOps';
import ScriptTypeOps, { ScriptType } from '../ops/ScriptTypeOps';
import ServiceOps, { Service } from '../ops/ServiceOps';
import SessionOps, { Session } from '../ops/SessionOps';
import ThemeOps, { Theme } from '../ops/ThemeOps';
Expand Down Expand Up @@ -185,6 +186,7 @@ export type Frodo = {
};

script: Script;
scriptType: ScriptType;
server: Server;
service: Service;
session: Session;
Expand Down Expand Up @@ -353,6 +355,7 @@ const FrodoLib = (config: StateInterface = {}): Frodo => {
},

script: ScriptOps(state),
scriptType: ScriptTypeOps(state),
server: ServerOps(state),
service: ServiceOps(state),
session: SessionOps(state),
Expand Down
109 changes: 109 additions & 0 deletions src/ops/ScriptTypeOps.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* To record and update snapshots, you must perform 3 steps in order:
*
* 1. Record API responses
*
* To record API responses, you must call the test:record script and
* override all the connection state required to connect to the
* env to record from:
*
* ATTENTION: For the recording to succeed, you MUST make sure to use a
* user account, not a service account.
*
* FRODO_DEBUG=1 FRODO_HOST=frodo-dev npm run test:record ScriptTypeOps
*
* The above command assumes that you have a connection profile for
* 'frodo-dev' on your development machine.
*
* 2. Update snapshots
*
* After recording API responses, you must manually update/create snapshots
* by running:
*
* FRODO_DEBUG=1 npm run test:update ScriptTypeOps
*
* 3. Test your changes
*
* If 1 and 2 didn't produce any errors, you are ready to run the tests in
* replay mode and make sure they all succeed as well:
*
* FRODO_DEBUG=1 npm run test:only ScriptTypeOps
*
* Note: FRODO_DEBUG=1 is optional and enables debug logging for some output
* in case things don't function as expected
*/
import { autoSetupPolly } from "../utils/AutoSetupPolly";
import { filterRecording } from "../utils/PollyUtils";
import * as ScriptTypeOps from "./ScriptTypeOps";
import { state } from "../lib/FrodoLib";

const ctx = autoSetupPolly();

describe('ScriptTypeOps', () => {
beforeEach(async () => {
if (process.env.FRODO_POLLY_MODE === 'record') {
ctx.polly.server.any().on('beforePersist', (_req, recording) => {
filterRecording(recording);
});
}
});

describe('createScriptTypeExportTemplate()', () => {
test('0: Method is implemented', async () => {
expect(ScriptTypeOps.createScriptTypeExportTemplate).toBeDefined();
});

test('1: Create ScriptType Export Template', async () => {
const response = ScriptTypeOps.createScriptTypeExportTemplate({ state });
expect(response).toMatchSnapshot({
meta: expect.any(Object),
});
});
});

describe('readScriptType()', () => {
test('0: Method is implemented', async () => {
expect(ScriptTypeOps.readScriptType).toBeDefined();
});
//TODO: create tests
});

describe('readScriptTypes()', () => {
test('0: Method is implemented', async () => {
expect(ScriptTypeOps.readScriptTypes).toBeDefined();
});

test('1: Read ScriptTypes', async () => {
const response = await ScriptTypeOps.readScriptTypes({ state });
expect(response).toMatchSnapshot();
});
});

describe('exportScriptTypes()', () => {
test('0: Method is implemented', async () => {
expect(ScriptTypeOps.exportScriptTypes).toBeDefined();
});

test('1: Export ScriptTypes', async () => {
const response = await ScriptTypeOps.exportScriptTypes({ state });
expect(response).toMatchSnapshot({
meta: expect.any(Object),
});
});
});

describe('updateScriptType()', () => {
test('0: Method is implemented', async () => {
expect(ScriptTypeOps.updateScriptType).toBeDefined();
});
//TODO: create tests
});

describe('importScriptTypes()', () => {
test('0: Method is implemented', async () => {
expect(ScriptTypeOps.importScriptTypes).toBeDefined();
});
//TODO: create tests
});

});
Loading
Loading