test_signatures.py 3.0 KB

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