Skip to content

Commit

Permalink
Merge pull request #8 from Mastercard/feature/decrypt_primitive_on_sa…
Browse files Browse the repository at this point in the history
…me_path

Added fix to decrypt primitive on same path
  • Loading branch information
pqlMC authored Oct 8, 2019
2 parents 033a854 + 9b5bdee commit b02caac
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion client_encryption/json_path_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def update_node(tree, path, node_str):
else:
current_node = tree

if to_set in current_node and type(current_node[to_set]) is dict:
if to_set in current_node and type(current_node[to_set]) is dict and type(json.loads(node_str)) is dict:
current_node[to_set].update(json.loads(node_str))
else:
current_node[to_set] = json.loads(node_str)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_field_level_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,25 @@ def test_decrypt_payload_when_root_as_in_and_out_path(self):

self.assertDictEqual({"field1": "value1", "field2": "value2"}, payload)

def test_DecryptPayload_ShouldOverwriteInputObject_WhenOutPathSameAsInPath_PrimitiveTypeData(self):
self._config._paths["$"]._to_decrypt = {"data": "data"}
self._config._data_encoding = Encoding.HEX

encrypted_payload = {
"data": {
"encryptedValue": "741a932b1ed546072384eef82f42c781",
"encryptedKey": "0069fd96179b119cda77ec56be29b184d25c639af27c1f59b6b5e4de4e9bcba883d7933dda5cb6bb8888ea402cbd0f2cddf572b2baee2dd57c94081e4db318a0ded28ae96a80eff1ed421ca7bae6418fae3a1ce9744c02d4aa4dc53efa69b483a2e030919df30a87f95bb18595b4beb15b65eff3c1332c2d54100bd39ccb3ab7eeea648e0ccc473586002063a380dddd940aaa075b998047bd75a5cdb79142c150fa87e9ec2706569a5f7f06bc36c959f144cd22fbf5e690388902eca06d3cc4492d50b72ed5e96f66e6f03087931d3147401720fb512c0c3d5b89fa029fba157fbb1571a4712377b68bac4344dbf75535a1b40197a293eadc563ea035b62591",
"iv": "f84e565520f5b75cbb6a13c97fdaea2b",
"oaepHashingAlgo": "SHA256",
"certFingerprint": "80810fc13a8319fcf0e2ec322c82a4c304b782cc3ce671176343cfe8160c2279",
"keyFingerprint": "761b003c1eade3a5490e5000d37887baa5e6ec0e226c07706e599451fc032a79"
}
}

payload = to_test.decrypt_payload(encrypted_payload, self._config)

self.assertEqual("string", payload['data'])

def test_decrypt_payload_when_decryption_error_occurs(self):
encrypted_payload = {
"encryptedData": {
Expand Down
33 changes: 30 additions & 3 deletions tests/test_json_path_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_get_node_not_existing(self):
# too many new nodes
self.assertRaises(KeyError, to_test.get_node, sample_json, "node1.node2.newnode.newnode2", True)

def test_set_node(self):
def test_update_node(self):
sample_json = self.__get_sample_json()
node = to_test.update_node(sample_json, "$", '{"node3": {"brightness": 6}}')

Expand Down Expand Up @@ -138,18 +138,45 @@ def test_set_node(self):
}
}, node)

def test_set_node_empty_path(self):
def test_update_node_empty_path(self):
sample_json = self.__get_sample_json()
self.assertRaises(ValueError, to_test.update_node, sample_json, None, '{"node3": {"brightness": 6}}')

sample_json = self.__get_sample_json()
self.assertRaises(ValueError, to_test.update_node, sample_json, "", '{"node3": {"brightness": 6}}')

def test_set_node_not_json(self):
def test_update_node_not_json(self):
sample_json = self.__get_sample_json()

self.assertRaises(json.JSONDecodeError, to_test.update_node, sample_json, "node1.node2", "not a json string")

def test_update_node_primitive_type(self):
sample_json = self.__get_sample_json()

node = to_test.update_node(sample_json, "node1.node2", '"I am a primitive data type"')

self.assertIsInstance(node["node1"]["node2"], str, "Not a string")
self.assertDictEqual({"node1": {
"node2": "I am a primitive data type"
}
}, node)

node = to_test.update_node(sample_json, "node1.node2", '4378462')

self.assertIsInstance(node["node1"]["node2"], int, "Not an int")
self.assertDictEqual({"node1": {
"node2": 4378462
}
}, node)

node = to_test.update_node(sample_json, "node1.node2", 'true')

self.assertIsInstance(node["node1"]["node2"], bool, "Not a bool")
self.assertDictEqual({"node1": {
"node2": True
}
}, node)

def test_pop_node(self):
original_json = self.__get_sample_json()

Expand Down

0 comments on commit b02caac

Please sign in to comment.