@@ -82,19 +82,74 @@ def test_vulnerability_mixin_create_vulnerabilities(self):
8282 response_file = self .data / "vulnerabilities" / "idna_3.6_response.json"
8383 response_json = json .loads (response_file .read_text ())
8484 vulnerabilities_data = response_json ["results" ][0 ]["affected_by_vulnerabilities" ]
85+ vulnerabilities_data .append ({"vulnerability_id" : "VCID-0002" , "risk_score" : 5.0 })
8586
8687 package1 = make_package (
self .
dataspace ,
package_url = "pkg:pypi/[email protected] " )
8788 product1 = make_product (self .dataspace , inventory = [package1 ])
8889 package1 .create_vulnerabilities (vulnerabilities_data )
8990
90- self .assertEqual (1 , Vulnerability .objects .scope (self .dataspace ).count ())
91- self .assertEqual (1 , package1 .affected_by_vulnerabilities .count ())
92- vulnerability = package1 .affected_by_vulnerabilities .get ()
93- self .assertEqual ("VCID-j3au-usaz-aaag" , vulnerability .vulnerability_id )
94-
95- self .assertEqual (8.4 , package1 .risk_score )
91+ self .assertEqual (2 , Vulnerability .objects .scope (self .dataspace ).count ())
92+ self .assertEqual ("8.4" , str (package1 .risk_score ))
9693 self .assertEqual ("8.4" , str (product1 .productpackages .get ().weighted_risk_score ))
9794
95+ def test_vulnerability_mixin_update_risk_score (self ):
96+ package1 = make_package (self .dataspace )
97+
98+ # Test with no vulnerabilities
99+ package1 .update_risk_score ()
100+ self .assertIsNone (package1 .risk_score )
101+
102+ # Test with one vulnerability with risk score
103+ vulnerability1 = make_vulnerability (dataspace = self .dataspace , risk_score = 7.5 )
104+ vulnerability1 .add_affected (package1 )
105+ package1 .update_risk_score ()
106+ self .assertEqual ("7.5" , str (package1 .risk_score ))
107+
108+ # Test with multiple vulnerabilities, should use max
109+ vulnerability2 = make_vulnerability (dataspace = self .dataspace , risk_score = 9.2 )
110+ vulnerability2 .add_affected (package1 )
111+ package1 .update_risk_score ()
112+ self .assertEqual ("9.2" , str (package1 .risk_score ))
113+
114+ # Test with vulnerability with lower risk score, should keep max
115+ vulnerability3 = make_vulnerability (dataspace = self .dataspace , risk_score = 3.1 )
116+ vulnerability3 .add_affected (package1 )
117+ package1 .update_risk_score ()
118+ self .assertEqual ("9.2" , str (package1 .risk_score ))
119+
120+ # Test with all vulnerabilities having NULL risk scores
121+ package2 = make_package (self .dataspace )
122+ vulnerability4 = make_vulnerability (dataspace = self .dataspace , risk_score = None )
123+ vulnerability5 = make_vulnerability (dataspace = self .dataspace , risk_score = None )
124+ vulnerability4 .add_affected (package2 )
125+ vulnerability5 .add_affected (package2 )
126+ package2 .update_risk_score ()
127+ self .assertIsNone (package2 .risk_score )
128+
129+ def test_vulnerability_mixin_add_affected_by (self ):
130+ package1 = make_package (self .dataspace )
131+
132+ vulnerability1 = make_vulnerability (self .dataspace , risk_score = 1.0 )
133+ vulnerability2 = make_vulnerability (self .dataspace , risk_score = 10.0 )
134+ vulnerability3 = make_vulnerability (self .dataspace , risk_score = 5.0 )
135+
136+ package1 .add_affected_by (vulnerability1 )
137+ package1 .refresh_from_db ()
138+ self .assertEqual ("1.0" , str (package1 .risk_score ))
139+
140+ package1 .add_affected_by (vulnerability2 )
141+ package1 .refresh_from_db ()
142+ self .assertEqual ("10.0" , str (package1 .risk_score ))
143+
144+ package1 .add_affected_by (vulnerability3 )
145+ package1 .refresh_from_db ()
146+ self .assertEqual ("10.0" , str (package1 .risk_score ))
147+
148+ self .assertEqual (package1 , vulnerability1 .affected_packages .get ())
149+ self .assertEqual (package1 , vulnerability2 .affected_packages .get ())
150+ self .assertEqual (package1 , vulnerability3 .affected_packages .get ())
151+ self .assertEqual (3 , package1 .affected_by_vulnerabilities .count ())
152+
98153 def test_vulnerability_model_affected_packages_m2m (self ):
99154 package1 = make_package (self .dataspace )
100155 vulnerability1 = make_vulnerability (dataspace = self .dataspace , affecting = package1 )
0 commit comments