-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4183 from greenbone/add-enhance-error-feed-for-mi…
…ssing-detials Enhance error message feed for missing details
- Loading branch information
Showing
10 changed files
with
345 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* SPDX-FileCopyrightText: 2024 Greenbone AG | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import {describe, test, expect, testing, beforeEach} from '@gsa/testing'; | ||
|
||
import Http from 'gmp/http/http'; | ||
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(), | ||
}; | ||
}); | ||
|
||
global.XMLHttpRequest = testing.fn(() => ({ | ||
open: testing.fn(), | ||
send: testing.fn(), | ||
setRequestHeader: testing.fn(), | ||
status: 0, | ||
responseText: '', | ||
onreadystatechange: null, | ||
readyState: 0, | ||
})); | ||
|
||
describe('Http', () => { | ||
describe('handleResponseError', () => { | ||
let instance; | ||
let reject; | ||
let resolve; | ||
let xhr; | ||
let options; | ||
|
||
beforeEach(() => { | ||
instance = new Http(); | ||
resolve = testing.fn(); | ||
reject = testing.fn(); | ||
xhr = {status: 500}; | ||
options = {}; | ||
testing.clearAllMocks(); | ||
}); | ||
test('should handle response error without error handlers', async () => { | ||
await instance.handleResponseError(xhr, reject, resolve, options); | ||
expect(reject).toHaveBeenCalledWith(expect.any(Rejection)); | ||
}); | ||
|
||
test('401 error should call error handler', async () => { | ||
xhr.status = 401; | ||
await instance.handleResponseError(resolve, reject, xhr, options); | ||
expect(reject).toHaveBeenCalledWith(expect.any(Rejection)); | ||
expect(reject.mock.calls[0][0].reason).toBe( | ||
Rejection.REASON_UNAUTHORIZED, | ||
); | ||
}); | ||
|
||
test('404 error should append additional message', async () => { | ||
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(); | ||
|
||
expect(reject).toHaveBeenCalledWith(expect.any(Rejection)); | ||
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'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* SPDX-FileCopyrightText: 2024 Greenbone AG | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import {describe, test, expect} from '@gsa/testing'; | ||
|
||
import {createResponse, createHttp} from 'gmp/commands/testing'; | ||
|
||
import { | ||
getFeedAccessStatusMessage, | ||
findActionInXMLString, | ||
} from 'gmp/http/utils'; | ||
import {FeedStatus} from 'gmp/commands/feedstatus'; | ||
|
||
describe('Http', () => { | ||
describe('getFeedAccessStatusMessage', () => { | ||
const setupTest = async (feedOwnerSet, feedResourcesAccess) => { | ||
const response = createResponse({ | ||
get_feeds: { | ||
get_feeds_response: { | ||
feed_owner_set: feedOwnerSet, | ||
feed_resources_access: feedResourcesAccess, | ||
}, | ||
}, | ||
}); | ||
const fakeHttp = createHttp(response); | ||
const feedCmd = new FeedStatus(fakeHttp); | ||
await feedCmd.checkFeedOwnerAndPermissions(); | ||
return getFeedAccessStatusMessage(fakeHttp); | ||
}; | ||
|
||
test.each([ | ||
[ | ||
0, | ||
1, | ||
'The feed owner is currently not set. This issue may be due to the feed not having completed its synchronization.\nPlease try again shortly.', | ||
], | ||
[ | ||
1, | ||
0, | ||
'Access to the feed resources is currently restricted. This issue may be due to the feed not having completed its synchronization.\nPlease try again shortly.', | ||
], | ||
[1, 1, ''], | ||
])( | ||
'should return correct message for feedOwnerSet: %i, feedResourcesAccess: %i', | ||
async (feedOwnerSet, feedResourcesAccess, expectedMessage) => { | ||
const message = await setupTest(feedOwnerSet, feedResourcesAccess); | ||
expect(message).toBe(expectedMessage); | ||
}, | ||
); | ||
}); | ||
|
||
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.