1313
1414from vulnerabilities .models import Alias
1515from vulnerabilities .models import Package
16+ from vulnerabilities .models import PackageRelatedVulnerability
1617from vulnerabilities .models import Vulnerability
1718from vulnerabilities .models import VulnerabilityReference
1819from vulnerabilities .models import VulnerabilityRelatedReference
@@ -25,6 +26,12 @@ def setUp(self):
2526 summary = str (i ),
2627 )
2728 self .vulnerability = Vulnerability .objects .create (summary = "test" )
29+ self .pkg1 = Package .objects .create (name = "flask" , type = "pypi" , version = "0.1.2" )
30+ self .pkg2 = Package .objects .create (name = "flask" , type = "debian" , version = "0.1.2" )
31+ for pkg in [self .pkg1 , self .pkg2 ]:
32+ PackageRelatedVulnerability .objects .create (
33+ package = pkg , vulnerability = self .vulnerability , fix = True
34+ )
2835
2936 def test_api_status (self ):
3037 response = self .client .get ("/api/vulnerabilities/" , format = "json" )
@@ -43,33 +50,76 @@ def test_api_with_single_vulnerability(self):
4350 "vulnerability_id" : f"VULCOID-{ int_to_base36 (self .vulnerability .id ).upper ()} " ,
4451 "summary" : "test" ,
4552 "aliases" : [],
46- "fixed_packages" : [],
53+ "fixed_packages" : [
54+ {
55+ "url" : f"http://testserver/api/packages/{ self .pkg1 .id } " ,
56+ "purl" :
"pkg:pypi/[email protected] " ,
57+ },
58+ {
59+ "url" : f"http://testserver/api/packages/{ self .pkg2 .id } " ,
60+ "purl" :
"pkg:debian/[email protected] " ,
61+ },
62+ ],
63+ "affected_packages" : [],
64+ "references" : [],
65+ }
66+
67+ def test_api_with_single_vulnerability_with_filters (self ):
68+ response = self .client .get (
69+ f"/api/vulnerabilities/{ self .vulnerability .id } ?type=pypi" , format = "json"
70+ ).data
71+ assert response == {
72+ "url" : f"http://testserver/api/vulnerabilities/{ self .vulnerability .id } " ,
73+ "vulnerability_id" : f"VULCOID-{ int_to_base36 (self .vulnerability .id ).upper ()} " ,
74+ "summary" : "test" ,
75+ "aliases" : [],
76+ "fixed_packages" : [
77+ {
78+ "url" : f"http://testserver/api/packages/{ self .pkg1 .id } " ,
79+ "purl" :
"pkg:pypi/[email protected] " ,
80+ },
81+ ],
4782 "affected_packages" : [],
4883 "references" : [],
4984 }
5085
5186
5287class APITestCasePackage (TestCase ):
5388 def setUp (self ):
89+ vuln = Vulnerability .objects .create (
90+ summary = "test-vuln" ,
91+ )
92+ self .vuln = vuln
5493 for i in range (0 , 10 ):
5594 query_kwargs = dict (
5695 type = "generic" ,
5796 namespace = "nginx" ,
58- name = f "test- { i } " ,
97+ name = "test" ,
5998 version = str (i ),
6099 qualifiers = {},
61100 subpath = "" ,
62101 )
63- Package .objects .create (** query_kwargs )
102+ vuln_package = Package .objects .create (** query_kwargs )
103+ PackageRelatedVulnerability .objects .create (
104+ package = vuln_package ,
105+ vulnerability = vuln ,
106+ fix = False ,
107+ )
108+ self .vuln_package = vuln_package
64109 query_kwargs = dict (
65110 type = "generic" ,
66111 namespace = "nginx" ,
67- name = "test-vulnDB " ,
68- version = "1.0 " ,
112+ name = "test" ,
113+ version = "11 " ,
69114 qualifiers = {},
70115 subpath = "" ,
71116 )
72117 self .package = Package .objects .create (** query_kwargs )
118+ PackageRelatedVulnerability .objects .create (
119+ package = self .package ,
120+ vulnerability = vuln ,
121+ fix = True ,
122+ )
73123
74124 def test_api_status (self ):
75125 response = self .client .get ("/api/packages/" , format = "json" )
@@ -79,19 +129,80 @@ def test_api_response(self):
79129 response = self .client .get ("/api/packages/" , format = "json" ).data
80130 self .assertEqual (response ["count" ], 11 )
81131
82- def test_api_with_single_vulnerability (self ):
132+ def test_api_with_single_vulnerability_and_fixed_package (self ):
83133 response = self .client .get (f"/api/packages/{ self .package .id } " , format = "json" ).data
84134 assert response == {
85135 "url" : f"http://testserver/api/packages/{ self .package .id } " ,
86- "purl" :
"pkg:generic/nginx/test[email protected] " ,
136+ "purl" : "pkg:generic/nginx/test@11 " ,
87137 "type" : "generic" ,
88138 "namespace" : "nginx" ,
89- "name" : "test-vulnDB " ,
90- "version" : "1.0 " ,
139+ "name" : "test" ,
140+ "version" : "11 " ,
91141 "unresolved_vulnerabilities" : [],
92142 "qualifiers" : {},
93143 "subpath" : "" ,
144+ "fixed_packages" : [
145+ {
146+ "url" : f"http://testserver/api/packages/{ self .package .id } " ,
147+ "purl" : "pkg:generic/nginx/test@11" ,
148+ "fixing_vulnerabilities" : [
149+ {
150+ "url" : f"http://testserver/api/vulnerabilities/{ self .vuln .id } " ,
151+ "vulnerability_id" : f"VULCOID-{ int_to_base36 (self .vuln .id ).upper ()} " ,
152+ }
153+ ],
154+ }
155+ ],
94156 "affected_by_vulnerabilities" : [],
157+ "fixing_vulnerabilities" : [
158+ {
159+ "url" : f"http://testserver/api/vulnerabilities/{ self .vuln .id } " ,
160+ "vulnerability_id" : f"VULCOID-{ int_to_base36 (self .vuln .id ).upper ()} " ,
161+ "summary" : "test-vuln" ,
162+ "references" : [],
163+ }
164+ ],
165+ }
166+
167+ def test_api_with_single_vulnerability_and_vulnerable_package (self ):
168+ response = self .client .get (f"/api/packages/{ self .vuln_package .id } " , format = "json" ).data
169+ assert response == {
170+ "url" : f"http://testserver/api/packages/{ self .vuln_package .id } " ,
171+ "purl" : "pkg:generic/nginx/test@9" ,
172+ "type" : "generic" ,
173+ "namespace" : "nginx" ,
174+ "name" : "test" ,
175+ "version" : "9" ,
176+ "unresolved_vulnerabilities" : [
177+ {
178+ "url" : f"http://testserver/api/vulnerabilities/{ self .vuln .id } " ,
179+ "vulnerability_id" : f"VULCOID-{ int_to_base36 (self .vuln .id ).upper ()} " ,
180+ "summary" : "test-vuln" ,
181+ "references" : [],
182+ }
183+ ],
184+ "qualifiers" : {},
185+ "subpath" : "" ,
186+ "fixed_packages" : [
187+ {
188+ "url" : f"http://testserver/api/packages/{ self .package .id } " ,
189+ "purl" : "pkg:generic/nginx/test@11" ,
190+ "fixing_vulnerabilities" : [
191+ {
192+ "url" : f"http://testserver/api/vulnerabilities/{ self .vuln .id } " ,
193+ "vulnerability_id" : f"VULCOID-{ int_to_base36 (self .vuln .id ).upper ()} " ,
194+ }
195+ ],
196+ }
197+ ],
198+ "affected_by_vulnerabilities" : [
199+ {
200+ "url" : f"http://testserver/api/vulnerabilities/{ self .vuln .id } " ,
201+ "vulnerability_id" : f"VULCOID-{ int_to_base36 (self .vuln .id ).upper ()} " ,
202+ "summary" : "test-vuln" ,
203+ "references" : [],
204+ }
205+ ],
95206 "fixing_vulnerabilities" : [],
96207 }
97208
0 commit comments