File tree Expand file tree Collapse file tree 4 files changed +49
-4
lines changed
Expand file tree Collapse file tree 4 files changed +49
-4
lines changed Original file line number Diff line number Diff line change 4040 "evaluate" : true
4141 }
4242 }
43+ },
44+ {
45+ "id" : " JSON_P12" ,
46+ "title" : " JSON Custom profile" ,
47+ "rules" : {
48+ "R3" : {
49+ "evaluate" : true
50+ }
51+ }
52+ },
53+ {
54+ "title" : " JSON Custom profile with no given id" ,
55+ "base_profile_id" : " P1" ,
56+ "rules" : {
57+ "R3" : {
58+ "evaluate" : true
59+ }
60+ }
4361 }
4462 ]
4563}
Original file line number Diff line number Diff line change 1+ {
2+ "profiles" : [
3+ {
4+ "title" : " JSON Tailored Profile P11" ,
5+ "rules" : {
6+ "R3" : {
7+ "evaluate" : true
8+ }
9+ }
10+ }
11+ ]
12+ }
Original file line number Diff line number Diff line change 11import importlib
2+ import json
3+ import os .path
4+ import pathlib
5+
26import pytest
37
48NS = "http://checklists.nist.gov/xccdf/1.2"
@@ -94,3 +98,12 @@ def test_refine_rule():
9498 "'high'." )
9599 assert p .rule_refinements (fav , "severity" ) == "high"
96100 assert p .rule_refinements (fav , "role" ) == "full"
101+
102+ def test_no_id ():
103+ p = autotailor .Profile ()
104+ profile_dict = None
105+ file_path = pathlib .Path (__file__ ).parent .joinpath ("custom_no_ids.json" )
106+ with open (file_path ) as fp :
107+ profile_dict = json .load (fp )
108+ with pytest .raises (ValueError ):
109+ p .import_json_tailoring_profile (profile_dict )
Original file line number Diff line number Diff line change @@ -268,7 +268,8 @@ class Profile:
268268 def to_xml (self , root ):
269269 profile = ET .SubElement (root , "{%s}Profile" % NS )
270270 profile .set ("id" , self ._full_profile_id (self .profile_id ))
271- profile .set ("extends" , self ._full_profile_id (self .extends ))
271+ if self .extends :
272+ profile .set ("extends" , self ._full_profile_id (self .extends ))
272273
273274 # Title has to be there due to the schema definition.
274275 title = ET .SubElement (profile , "{%s}title" % NS )
@@ -285,9 +286,10 @@ class Profile:
285286 self .value_refinements_to_xml (profile )
286287
287288 def import_json_tailoring_profile (self , profile_dict ):
288- self .extends = profile_dict ["base_profile_id" ]
289-
290- self .profile_id = profile_dict .get ("id" , self .profile_id )
289+ if not profile_dict .get ("base_profile_id" ) and not profile_dict .get ("id" ):
290+ raise ValueError ("You must define a base_profile_id or an id" )
291+ self .extends = profile_dict .get ("base_profile_id" )
292+ self .profile_id = profile_dict .get ("id" )
291293 self .profile_title = profile_dict .get ("title" , self .profile_title )
292294
293295 self ._import_groups_from_tailoring (profile_dict )
You can’t perform that action at this time.
0 commit comments