/ tests / e2e / helpers / assertions.py
assertions.py
 1  """Assertion helpers for Auths E2E tests."""
 2  
 3  import json
 4  import re
 5  from pathlib import Path
 6  
 7  import jsonschema
 8  
 9  
10  def validate_json_schema(data: dict, schema_name: str) -> None:
11      """Validate data against a JSON Schema from the schemas/ directory."""
12      schemas_dir = Path(__file__).resolve().parent.parent.parent.parent / "schemas"
13      schema_path = schemas_dir / schema_name
14      if not schema_path.exists():
15          raise FileNotFoundError(f"Schema not found: {schema_path}")
16  
17      with open(schema_path) as f:
18          schema = json.load(f)
19  
20      jsonschema.validate(instance=data, schema=schema)
21  
22  
23  DID_KERI_PATTERN = re.compile(r"^did:keri:E[A-Za-z0-9_-]+$")
24  DID_KEY_PATTERN = re.compile(r"^did:key:z6Mk[A-Za-z0-9]+$")
25  
26  
27  def assert_did_format(did: str) -> None:
28      """Validate that a DID string matches expected format."""
29      assert DID_KERI_PATTERN.match(did) or DID_KEY_PATTERN.match(did), (
30          f"Invalid DID format: {did!r}. "
31          f"Expected did:keri:E... or did:key:z6Mk..."
32      )