|
22 | 22 | # Visit https://github.com/nexB/vulnerablecode/ for support and download. |
23 | 23 |
|
24 | 24 | import os |
| 25 | +from collections import OrderedDict |
25 | 26 | from random import choices |
26 | 27 | from unittest.mock import MagicMock |
27 | 28 | from urllib.parse import quote |
|
31 | 32 |
|
32 | 33 | from vulnerabilities.api import PackageSerializer |
33 | 34 | from vulnerabilities.models import Package |
| 35 | +from rest_framework.test import APIRequestFactory |
| 36 | +from rest_framework.test import APIClient |
34 | 37 |
|
35 | 38 |
|
36 | 39 | BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
@@ -188,3 +191,148 @@ def test_package_serializer(self): |
188 | 191 | purls = {r["purl"] for r in response} |
189 | 192 | self.assertIn("pkg:deb/debian/mimetex@1.50-1.1?distro=jessie", purls) |
190 | 193 | self.assertIn("pkg:deb/debian/mimetex@1.74-1?distro=jessie", purls) |
| 194 | + |
| 195 | + |
| 196 | +class TestBulkAPIResponse(TestCase): |
| 197 | + fixtures = ["debian.json"] |
| 198 | + |
| 199 | + def test_bulk_vulnerabilities_api(self): |
| 200 | + request_body = {"vulnerabilities": ["CVE-2009-1382", "CVE-2014-8242", "RANDOM-CVE"]} |
| 201 | + expected_response = { |
| 202 | + "CVE-2009-1382": { |
| 203 | + "resolved_packages": [ |
| 204 | + OrderedDict( |
| 205 | + [ |
| 206 | + ("url", "http://testserver/api/packages/2/"), |
| 207 | + ("purl", "pkg:deb/debian/mimetex@1.74-1?distro=jessie"), |
| 208 | + ] |
| 209 | + ), |
| 210 | + OrderedDict( |
| 211 | + [ |
| 212 | + ("url", "http://testserver/api/packages/3/"), |
| 213 | + ("purl", "pkg:deb/debian/mimetex@1.50-1.1?distro=jessie"), |
| 214 | + ] |
| 215 | + ), |
| 216 | + ], |
| 217 | + "unresolved_packages": [], |
| 218 | + "url": "http://testserver/api/vulnerabilities/2/", |
| 219 | + }, |
| 220 | + "CVE-2014-8242": { |
| 221 | + "resolved_packages": [], |
| 222 | + "unresolved_packages": [ |
| 223 | + OrderedDict( |
| 224 | + [ |
| 225 | + ("url", "http://testserver/api/packages/1/"), |
| 226 | + ("purl", "pkg:deb/debian/librsync@0.9.7-10?distro=jessie"), |
| 227 | + ] |
| 228 | + ) |
| 229 | + ], |
| 230 | + "url": "http://testserver/api/vulnerabilities/1/", |
| 231 | + }, |
| 232 | + "RANDOM-CVE": {}, |
| 233 | + } |
| 234 | + |
| 235 | + response = self.client.post( |
| 236 | + "/api/vulnerabilities/bulk_search/", data=request_body, content_type="application/json" |
| 237 | + ).data |
| 238 | + assert response == expected_response |
| 239 | + |
| 240 | + def test_bulk_packages_api(self): |
| 241 | + request_body = { |
| 242 | + "packages": [ |
| 243 | + "pkg:deb/debian/librsync@0.9.7-10?distro=jessie", |
| 244 | + "pkg:deb/debian/mimetex@1.50-1.1?distro=jessie", |
| 245 | + ] |
| 246 | + } |
| 247 | + response = self.client.post( |
| 248 | + "/api/packages/bulk_search/", data=request_body, content_type="application/json" |
| 249 | + ).data |
| 250 | + expected_response = { |
| 251 | + "pkg:deb/debian/librsync@0.9.7-10?distro=jessie": { |
| 252 | + "resolved_vulnerabilities": [], |
| 253 | + "unresolved_vulnerabilities": [ |
| 254 | + OrderedDict( |
| 255 | + [ |
| 256 | + ("url", "http://testserver/api/vulnerabilities/1/"), |
| 257 | + ("vulnerability_id", "CVE-2014-8242"), |
| 258 | + ] |
| 259 | + ) |
| 260 | + ], |
| 261 | + }, |
| 262 | + "pkg:deb/debian/mimetex@1.50-1.1?distro=jessie": { |
| 263 | + "resolved_vulnerabilities": [ |
| 264 | + OrderedDict( |
| 265 | + [ |
| 266 | + ("url", "http://testserver/api/vulnerabilities/2/"), |
| 267 | + ("vulnerability_id", "CVE-2009-1382"), |
| 268 | + ] |
| 269 | + ), |
| 270 | + OrderedDict( |
| 271 | + [ |
| 272 | + ("url", "http://testserver/api/vulnerabilities/3/"), |
| 273 | + ("vulnerability_id", "CVE-2009-2459"), |
| 274 | + ] |
| 275 | + ), |
| 276 | + ], |
| 277 | + "unresolved_vulnerabilities": [], |
| 278 | + }, |
| 279 | + } |
| 280 | + |
| 281 | + assert response == expected_response |
| 282 | + |
| 283 | + def test_invalid_request_bulk_packages(self): |
| 284 | + error_response = { |
| 285 | + "Error": "Request needs to contain a key 'packages' which has the value of a list of package urls" # nopep8 |
| 286 | + } |
| 287 | + invalid_key_request_data = {"pkg": []} |
| 288 | + response = self.client.post( |
| 289 | + "/api/packages/bulk_search/", |
| 290 | + data=invalid_key_request_data, |
| 291 | + content_type="application/json", |
| 292 | + ).data |
| 293 | + assert response == error_response |
| 294 | + |
| 295 | + valid_key_invalid_datatype_request_data = {"packages": {}} |
| 296 | + response = self.client.post( |
| 297 | + "/api/packages/bulk_search/", |
| 298 | + data=valid_key_invalid_datatype_request_data, |
| 299 | + content_type="application/json", |
| 300 | + ).data |
| 301 | + assert response == error_response |
| 302 | + |
| 303 | + invalid_purl_request_data = { |
| 304 | + "packages": [ |
| 305 | + "pkg:deb/debian/librsync@0.9.7-10?distro=jessie", |
| 306 | + "pg:deb/debian/mimetex@1.50-1.1?distro=jessie", |
| 307 | + ] |
| 308 | + } |
| 309 | + response = self.client.post( |
| 310 | + "/api/packages/bulk_search/", |
| 311 | + data=invalid_purl_request_data, |
| 312 | + content_type="application/json", |
| 313 | + ).data |
| 314 | + purl_error_respones = { |
| 315 | + "Error": "purl is missing the required \"pkg\" scheme component: 'pg:deb/debian/mimetex@1.50-1.1?distro=jessie'." # nopep8 |
| 316 | + } |
| 317 | + assert response == purl_error_respones |
| 318 | + |
| 319 | + def test_invalid_request_bulk_vulnerabilities(self): |
| 320 | + error_response = { |
| 321 | + "Error": "Request needs to contain a key 'vulnerabilities' which has the value of a list of vulnerability ids" # nopep8 |
| 322 | + } |
| 323 | + |
| 324 | + wrong_key_data = {"xyz": []} |
| 325 | + response = self.client.post( |
| 326 | + "/api/vulnerabilities/bulk_search/", |
| 327 | + data=wrong_key_data, |
| 328 | + content_type="application/json", |
| 329 | + ).data |
| 330 | + assert response == error_response |
| 331 | + |
| 332 | + wrong_type_data = {"vulnerabilities": {}} |
| 333 | + response = self.client.post( |
| 334 | + "/api/vulnerabilities/bulk_search/", |
| 335 | + data=wrong_key_data, |
| 336 | + content_type="application/json", |
| 337 | + ).data |
| 338 | + assert response == error_response |
0 commit comments