@@ -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
0 commit comments