diff options
author | Christian Heimes <cheimes@redhat.com> | 2015-07-15 17:55:05 +0200 |
---|---|---|
committer | Christian Heimes <cheimes@redhat.com> | 2015-07-15 21:19:54 +0200 |
commit | 9fa1d0c968977ef23e26556b0a8e8e76b32c7288 (patch) | |
tree | 9acac7d02a5020e204de639c69d25e70a4b654f7 /tests | |
parent | 4aa89241e3a0423198cc6dce1f6f193eca74cac6 (diff) | |
download | pki-9fa1d0c968977ef23e26556b0a8e8e76b32c7288.tar.gz pki-9fa1d0c968977ef23e26556b0a8e8e76b32c7288.tar.xz pki-9fa1d0c968977ef23e26556b0a8e8e76b32c7288.zip |
Handle JSON decode error in handle_exceptions()
pki.handle_exceptions() raises a JSON decode exception when the body of
the HTTPException is not a valid JSON string. The JSON exception hides
the true error message.
The patch also fixes a bug in PKIException.from_json(). The code and
ClassName attribute are now correctly set. Finally we have our first
unit test.
https://fedorahosted.org/pki/ticket/1488
https://fedorahosted.org/freeipa/ticket/5129
Diffstat (limited to 'tests')
-rw-r--r-- | tests/python/test_pki.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/tests/python/test_pki.py b/tests/python/test_pki.py new file mode 100644 index 000000000..d30a1cd84 --- /dev/null +++ b/tests/python/test_pki.py @@ -0,0 +1,65 @@ +# Authors: +# Christian Heimes <cheimes@redhat.com> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Copyright (C) 2015 Red Hat, Inc. +# All rights reserved. +# + +import unittest + +import pki +import requests +import json + + +class TestHTTPError(requests.exceptions.HTTPError): + def __init__(self, body): + super(TestHTTPError, self).__init__() + self.response = requests.Response() + self.response._content = body + +class PKITests(unittest.TestCase): + def test_handle_exceptions(self): + + @pki.handle_exceptions() + def raiser(body): + raise TestHTTPError(body) + + body = json.dumps({ + 'Message': 'message', + 'Code': 42, + 'ClassName': 'com.netscape.certsrv.base.BadRequestException', + 'Attributes': { + 'Attribute': [], + }, + }) + + with self.assertRaises(pki.BadRequestException) as e: + raiser(body) + + self.assertEqual(e.exception.message, 'message') + self.assertEqual(e.exception.code, 42) + self.assertEqual( + e.exception.ClassName, + 'com.netscape.certsrv.base.BadRequestException' + ) + + with self.assertRaises(TestHTTPError) as e: + raiser('no json body') + + +if __name__ == '__main__': + unittest.main() |