@intuitionrobotics/permissions
Version:
108 lines • 6.1 kB
JavaScript
"use strict";
/*
* ts-common is the basic building blocks of our typescript projects
*
* Copyright (C) 2020 Intuition Robotics
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.testUserPermissionsTime = testUserPermissionsTime;
const ts_common_1 = require("@intuitionrobotics/ts-common");
const managment_1 = require("../modules/db-types/managment");
const assign_1 = require("../modules/db-types/assign");
const permissions_assert_1 = require("../modules/permissions-assert");
function makeAlphaBetIdForTestOnly(length) {
let result = '';
const characters = `abcdefghijklmnopqrstuvwxyz`;
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function testUserPermissionsTime() {
return __awaiter(this, void 0, void 0, function* () {
console.log("---Inside of permissions test---");
const projectId = 'project-test-ten';
const apiId = (0, ts_common_1.generateHex)(32);
const userId = (0, ts_common_1.generateHex)(32);
const apiPath = '/v1/test/test-it10';
const userUuid = 'test10@intuitionrobotics.com';
const permissionId = (0, ts_common_1.generateHex)(32);
const domainId = (0, ts_common_1.generateHex)(32);
const permissionValue = 50;
const customField = { UnitId: 'eq1' };
yield managment_1.ProjectPermissionsDB.upsert({ _id: projectId, name: 'project test' });
yield managment_1.DomainPermissionsDB.upsert({ _id: domainId, projectId: projectId, namespace: 'domain-test' });
const accessLevel = yield managment_1.AccessLevelPermissionsDB.upsert({ _id: permissionId, name: 'test-permission', domainId, value: permissionValue });
yield managment_1.ApiPermissionsDB.upsert({ projectId: projectId, _id: apiId, path: apiPath, accessLevelIds: [permissionId] });
const groupIdArray = [];
const dbInstances = [];
for (let counter = 0; counter < 100; counter++) {
const groupId = (0, ts_common_1.generateHex)(32);
const baseAccessLevel = { domainId: accessLevel.domainId, value: accessLevel.value };
dbInstances.push({
_id: groupId,
accessLevelIds: [accessLevel._id],
__accessLevels: [baseAccessLevel],
customFields: [customField],
label: `group-${makeAlphaBetIdForTestOnly(5)}`
});
groupIdArray.push({ groupId, customField: { test: "test" } });
}
console.log('dbInstances ready to upsert');
// @ts-ignore
const collection = assign_1.GroupPermissionsDB.collection;
yield collection.runInTransaction((transaction) => __awaiter(this, void 0, void 0, function* () {
// @ts-ignore
yield Promise.all(dbInstances.map(dbInstance => assign_1.GroupPermissionsDB.assertUniqueness(transaction, dbInstance)));
return transaction.upsertAll(collection, dbInstances);
}));
yield assign_1.UserPermissionsDB.upsert({ _id: userId, accountId: userUuid, groups: groupIdArray });
const tests = new Array().fill(0, 0, 50);
const durations = yield Promise.all(tests.map(test => runAssertion(projectId, apiPath, userUuid, customField)));
const sum = durations.reduce((_sum, val) => _sum + val, 0);
console.log(`Call to assertion on ${tests.length} call took on agerage ${sum / tests.length}ms`);
// ----deletes db documents---
yield assign_1.UserPermissionsDB.delete({ where: { _id: userId } });
for (let counter = 0; counter < 10; counter++) {
const startIndex = counter * 10;
const subDbInstances = dbInstances.slice(startIndex, startIndex + 10);
yield assign_1.GroupPermissionsDB.delete({ where: { _id: { $in: subDbInstances.map(e => e._id) } } });
}
yield managment_1.AccessLevelPermissionsDB.delete({ where: { _id: permissionId } });
yield managment_1.DomainPermissionsDB.delete({ where: { _id: domainId } });
yield managment_1.ApiPermissionsDB.delete({ where: { _id: apiId } });
yield managment_1.ProjectPermissionsDB.delete({ where: { _id: projectId } });
});
}
function runAssertion(projectId, apiPath, userUuid, customField) {
return __awaiter(this, void 0, void 0, function* () {
const start = (0, ts_common_1.currentTimeMillies)();
yield permissions_assert_1.PermissionsAssert.assertUserPermissions(projectId, apiPath, userUuid, customField);
const runTime = (0, ts_common_1.currentTimeMillies)() - start;
console.log(`Call to assertion took ${runTime}ms`);
return runTime;
});
}
//# sourceMappingURL=permission-user-assert-benchmark.js.map