Skip to content

Commit b7dfc9b

Browse files
committed
optimizations
1 parent 42404d8 commit b7dfc9b

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

src/groups.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Ajv from 'ajv';
77
dotenv.config();
88

99
const sharedState = {};
10+
const ajv = new Ajv(); // Reuse Ajv instance for better performance
1011

1112
function verifyGroup(group, schema, schemaExtensions = []) {
1213
// Ensure the group contains no other attributes then defined in the schema
@@ -23,12 +24,19 @@ function verifyGroup(group, schema, schemaExtensions = []) {
2324
}
2425

2526
async function populateUserIds(request, configuration, t){
26-
// request from JSON to string
27+
// Check if replacement is needed before making API call
2728
const requestString = JSON.stringify(request);
28-
// Get the user ID first
29-
const userId = await lookupUserId(configuration, t);
29+
if (!requestString.includes('AUTO')) {
30+
return request;
31+
}
32+
33+
// Get the user ID (cached in sharedState if available)
34+
if (!sharedState.userId) {
35+
sharedState.userId = await lookupUserId(configuration, t);
36+
}
37+
3038
// replace each occurrence of AUTO with the actual user ID
31-
const replacedString = requestString.replace(/AUTO/g, userId);
39+
const replacedString = requestString.replace(/AUTO/g, sharedState.userId);
3240
// return string to JSON
3341
return JSON.parse(replacedString);
3442
}
@@ -50,14 +58,6 @@ function runTests(groupSchema, groupSchemaExtensions = [], configuration) {
5058
assert.strictEqual(displayNameAttribute.required, true, 'displayName attribute should be marked as required');
5159
});
5260

53-
// before all, ensure schema is set
54-
test.beforeEach(() => {
55-
if (!groupSchema) {
56-
t.skip('Schema is not set');
57-
return;
58-
}
59-
});
60-
6161
test('Retrieves a list of groups', async (t) => {
6262
const testAxios = getAxiosInstance(configuration, t);
6363
const response = await testAxios.get('/Groups');
@@ -145,7 +145,7 @@ function runTests(groupSchema, groupSchemaExtensions = [], configuration) {
145145
}
146146

147147
if (configuration?.groups?.operations?.includes('POST')) {
148-
for (const [index, creation] of (configuration.groups.post_tests || []).entries()) {
148+
for (const [index, creation] of (configuration?.groups?.post_tests || []).entries()) {
149149
test(`Creates a new group - Alternative ${index + 1}`, async (t) => {
150150
const testAxios = getAxiosInstance(configuration, t);
151151

@@ -156,7 +156,6 @@ function runTests(groupSchema, groupSchemaExtensions = [], configuration) {
156156

157157
// Verify response matches the expected response format
158158
if (creation.response) {
159-
const ajv = new Ajv();
160159
const valid = ajv.validate(creation.response, canonicalize(response.data, groupSchema) );
161160
assert.ok(valid, `Response doesn't match the expected schema: ${JSON.stringify(ajv.errors)}`);
162161
}
@@ -182,7 +181,7 @@ function runTests(groupSchema, groupSchemaExtensions = [], configuration) {
182181
}
183182

184183
if (configuration?.groups?.operations?.includes('PUT')) {
185-
for (const [index, update] of (configuration.groups.put_tests || []).entries()) {
184+
for (const [index, update] of (configuration?.groups?.put_tests || []).entries()) {
186185
test(`Updates a group using PUT - Alternative ${index + 1}`, async (t) => {
187186
const testAxios = getAxiosInstance(configuration, t);
188187

@@ -200,7 +199,6 @@ function runTests(groupSchema, groupSchemaExtensions = [], configuration) {
200199

201200
// Verify response matches the expected response format
202201
if (update.response) {
203-
const ajv = new Ajv();
204202
const valid = ajv.validate(update.response, canonicalize(response.data, groupSchema));
205203
assert.ok(valid, `Response doesn't match the expected schema: ${JSON.stringify(ajv.errors)}`);
206204
}
@@ -209,7 +207,7 @@ function runTests(groupSchema, groupSchemaExtensions = [], configuration) {
209207
}
210208

211209
if (configuration?.groups?.operations?.includes('PATCH')) {
212-
for (const [index, patch] of (configuration.groups.patch_tests || []).entries()) {
210+
for (const [index, patch] of (configuration?.groups?.patch_tests || []).entries()) {
213211
test(`Updates a group using PATCH - Alternative ${index + 1}`, async (t) => {
214212
const testAxios = getAxiosInstance(configuration, t);
215213

@@ -227,7 +225,6 @@ function runTests(groupSchema, groupSchemaExtensions = [], configuration) {
227225

228226
// Verify response matches the expected response format
229227
if (patch.response) {
230-
const ajv = new Ajv();
231228
const valid = ajv.validate(patch.response, canonicalize(response.data, groupSchema));
232229
assert.ok(valid, `Response doesn't match the expected schema: ${JSON.stringify(ajv.errors)}`);
233230
}
@@ -274,7 +271,7 @@ function runTests(groupSchema, groupSchemaExtensions = [], configuration) {
274271
}
275272

276273
if (configuration?.groups?.operations?.includes('DELETE')) {
277-
for (const [index, deletion] of (configuration.groups.delete_tests || []).entries()) {
274+
for (const [index, deletion] of (configuration?.groups?.delete_tests || []).entries()) {
278275
await test(`Deletes a group - Alternative ${index + 1}`, async (t) => {
279276
const testAxios = getAxiosInstance(configuration, t);
280277

0 commit comments

Comments
 (0)