Skip to content

Commit 47e53b9

Browse files
author
e107109
committed
Addressing comments
1 parent d26faac commit 47e53b9

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

client_encryption/field_level_encryption.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ def encrypt_payload(payload, config, _params=None):
1212
"""Encrypt some fields of a JSON payload using the given configuration."""
1313

1414
try:
15-
if type(payload) is dict or type(payload) is list:
16-
json_payload = copy.deepcopy(payload)
17-
else:
18-
json_payload = json.loads(payload)
15+
json_payload = copy.deepcopy(payload) if type(payload) is dict or type(payload) is list else json.loads(payload)
1916

2017
for elem, target in config.paths["$"].to_encrypt.items():
2118
if not _params:
@@ -50,10 +47,7 @@ def decrypt_payload(payload, config, _params=None):
5047
"""Decrypt some fields of a JSON payload using the given configuration."""
5148

5249
try:
53-
if type(payload) is dict or type(payload) is list:
54-
json_payload = payload
55-
else:
56-
json_payload = json.loads(payload)
50+
json_payload = payload if type(payload) is dict or type(payload) is list else json.loads(payload)
5751

5852
for elem, target in config.paths["$"].to_decrypt.items():
5953
try:

client_encryption/json_path_utils.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ def update_node(tree, path, node_str):
3232
if __not_root(path):
3333
parent = path.split(_SEPARATOR)
3434
to_set = parent.pop()
35-
if parent:
36-
current_node = __get_node(tree, parent, False)
37-
else:
38-
current_node = tree
35+
current_node = __get_node(tree, parent, False) if parent else tree
3936

4037
try:
4138
node_json = json.loads(node_str)

tests/test_json_path_utils.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ def __get_sample_json():
2121
}
2222
}
2323

24+
@staticmethod
25+
def __get_array_sample_json():
26+
return {
27+
"node1": [
28+
{
29+
"node2": {
30+
"colour": "red",
31+
"shape": "circle",
32+
"position": {
33+
"lat": 1,
34+
"long": 3
35+
}
36+
}
37+
}
38+
]
39+
}
40+
2441
def test_get_node(self):
2542
sample_json = self.__get_sample_json()
2643

@@ -151,6 +168,29 @@ def test_update_node_not_json(self):
151168

152169
self.assertIsInstance(node["node1"]["node2"], str, "not a json string")
153170

171+
def test_update_node_array_with_str(self):
172+
sample_json = self.__get_array_sample_json()
173+
node = to_test.update_node(sample_json, "node1.node2", "not a json string")
174+
175+
self.assertIsInstance(node["node1"][0]["node2"], str, "not a json string")
176+
177+
def test_update_node_array_with_json_str(self):
178+
sample_json = self.__get_array_sample_json()
179+
node = to_test.update_node(sample_json, "node1.node2", '{"position": {"brightness": 6}}')
180+
181+
self.assertIsInstance(node["node1"][0]["node2"]["position"], dict)
182+
self.assertDictEqual({'node1': [
183+
{'node2': {
184+
'colour': 'red',
185+
'shape': 'circle',
186+
'position': {
187+
'brightness': 6
188+
}
189+
}
190+
}
191+
]}, node)
192+
193+
154194
def test_update_node_primitive_type(self):
155195
sample_json = self.__get_sample_json()
156196

0 commit comments

Comments
 (0)