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

fix: update pre-built searches to avoid potentially comparing defined… #1139

Merged
merged 5 commits into from
Feb 11, 2025
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
87 changes: 87 additions & 0 deletions packages/javascript/bh-shared-ui/src/commonSearches.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { CommonSearches, CommonSearchType } from './commonSearches';
import {
ActiveDirectoryNodeKind,
ActiveDirectoryRelationshipKind,
AzureNodeKind,
AzureRelationshipKind,
} from './graphSchema';

describe('common search list', () => {
const nodeKindPattern = /((?<=\(.?:).*?(?=\)))/gm;
const relKindPattern = /((?<=\[.?:).*?(?=\]))/gm;

test('the queries in the list only include nodes and edges that are defined in our schema', () => {
CommonSearches.forEach((commonSearchType: CommonSearchType) => {
commonSearchType.queries.forEach((query) => {
const queryNodeLabels = query.cypher.match(nodeKindPattern);
const queryRelLabels = query.cypher.match(relKindPattern);

if (queryNodeLabels) {
queryNodeLabels.forEach((result) => {
const inAD = Object.values(ActiveDirectoryNodeKind).includes(result as ActiveDirectoryNodeKind);
const inAZ = Object.values(AzureNodeKind).includes(result as AzureNodeKind);
const inSchema = inAD || inAZ;

expect(inSchema).toBeTruthy();
});
}

if (queryRelLabels) {
queryRelLabels.forEach((result) => {
// Trim off any depth specifications
if (result.includes('*')) result = result.slice(0, result.indexOf('*'));

// Turn the match into an array because sometimes there are multiple edges
const results = result.split('|');

results.forEach((edgeKind) => {
const inAD = Object.values(ActiveDirectoryRelationshipKind).includes(
edgeKind as ActiveDirectoryRelationshipKind
);
const inAZ = Object.values(AzureRelationshipKind).includes(
edgeKind as AzureRelationshipKind
);
const inSchema = inAD || inAZ;

expect(inSchema).toBeTruthy();
});
});
}
});
});
});

test('typos will be flagged', () => {
// The typo is 'AZAutmomationContributor'
const query = `MATCH p = (:User)-[:SyncedToEntraUser]->(:AZUser)-[:AZMemberOf]->(:AZGroup)-[:AZOwner|AZUserAccessAdministrator|AZGetCertificates|AZGetKeys|AZGetSecrets|AZAvereContributor|AZKeyVaultContributor|AZContributor|AZVMAdminLogin|AZVMContributor|AZAKSContributor|AZAutmomationContributor|AZLogicAppContributor|AZWebsiteContributor]->(:AZBase)\nRETURN p\nLIMIT 1000`;

const queryRelLabels = query.match(relKindPattern);

let hasTypo = false;

if (queryRelLabels) {
queryRelLabels.forEach((result) => {
if (result.includes('*')) result = result.slice(0, result.indexOf('*'));

const results = result.split('|');

results.forEach((edgeKind) => {
const inAD = Object.values(ActiveDirectoryRelationshipKind).includes(
edgeKind as ActiveDirectoryRelationshipKind
);
const inAZ = Object.values(AzureRelationshipKind).includes(edgeKind as AzureRelationshipKind);
const inSchema = inAD || inAZ;

// This gets set to true when an edge kind is not in our schema
// In this case it is because there is a typo
if (!inSchema) hasTypo = true;

// The previous test has an assertion at this point and so if a query has a typo
// like this test query does the first test block will fail
});
});
}

expect(hasTypo).toBe(true);
});
});
Loading
Loading