| 1 | """Runs the SDK against sdks/testing/mock_server.py: python -m unittest discover -s tests (from sdks/python).""" |
| 2 | |
| 3 | import importlib.util |
| 4 | import json |
| 5 | import pathlib |
| 6 | import sys |
| 7 | import threading |
| 8 | import unittest |
| 9 | import urllib.request |
| 10 | from datetime import timezone |
| 11 | from decimal import Decimal |
| 12 | from http.server import ThreadingHTTPServer |
| 13 | |
| 14 | ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 15 | sys.path.insert(0, str(ROOT / "src")) |
| 16 | |
| 17 | import hbank # noqa: E402 |
| 18 | from hbank.models import _time # noqa: E402 |
| 19 | |
| 20 | spec = importlib.util.spec_from_file_location("mock_server", ROOT.parent / "testing" / "mock_server.py") |
| 21 | mock = importlib.util.module_from_spec(spec) |
| 22 | spec.loader.exec_module(mock) |
| 23 | |
| 24 | |
| 25 | class ClientTests(unittest.TestCase): |
| 26 | @classmethod |
| 27 | def setUpClass(cls): |
| 28 | cls.server = ThreadingHTTPServer(("127.0.0.1", 0), mock.Handler) |
| 29 | cls.base = f"http://127.0.0.1:{cls.server.server_port}" |
| 30 | mock.BASE = cls.base |
| 31 | threading.Thread(target=cls.server.serve_forever, daemon=True).start() |
| 32 | cls.bank = hbank.HBank("test_key", base_url=cls.base) |
| 33 | |
| 34 | @classmethod |
| 35 | def tearDownClass(cls): |
| 36 | cls.server.shutdown() |
| 37 | |
| 38 | def pay(self, tid): |
| 39 | urllib.request.urlopen(urllib.request.Request(f"{self.base}/_mock/pay/{tid}", method="POST")).read() |
| 40 | |
| 41 | def test_payment_flow(self): |
| 42 | tid = self.bank.create_payment(Decimal("24.90"), "EUR", description="Order 1042", |
| 43 | callback_url="https://shop.example/cb") |
| 44 | self.assertEqual(self.bank.checkout_url(tid), f"{self.base}/api/payment?tid={tid}") |
| 45 | code = self.bank.create_payment_code(tid) |
| 46 | self.assertTrue(code.qr_svg.endswith("/qr.svg")) |
| 47 | self.assertEqual(self.bank.create_payment_code(tid).code, code.code) |
| 48 | |
| 49 | p = self.bank.get_payment(tid) |
| 50 | self.assertEqual((p.status, p.amount, p.description, p.completed), ("pending", Decimal("24.90"), "Order 1042", False)) |
| 51 | self.pay(tid) |
| 52 | p = self.bank.get_payment(tid) |
| 53 | self.assertTrue(p.completed) |
| 54 | self.assertEqual(p.refundable, Decimal("24.90")) |
| 55 | |
| 56 | r = self.bank.refund(tid, "10", reason="Returned one item") |
| 57 | self.assertEqual((r.refund.amount, r.refund.reason), (Decimal("10"), "Returned one item")) |
| 58 | self.assertEqual((r.payment.refunded, r.payment.refundable), (Decimal("10"), Decimal("14.90"))) |
| 59 | rest = self.bank.refund(tid) |
| 60 | self.assertEqual(rest.refund.amount, Decimal("14.90")) |
| 61 | with self.assertRaises(hbank.HBankError) as e: |
| 62 | self.bank.refund(tid) |
| 63 | self.assertEqual((e.exception.code, e.exception.status), ("not_refundable", 409)) |
| 64 | self.assertTrue(e.exception.message) |
| 65 | |
| 66 | def test_amounts_keep_their_digits(self): |
| 67 | for amount in (24.9, "24.90", 24, Decimal("0.1")): |
| 68 | tid = self.bank.create_payment(amount, "EUR") |
| 69 | self.assertEqual(self.bank.get_payment(tid).amount, Decimal(str(amount)).quantize(Decimal("0.01"))) |
| 70 | |
| 71 | def test_errors(self): |
| 72 | with self.assertRaises(hbank.HBankError) as e: |
| 73 | self.bank.create_payment(0, "EUR") |
| 74 | self.assertEqual((e.exception.code, e.exception.status), ("invalid_amount", 400)) |
| 75 | with self.assertRaises(hbank.HBankError) as e: |
| 76 | self.bank.create_payment(1, "XXX") |
| 77 | self.assertEqual(e.exception.code, "unknown_currency") |
| 78 | with self.assertRaises(hbank.HBankError) as e: |
| 79 | self.bank.get_payment(999999) |
| 80 | self.assertEqual((e.exception.code, e.exception.status), ("transaction_not_found", 404)) |
| 81 | with self.assertRaises(hbank.AuthenticationError) as e: |
| 82 | hbank.HBank("nope", base_url=self.base).list_subscriptions() |
| 83 | self.assertEqual((e.exception.code, e.exception.status), ("invalid_api_key", 403)) |
| 84 | with self.assertRaises(hbank.HBankConnectionError): |
| 85 | hbank.HBank("k", base_url="http://127.0.0.1:1", timeout=2).rate("EUR", "USD") |
| 86 | |
| 87 | def test_subscriptions(self): |
| 88 | s = self.bank.create_subscription("9.99", "EUR", "month", description="Pro plan") |
| 89 | self.assertEqual((s.status, s.amount, s.interval, s.interval_count), ("offered", Decimal("9.99"), "month", 1)) |
| 90 | self.assertIsNone(s.subscribed_at) |
| 91 | self.assertEqual(self.bank.get_subscription(s.id).id, s.id) |
| 92 | self.assertIn(s.id, [x.id for x in self.bank.list_subscriptions()]) |
| 93 | |
| 94 | c = self.bank.checkout_subscription(s.id, callback_url="https://shop.example/cb") |
| 95 | self.pay(c.transaction_id) |
| 96 | self.assertEqual(self.bank.get_payment(c.transaction_id).subscription_id, s.id) |
| 97 | active = self.bank.get_subscription(s.id) |
| 98 | self.assertEqual(active.status, "active") |
| 99 | self.assertEqual(active.subscribed_at.tzinfo, timezone.utc) |
| 100 | with self.assertRaises(hbank.HBankError) as e: |
| 101 | self.bank.checkout_subscription(s.id) |
| 102 | self.assertEqual(e.exception.code, "not_open") |
| 103 | |
| 104 | canceled = self.bank.cancel_subscription(s.id) |
| 105 | self.assertTrue(canceled.canceled) |
| 106 | self.assertEqual(canceled.canceled_by, "merchant") |
| 107 | with self.assertRaises(hbank.HBankError) as e: |
| 108 | self.bank.cancel_subscription(s.id) |
| 109 | self.assertEqual(e.exception.code, "already_canceled") |
| 110 | |
| 111 | one_call = self.bank.create_subscription_checkout(5, "EUR", "week", interval_count=2) |
| 112 | self.assertEqual(one_call.subscription.interval_count, 2) |
| 113 | with self.assertRaises(hbank.HBankError) as e: |
| 114 | self.bank.create_subscription(5, "EUR", "fortnight") |
| 115 | self.assertEqual(e.exception.code, "invalid_interval") |
| 116 | |
| 117 | def test_several_subscriptions_in_one_checkout(self): |
| 118 | offered = self.bank.create_subscription("5", "EUR", "month", description="Storage") |
| 119 | c = self.bank.create_subscriptions_checkout( |
| 120 | [offered.id, {"amount": "2.50", "currency": "EUR", "interval": "week", "description": "Support"}], |
| 121 | callback_url="https://shop.example/cb") |
| 122 | self.assertEqual([s.id for s in c.subscriptions][0], offered.id) |
| 123 | self.assertEqual([s.description for s in c.subscriptions], ["Storage", "Support"]) |
| 124 | self.pay(c.transaction_id) |
| 125 | payment = self.bank.get_payment(c.transaction_id) |
| 126 | self.assertEqual(payment.amount, Decimal("7.50")) |
| 127 | self.assertIsNone(payment.subscription_id) |
| 128 | self.assertEqual(payment.subscription_ids, [s.id for s in c.subscriptions]) |
| 129 | self.assertTrue(all(self.bank.get_subscription(s.id).status == "active" for s in c.subscriptions)) |
| 130 | |
| 131 | with self.assertRaises(hbank.HBankError) as e: |
| 132 | self.bank.create_subscriptions_checkout([{"amount": 1, "currency": "EUR", "interval": "month"}, |
| 133 | {"amount": 1, "currency": "USD", "interval": "month"}]) |
| 134 | self.assertEqual(e.exception.code, "mixed_currencies") |
| 135 | with self.assertRaises(hbank.HBankError) as e: |
| 136 | self.bank.create_subscriptions_checkout([offered.id]) |
| 137 | self.assertEqual(e.exception.code, "not_open") |
| 138 | |
| 139 | def test_currency(self): |
| 140 | self.assertEqual(self.bank.convert(100, "EUR", "USD"), Decimal("108.37")) |
| 141 | rate = self.bank.rate("EUR", "USD") |
| 142 | self.assertEqual(rate.to_rate, Decimal("1.0837")) |
| 143 | self.assertGreater(rate.from_rate, 0) |
| 144 | |
| 145 | |
| 146 | class CallbackTests(unittest.TestCase): |
| 147 | def test_parse_and_answer(self): |
| 148 | cb = hbank.parse_callback(json.dumps({"id": "tx_8fK2mQ7xRb4Tn9WcYz3LpH6v", "amount": 24.90, "currency": "EUR", "status": "completed", |
| 149 | "description": "Order 1042"})) |
| 150 | self.assertEqual((cb.id, cb.amount, cb.currency), ("tx_8fK2mQ7xRb4Tn9WcYz3LpH6v", Decimal("24.9"), "EUR")) |
| 151 | self.assertEqual(hbank.approve(), {"resume": True}) |
| 152 | self.assertEqual(hbank.refuse("Out of stock"), {"resume": False, "error": "Out of stock"}) |
| 153 | with self.assertRaises(hbank.HBankError): |
| 154 | hbank.parse_callback(b"{}") |
| 155 | |
| 156 | def test_signature(self): |
| 157 | body = '{"id":7310,"amount":24.9,"currency":"EUR","status":"awaiting_approval","description":"Order 1042"}' |
| 158 | header = "t=1700000000,v1=fcfc30b9667c3a8e8aea68178c36716153e075f79c61521bf3586088d9877281" |
| 159 | self.assertTrue(hbank.verify_signature("hbcb_test", header, body, now=1700000100)) |
| 160 | self.assertTrue(hbank.verify_signature("hbcb_test", header, body.encode(), now=1700000100)) |
| 161 | self.assertFalse(hbank.verify_signature("hbcb_other", header, body, now=1700000100)) |
| 162 | self.assertFalse(hbank.verify_signature("hbcb_test", header, body.replace("24.9", "0.1"), now=1700000100)) |
| 163 | self.assertFalse(hbank.verify_signature("hbcb_test", header, body, now=1700000400)) # too old |
| 164 | self.assertFalse(hbank.verify_signature("hbcb_test", None, body)) |
| 165 | self.assertFalse(hbank.verify_signature("hbcb_test", "garbage", body)) |
| 166 | |
| 167 | def test_dotnet_timestamps(self): |
| 168 | t = _time("2026-10-03T09:12:44.1234567Z") |
| 169 | self.assertEqual((t.microsecond, t.tzinfo), (123456, timezone.utc)) |
| 170 | self.assertEqual(_time("2026-10-03T09:12:44").tzinfo, timezone.utc) |
| 171 | self.assertEqual(_time("2026-10-03T09:12:44+02:00").utcoffset().total_seconds(), 7200) |
| 172 | |
| 173 | |
| 174 | if __name__ == "__main__": |
| 175 | unittest.main() |