diff --git a/src/gmp/http/__tests__/http.js b/src/gmp/http/__tests__/http.js index f7210abb20..af33acc76a 100644 --- a/src/gmp/http/__tests__/http.js +++ b/src/gmp/http/__tests__/http.js @@ -10,10 +10,12 @@ import Rejection from '../rejection'; import {vi} from 'vitest'; const mockGetFeedAccessStatusMessage = testing.fn(); +const mockFindActionInXMLString = testing.fn(); vi.mock('gmp/http/utils', async () => { return { getFeedAccessStatusMessage: () => mockGetFeedAccessStatusMessage(), + findActionInXMLString: () => mockFindActionInXMLString(), }; }); @@ -61,6 +63,7 @@ describe('Http', () => { xhr.status = 404; const additionalMessage = 'Additional feed access status message'; mockGetFeedAccessStatusMessage.mockResolvedValue(additionalMessage); + mockFindActionInXMLString.mockReturnValue(true); await instance.handleResponseError(resolve, reject, xhr, options); expect(mockGetFeedAccessStatusMessage).toHaveBeenCalled(); @@ -69,5 +72,17 @@ describe('Http', () => { const rejectedResponse = reject.mock.calls[0][0]; expect(rejectedResponse.message).toContain(additionalMessage); }); + + test('404 error should not append additional message', async () => { + xhr.status = 404; + mockFindActionInXMLString.mockReturnValue(false); + + await instance.handleResponseError(resolve, reject, xhr, options); + expect(mockGetFeedAccessStatusMessage).not.toHaveBeenCalled(); + + expect(reject).toHaveBeenCalledWith(expect.any(Rejection)); + const rejectedResponse = reject.mock.calls[0][0]; + expect(rejectedResponse.message).toContain('Unknown Error'); + }); }); }); diff --git a/src/gmp/http/__tests__/utils.js b/src/gmp/http/__tests__/utils.js index a67302f51a..daaa0dd6b9 100644 --- a/src/gmp/http/__tests__/utils.js +++ b/src/gmp/http/__tests__/utils.js @@ -7,7 +7,10 @@ import {describe, test, expect} from '@gsa/testing'; import {createResponse, createHttp} from 'gmp/commands/testing'; -import {getFeedAccessStatusMessage} from 'gmp/http/utils'; +import { + getFeedAccessStatusMessage, + findActionInXMLString, +} from 'gmp/http/utils'; import {FeedStatus} from 'gmp/commands/feedstatus'; describe('Http', () => { @@ -47,4 +50,49 @@ describe('Http', () => { }, ); }); + + describe('findActionInXMLString', () => { + test.each([ + { + description: + 'should return true if an action is found in the XML string', + xmlString: ` + + Run Wizard + + `, + actions: ['Run Wizard', 'Create Task', 'Save Task'], + expected: true, + }, + { + description: + 'should return false if no action is found in the XML string', + xmlString: ` + + Delete Task + + `, + actions: ['Run Wizard', 'Create Task', 'Save Task'], + expected: false, + }, + { + description: 'should return false if the XML string is empty', + xmlString: '', + actions: ['Run Wizard', 'Create Task', 'Save Task'], + expected: false, + }, + { + description: 'should return false if the actions array is empty', + xmlString: ` + + Run Wizard + + `, + actions: [], + expected: false, + }, + ])('$description', ({xmlString, actions, expected}) => { + expect(findActionInXMLString(xmlString, actions)).toBe(expected); + }); + }); }); diff --git a/src/gmp/http/http.js b/src/gmp/http/http.js index 9a4dc88dd3..50a23501ea 100644 --- a/src/gmp/http/http.js +++ b/src/gmp/http/http.js @@ -14,7 +14,11 @@ import Response from './response'; import DefaultTransform from './transform/default'; -import {buildUrlParams, getFeedAccessStatusMessage} from './utils'; +import { + buildUrlParams, + getFeedAccessStatusMessage, + findActionInXMLString, +} from './utils'; const log = logger.getLogger('gmp.http'); @@ -177,13 +181,25 @@ class Http { let rejectedResponse = await this.transformRejection(rej, options); - if (rej.status === 404) { + const actionsRequiringFeedAccess = [ + 'Run Wizard', + 'Create Task', + 'Save Task', + 'Create Target', + 'Save Target', + ]; + + if ( + rej.status === 404 && + findActionInXMLString(request.response, actionsRequiringFeedAccess) + ) { const additionalMessage = await getFeedAccessStatusMessage(this); if (additionalMessage) { rejectedResponse.message = `${rejectedResponse.message}\n${additionalMessage}`; } } + reject(rejectedResponse); } catch (error) { log.error('Could not handle response error', error); diff --git a/src/gmp/http/utils.js b/src/gmp/http/utils.js index 136b148697..995d8bcaad 100644 --- a/src/gmp/http/utils.js +++ b/src/gmp/http/utils.js @@ -50,3 +50,11 @@ export async function getFeedAccessStatusMessage(context) { return ''; } + +export const findActionInXMLString = (string, actions) => { + const regex = /(.*?)<\/action>/g; + const matches = string.match(regex) || []; + return matches.some(match => + actions.includes(match.replace(/<\/?action>/g, '')), + ); +};