-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: tests enhanced reject error for missing feed details
- Loading branch information
1 parent
3988553
commit 2d64a3f
Showing
6 changed files
with
188 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* 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(); | ||
|
||
vi.mock('gmp/http/utils', async () => { | ||
return { | ||
getFeedAccessStatusMessage: () => mockGetFeedAccessStatusMessage(), | ||
}; | ||
}); | ||
|
||
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); | ||
|
||
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); | ||
}); | ||
}); | ||
}); |
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,50 @@ | ||
/* 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} 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); | ||
}, | ||
); | ||
}); | ||
}); |
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