test_client_registration.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # SPDX-License-Identifier: Apache-2.0
  2. # Copyright 2020 Contributors to OpenLEADR
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. from pyopenadr import OpenADRClient, OpenADRServer, enums
  13. from pyopenadr.utils import generate_id
  14. from pyopenadr.messaging import create_message, parse_message
  15. from datetime import datetime, timezone, timedelta
  16. import asyncio
  17. import sqlite3
  18. import pytest
  19. from aiohttp import web
  20. import os
  21. SERVER_PORT = 8001
  22. VEN_NAME = 'myven'
  23. VEN_ID = '1234abcd'
  24. VTN_ID = "TestVTN"
  25. CERTFILE = os.path.join(os.path.dirname(os.path.dirname(__file__)), "cert.pem")
  26. KEYFILE = os.path.join(os.path.dirname(os.path.dirname(__file__)), "key.pem")
  27. async def _on_create_party_registration(payload):
  28. registration_id = generate_id()
  29. payload = {'response': {'response_code': 200,
  30. 'response_description': 'OK',
  31. 'request_id': payload['request_id']},
  32. 'ven_id': VEN_ID,
  33. 'registration_id': registration_id,
  34. 'profiles': [{'profile_name': '2.0b',
  35. 'transports': {'transport_name': 'simpleHttp'}}],
  36. 'requested_oadr_poll_freq': timedelta(seconds=10)}
  37. return 'oadrCreatedPartyRegistration', payload
  38. @pytest.fixture
  39. async def start_server():
  40. server = OpenADRServer(vtn_id=VTN_ID)
  41. server.add_handler('on_create_party_registration', _on_create_party_registration)
  42. runner = web.AppRunner(server.app)
  43. await runner.setup()
  44. site = web.TCPSite(runner, 'localhost', SERVER_PORT)
  45. await site.start()
  46. yield
  47. await runner.cleanup()
  48. @pytest.fixture
  49. async def start_server_with_signatures():
  50. server = OpenADRServer(vtn_id=VTN_ID, cert=CERTFILE, key=KEYFILE, passphrase='openadr', verification_cert=CERTFILE)
  51. server.add_handler('on_create_party_registration', _on_create_party_registration)
  52. runner = web.AppRunner(server.app)
  53. await runner.setup()
  54. site = web.TCPSite(runner, 'localhost', SERVER_PORT)
  55. await site.start()
  56. yield
  57. await runner.cleanup()
  58. @pytest.mark.asyncio
  59. async def test_query_party_registration(start_server):
  60. client = OpenADRClient(ven_name=VEN_NAME,
  61. vtn_url=f"http://localhost:{SERVER_PORT}/OpenADR2/Simple/2.0b")
  62. response_type, response_payload = await client.query_registration()
  63. assert response_type == 'oadrCreatedPartyRegistration'
  64. assert response_payload['vtn_id'] == VTN_ID
  65. @pytest.mark.asyncio
  66. async def test_create_party_registration(start_server):
  67. client = OpenADRClient(ven_name=VEN_NAME,
  68. vtn_url=f"http://localhost:{SERVER_PORT}/OpenADR2/Simple/2.0b")
  69. response_type, response_payload = await client.create_party_registration()
  70. assert response_type == 'oadrCreatedPartyRegistration'
  71. assert response_payload['ven_id'] == VEN_ID
  72. @pytest.mark.asyncio
  73. async def test_create_party_registration_with_signatures(start_server_with_signatures):
  74. client = OpenADRClient(ven_name=VEN_NAME,
  75. vtn_url=f"http://localhost:{SERVER_PORT}/OpenADR2/Simple/2.0b",
  76. cert=CERTFILE, key=KEYFILE, passphrase='openadr', verification_cert=CERTFILE)
  77. response_type, response_payload = await client.create_party_registration()
  78. assert response_type == 'oadrCreatedPartyRegistration'
  79. assert response_payload['ven_id'] == VEN_ID