def _send_request(self, url, request_body): """Transmits the message to the server and checks the response for error status.""" request = urllib.request.Request(url, request_body.encode('utf-8'), { "Content-type": "application/x-ofx", "Accept": "*/*, application/x-ofx" }) stream = urllib.request.urlopen(request) response = stream.read() stream.close() response = Response(response) response.check_signon_status() parsed_ofx = response.as_dict() # FIXME: This needs to account for statement closing responses. if "BANKMSGSRSV1" in parsed_ofx: bank_status = \ parsed_ofx["BANKMSGSRSV1"]["STMTTRNRS"]["STATUS"] self._check_status(bank_status, "bank statement") elif "CREDITCARDMSGSRSV1" in parsed_ofx: creditcard_status = \ parsed_ofx["CREDITCARDMSGSRSV1"]["CCSTMTTRNRS"]["STATUS"] self._check_status(creditcard_status, "credit card statement") elif "SIGNUPMSGSRSV1" in parsed_ofx: acctinfo_status = \ parsed_ofx["SIGNUPMSGSRSV1"]["ACCTINFOTRNRS"]["STATUS"] self._check_status(acctinfo_status, "account information") return response
class ResponseTests(unittest.TestCase): def setUp(self): self.response_text = get_checking_stmt().decode('utf-8') self.response = Response(get_checking_stmt()) def test_signon_success(self): status = self.response.check_signon_status() self.assertTrue(status) def test_account_list(self): statements = self.response.get_statements() self.assertEqual(1, len(statements)) for stmt in statements: self.assertEqual("USD", stmt.get_currency()) self.assertEqual("20100424", stmt.get_begin_date()) self.assertEqual("20100723", stmt.get_end_date()) self.assertEqual("1129.49", stmt.get_balance()) self.assertEqual("20100723", stmt.get_balance_date()) account = stmt.get_account() self.assertEqual("987987987", account.aba_number) self.assertEqual("58152460", account.acct_number) self.assertEqual("CHECKING", account.get_ofx_accttype()) def test_as_xml(self): # First just sanity-check that ElementTree will throw an error # if given a non-XML document. try: response_elem = ElementTree.fromstring(self.response_text) self.fail("Expected parse exception but did not get one.") except: pass # Then see if we can get a real parse success, with no ExpatError. xml = self.response.as_xml() xml_elem = ElementTree.fromstring(xml) self.assertTrue(isinstance(xml_elem, ElementTree.Element)) # Finally, for kicks, try to get a value out of it. org_iter = xml_elem.getiterator("ORG") for org in org_iter: self.assertEqual("FAKEOFX", org.text)