test_failures.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from openleadr import OpenADRClient, OpenADRServer
  2. from openleadr.utils import generate_id
  3. import pytest
  4. from aiohttp import web
  5. import os
  6. import asyncio
  7. from datetime import timedelta
  8. @pytest.mark.asyncio
  9. async def test_http_level_error(start_server):
  10. client = OpenADRClient(vtn_url="http://this.is.an.error", ven_name=VEN_NAME)
  11. client.on_event = _client_on_event
  12. await client.run()
  13. await client.client_session.close()
  14. @pytest.mark.asyncio
  15. async def test_openadr_error(start_server):
  16. client = OpenADRClient(vtn_url=f"http://localhost:{SERVER_PORT}/OpenADR2/Simple/2.0b", ven_name=VEN_NAME)
  17. client.on_event = _client_on_event
  18. await client.run()
  19. await client.client_session.close()
  20. @pytest.mark.asyncio
  21. async def test_signature_error(start_server_with_signatures):
  22. client = OpenADRClient(vtn_url=f"http://localhost:{SERVER_PORT}/OpenADR2/Simple/2.0b", ven_name=VEN_NAME,
  23. vtn_fingerprint="INVALID")
  24. client.on_event = _client_on_event
  25. await client.run()
  26. await asyncio.sleep(0)
  27. await client.stop()
  28. ##########################################################################################
  29. SERVER_PORT = 8001
  30. VEN_NAME = 'myven'
  31. VEN_ID = '1234abcd'
  32. VTN_ID = "TestVTN"
  33. CERTFILE = os.path.join(os.path.dirname(__file__), "cert.pem")
  34. KEYFILE = os.path.join(os.path.dirname(__file__), "key.pem")
  35. async def _on_create_party_registration(payload):
  36. registration_id = generate_id()
  37. payload = {'response': {'response_code': 200,
  38. 'response_description': 'OK',
  39. 'request_id': payload['request_id']},
  40. 'ven_id': VEN_ID,
  41. 'registration_id': registration_id,
  42. 'profiles': [{'profile_name': '2.0b',
  43. 'transports': {'transport_name': 'simpleHttp'}}],
  44. 'requested_oadr_poll_freq': timedelta(seconds=1)}
  45. return 'oadrCreatedPartyRegistration', payload
  46. async def _client_on_event(event):
  47. pass
  48. async def _client_on_report(report):
  49. pass
  50. @pytest.fixture
  51. async def start_server():
  52. server = OpenADRServer(vtn_id=VTN_ID, http_port=SERVER_PORT)
  53. server.add_handler('on_create_party_registration', _on_create_party_registration)
  54. await server.run_async()
  55. yield
  56. await server.stop()
  57. @pytest.fixture
  58. async def start_server_with_signatures():
  59. server = OpenADRServer(vtn_id=VTN_ID, cert=CERTFILE, key=KEYFILE, passphrase='openadr',
  60. http_port=SERVER_PORT)
  61. server.add_handler('on_create_party_registration', _on_create_party_registration)
  62. await server.run_async()
  63. yield
  64. await server.stop()