test_client_registration.py 3.5 KB

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