From b6fbf307d980eee099f73e06d778632b8eb1af4d Mon Sep 17 00:00:00 2001 From: Kristina Date: Thu, 6 Jul 2023 08:36:39 -0500 Subject: [PATCH] Explore: Add checking for target datasources and add test (#70855) Add checking for target datasources and add test --- .../correlations/CorrelationsPage.test.tsx | 76 +++++++++++++++++++ .../features/correlations/useCorrelations.ts | 11 ++- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/public/app/features/correlations/CorrelationsPage.test.tsx b/public/app/features/correlations/CorrelationsPage.test.tsx index 707850973cc..d48c6223464 100644 --- a/public/app/features/correlations/CorrelationsPage.test.tsx +++ b/public/app/features/correlations/CorrelationsPage.test.tsx @@ -555,6 +555,82 @@ describe('CorrelationsPage', () => { }); }); + describe('With correlations with datasources the user cannot access', () => { + let queryCellsByColumnName: (columnName: Matcher) => HTMLTableCellElement[]; + beforeEach(async () => { + const renderResult = await renderWithContext( + { + loki: mockDataSource( + { + uid: 'loki', + name: 'loki', + readOnly: false, + jsonData: {}, + access: 'direct', + type: 'datasource', + }, + { + logs: true, + } + ), + }, + [ + { + sourceUID: 'loki', + targetUID: 'loki', + uid: '1', + label: 'Loki to Loki', + config: { + field: 'line', + target: {}, + type: 'query', + transformations: [ + { type: SupportedTransformationType.Regex, expression: 'url=http[s]?://(S*)', mapValue: 'path' }, + ], + }, + }, + { + sourceUID: 'loki', + targetUID: 'prometheus', + uid: '2', + label: 'Loki to Prometheus', + config: { + field: 'line', + target: {}, + type: 'query', + transformations: [ + { type: SupportedTransformationType.Regex, expression: 'url=http[s]?://(S*)', mapValue: 'path' }, + ], + }, + }, + { + sourceUID: 'prometheus', + targetUID: 'loki', + uid: '3', + label: 'Prometheus to Loki', + config: { field: 'label', target: {}, type: 'query' }, + }, + { + sourceUID: 'prometheus', + targetUID: 'prometheus', + uid: '4', + label: 'Prometheus to Prometheus', + config: { field: 'label', target: {}, type: 'query' }, + }, + ] + ); + queryCellsByColumnName = renderResult.queryCellsByColumnName; + }); + + it("doesn't show correlations from source or target datasources the user doesn't have access to", async () => { + await screen.findByRole('table'); + + const labels = queryCellsByColumnName('Label'); + expect(labels.length).toBe(1); + expect(labels[0].textContent).toBe('Loki to Loki'); + }); + }); + describe('Read only correlations', () => { const correlations: Correlation[] = [ { diff --git a/public/app/features/correlations/useCorrelations.ts b/public/app/features/correlations/useCorrelations.ts index f32af6080d8..d3a64a2903e 100644 --- a/public/app/features/correlations/useCorrelations.ts +++ b/public/app/features/correlations/useCorrelations.ts @@ -41,11 +41,18 @@ const toEnrichedCorrelationData = ({ ...correlation }: Correlation): CorrelationData | undefined => { const sourceDatasource = getDataSourceSrv().getInstanceSettings(sourceUID); - if (sourceDatasource) { + const targetDatasource = getDataSourceSrv().getInstanceSettings(targetUID); + + if ( + sourceDatasource && + sourceDatasource?.uid !== undefined && + targetDatasource && + targetDatasource.uid !== undefined + ) { return { ...correlation, source: sourceDatasource, - target: getDataSourceSrv().getInstanceSettings(targetUID)!, + target: targetDatasource, }; } else { return undefined;