From b37b548ff21dc78eb8fff2a736f4acddeff0bda3 Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Tue, 25 Aug 2015 23:07:56 +0300 Subject: Cleanup --- gsxws/core.py | 38 +++++++++++++++++++++++++++++++++----- gsxws/repairs.py | 2 +- requirements.pip | 1 - tests/fixtures/error_ca_fmip.xml | 14 ++++++++++++++ tests/test_gsxws.py | 19 +++++++++++++++++-- 5 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 tests/fixtures/error_ca_fmip.xml diff --git a/gsxws/core.py b/gsxws/core.py index afb1521..9ebe1bf 100644 --- a/gsxws/core.py +++ b/gsxws/core.py @@ -157,7 +157,7 @@ def get_format(locale=GSX_LOCALE): class GsxError(Exception): - def __init__(self, message=None, xml=None, url=None): + def __init__(self, message=None, xml=None, url=None, code=None): self.codes = [] self.messages = [] @@ -178,7 +178,8 @@ class GsxError(Exception): for el in root.findall('*//message'): self.messages.append(el.text) - super(Exception, self).__init__(self.message) + def __str__(self): + return repr(self.message) @property def code(self): @@ -305,8 +306,6 @@ class GsxRequest(object): def _submit(self, method, response=None, raw=False): "Constructs and submits the final SOAP message" - global GSX_SESSION - root = ET.SubElement(self.body, self.obj._namespace + method) if method is "Authenticate": @@ -337,7 +336,7 @@ class GsxRequest(object): logging.debug("Response: %s %s %s" % (res.status_code, res.reason, xml)) - if raw: + if raw is True: return ET.fromstring(self.xml_response) response = response or self._response @@ -351,6 +350,35 @@ class GsxRequest(object): return unicode(self).encode('utf-8') +class GsxResponse: + def __init__(self, http_response, xml, el_method, el_response, raw=False): + self.result = None # result status + self.response = None # objectified result + self.el_method = el_method + self.el_response = el_response + + if http_response.status_code > 200: + raise GsxError(xml=xml, url=self._url) + + self.response = objectify.parse(xml, self.el_response) + + logging.debug("Response: %s %s %s" % (http_response.status_code, http_response.reason, xml)) + + if raw is True: + return ET.fromstring(self.xml_response) + + if hasattr(self.response, 'outCome'): + self.result = self.response.outCome + if self.result == 'STOP': + raise GsxError(message=self.response.messages, code=self.result) + + def get_response(self): + if self.response is None: + raise GsxError('GSX request returned empty result') + + return self.response if len(self.response) > 1 else self.response[0] + + class GsxObject(object): "XML/SOAP representation of a GSX object" _data = {} diff --git a/gsxws/repairs.py b/gsxws/repairs.py index d84372c..0ad2d56 100644 --- a/gsxws/repairs.py +++ b/gsxws/repairs.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- - "gsxws/repairs.py" + import sys import logging diff --git a/requirements.pip b/requirements.pip index d33bb35..a3596c0 100644 --- a/requirements.pip +++ b/requirements.pip @@ -1,3 +1,2 @@ lxml -yaml requests diff --git a/tests/fixtures/error_ca_fmip.xml b/tests/fixtures/error_ca_fmip.xml new file mode 100644 index 0000000..84f7805 --- /dev/null +++ b/tests/fixtures/error_ca_fmip.xml @@ -0,0 +1,14 @@ + + + + + + n6SnRBjUSJWdf6YQBZ3WhLN + + STOP + A repair cannot be created for this product because Find My iPhone is active. Please have the customer turn off Find My iPhone before proceeding + + + + + diff --git a/tests/test_gsxws.py b/tests/test_gsxws.py index 07a7ca4..8690524 100644 --- a/tests/test_gsxws.py +++ b/tests/test_gsxws.py @@ -155,6 +155,14 @@ class TestErrorFunctions(TestCase): e = GsxError(msg) self.assertEqual(e.message, msg) + def test_error_ca_fmip(self): + from gsxws.core import GsxResponse + xml = open('tests/fixtures/error_ca_fmip.xml', 'r').read() + with self.assertRaisesRegexp(GsxError, 'A repair cannot be created'): + GsxResponse(xml=xml, el_method='CreateCarryInResponse', + el_response='repairConfirmation') + + class TestLookupFunctions(RemoteTestCase): def test_component_check(self): @@ -296,9 +304,16 @@ class TestPartFunction(RemoteTestCase): self.assertIsInstance(parts[0].partNumber, basestring) -class TestRemoteWarrantyFunctions(RemoteTestCase): +class TestRemoteWarrantyFunctions(TestCase): + @classmethod + def setUpClass(cls): + from gsxws.core import connect + connect(env['GSX_USER'], env['GSX_SOLDTO'], env['GSX_ENV']) + def setUp(self): super(TestRemoteWarrantyFunctions, self).setUp() + self.sn = env['GSX_SN'] + device = Product(sn=self.sn) self.product = Product(env['GSX_SN']) self.wty = self.product.warranty(ship_to=env['GSX_SHIPTO']) @@ -310,7 +325,7 @@ class TestRemoteWarrantyFunctions(RemoteTestCase): self.assertEqual(wty.warrantyStatus, 'Out Of Warranty (No Coverage)') def test_fmip_status(self): - self.assertContains(self.product.fmip_status, 'Find My iPhone is active') + self.assertIn('Find My iPhone is active', self.product.fmip_status) def test_fmip_active(self): self.assertTrue(self.product.fmip_is_active) -- cgit v1.2.3