def test_new_registration(self): client = FakeClient() store = {} with raises(Exception, message="Timeout."): next(client.register(store, timeout=1)) assert 'client-key' not in json.dumps(client.sent_message) store["client_key"] = "KEY!@#" with raises(Exception, message="Timeout."): next(client.register(store, timeout=1)) assert 'KEY!@#' in json.dumps(client.sent_message)
def test_registration(self): client = FakeClient() sent_event = Event() def make_response(prompt, registered, wrong): def send_response(): sent_event.wait() sent_event.clear() if prompt: client.received_message( json.dumps({ "id": "1", "payload": { "pairingType": "PROMPT" } })) if registered: client.received_message( json.dumps({ "id": "1", "payload": { "client-key": "xyz" }, "type": "registered" })) if wrong: client.received_message( json.dumps({ "id": "1", "type": "wrong-response" })) return send_response def patched_send_message(*args, **kwargs): kwargs["unique_id"] = "1" obj = WebOSClient.send_message(client, *args, **kwargs) sent_event.set() return obj client.send_message = patched_send_message store = {} Thread(target=make_response(True, True, False)).start() gen = client.register(store, timeout=10) assert next(gen) == WebOSClient.PROMPTED assert next(gen) == WebOSClient.REGISTERED assert store == {"client_key": "xyz"} # Test with non-empty store. Thread(target=make_response(False, True, False)).start() assert list(client.register(store, timeout=10)) ==\ [WebOSClient.REGISTERED] assert "xyz" in json.dumps(client.sent_message) # Test wrong response. Thread(target=make_response(False, False, True)).start() with raises(Exception): list(client.register(store, timeout=10))
def test_registration_timeout(self): client = FakeClient() with raises(Exception): list(client.register({}, timeout=5))