1313
1414import json
1515import logging
16+ import os
1617import unittest
1718
1819import jsonschema
1920from jsonschema import Draft202012Validator
2021from referencing import Registry , Resource
21- import os
2222
2323import ssvc .decision_points # noqa F401
2424from ssvc .decision_points .base import REGISTERED_DECISION_POINTS
25-
2625# importing these causes the decision points to register themselves
2726from ssvc .decision_points .critical_software import CRITICAL_SOFTWARE_1 # noqa
2827from ssvc .decision_points .high_value_asset import HIGH_VALUE_ASSET_1 # noqa
2928from ssvc .decision_points .in_kev import IN_KEV_1
3029from ssvc .dp_groups .cvss .collections import CVSSv1 , CVSSv2 , CVSSv3 , CVSSv4 # noqa
31-
3230# importing these causes the decision points to register themselves
3331from ssvc .dp_groups .ssvc .collections import SSVCv1 , SSVCv2 , SSVCv2_1 # noqa
3432
35- def retrieve_local (uri ):
36- fileuri = uri .replace ("https://certcc.github.io/SSVC" , os .getcwd ())
37- if os .path .exists (fileuri ):
38- fh = open (fileuri )
33+
34+ def retrieve_local (uri : str ) -> Resource :
35+ # retrieve_local gets called anytime we're trying to get a schema.
36+ # Because our schemas refer to each other by https: uris, we need this function
37+ # to load the schema from a local file instead of trying to download it from the internet
38+
39+ # here we compute the path to the data directory where the schemas are stored
40+ my_file_path = os .path .abspath (__file__ )
41+ my_dir = os .path .dirname (my_file_path )
42+ data_path = os .path .join (my_dir , ".." , ".." , "data" )
43+ data_path = os .path .abspath (data_path )
44+
45+ fileuri = uri .replace ("https://certcc.github.io/SSVC/data" , data_path )
46+
47+ with open (fileuri ) as fh :
3948 schema = json .load (fh )
40- fh .close ()
41- return Resource .from_contents (schema )
42- raise FileNotFoundError (f"Could not find DEBUG path issues { fileuri } " )
49+ return Resource .from_contents (schema )
50+
4351
4452registry = Registry (retrieve = retrieve_local )
4553
@@ -79,7 +87,9 @@ def test_decision_point_validation(self):
7987 loaded = json .loads (as_json )
8088
8189 try :
82- Draft202012Validator ({"$ref" : schema_url }, registry = registry ).validate (loaded )
90+ Draft202012Validator ({"$ref" : schema_url }, registry = registry ).validate (
91+ loaded
92+ )
8393 except jsonschema .exceptions .ValidationError as e :
8494 exp = e
8595
@@ -96,12 +106,16 @@ def test_decision_point_group_validation(self):
96106 loaded = json .loads (as_json )
97107
98108 try :
99- Draft202012Validator ({"$ref" : schema_url },registry = registry ).validate (loaded )
109+ Draft202012Validator ({"$ref" : schema_url }, registry = registry ).validate (
110+ loaded
111+ )
100112 except jsonschema .exceptions .ValidationError as e :
101113 exp = e
102114
103115 self .assertIsNone (exp , f"Validation failed for { dpg .name } { dpg .version } " )
104- self .logger .debug (f"Validation passed for Decision Point Group { dpg .name } v{ dpg .version } " )
116+ self .logger .debug (
117+ f"Validation passed for Decision Point Group { dpg .name } v{ dpg .version } "
118+ )
105119
106120
107121if __name__ == "__main__" :
0 commit comments