Skip to content

Commit ad7005b

Browse files
authored
Merge pull request #6 from Mastercard/feature/target_node_issue
Node cleanup issue
2 parents d714132 + 1e889e6 commit ad7005b

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

client_encryption/field_level_encryption.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def decrypt_payload(payload, config, _params=None):
7171
else:
7272
params = _params
7373

74-
cleanup_node(json_payload, elem)
74+
cleanup_node(json_payload, elem, target)
7575

7676
try:
7777
update_node(json_payload, target, _decrypt_bytes(params.key, params.iv_spec, cipher_text))

client_encryption/json_path_utils.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,20 @@ def pop_node(tree, path):
6969
return node
7070

7171

72-
def cleanup_node(tree, path):
73-
"""Remove a node if no child is found give a path"""
72+
def cleanup_node(tree, path, target):
73+
"""Remove a node if not in target path and no child is found given a path"""
7474

7575
if path and __not_root(path):
76-
parent = path.split(_SEPARATOR)
77-
to_delete = parent.pop()
78-
if parent:
79-
node = __get_node(tree, parent, False)
80-
else:
81-
node = tree
82-
83-
if not node[to_delete]:
84-
del node[to_delete]
76+
if not (target and target.startswith(path)):
77+
parent = path.split(_SEPARATOR)
78+
to_delete = parent.pop()
79+
if parent:
80+
node = __get_node(tree, parent, False)
81+
else:
82+
node = tree
83+
84+
if not node[to_delete]:
85+
del node[to_delete]
8586

8687
elif not path:
8788
raise ValueError("Cannot accept empty path")

tests/test_json_path_utils.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def test_cleanup_node(self):
206206
original_json = self.__get_sample_json()
207207

208208
sample_json = self.__get_sample_json()
209-
node = to_test.cleanup_node(sample_json, "node1.node2.colour")
209+
node = to_test.cleanup_node(sample_json, "node1.node2.colour", "target")
210210
self.assertIsInstance(node, dict, "Not a dictionary")
211211
self.assertDictEqual(original_json, node)
212212
self.assertDictEqual(original_json, sample_json)
@@ -215,20 +215,49 @@ def test_cleanup_node(self):
215215
del sample_json["node1"]["node2"]["colour"]
216216
del sample_json["node1"]["node2"]["shape"]
217217
del sample_json["node1"]["node2"]["position"]
218-
node = to_test.cleanup_node(sample_json, "node1.node2")
218+
node = to_test.cleanup_node(sample_json, "node1.node2", "target")
219219
self.assertIsInstance(node, dict, "Not a dictionary")
220220
self.assertDictEqual({"node1": {}}, node)
221221
self.assertDictEqual({"node1": {}}, sample_json)
222222

223+
def test_cleanup_node_in_target(self):
224+
sample_json = self.__get_sample_json()
225+
del sample_json["node1"]["node2"]["colour"]
226+
del sample_json["node1"]["node2"]["shape"]
227+
del sample_json["node1"]["node2"]["position"]
228+
node = to_test.cleanup_node(sample_json, "node1.node2", "node1.node2.target")
229+
self.assertIsInstance(node, dict, "Not a dictionary")
230+
self.assertDictEqual({"node1": {"node2": {}}}, node)
231+
self.assertDictEqual({"node1": {"node2": {}}}, sample_json)
232+
223233
def test_cleanup_node_empty_path(self):
224234
sample_json = self.__get_sample_json()
225-
self.assertRaises(ValueError, to_test.cleanup_node, sample_json, None)
235+
self.assertRaises(ValueError, to_test.cleanup_node, sample_json, None, "target")
236+
237+
sample_json = self.__get_sample_json()
238+
self.assertRaises(ValueError, to_test.cleanup_node, sample_json, "", "target")
239+
240+
def test_cleanup_node_empty_target(self):
241+
sample_json = self.__get_sample_json()
242+
del sample_json["node1"]["node2"]["colour"]
243+
del sample_json["node1"]["node2"]["shape"]
244+
del sample_json["node1"]["node2"]["position"]
245+
node = to_test.cleanup_node(sample_json, "node1.node2", None)
246+
self.assertIsInstance(node, dict, "Not a dictionary")
247+
self.assertDictEqual({"node1": {}}, node)
248+
self.assertDictEqual({"node1": {}}, sample_json)
226249

227250
sample_json = self.__get_sample_json()
228-
self.assertRaises(ValueError, to_test.cleanup_node, sample_json, "")
251+
del sample_json["node1"]["node2"]["colour"]
252+
del sample_json["node1"]["node2"]["shape"]
253+
del sample_json["node1"]["node2"]["position"]
254+
node = to_test.cleanup_node(sample_json, "node1.node2", "")
255+
self.assertIsInstance(node, dict, "Not a dictionary")
256+
self.assertDictEqual({"node1": {}}, node)
257+
self.assertDictEqual({"node1": {}}, sample_json)
229258

230259
def test_cleanup_node_not_existing(self):
231260
sample_json = self.__get_sample_json()
232261

233-
self.assertRaises(KeyError, to_test.pop_node, sample_json, "node0")
234-
self.assertRaises(KeyError, to_test.pop_node, sample_json, "node1.node2.node3")
262+
self.assertRaises(KeyError, to_test.cleanup_node, sample_json, "node0", "target")
263+
self.assertRaises(KeyError, to_test.cleanup_node, sample_json, "node1.node2.node3", "target")

0 commit comments

Comments
 (0)