|
| 1 | +import unittest |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) |
| 6 | +sys.path.append(PROJECT_ROOT) |
| 7 | + |
| 8 | +from Stamp_Retentions.Stamp_Retentions import Stamp_Retentions |
| 9 | + |
| 10 | + |
| 11 | +class TestStampRetentions(unittest.TestCase): |
| 12 | + expected = "success" |
| 13 | + message = "307. El comprobante contiene un timbre previo." |
| 14 | + messageAuth = "401 - El rango de la fecha de generación no debe de ser mayor a 72 horas para la emisión del timbre." |
| 15 | + |
| 16 | + @staticmethod |
| 17 | + def open_file(pathFile): |
| 18 | + try: |
| 19 | + return open(pathFile, "r", encoding='utf-8').read() |
| 20 | + except UnicodeDecodeError: |
| 21 | + return open(pathFile, "r", encoding='latin_1', errors='ignore').read() |
| 22 | + |
| 23 | + def testStampRetentions_xml(self): |
| 24 | + """Prueba timbrado con XML usando token""" |
| 25 | + print("\n=== Prueba: Timbrado con XML (token) ===") |
| 26 | + |
| 27 | + stamp = Stamp_Retentions("http://services.test.sw.com.mx", os.environ["SDKTEST_TOKEN"]) |
| 28 | + xml_content = TestStampRetentions.open_file("Test/resources/retenciones20.xml") |
| 29 | + response = stamp.stamp_retetions_v3(xml_content) |
| 30 | + |
| 31 | + TestStampRetentions.log_response("xml", response) |
| 32 | + if response.get_status() == "error": |
| 33 | + self.assertTrue(self.message == response.get_message() or self.messageAuth == response.get_message()) |
| 34 | + else: |
| 35 | + self.assertTrue(self.expected == response.get_status()) |
| 36 | + |
| 37 | + def testStampRetentions_xml_Error(self): |
| 38 | + """Prueba error timbrado con XML CFDI""" |
| 39 | + print("\n=== Prueba: Timbrado con XML (token) ===") |
| 40 | + |
| 41 | + stamp = Stamp_Retentions("http://services.test.sw.com.mx", os.environ["SDKTEST_TOKEN"]) |
| 42 | + xml_content = TestStampRetentions.open_file("Test/resources/xml40.xml") |
| 43 | + response = stamp.stamp_retetions_v3(xml_content) |
| 44 | + |
| 45 | + TestStampRetentions.log_response("xml", response) |
| 46 | + self.assertTrue(self.expected != response.get_status()) |
| 47 | + |
| 48 | + def testStampRetentions_auth(self): |
| 49 | + """Prueba timbrado con autenticación de cuenta""" |
| 50 | + print("\n=== Prueba: Timbrado con auth ===") |
| 51 | + |
| 52 | + stamp = Stamp_Retentions( |
| 53 | + "http://services.test.sw.com.mx", |
| 54 | + None, |
| 55 | + os.environ["SDKTEST_USER"], |
| 56 | + os.environ["SDKTEST_PASSWORD"] |
| 57 | + ) |
| 58 | + xml_content = TestStampRetentions.open_file("Test/resources/retenciones20.xml") |
| 59 | + response = stamp.stamp_retetions_v3(xml_content) |
| 60 | + |
| 61 | + TestStampRetentions.log_response("auth", response) |
| 62 | + if response.get_status() == "error": |
| 63 | + self.assertTrue(self.message == response.get_message() or self.messageAuth == response.get_message()) |
| 64 | + else: |
| 65 | + self.assertTrue(self.expected == response.get_status()) |
| 66 | + |
| 67 | + def testStampRetentions_authError(self): |
| 68 | + """Prueba error timbrado con autenticación de cuenta""" |
| 69 | + print("\n=== Prueba: Timbrado con auth ===") |
| 70 | + |
| 71 | + stamp = Stamp_Retentions( |
| 72 | + "http://services.test.sw.com.mx", |
| 73 | + None, |
| 74 | + "wrongUser", |
| 75 | + os.environ["SDKTEST_PASSWORD"] |
| 76 | + ) |
| 77 | + xml_content = TestStampRetentions.open_file("Test/resources/retenciones20.xml") |
| 78 | + response = stamp.stamp_retetions_v3(xml_content) |
| 79 | + TestStampRetentions.log_response("auth", response) |
| 80 | + self.assertTrue(self.expected != response.get_status()) |
| 81 | + |
| 82 | + @staticmethod |
| 83 | + def log_response(label, response): |
| 84 | + print(f"[{label}] status:", response.get_status()) |
| 85 | + if response.get_status() == "error": |
| 86 | + print("message:", response.get_message()) |
| 87 | + print("messageDetail:", response.get_messageDetail()) |
| 88 | + else: |
| 89 | + data = response.get_data() |
| 90 | + if isinstance(data, dict) and "retencion" in data: |
| 91 | + xml = data["retencion"] |
| 92 | + print("retencion length:", len(xml)) |
| 93 | + print("retencion head:", xml[:200]) |
| 94 | + else: |
| 95 | + print("data:", data) |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + suite = unittest.TestLoader().loadTestsFromTestCase(TestStampRetentions) |
| 100 | + unittest.TextTestRunner(verbosity=2).run(suite) |
| 101 | + |
| 102 | + |
0 commit comments