Skip to content

Commit 15868de

Browse files
committed
Fix Live Evaluation API tests #1953
Signed-off-by: Michael Ehab Mikhail <michael.ehab@hotmail.com>
1 parent 5d4312b commit 15868de

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

vulnerabilities/api_v2.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from vulnerabilities.models import Weakness
4545
from vulnerabilities.tasks import enqueue_ad_hoc_pipeline
4646
from vulnerabilities.throttling import PermissionBasedUserRateThrottle
47+
from vulnerablecode.settings import VULNERABLECODE_ENABLE_LIVE_EVALUATION_API
4748

4849

4950
class CharInFilter(filters.BaseInFilter, filters.CharFilter):
@@ -1316,6 +1317,11 @@ class LiveEvaluationViewSet(viewsets.GenericViewSet):
13161317
)
13171318
@action(detail=False, methods=["post"])
13181319
def evaluate(self, request):
1320+
if not VULNERABLECODE_ENABLE_LIVE_EVALUATION_API:
1321+
return Response(
1322+
{"error": "Live evaluation API is disabled."},
1323+
status=status.HTTP_403_FORBIDDEN,
1324+
)
13191325
serializer = self.get_serializer(data=request.data)
13201326
if not serializer.is_valid():
13211327
return Response(
@@ -1385,6 +1391,12 @@ def evaluate(self, request):
13851391
)
13861392
@action(detail=False, methods=["get"], url_path=r"status/(?P<live_run_id>[0-9a-f\-]{36})")
13871393
def status(self, request, live_run_id=None):
1394+
if not VULNERABLECODE_ENABLE_LIVE_EVALUATION_API:
1395+
return Response(
1396+
{"error": "Live evaluation API is disabled."},
1397+
status=status.HTTP_403_FORBIDDEN,
1398+
)
1399+
13881400
from vulnerabilities.models import LivePipelineRun
13891401

13901402
try:

vulnerabilities/tests/test_api_v2.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,7 @@ def setUp(self):
912912
self.client = APIClient(enforce_csrf_checks=True)
913913
self.url = "/api/v2/live-evaluation/evaluate"
914914

915+
@patch("vulnerabilities.api_v2.VULNERABLECODE_ENABLE_LIVE_EVALUATION_API", True)
915916
@patch("vulnerabilities.api_v2.LIVE_IMPORTERS_REGISTRY")
916917
@patch("vulnerabilities.api_v2.enqueue_ad_hoc_pipeline")
917918
@patch("django.urls.reverse")
@@ -920,7 +921,6 @@ class MockImporter:
920921
pipeline_id = "pypa_live_importer_v2"
921922
supported_types = ["pypi"]
922923

923-
os.environ["VULNERABLECODE_ENABLE_LIVE_EVALUATION_API"] = "true"
924924
mock_registry.values.return_value = [MockImporter]
925925
valid_uuid = "00000000-0000-0000-0000-000000000001"
926926
mock_enqueue.return_value = (valid_uuid, ["mock-run-id"])
@@ -936,30 +936,36 @@ class MockImporter:
936936
assert "status_url" in response.data
937937
assert response.data["status_url"].endswith(f"/api/v2/live-evaluation/status/{valid_uuid}")
938938

939+
@patch("vulnerabilities.api_v2.VULNERABLECODE_ENABLE_LIVE_EVALUATION_API", True)
939940
@patch("vulnerabilities.api_v2.LIVE_IMPORTERS_REGISTRY")
940941
def test_evaluate_no_importer_found(self, mock_registry):
941942
class MockImporter:
942943
pipeline_id = "dummy"
943944
supported_types = ["npm"]
944945

945-
os.environ["VULNERABLECODE_ENABLE_LIVE_EVALUATION_API"] = "true"
946946
mock_registry.values.return_value = [MockImporter]
947947
data = {"purl": "pkg:pypi/django@3.2"}
948948
response = self.client.post(self.url, data, format="json")
949949
assert response.status_code == 400
950950
assert "No live importers found" in response.data["error"]
951951

952+
@patch("vulnerabilities.api_v2.VULNERABLECODE_ENABLE_LIVE_EVALUATION_API", True)
952953
def test_evaluate_invalid_purl(self):
953-
os.environ["VULNERABLECODE_ENABLE_LIVE_EVALUATION_API"] = "true"
954954
data = {"purl": "not_a_valid_purl"}
955955
response = self.client.post(self.url, data, format="json")
956956
assert response.status_code == 400
957957
assert "Invalid PackageURL" in response.data["error"]
958958

959+
@patch("vulnerabilities.api_v2.VULNERABLECODE_ENABLE_LIVE_EVALUATION_API", True)
959960
@patch("vulnerabilities.models.LivePipelineRun.objects.get")
960961
def test_status_not_found(self, mock_live_get):
961-
os.environ["VULNERABLECODE_ENABLE_LIVE_EVALUATION_API"] = "true"
962962
mock_live_get.side_effect = LivePipelineRun.DoesNotExist()
963963
url = "/api/v2/live-evaluation/status/00000000-0000-0000-0000-000000000000"
964964
response = self.client.get(url)
965965
assert response.status_code == 404
966+
967+
@patch("vulnerabilities.api_v2.VULNERABLECODE_ENABLE_LIVE_EVALUATION_API", False)
968+
def test_evaluate_disabled_returns_403(self):
969+
data = {"purl": "pkg:pypi/django@3.2"}
970+
response = self.client.post(self.url, data, format="json")
971+
assert response.status_code == 403

vulnerablecode/urls.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
from vulnerabilities.views import VulnerabilitySearch
4747
from vulnerablecode.settings import DEBUG
4848
from vulnerablecode.settings import DEBUG_TOOLBAR
49-
from vulnerablecode.settings import VULNERABLECODE_ENABLE_LIVE_EVALUATION_API
5049

5150

5251
# See the comment at https://stackoverflow.com/a/46163870.
@@ -73,8 +72,7 @@ def __init__(self, *args, **kwargs):
7372
api_v2_router.register("pipelines", PipelineScheduleV2ViewSet, basename="pipelines")
7473
api_v2_router.register("advisory-codefixes", CodeFixV2ViewSet, basename="advisory-codefix")
7574

76-
if VULNERABLECODE_ENABLE_LIVE_EVALUATION_API:
77-
api_v2_router.register("live-evaluation", LiveEvaluationViewSet, basename="live-evaluation")
75+
api_v2_router.register("live-evaluation", LiveEvaluationViewSet, basename="live-evaluation")
7876

7977

8078
urlpatterns = [

0 commit comments

Comments
 (0)