-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle erroneous requests and add tests for this for bulk api
Signed-off-by: Shivam Sandbhor <[email protected]>
- Loading branch information
Showing
2 changed files
with
84 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -239,7 +239,9 @@ def test_bulk_vulnerabilities_api(self): | |
}, | ||
"RANDOM-CVE": {}, | ||
} | ||
response = self.client.post("/api/vulnerabilities/bulk_search/", request_body).data | ||
response = self.client.post( | ||
"/api/vulnerabilities/bulk_search/", data=request_body, content_type="application/json" | ||
).data | ||
assert response == expected_response | ||
|
||
def test_bulk_packages_api(self): | ||
|
@@ -249,7 +251,9 @@ def test_bulk_packages_api(self): | |
"pkg:deb/debian/[email protected]?distro=jessie", | ||
] | ||
} | ||
response = self.client.post("/api/packages/bulk_search/", request_body).data | ||
response = self.client.post( | ||
"/api/packages/bulk_search/", data=request_body, content_type="application/json" | ||
).data | ||
expected_response = { | ||
"pkg:deb/debian/[email protected]?distro=jessie": { | ||
"url": "http://testserver/api/packages/1/", | ||
|
@@ -298,3 +302,60 @@ def test_bulk_packages_api(self): | |
} | ||
|
||
assert response == expected_response | ||
|
||
def test_invalid_request_bulk_packages(self): | ||
error_response = { | ||
"Error": "Request needs to contain a key 'packages' which has the value of a list of package urls" # nopep8 | ||
} | ||
invalid_key_request_data = {"pkg": []} | ||
response = self.client.post( | ||
"/api/packages/bulk_search/", | ||
data=invalid_key_request_data, | ||
content_type="application/json", | ||
).data | ||
assert response == error_response | ||
|
||
valid_key_invalid_datatype_request_data = {"packages": {}} | ||
response = self.client.post( | ||
"/api/packages/bulk_search/", | ||
data=valid_key_invalid_datatype_request_data, | ||
content_type="application/json", | ||
).data | ||
assert response == error_response | ||
|
||
invalid_purl_request_data = { | ||
"packages": [ | ||
"pkg:deb/debian/[email protected]?distro=jessie", | ||
"pg:deb/debian/[email protected]?distro=jessie", | ||
] | ||
} | ||
response = self.client.post( | ||
"/api/packages/bulk_search/", | ||
data=invalid_purl_request_data, | ||
content_type="application/json", | ||
).data | ||
purl_error_respones = { | ||
"Error": "purl is missing the required \"pkg\" scheme component: 'pg:deb/debian/[email protected]?distro=jessie'." # nopep8 | ||
} | ||
assert response == purl_error_respones | ||
|
||
def test_invalid_request_bulk_vulnerabilities(self): | ||
error_response = { | ||
"Error": "Request needs to contain a key 'vulnerabilities' which has the value of a list of vulnerability ids" # nopep8 | ||
} | ||
|
||
wrong_key_data = {"xyz": []} | ||
response = self.client.post( | ||
"/api/vulnerabilities/bulk_search/", | ||
data=wrong_key_data, | ||
content_type="application/json", | ||
).data | ||
assert response == error_response | ||
|
||
wrong_type_data = {"vulnerabilities": {}} | ||
response = self.client.post( | ||
"/api/vulnerabilities/bulk_search/", | ||
data=wrong_key_data, | ||
content_type="application/json", | ||
).data | ||
assert response == error_response |