Skip to content

Commit

Permalink
add: make conditions more specific
Browse files Browse the repository at this point in the history
  • Loading branch information
daniele-mng committed Oct 15, 2024
1 parent 2d64a3f commit d872a7e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/gmp/http/__tests__/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};
});

Expand Down Expand Up @@ -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();
Expand All @@ -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');
});
});
});
50 changes: 49 additions & 1 deletion src/gmp/http/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -47,4 +50,49 @@ describe('Http', () => {
},
);
});

describe('findActionInXMLString', () => {
test.each([
{
description:
'should return true if an action is found in the XML string',
xmlString: `
<response>
<action>Run Wizard</action>
</response>
`,
actions: ['Run Wizard', 'Create Task', 'Save Task'],
expected: true,
},
{
description:
'should return false if no action is found in the XML string',
xmlString: `
<response>
<action>Delete Task</action>
</response>
`,
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: `
<response>
<action>Run Wizard</action>
</response>
`,
actions: [],
expected: false,
},
])('$description', ({xmlString, actions, expected}) => {
expect(findActionInXMLString(xmlString, actions)).toBe(expected);
});
});
});
20 changes: 18 additions & 2 deletions src/gmp/http/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/gmp/http/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,11 @@ export async function getFeedAccessStatusMessage(context) {

return '';
}

export const findActionInXMLString = (string, actions) => {
const regex = /<action>(.*?)<\/action>/g;
const matches = string.match(regex) || [];
return matches.some(match =>
actions.includes(match.replace(/<\/?action>/g, '')),
);
};

0 comments on commit d872a7e

Please sign in to comment.