test_client_registration.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 openleadr import OpenADRClient, OpenADRServer, enums
  13. from openleadr.utils import generate_id, certificate_fingerprint
  14. from openleadr.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. return VEN_ID, registration_id
  30. @pytest.fixture
  31. async def start_server():
  32. server = OpenADRServer(vtn_id=VTN_ID, http_port=SERVER_PORT)
  33. server.add_handler('on_create_party_registration', _on_create_party_registration)
  34. await server.run_async()
  35. yield
  36. await server.stop()
  37. @pytest.fixture
  38. async def start_server_with_signatures():
  39. server = OpenADRServer(vtn_id=VTN_ID, cert=CERTFILE, key=KEYFILE, passphrase='openadr', fingerprint_lookup=fingerprint_lookup, http_port=SERVER_PORT)
  40. server.add_handler('on_create_party_registration', _on_create_party_registration)
  41. await server.run_async()
  42. yield
  43. await server.stop()
  44. @pytest.mark.asyncio
  45. async def test_query_party_registration(start_server):
  46. client = OpenADRClient(ven_name=VEN_NAME,
  47. vtn_url=f"http://localhost:{SERVER_PORT}/OpenADR2/Simple/2.0b")
  48. response_type, response_payload = await client.query_registration()
  49. assert response_type == 'oadrCreatedPartyRegistration'
  50. assert response_payload['vtn_id'] == VTN_ID
  51. await client.stop()
  52. @pytest.mark.asyncio
  53. async def test_create_party_registration(start_server):
  54. client = OpenADRClient(ven_name=VEN_NAME,
  55. vtn_url=f"http://localhost:{SERVER_PORT}/OpenADR2/Simple/2.0b")
  56. response_type, response_payload = await client.create_party_registration()
  57. assert response_type == 'oadrCreatedPartyRegistration'
  58. assert response_payload['ven_id'] == VEN_ID
  59. await client.stop()
  60. def fingerprint_lookup(ven_id):
  61. with open(CERTFILE) as file:
  62. cert = file.read()
  63. return certificate_fingerprint(cert)
  64. @pytest.mark.asyncio
  65. async def test_create_party_registration_with_signatures(start_server_with_signatures):
  66. with open(CERTFILE) as file:
  67. cert = file.read()
  68. client = OpenADRClient(ven_name=VEN_NAME,
  69. vtn_url=f"http://localhost:{SERVER_PORT}/OpenADR2/Simple/2.0b",
  70. cert=CERTFILE, key=KEYFILE, passphrase='openadr', vtn_fingerprint=certificate_fingerprint(cert))
  71. response_type, response_payload = await client.create_party_registration()
  72. assert response_type == 'oadrCreatedPartyRegistration'
  73. assert response_payload['ven_id'] == VEN_ID
  74. await client.stop()