test_signatures.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.utils import generate_id
  13. from pyopenadr.messaging import create_message, parse_message, validate_message
  14. from pyopenadr.signature import extract, calculate_digest
  15. from xml.etree.ElementTree import canonicalize
  16. from hashlib import sha256
  17. from base64 import b64encode
  18. from datetime import datetime, timedelta, timezone
  19. def test_message_validation():
  20. msg = create_message('oadrPoll', ven_id='123')
  21. parsed_type, parsed_message = parse_message(msg)
  22. assert parsed_type == 'oadrPoll'
  23. validate_message(msg)
  24. def test_message_validation_complex():
  25. now = datetime.now(timezone.utc)
  26. event_id = generate_id()
  27. active_period = {"dtstart": now + timedelta(minutes=1),
  28. "duration": timedelta(minutes=9)}
  29. event_descriptor = {"event_id": event_id,
  30. "modification_number": 1,
  31. "modification_date_time": now,
  32. "priority": 1,
  33. "market_context": "http://MarketContext1",
  34. "created_date_time": now,
  35. "event_status": "near",
  36. "test_event": "false",
  37. "vtn_comment": "This is an event"}
  38. event_signals = [{"intervals": [{"duration": timedelta(minutes=1), "uid": 1, "signal_payload": 8},
  39. {"duration": timedelta(minutes=1), "uid": 2, "signal_payload": 10},
  40. {"duration": timedelta(minutes=1), "uid": 3, "signal_payload": 12},
  41. {"duration": timedelta(minutes=1), "uid": 4, "signal_payload": 14},
  42. {"duration": timedelta(minutes=1), "uid": 5, "signal_payload": 16},
  43. {"duration": timedelta(minutes=1), "uid": 6, "signal_payload": 18},
  44. {"duration": timedelta(minutes=1), "uid": 7, "signal_payload": 20},
  45. {"duration": timedelta(minutes=1), "uid": 8, "signal_payload": 10},
  46. {"duration": timedelta(minutes=1), "uid": 9, "signal_payload": 20}],
  47. "signal_name": "LOAD_CONTROL",
  48. #"signal_name": "simple",
  49. #"signal_type": "level",
  50. "signal_type": "x-loadControlCapacity",
  51. "signal_id": generate_id(),
  52. "current_value": 9.99}]
  53. event_targets = [{"ven_id": 'VEN001'}, {"ven_id": 'VEN002'}]
  54. event = {'active_period': active_period,
  55. 'event_descriptor': event_descriptor,
  56. 'event_signals': event_signals,
  57. 'targets': event_targets,
  58. 'response_required': 'always'}
  59. msg = create_message('oadrDistributeEvent',
  60. request_id=generate_id(),
  61. response={'request_id': 123, 'response_code': 200, 'response_description': 'OK'},
  62. events=[event])
  63. parsed_type, parsed_msg = parse_message(msg)
  64. assert parsed_type == 'oadrDistributeEvent'
  65. validate_message(msg)