"""B2 token receipts can return through either admitted monitoring connection.""" import struct import time import pytest from test_vps_host import store # noqa: F401 def receipt(token, code=0): data = struct.pack(" 0 commands.receive(imei, receipt(token, 6), "primary") assert commands.get(body["id"])["device_accepted_at"] == accepted["device_accepted_at"] commands.receive(imei, receipt(token, 5), "secondary") assert commands.get(body["id"])["state"] == "device_rejected" @pytest.mark.parametrize("source", ["unknown-session", "wrong-token", "other-tracker", "closed"]) def test_unrelated_receipt_never_completes_command(pending, source): commands, imei, other, body, token = pending session = "secondary" if source == "unknown-session": session = "unadmitted" elif source == "wrong-token": token = bytes(value ^ 255 for value in token) elif source == "other-tracker": imei, session = other, "another-tracker" else: commands.sessions[imei].remove(session) commands.receive(imei, receipt(token), session) assert commands.get(body["id"])["state"] == "dispatched" def test_new_connection_can_deliver_exact_receipt_without_replay(pending): commands, imei, _, body, token = pending commands.sessions[imei] = {"reconnected"} commands.receive(imei, receipt(token), "reconnected") assert commands.get(body["id"])["state"] == "device_confirmed" assert commands.take(imei, "reconnected") is None def test_revoked_device_receipt_is_not_accepted(pending): commands, imei, _, body, token = pending commands.store.set_devices([]) commands.receive(imei, receipt(token), "secondary") assert commands.get(body["id"])["state"] == "dispatched"