test_failures.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. @pytest.mark.asyncio
  14. async def test_openadr_error(start_server):
  15. client = OpenADRClient(vtn_url=f"http://localhost:{SERVER_PORT}/OpenADR2/Simple/2.0b", ven_name=VEN_NAME)
  16. client.on_event = _client_on_event
  17. await client.run()
  18. @pytest.mark.asyncio
  19. async def test_signature_error(start_server_with_signatures):
  20. client = OpenADRClient(vtn_url=f"http://localhost:{SERVER_PORT}/OpenADR2/Simple/2.0b", ven_name=VEN_NAME,
  21. vtn_fingerprint="INVALID")
  22. client.on_event = _client_on_event
  23. await client.run()
  24. await asyncio.sleep(3)
  25. ##########################################################################################
  26. SERVER_PORT = 8001
  27. VEN_NAME = 'myven'
  28. VEN_ID = '1234abcd'
  29. VTN_ID = "TestVTN"
  30. CERTFILE = os.path.join(os.path.dirname(__file__), "cert.pem")
  31. KEYFILE = os.path.join(os.path.dirname(__file__), "key.pem")
  32. async def _on_create_party_registration(payload):
  33. registration_id = generate_id()
  34. payload = {'response': {'response_code': 200,
  35. 'response_description': 'OK',
  36. 'request_id': payload['request_id']},
  37. 'ven_id': VEN_ID,
  38. 'registration_id': registration_id,
  39. 'profiles': [{'profile_name': '2.0b',
  40. 'transports': {'transport_name': 'simpleHttp'}}],
  41. 'requested_oadr_poll_freq': timedelta(seconds=1)}
  42. return 'oadrCreatedPartyRegistration', payload
  43. async def _client_on_event(event):
  44. pass
  45. async def _client_on_report(report):
  46. pass
  47. @pytest.fixture
  48. async def start_server():
  49. server = OpenADRServer(vtn_id=VTN_ID)
  50. server.add_handler('on_create_party_registration', _on_create_party_registration)
  51. runner = web.AppRunner(server.app)
  52. await runner.setup()
  53. site = web.TCPSite(runner, 'localhost', SERVER_PORT)
  54. await site.start()
  55. yield
  56. await runner.cleanup()
  57. @pytest.fixture
  58. async def start_server_with_signatures():
  59. server = OpenADRServer(vtn_id=VTN_ID, cert=CERTFILE, key=KEYFILE, passphrase='openadr')
  60. server.add_handler('on_create_party_registration', _on_create_party_registration)
  61. runner = web.AppRunner(server.app)
  62. await runner.setup()
  63. site = web.TCPSite(runner, 'localhost', SERVER_PORT)
  64. await site.start()
  65. yield
  66. await runner.cleanup()