From 1c870c915125912fec3569cf283b58a2a35281a5 Mon Sep 17 00:00:00 2001 From: Jeremy Davis Date: Mon, 24 Feb 2020 11:02:37 +0100 Subject: [PATCH] SONAR-13117 Handle 403 gracefully --- server/sonar-web/src/main/js/api/issues.ts | 2 +- .../CrossComponentSourceViewerWrapper.tsx | 24 +++++++++++++++---- ...CrossComponentSourceViewerWrapper-test.tsx | 10 ++++++++ ...ComponentSourceViewerWrapper-test.tsx.snap | 9 +++++++ 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/server/sonar-web/src/main/js/api/issues.ts b/server/sonar-web/src/main/js/api/issues.ts index a6b8b87c70c..d0756260502 100644 --- a/server/sonar-web/src/main/js/api/issues.ts +++ b/server/sonar-web/src/main/js/api/issues.ts @@ -179,5 +179,5 @@ export function getIssueFlowSnippets(issueKey: string): Promise { @@ -66,7 +70,8 @@ export default class CrossComponentSourceViewerWrapper extends React.PureCompone state: State = { components: {}, duplicationsByLine: {}, - loading: true + loading: true, + notAccessible: false }; componentDidMount() { @@ -122,9 +127,12 @@ export default class CrossComponentSourceViewerWrapper extends React.PureCompone } } }, - () => { + (response: Response) => { + if (response.status !== 403) { + throwGlobalError(response); + } if (this.mounted) { - this.setState({ loading: false }); + this.setState({ loading: false, notAccessible: response.status === 403 }); } } ); @@ -197,7 +205,7 @@ export default class CrossComponentSourceViewerWrapper extends React.PureCompone }; render() { - const { loading } = this.state; + const { loading, notAccessible } = this.state; if (loading) { return ( @@ -207,6 +215,14 @@ export default class CrossComponentSourceViewerWrapper extends React.PureCompone ); } + if (notAccessible) { + return ( + + {translate('code_viewer.no_source_code_displayed_due_to_security')} + + ); + } + const { components, duplications, duplicationsByLine, linePopup } = this.state; const issuesByComponent = issuesByComponentAndLine(this.props.issues); const locationsByComponent = groupLocationsByComponent(this.props.locations, components); diff --git a/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/__tests__/CrossComponentSourceViewerWrapper-test.tsx b/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/__tests__/CrossComponentSourceViewerWrapper-test.tsx index 2dcfa8f3ee9..318e6646d6b 100644 --- a/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/__tests__/CrossComponentSourceViewerWrapper-test.tsx +++ b/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/__tests__/CrossComponentSourceViewerWrapper-test.tsx @@ -66,6 +66,16 @@ it('Should fetch data', async () => { expect(getIssueFlowSnippets).toBeCalledWith('foo'); }); +it('Should handle no access rights', async () => { + (getIssueFlowSnippets as jest.Mock).mockRejectedValueOnce({ status: 403 }); + + const wrapper = shallowRender(); + await waitAndUpdate(wrapper); + + expect(wrapper.state().notAccessible).toBe(true); + expect(wrapper).toMatchSnapshot(); +}); + it('should handle issue popup', () => { const wrapper = shallowRender(); // open diff --git a/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/__tests__/__snapshots__/CrossComponentSourceViewerWrapper-test.tsx.snap b/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/__tests__/__snapshots__/CrossComponentSourceViewerWrapper-test.tsx.snap index 5f6913c5ace..c6362cb8a71 100644 --- a/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/__tests__/__snapshots__/CrossComponentSourceViewerWrapper-test.tsx.snap +++ b/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/__tests__/__snapshots__/CrossComponentSourceViewerWrapper-test.tsx.snap @@ -1,5 +1,14 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Should handle no access rights 1`] = ` + + code_viewer.no_source_code_displayed_due_to_security + +`; + exports[`should handle duplication popup 1`] = ` [Function] -- 2.39.5