From 3d9f7e20194282f0136ccba9ab57ee98f3627c74 Mon Sep 17 00:00:00 2001 From: Dave Lasley Date: Fri, 7 Apr 2017 10:25:33 -0700 Subject: [PATCH 1/2] [IMP] Decode decryption response * Add better documentation to `decrypt` * Decode aspects of the decrypt response to allow for more usable return --- .travis.yml | 4 ++-- red_october/red_october.py | 13 ++++++++++--- red_october/tests/test_red_october.py | 22 ++++++++++++++++++++-- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9a18e30..331488b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,8 +13,8 @@ virtualenv: env: global: - - VERSION="0.1.0" - - RELEASE="0.0.2" + - VERSION="0.2" + - RELEASE="0.2.1" - PROJECT="Python Red October" - BRANCH_PROD="master" - BRANCH_DOC="gh-pages" diff --git a/red_october/red_october.py b/red_october/red_october.py index d9bf081..95a2d0b 100644 --- a/red_october/red_october.py +++ b/red_october/red_october.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- -# Copyright 2016 LasLabs Inc. +# Copyright 2016-2017 LasLabs Inc. # License MIT (https://opensource.org/licenses/MIT). +import json import requests from datetime import timedelta @@ -152,13 +153,19 @@ def decrypt(self, data): the set of ``owners`` have delegated their keys to the server, or if the decryption credentials are incorrect. Returns: - str: Decrypted string + dict: Response object with the following keys: + * `Data` (`str`): Decrypted data + * `Secure` (`bool`): Not documented. Seems to always be `True` + * `Delegates` (`list` of `str`): Delegate names """ data = self._clean_mapping({ 'Data': data, }) try: - return self.call('decrypt', data=data) + response = self.call('decrypt', data=data) + response = json.loads(response.decode('base64')) + response['Data'] = response['Data'].decode('base64') + return response except RedOctoberRemoteException as e: raise RedOctoberDecryptException(e.message) diff --git a/red_october/tests/test_red_october.py b/red_october/tests/test_red_october.py index 5f83453..4ed4359 100644 --- a/red_october/tests/test_red_october.py +++ b/red_october/tests/test_red_october.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- -# Copyright 2016 LasLabs Inc. +# Copyright 2016-2017 LasLabs Inc. # License MIT (https://opensource.org/licenses/MIT). +import json import mock import unittest @@ -87,14 +88,31 @@ def test_encrypt(self, call): call.assert_called_once_with('encrypt', data=expect) @mock.patch.object(RedOctober, 'call') - def test_decrypt(self, call): + def test_decrypt_call(self, call): """ It should call with proper args """ expect = { 'Data': self.data64, } + response = { + 'Data': self.data64, + } + call.return_value = json.dumps(response).encode('base64') self.red_october.decrypt(self.data64) call.assert_called_once_with('decrypt', data=expect) + @mock.patch.object(RedOctober, 'call') + def test_decrypt_return(self, call): + """ It should return proper value. """ + expect = { + 'Data': self.data64, + 'Secure': True, + 'Delegates': self.owners, + } + call.return_value = json.dumps(expect).encode('base64') + res = self.red_october.decrypt(self.data64) + expect['Data'] = expect['Data'].decode('base64') + self.assertDictEqual(res, expect) + @mock.patch.object(RedOctober, 'call') def test_decrypt_fail(self, call): """ It should raise proper error on failure. """ From 3aa7cfcf31be011e175f8088014588eeb8365054 Mon Sep 17 00:00:00 2001 From: Dave Lasley Date: Fri, 7 Apr 2017 18:36:54 -0700 Subject: [PATCH 2/2] [FIX] Error message on fail * Use the status instead of the response on error --- red_october/red_october.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/red_october/red_october.py b/red_october/red_october.py index 95a2d0b..02edff5 100644 --- a/red_october/red_october.py +++ b/red_october/red_october.py @@ -373,10 +373,7 @@ def call(self, endpoint, method='POST', params=None, data=None): response = response.json() if response['Status'] != 'ok': raise RedOctoberRemoteException( - '\n'.join([ - 'Response:', - '\n'.join(response.get('Response', [])), - ]) + response['Status'], ) try: return response['Response']