Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,13 @@ const server = setupServer(
})
);
}),
rest.post('/api/v2/graphs/cypher', (req, res, ctx) => {
rest.get('/api/v2/nodes/:id', (req, res, ctx) => {
return res(
ctx.json({
data: {
nodes: {
'42': {
kind: 'Unknown',
properties: { objectid: 'unknown kind' },
},
},
node_id: 42,
kinds: [{ node_kind_id: 1, name: 'Unknown' }],
properties: { objectid: 'unknown kind' },
},
})
);
Expand Down Expand Up @@ -135,7 +132,7 @@ describe('EntityObjectInformation', () => {
expect(await screen.findByText('test')).toBeInTheDocument();
});

it('Calls the cypher search endpoint for a node with a type that is not in our schema', async () => {
it('Calls the get node by id endpoint for a node with a type that is not in our schema', async () => {
const testId = '1';
const nodeType = 'Unknown';
const databaseId = '42';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,13 @@ vi.mock('../../utils/content', () => ({

vi.mock('../../utils/api', () => ({
apiClient: {
cypherSearch: vi.fn(() =>
getNodeByID: vi.fn(() =>
Promise.resolve({
data: {
data: {
nodes: {
'1': {
kinds: ['Admin_Tier_0'],
properties: mockEntityProperties,
},
},
node_id: 1,
kinds: [{ node_kind_id: 1, name: 'Admin_Tier_0' }],
properties: mockEntityProperties,
Comment thread
urangel marked this conversation as resolved.
},
},
})
Expand All @@ -68,24 +65,6 @@ const entityObjectIdRequest = () =>
)
);

const entityGraphIdRequest = () =>
rest.post(`/api/v2/graphs/cypher`, async (_req, res, ctx) =>
res(
ctx.json({
data: {
data: {
nodes: {
'1': {
kinds: ['Admin_Tier_0'],
properties: mockEntityProperties,
},
},
},
},
})
)
);

const EntityNodeType = 'User' as const;
const EntityApiPathType = 'users' as const;
const EntityGraphId = '5223' as const;
Expand All @@ -106,7 +85,7 @@ const mockEntityProperties = {
};

describe('useFetchEntityInfo', () => {
const server = setupServer(entityObjectIdRequest(), entityGraphIdRequest());
const server = setupServer(entityObjectIdRequest());

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
Expand Down Expand Up @@ -150,7 +129,7 @@ describe('useFetchEntityInfo', () => {
});

it('fetches new information when a different databaseId is passed', async () => {
const cypherSearchSpy = vi.spyOn(apiClient, 'cypherSearch');
const getNodeByIDSpy = vi.spyOn(apiClient, 'getNodeByID');

const { result, rerender } = renderHook<FetchEntityInfoResult, FetchEntityInfoParams>(
(params) => useFetchEntityInfo(params),
Expand All @@ -163,12 +142,12 @@ describe('useFetchEntityInfo', () => {
expect(result.current.isSuccess).toBe(true);
});

expect(cypherSearchSpy).toHaveBeenCalledTimes(1);
expect(getNodeByIDSpy).toHaveBeenCalledTimes(1);

rerender({ ...initialPropsCustom, databaseId: '7' });

await waitFor(() => {
expect(cypherSearchSpy).toHaveBeenCalledTimes(2);
expect(getNodeByIDSpy).toHaveBeenCalledTimes(2);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { RequestOptions } from 'js-client-library';
import { useQuery, UseQueryResult } from 'react-query';
import { apiClient } from '../../utils/api';
import { entityInformationEndpoints } from '../../utils/content';
import { getNodeByDatabaseIdCypher } from '../../utils/entityInfoDisplay';
import { validateNodeType } from './utils';

export type FetchEntityInfoParams = {
Expand Down Expand Up @@ -46,48 +45,34 @@ export const useFetchEntityInfo: (param: FetchEntityInfoParams) => FetchEntityIn
nodeType,
databaseId,
}) => {
const requestDetails: {
endpoint: (
params: string,
options?: RequestOptions,
includeProperties?: boolean
) => Promise<Record<string, any>>;
param: string;
} = {
endpoint: async function () {
return {};
},
param: '',
};

const validatedKind = validateNodeType(nodeType);

if (validatedKind) {
requestDetails.endpoint = entityInformationEndpoints[validatedKind];
requestDetails.param = objectId;
} else if (databaseId) {
requestDetails.endpoint = apiClient.cypherSearch;
requestDetails.param = getNodeByDatabaseIdCypher(databaseId);
}

const informationAvailable = !!validatedKind || !!databaseId;
const informationAvailable = !!databaseId || !!validatedKind;

return {
...useQuery(
[FetchEntityCacheId, nodeType, objectId, databaseId],
({ signal }) =>
requestDetails.endpoint(requestDetails.param, { signal }, true).then((res) => {
if (validatedKind) {
({ signal }) => {
if (databaseId) {
return apiClient.getNodeByID(Number(databaseId), { signal }).then((res) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it redundant to perform an additional validations on databaseId to ensure a valid ID before reaching backend?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question. The type of databaseId is defined as string | undefined so the property may or may not be available depending on the context in which one is trying to load entity information. This may have changed since this code was written originally so I'll double check but that is why the validation is happening.

const kinds = res.data.data.kinds.map((kind) => kind.name);
const properties = res.data.data.properties;
return { kinds, properties };
});
} else if (validatedKind) {
const endpoint = entityInformationEndpoints[validatedKind] as (
params: string,
options?: RequestOptions,
includeProperties?: boolean
) => Promise<Record<string, any>>;
return endpoint(objectId, { signal }, true).then((res) => {
const kinds = res.data.data.kinds;
const properties = res.data.data.props;
return { kinds, properties };
} else if (databaseId) {
const data = Object.values(res.data.data.nodes as Record<string, any>)[0];
const kinds = data.kinds;
const properties = data.properties;
return { kinds, properties };
} else return { kinds: [], properties: {} };
}),
});
}
return Promise.resolve({ kinds: [], properties: {} });
},
{
refetchOnWindowFocus: false,
retry: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ export const getEntityName = (selectedEntity: SelectedNode | null | undefined) =
return name;
};

export const getNodeByDatabaseIdCypher = (id: string): string => `MATCH (n) WHERE ID(n) = ${id} RETURN n LIMIT 1`;

// Map containing all properties that should display as bitwise integers in the entity panel.
// The key is the property string, the value is the amount of significant digits the hex value should display with.
const BitwiseInts = new Map([['certificatemappingmethodsraw', 2]]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,13 @@ describe('Selected Details Tab Content', () => {
})
);
}),
rest.post('/api/v2/graphs/cypher', async (_req, res, ctx) => {
rest.get('/api/v2/nodes/:id', async (_req, res, ctx) => {
return res(
ctx.json({
data: {
nodes: {
'123': {
properties: {
customprop: 'OpenGraph Value',
},
},
kinds: [{ name: 'Custom' }],
properties: {
customprop: 'OpenGraph Value',
},
},
})
Expand Down
Loading