test_failures.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.client_session.close()
  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)
  53. server.add_handler('on_create_party_registration', _on_create_party_registration)
  54. runner = web.AppRunner(server.app)
  55. await runner.setup()
  56. site = web.TCPSite(runner, 'localhost', SERVER_PORT)
  57. await site.start()
  58. yield
  59. await runner.cleanup()
  60. @pytest.fixture
  61. async def start_server_with_signatures():
  62. server = OpenADRServer(vtn_id=VTN_ID, cert=CERTFILE, key=KEYFILE, passphrase='openadr')
  63. server.add_handler('on_create_party_registration', _on_create_party_registration)
  64. runner = web.AppRunner(server.app)
  65. await runner.setup()
  66. site = web.TCPSite(runner, 'localhost', SERVER_PORT)
  67. await site.start()
  68. yield
  69. await runner.cleanup()