def on_message(client: mqtt.Client, userdata, msg: mqtt.MQTTMessage): ack: AckMessage = AckMessage.from_json(msg.payload) tokens = msg.topic.split('/') session_id = tokens[1] if ack.status == Status.NEW: new_ack = AckMessage(txid=ack.txid, status=Status.PENDING, transaction_currency="NANO", transaction_hash="B50DB45966850AD4B11ECFDD8AE0A7AD97DF74864631D8C1495E46DDDFC1802A") logging.info("Waiting...") time.sleep(WAITING_INTERVAL) logging.info("Sending first ack...") client.publish("acks/{}".format(session_id), new_ack.to_json()) new_ack.status = Status.PAID logging.info("Waiting...") def pub_second_ack(): logging.info("Sending second ack...") client.publish("acks/{}".format(session_id), new_ack.to_json()) t = Timer(WAITING_INTERVAL, pub_second_ack) t.start()
async def test_on_ack(mock_mqtt): wallet = Wallet.factory("manta://localhost:8000/123") expected = AckMessage(txid="0", transaction_hash="myhash", status=Status.PENDING) mock_mqtt.push("acks/123", expected.to_json()) ack_message = await wallet.acks.get() assert ack_message == expected
def reply(topic, payload): order = MerchantOrderRequestMessage.from_json(payload) tokens = topic.split("/") device = tokens[1] r = AckMessage(txid="0", status=Status.NEW, url="manta://testpp.com/{}".format(order.session_id)) return "acks/{}".format(device), r.to_json()
async def test_ack(mock_mqtt): store = Store('device1') ack_message: AckMessage = None expected_ack = AckMessage(txid='1234', transaction_hash="hash_1234", status=Status.PENDING) mock_mqtt.push('acks/123', expected_ack.to_json()) ack_message = await store.acks.get() assert expected_ack == ack_message
def se(topic, payload): nonlocal mock_mqtt if topic == "merchant_order_request/device1": order = MerchantOrderRequestMessage.from_json(payload) reply = AckMessage(status=Status.NEW, url="manta://testpp.com/{}".format( order.session_id), txid="0") topic = "acks/{}".format(order.session_id) mock_mqtt.push(topic, reply.to_json())
def test_create(self, tx_storage: TXStorageMemory): ack = AckMessage(amount=Decimal("10"), status=Status.NEW, txid="0") order = MerchantOrderRequestMessage(amount=Decimal("10"), session_id="123", fiat_currency="EUR") tx_storage.create(0, "123", "app0@user0", order, ack) assert 1 == len(tx_storage)
def test_confirm(mock_mqtt, payproc): test_payment_message(mock_mqtt, payproc) payproc.confirm("1423") ack = AckMessage( txid="0", status=Status.PAID, transaction_hash="myhash", transaction_currency="NANO", ) mock_mqtt.publish.assert_called_with("acks/1423", JsonContains(ack))
def test_receive_merchant_order_request(mock_mqtt, payproc): request = MerchantOrderRequestMessage( amount=Decimal("1000"), session_id="1423", fiat_currency="eur", ) expected = AckMessage(txid="0", url="manta://localhost/1423", status=Status.NEW) mock_mqtt.push("merchant_order_request/device1", request.to_json()) mock_mqtt.publish.assert_any_call("acks/1423", JsonContains(expected)) mock_mqtt.subscribe.assert_any_call("payments/1423") mock_mqtt.subscribe.assert_any_call("payment_requests/1423/+")
def test_invalidate(mock_mqtt, payproc): test_payment_message(mock_mqtt, payproc) payproc.invalidate("1423", "Timeout") ack = AckMessage( txid="0", status=Status.INVALID, transaction_hash="myhash", transaction_currency="NANO", memo="Timeout", ) mock_mqtt.publish.assert_called_with("acks/1423", JsonContains(ack))
def test_payment_message(mock_mqtt, payproc): test_get_payment_request_all(mock_mqtt, payproc) message = PaymentMessage(crypto_currency="NANO", transaction_hash="myhash") ack = AckMessage( txid="0", status=Status.PENDING, transaction_hash="myhash", transaction_currency="NANO", ) mock_mqtt.push("payments/1423", message.to_json()) mock_mqtt.publish.assert_called_with("acks/1423", JsonContains(ack))
def test_receive_merchant_cancel_order(mock_mqtt, payproc): test_receive_merchant_order_request(mock_mqtt, payproc) mock_mqtt.push("merchant_order_cancel/1423", "") expected = AckMessage( txid="0", url="manta://localhost/1423", status=Status.INVALID, memo="Canceled by Merchant", ) mock_mqtt.publish.assert_called_with("acks/1423", JsonContains(expected))
def test_payment_message_unsupported(mock_mqtt, payproc): test_get_payment_request_all(mock_mqtt, payproc) message = PaymentMessage(crypto_currency="ETH", transaction_hash="myhash") ack = AckMessage( txid="0", status=Status.PENDING, transaction_hash="myhash", transaction_currency="NANO", ) mock_mqtt.reset_mock() mock_mqtt.push("payments/1423", message.to_json()) mock_mqtt.publish.assert_not_called()
def test_receive_merchant_order_request_legacy(mock_mqtt, payproc): request = MerchantOrderRequestMessage( amount=Decimal("1000"), session_id="1423", fiat_currency="eur", crypto_currency="btc", ) expected = AckMessage(txid="0", url="bitcoin:btc_daddress?amount=5", status=Status.NEW) mock_mqtt.push("merchant_order_request/device1", request.to_json()) mock_mqtt.publish.assert_any_call("acks/1423", JsonContains(expected))
def test_receive_merchant_order_request_unkwnown_field(mock_mqtt, payproc): request = MerchantOrderRequestMessage( amount=Decimal("1000"), session_id="1423", fiat_currency="eur" ) expected = AckMessage(txid="0", url="manta://localhost/1423", status=Status.NEW) request_json = request.unstructure() request_json["extra_field"] = "extra" mock_mqtt.push("merchant_order_request/device1", json.dumps(request_json)) mock_mqtt.publish.assert_any_call("acks/1423", JsonContains(expected)) mock_mqtt.subscribe.assert_any_call("payments/1423") mock_mqtt.subscribe.assert_any_call("payment_requests/1423/+")
def test_ack_paid(self, tx_storage): self.test_create(tx_storage) ack = AckMessage(amount=Decimal("10"), status=Status.NEW, txid="1") order = MerchantOrderRequestMessage(amount=Decimal("10"), session_id="321", fiat_currency="EUR") tx_storage.create(1, "321", "app0@user0", order, ack) state = tx_storage.get_state_for_session("123") new_ack = attr.evolve(state.ack, status=Status.PAID) state.ack = new_ack assert 1 == len(tx_storage) assert Status.NEW == tx_storage.get_state_for_session("321").ack.status